summaryrefslogtreecommitdiff
path: root/productinventory.cpp
blob: 5842a8100289ccace6ab41c8d568b235bc443562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * productinventory.cpp is a part of ProductInventory
 * An inventory system designed for makeup and related things
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "productinventory.h"
#include "ui_productinventory.h"

/*!
 * constructor
 * \param parent parent
 */
ProductInventory::ProductInventory(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::ProductInventory)
{
	ui->setupUi(this);
	/* database and statusbar init */
	db = QSqlDatabase::addDatabase("QMYSQL");
	conStatus = "Disconnected";
	conColor = "red";
	isConnected = false;
	statusMessage(conStatus, conColor, 0);

	/* make column headers resize to fit display */
	ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

	/* signal/slot connections */
	connect(ui->statusBar, SIGNAL(messageChanged(const QString)), this, SLOT(statusChanged(QString)));

	ui->tableWidget->hide();
	headerLabels.append(QString("Brand"));
	headerLabels.append(QString("Color"));
	headerLabels.append(QString("Comment"));
	headerLabels.append(QString("Images"));
	headerLabels.append(QString("Date added"));
	createTable("Connect to the database to populate the tables");

	categoryLayout = new QGridLayout;
	ui->filterCategoryScrollArea->setLayout(categoryLayout);
}

/*!
 * destructor
 */
ProductInventory::~ProductInventory()
{
	clearTables();
	delete categoryLayout;
	delete ui;
}

/*!
 * write a message to the statusbar
 * \param msg message to display
 * \param color text color
 * \param ms milliseconds to show message (0 = forever until overwritten)
 */
void
ProductInventory::statusMessage(QString msg, QString color, int ms)
{
	ui->statusBar->setStyleSheet("color: " + color);
	ui->statusBar->showMessage(msg, ms);
}

/*!
 * overload for writing a message to the status bar without a color (defaults to black)
 * \param msg message to display
 * \param ms milliseconds to show message (0 = forever until overwritten)
 */
void
ProductInventory::statusMessage(QString msg, int ms)
{
	statusMessage(msg, "black", ms);
}

/*!
 * slot to receive signal when statusbar message changes
 * \param msg new message
 */
void
ProductInventory::statusChanged(QString msg)
{
	/*
	 * if we have an empty message, display the connection status
	 * indefinitely (empty message received when it is cleared)
	 * BUG: called on hover in file menu
	*/
	if (msg == "")
		statusMessage(conStatus, conColor, 0);
}

/*!
 * slot to receive signal when the connect button is clicked
 */
void
ProductInventory::on_connectButton_clicked()
{
	/*
	 * we use the one button for both connect and disconnect,
	 * so we need to keep track of our status in isConnected
	 * change the button appropriately
	 */
	if (getConnectionStatus())
	{
		// TODO log
		if (dbDisconnect())
		{
			qDebug() << "Disconnected from database";
		}
	}
	else
	{
		db.setHostName(ui->HostInput->text());
		db.setPort((ui->portInput->value()));
		db.setUserName(ui->usernameInput->text());
		db.setPassword(ui->passwordInput->text());
		if (dbConnect())
		{
			// TODO log
			qDebug() << "Connected to database";
			clearTables();
			populateInterface();
		}
	}
}

/*!
 * called when window is resized
 * \param event
 */
void
ProductInventory::resizeEvent(QResizeEvent* event)
{
	QMainWindow::resizeEvent(event);
	ui->tableWidget->resizeRowsToContents(); // not needed?
	resizeRows();
}

void ProductInventory::on_mustHaveImagesCheckBox_clicked()
{
	ui->mustNotHaveImagesCheckBox->setChecked(false);
}

void ProductInventory::on_mustNotHaveImagesCheckBox_clicked()
{
	ui->mustHaveImagesCheckBox->setChecked(false);
}

/*!
 * add a single item (row) to the table
 * \param table	the table object to add the item to
 * \param values QList<QString> with values to add to the new row
 */
void
ProductInventory::addItemToTable(QTableWidget *table, QList<QString> *values)
{
	QList<QString>::iterator i;
	int col = 0;
	int row = ui->tableWidget->rowCount();
	table->insertRow(row);
	for (i = values->begin(); i != values->end(); i++)
	{
		QTableWidgetItem *test1 = new QTableWidgetItem(*i);
		table->setItem(row, col, test1);
		col++;
	}
}

/*!
 * clear the default table and all other added tables
 * by iterating through the tables list
 */
void
ProductInventory::clearTables()
{
	qDebug() << "clearing table and deleting items";
	ui->tableWidget->setRowCount(0); // automagically deletes items
	QList<QTableWidget *>::iterator i;
	for (i = tables.begin(); i != tables.end(); i++)
	{
		(*i)->setRowCount(0);
		(*i)->hide();
		delete (*i);
		tables.erase(i);
	}
	QList<QLabel *>::iterator l;
	for (l = labels.begin(); l != labels.end(); l++)
	{
		(*l)->hide();
		labels.erase(l);
		delete *l;
	}

	QList<QCheckBox *>::iterator c;
	for (c = checkboxes.begin(); c != checkboxes.end(); c++)
	{
		checkboxes.erase(c);
		delete *c;
	}

}

/*!
 * create a QTableWidget, set some properties, append to tables list
 * \return index into tables or -1 on error
 */
int
ProductInventory::createTable(QString category)
{
	QTableWidget *table = new QTableWidget;
	if (table == nullptr)
		return -1;
	table->setMinimumHeight(100);
	table->setColumnCount(headerLabels.size());
	table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	table->setHorizontalHeaderLabels(headerLabels);
	table->verticalHeader()->hide();
	QLabel *label = new QLabel(category);
	if (label == nullptr)
		return -1;
	labels.append(label);
	QFont f( "Arial", 10, QFont::Bold);
	label->setFont(f);
	ui->scrollArea->widget()->layout()->addWidget(label);
	ui->scrollArea->widget()->layout()->addWidget(table);
	tables.append(table);
	return tables.size()-1;
}

/*!
 * resize each row in every table to fit its content
 */
void
ProductInventory::resizeRows()
{
	QList<QTableWidget *>::iterator i;
	for (i = tables.begin(); i != tables.end(); i++)
	{
		(*i)->resizeRowsToContents();
	}
}