src/ProdOnTree.cpp

changeset 232
6d3ba9c44f95
child 234
51aea8b798f0
equal deleted inserted replaced
231:54b5abd46958 232:6d3ba9c44f95
1 /**
2 * ProdOnTree.cpp is part of bmsapp.
3 *
4 * bmsapp is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bmsapp is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "ProdOnTree.h"
18 #include "MainWindow.h"
19 #include "EditProduct.h"
20 #include "config.h"
21 #include "Utils.h"
22
23
24 ProdOnTree::ProdOnTree(QWidget *parent) : QDialog(parent)
25 {
26 qDebug() << "ProdOnTree start";
27
28 gridLayout = new QGridLayout(this);
29 gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
30 treeWidget = new QTreeWidget(this);
31 treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
32 QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
33 sizePolicy.setHorizontalStretch(0);
34 sizePolicy.setVerticalStretch(0);
35 sizePolicy.setHeightForWidth(treeWidget->sizePolicy().hasHeightForWidth());
36 treeWidget->setSizePolicy(sizePolicy);
37 gridLayout->addWidget(treeWidget, 0, 0, 1, 1);
38
39 recipeBox = new QGroupBox(this);
40 recipeBox->setObjectName(QString::fromUtf8("recipeBox"));
41 QSizePolicy sizePolicy3(QSizePolicy::Preferred, QSizePolicy::Expanding);
42 sizePolicy3.setHorizontalStretch(0);
43 sizePolicy3.setVerticalStretch(0);
44 sizePolicy3.setHeightForWidth(recipeBox->sizePolicy().hasHeightForWidth());
45 recipeBox->setSizePolicy(sizePolicy3);
46 recipeBox->setMinimumSize(QSize(500, 0));
47
48 volumeLabel = new QLabel(recipeBox);
49 volumeLabel->setObjectName(QString::fromUtf8("volumeLabel"));
50 volumeLabel->setGeometry(QRect(100, 30, 141, 20));
51 volumeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
52 volumeLabel->setText(tr("Brew volume:"));
53 efficiencyLabel = new QLabel(recipeBox);
54 efficiencyLabel->setObjectName(QString::fromUtf8("efficiencyLabel"));
55 efficiencyLabel->setGeometry(QRect(100, 120, 141, 20));
56 efficiencyLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
57 efficiencyLabel->setText(tr("Brewhouse efficiency:"));
58 boilvolumeLabel = new QLabel(recipeBox);
59 boilvolumeLabel->setObjectName(QString::fromUtf8("boilvolumeLabel"));
60 boilvolumeLabel->setGeometry(QRect(100, 60, 141, 20));
61 boilvolumeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
62 boilvolumeLabel->setText(tr("Boil volume:"));
63 boiltimeLabel = new QLabel(recipeBox);
64 boiltimeLabel->setObjectName(QString::fromUtf8("boiltimeLabel"));
65 boiltimeLabel->setGeometry(QRect(100, 90, 141, 20));
66 boiltimeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
67 boiltimeLabel->setText(tr("Boil time:"));
68 ogLabel = new QLabel(recipeBox);
69 ogLabel->setObjectName(QString::fromUtf8("ogLabel"));
70 ogLabel->setGeometry(QRect(100, 150, 141, 20));
71 ogLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
72 ogLabel->setText(tr("Original gravity:"));
73 fgLabel = new QLabel(recipeBox);
74 fgLabel->setObjectName(QString::fromUtf8("fgLabel"));
75 fgLabel->setGeometry(QRect(100, 180, 141, 20));
76 fgLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
77 fgLabel->setText(tr("Final gravity:"));
78 abvLabel = new QLabel(recipeBox);
79 abvLabel->setObjectName(QString::fromUtf8("abvLabel"));
80 abvLabel->setGeometry(QRect(100, 210, 141, 20));
81 abvLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
82 abvLabel->setText(tr("Alcohol by Volume:"));
83 co2Label = new QLabel(recipeBox);
84 co2Label->setObjectName(QString::fromUtf8("co2Label"));
85 co2Label->setGeometry(QRect(100, 240, 141, 20));
86 co2Label->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
87 co2Label->setText(tr("CO2 Volume:"));
88 colorLabel = new QLabel(recipeBox);
89 colorLabel->setObjectName(QString::fromUtf8("colorLabel"));
90 colorLabel->setGeometry(QRect(100, 270, 141, 20));
91 colorLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
92 colorLabel->setText(tr("Color EBC:"));
93 colormethodLabel = new QLabel(recipeBox);
94 colormethodLabel->setObjectName(QString::fromUtf8("colormethodLabel"));
95 colormethodLabel->setGeometry(QRect(100, 300, 141, 20));
96 colormethodLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
97 colormethodLabel->setText(tr("Color method:"));
98 ibuLabel = new QLabel(recipeBox);
99 ibuLabel->setObjectName(QString::fromUtf8("ibuLabel"));
100 ibuLabel->setGeometry(QRect(100, 330, 141, 20));
101 ibuLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
102 ibuLabel->setText(tr("Bitterness IBU:"));
103 ibumethodLabel = new QLabel(recipeBox);
104 ibumethodLabel->setObjectName(QString::fromUtf8("ibumethodLabel"));
105 ibumethodLabel->setGeometry(QRect(100, 360, 141, 20));
106 ibumethodLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
107 ibumethodLabel->setText(tr("Bitterness method:"));
108 remarksLabel = new QLabel(recipeBox);
109 remarksLabel->setObjectName(QString::fromUtf8("remarksLabel"));
110 remarksLabel->setGeometry(QRect(30, 390, 101, 20));
111 remarksLabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
112 remarksLabel->setText(tr("Remarks:"));
113
114 volumeEdit = new QDoubleSpinBox(recipeBox);
115 volumeEdit->setObjectName(QString::fromUtf8("volumeEdit"));
116 volumeEdit->setGeometry(QRect(260, 30, 111, 24));
117 volumeEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
118 volumeEdit->setReadOnly(true);
119 volumeEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
120 volumeEdit->setDecimals(1);
121 volumeEdit->setMaximum(100000.000000000000000);
122 volumeEdit->setSuffix(tr(" L"));
123 boilvolumeEdit = new QDoubleSpinBox(recipeBox);
124 boilvolumeEdit->setObjectName(QString::fromUtf8("boilvolumeEdit"));
125 boilvolumeEdit->setGeometry(QRect(260, 60, 111, 24));
126 boilvolumeEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
127 boilvolumeEdit->setReadOnly(true);
128 boilvolumeEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
129 boilvolumeEdit->setDecimals(1);
130 boilvolumeEdit->setMaximum(100000.000000000000000);
131 boilvolumeEdit->setSuffix(tr(" L"));
132 efficiencyEdit = new QDoubleSpinBox(recipeBox);
133 efficiencyEdit->setObjectName(QString::fromUtf8("efficiencyEdit"));
134 efficiencyEdit->setGeometry(QRect(260, 120, 111, 24));
135 efficiencyEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
136 efficiencyEdit->setReadOnly(true);
137 efficiencyEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
138 efficiencyEdit->setDecimals(1);
139 efficiencyEdit->setMaximum(100.000000000000000);
140 efficiencyEdit->setSuffix(tr(" %"));
141 ogEdit = new QDoubleSpinBox(recipeBox);
142 ogEdit->setObjectName(QString::fromUtf8("ogEdit"));
143 ogEdit->setGeometry(QRect(260, 150, 111, 24));
144 ogEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
145 ogEdit->setReadOnly(true);
146 ogEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
147 ogEdit->setDecimals(3);
148 ogEdit->setMaximum(1.200000000000000);
149 fgEdit = new QDoubleSpinBox(recipeBox);
150 fgEdit->setObjectName(QString::fromUtf8("fgEdit"));
151 fgEdit->setGeometry(QRect(260, 180, 111, 24));
152 fgEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
153 fgEdit->setReadOnly(true);
154 fgEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
155 fgEdit->setDecimals(3);
156 fgEdit->setMaximum(1.200000000000000);
157 abvEdit = new QDoubleSpinBox(recipeBox);
158 abvEdit->setObjectName(QString::fromUtf8("abvEdit"));
159 abvEdit->setGeometry(QRect(260, 210, 111, 24));
160 abvEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
161 abvEdit->setReadOnly(true);
162 abvEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
163 abvEdit->setDecimals(1);
164 abvEdit->setMaximum(100.000000000000000);
165 abvEdit->setSuffix(tr(" %"));
166 co2Edit = new QDoubleSpinBox(recipeBox);
167 co2Edit->setObjectName(QString::fromUtf8("co2Edit"));
168 co2Edit->setGeometry(QRect(260, 240, 111, 24));
169 co2Edit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
170 co2Edit->setReadOnly(true);
171 co2Edit->setButtonSymbols(QAbstractSpinBox::NoButtons);
172 co2Edit->setDecimals(2);
173 co2Edit->setMaximum(100.000000000000000);
174 boiltimeEdit = new QSpinBox(recipeBox);
175 boiltimeEdit->setObjectName(QString::fromUtf8("boiltimeEdit"));
176 boiltimeEdit->setGeometry(QRect(260, 90, 111, 24));
177 boiltimeEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
178 boiltimeEdit->setReadOnly(true);
179 boiltimeEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
180 boiltimeEdit->setMaximum(1440);
181 boiltimeEdit->setSuffix(tr(" min"));
182 colorEdit = new QSpinBox(recipeBox);
183 colorEdit->setObjectName(QString::fromUtf8("colorEdit"));
184 colorEdit->setGeometry(QRect(260, 270, 111, 24));
185 colorEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
186 colorEdit->setReadOnly(true);
187 colorEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
188 colorEdit->setMaximum(1440);
189 ibuEdit = new QSpinBox(recipeBox);
190 ibuEdit->setObjectName(QString::fromUtf8("ibuEdit"));
191 ibuEdit->setGeometry(QRect(260, 330, 111, 24));
192 ibuEdit->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
193 ibuEdit->setReadOnly(true);
194 ibuEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
195 ibuEdit->setMaximum(1440);
196 remarksEdit = new QPlainTextEdit(recipeBox);
197 remarksEdit->setObjectName(QString::fromUtf8("remarksEdit"));
198 remarksEdit->setGeometry(QRect(30, 410, 441, 111));
199 remarksEdit->setReadOnly(true);
200 colormethodEdit = new QLineEdit(recipeBox);
201 colormethodEdit->setObjectName(QString::fromUtf8("colormethodEdit"));
202 colormethodEdit->setGeometry(QRect(260, 300, 113, 23));
203 colormethodEdit->setReadOnly(true);
204 ibumethodEdit = new QLineEdit(recipeBox);
205 ibumethodEdit->setObjectName(QString::fromUtf8("ibumethodEdit"));
206 ibumethodEdit->setGeometry(QRect(260, 360, 113, 23));
207 ibumethodEdit->setReadOnly(true);
208
209 gridLayout->addWidget(recipeBox, 0, 1, 2, 1);
210
211 groupBox = new QGroupBox(this);
212 groupBox->setObjectName(QString::fromUtf8("groupBox"));
213 groupBox->setEnabled(true);
214 groupBox->setFlat(false);
215 horizontalLayout = new QHBoxLayout(groupBox);
216 horizontalLayout->setSpacing(6);
217 horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
218 horizontalLayout->setContentsMargins(0, 0, 0, 0);
219
220 quitButton = new QPushButton(groupBox);
221 quitButton->setObjectName(QString::fromUtf8("quitButton"));
222 quitButton->setMinimumSize(QSize(80, 24));
223 quitButton->setText(tr("Quit"));
224 QIcon icon;
225 icon.addFile(QString::fromUtf8(":icons/silk/door_out.png"), QSize(), QIcon::Normal, QIcon::Off);
226 quitButton->setIcon(icon);
227 horizontalLayout->addWidget(quitButton, 0, Qt::AlignLeft);
228
229 openButton = new QPushButton(groupBox);
230 openButton->setObjectName(QString::fromUtf8("openButton"));
231 openButton->setMinimumSize(QSize(80, 24));
232 openButton->setText(tr("Open"));
233 QIcon icon1;
234 icon1.addFile(QString::fromUtf8(":icons/silk/cup_go.png"), QSize(), QIcon::Normal, QIcon::Off);
235 openButton->setIcon(icon1);
236 horizontalLayout->addWidget(openButton, 0, Qt::AlignRight);
237
238 gridLayout->addWidget(groupBox, 1, 0, 1, 1);
239
240 record = -2;
241 connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromProdOnTree()));
242 connect(openButton, SIGNAL(clicked()), this, SLOT(on_openButton_clicked()));
243 connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
244
245 treeWidget->setColumnCount(4);
246 treeWidget->setHeaderLabels({ tr("Guide"), tr("Group"), tr("Style"), tr("Product") });
247 treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
248 treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
249
250 connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_clicked(QTreeWidgetItem*, int)));
251 connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(on_item_doubleclicked(QTreeWidgetItem*, int)));
252
253 emit refreshTable();
254 }
255
256
257 void ProdOnTree::refreshTable()
258 {
259 QTreeWidgetItem *st_guide, *st_group, *st_name, *name;
260
261 qDebug() << "ProdOnTree reload";
262
263 treeWidget->clear();
264
265 QSqlQuery query0;
266 query0.prepare("SELECT DISTINCT st_guide FROM products WHERE stage='11' ORDER BY st_guide");
267 query0.exec();
268 while (query0.next()) {
269 st_guide = new QTreeWidgetItem( QStringList( { query0.value(0).toString() } ));
270 treeWidget->addTopLevelItem( st_guide );
271 st_guide->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
272
273 QSqlQuery query1;
274 query1.prepare("SELECT DISTINCT st_letter FROM products WHERE stage='11' AND st_guide=:guide ORDER BY st_letter");
275 query1.bindValue(":guide", query0.value(0).toString());
276 query1.exec();
277 while (query1.next()) {
278 st_group = new QTreeWidgetItem(QStringList({ "", query1.value(0).toString() }));
279 st_group->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
280 st_guide->addChild( st_group );
281
282 QSqlQuery query2;
283 query2.prepare("SELECT DISTINCT st_name FROM products WHERE stage='11' AND st_guide=:guide AND st_letter=:group ORDER BY st_name");
284 query2.bindValue(":guide", query0.value(0).toString());
285 query2.bindValue(":group", query1.value(0).toString());
286 query2.exec();
287 while (query2.next()) {
288 st_name = new QTreeWidgetItem(QStringList({ "", "", query2.value(0).toString() }));
289 st_name->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
290 st_group->addChild( st_name );
291
292 QSqlQuery query3;
293 query3.prepare("SELECT name,record,code FROM products WHERE stage='11' AND st_guide=:guide AND st_letter=:group AND st_name=:name "
294 "ORDER BY code,name");
295 query3.bindValue(":guide", query0.value(0).toString());
296 query3.bindValue(":group", query1.value(0).toString());
297 query3.bindValue(":name", query2.value(0).toString());
298 query3.exec();
299 while (query3.next()) {
300 name = new QTreeWidgetItem(QStringList({ "", "", "",
301 query3.value(2).toString() + " - " + query3.value(0).toString(), query3.value(1).toString() }));
302 st_name->addChild( name );
303 }
304 }
305 treeWidget->expandItem(st_group);
306 }
307 treeWidget->expandItem(st_guide);
308 }
309
310 QSqlQuery query("SELECT record FROM products WHERE stage='11'");
311 emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
312
313 showRecipe();
314 }
315
316
317 void ProdOnTree::showRecipe()
318 {
319 QString w;
320
321 qDebug() << "showRecipe" << record;
322
323 const QStringList c_method({"Morey", "Mosher", "Daniels", "Halberstadt", "Naudts" });
324 const QStringList i_method({"Tinseth", "Rager", "Daniels" });
325
326 /*
327 * If no recipe is "pre" selected, return.
328 */
329 if (record < 1) {
330 recipeBox->setEnabled(false);
331 return;
332 }
333
334 /*
335 * Fill in basic details of the selected recipe.
336 */
337 QSqlQuery query;
338 query.prepare("SELECT batch_size,boil_size,boil_time,efficiency,est_og,est_fg,est_abv,est_color,color_method,est_ibu,ibu_method,est_carb,notes,name "
339 "FROM products WHERE record=:record");
340 query.bindValue(":record", record);
341 query.exec();
342 if (query.size() == 1) {
343 query.first();
344 volumeEdit->setValue(query.value(0).toDouble());
345 boilvolumeEdit->setValue(query.value(1).toDouble());
346 boiltimeEdit->setValue(query.value(2).toInt());
347 efficiencyEdit->setValue(query.value(3).toDouble());
348 ogEdit->setValue(query.value(4).toDouble());
349 fgEdit->setValue(query.value(5).toDouble());
350 abvEdit->setValue(query.value(6).toInt());
351 colorEdit->setValue(query.value(7).toInt());
352 colorEdit->setStyleSheet(Utils::ebc_to_style(query.value(7).toInt()));
353 colormethodEdit->setText(c_method[query.value(8).toInt()]);
354 ibuEdit->setValue(query.value(9).toInt());
355 ibumethodEdit->setText(i_method[query.value(10).toInt()]);
356 co2Edit->setValue(query.value(11).toDouble());
357 remarksEdit->setPlainText(query.value(12).toString());
358 recipeBox->setTitle(query.value(13).toString());
359 recipeBox->setEnabled(true);
360 } else {
361 qDebug() << "Error getting product record" << record;
362 }
363 }
364
365
366 ProdOnTree::~ProdOnTree() {}
367
368
369 void ProdOnTree::edit(int recno)
370 {
371 EditProduct dialog(recno, this);
372 /* Signal from editor if a refresh is needed */
373 connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
374 dialog.setModal(true);
375 dialog.exec();
376 }
377
378
379 void ProdOnTree::on_openButton_clicked()
380 {
381 if (record > 0)
382 edit(record);
383 }
384
385
386 void ProdOnTree::on_item_clicked(QTreeWidgetItem *item, int col)
387 {
388 record = -2; // Invalid
389
390 if (col == 0) {
391 if (item->isExpanded()) {
392 treeWidget->collapseItem(item);
393 } else {
394 treeWidget->expandItem(item);
395 }
396 } else if (col == 1) {
397 if (item->isExpanded()) {
398 treeWidget->collapseItem(item);
399 } else {
400 treeWidget->expandItem(item);
401 }
402 } else if (col == 2 && item->text(2).length()) {
403 if (item->isExpanded()) {
404 item->setExpanded(false);
405 } else {
406 item->setExpanded(true);
407 }
408 } else if (col == 3) {
409 /*
410 * if a recipe name is selected then:
411 * item column 3 contains the recipe name,
412 * item column 4 contains the recipe record number.
413 */
414 if (item->text(4).toInt() > 0) {
415 if (record != item->text(4).toInt()) {
416 record = item->text(4).toInt();
417 showRecipe();
418 }
419 }
420 }
421 }
422
423
424 void ProdOnTree::on_item_doubleclicked(QTreeWidgetItem *item, int col)
425 {
426 if ((col == 3) && (item->text(4).toInt() > 0)) {
427 edit(item->text(4).toInt());
428 }
429 }
430
431

mercurial