src/ProdOnDate.cpp

changeset 234
51aea8b798f0
child 336
e97f9e87d94b
equal deleted inserted replaced
233:baf666307ebb 234:51aea8b798f0
1 /**
2 * ProdOnDate.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 "ProdOnDate.h"
18 #include "MainWindow.h"
19 #include "EditProduct.h"
20 #include "config.h"
21 #include "Utils.h"
22
23
24 ProdOnDate::ProdOnDate(QWidget *parent) : QDialog(parent)
25 {
26 qDebug() << "ProdOnDate 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(fromProdOnDate()));
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("Year"), tr("Date"), tr("Code"), 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 ProdOnDate::refreshTable()
258 {
259 QTreeWidgetItem *year, *name;
260
261 qDebug() << "ProdOnDate reload";
262
263 treeWidget->clear();
264 bool first = true;
265
266 QSqlQuery query0;
267 query0.prepare("SELECT DISTINCT YEAR(brew_date_start) AS YEAR FROM products WHERE stage='11' ORDER BY YEAR(brew_date_start) DESC");
268 query0.exec();
269 while (query0.next()) {
270 year = new QTreeWidgetItem( QStringList( { query0.value(0).toString() } ));
271 treeWidget->addTopLevelItem( year );
272 year->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
273
274 QSqlQuery query1;
275 query1.prepare("SELECT name,record,code,brew_date_start FROM products WHERE stage='11' AND YEAR(brew_date_start)=:theyear ORDER BY code,name");
276 query1.bindValue(":theyear", query0.value(0).toString());
277 query1.exec();
278 while (query1.next()) {
279 name = new QTreeWidgetItem(QStringList({ "", query1.value("brew_date_start").toDate().toString("dd MMM yyyy"),
280 query1.value("code").toString(), query1.value("name").toString(), query1.value("record").toString() }));
281 year->addChild( name );
282 }
283 if (first)
284 treeWidget->expandItem(year);
285 first = false;
286 }
287
288 QSqlQuery query("SELECT record FROM products WHERE stage='11'");
289 emit setStatus(QString(tr("Total items: %1")).arg(query.size()));
290
291 showRecipe();
292 }
293
294
295 void ProdOnDate::showRecipe()
296 {
297 QString w;
298
299 qDebug() << "showRecipe" << record;
300
301 const QStringList c_method({"Morey", "Mosher", "Daniels", "Halberstadt", "Naudts" });
302 const QStringList i_method({"Tinseth", "Rager", "Daniels" });
303
304 /*
305 * If no recipe is "pre" selected, return.
306 */
307 if (record < 1) {
308 recipeBox->setEnabled(false);
309 return;
310 }
311
312 /*
313 * Fill in basic details of the selected recipe.
314 */
315 QSqlQuery query;
316 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,code "
317 "FROM products WHERE record=:record");
318 query.bindValue(":record", record);
319 query.exec();
320 if (query.size() == 1) {
321 query.first();
322 volumeEdit->setValue(query.value(0).toDouble());
323 boilvolumeEdit->setValue(query.value(1).toDouble());
324 boiltimeEdit->setValue(query.value(2).toInt());
325 efficiencyEdit->setValue(query.value(3).toDouble());
326 ogEdit->setValue(query.value(4).toDouble());
327 fgEdit->setValue(query.value(5).toDouble());
328 abvEdit->setValue(query.value(6).toInt());
329 colorEdit->setValue(query.value(7).toInt());
330 colorEdit->setStyleSheet(Utils::ebc_to_style(query.value(7).toInt()));
331 colormethodEdit->setText(c_method[query.value(8).toInt()]);
332 ibuEdit->setValue(query.value(9).toInt());
333 ibumethodEdit->setText(i_method[query.value(10).toInt()]);
334 co2Edit->setValue(query.value(11).toDouble());
335 remarksEdit->setPlainText(query.value(12).toString());
336 recipeBox->setTitle(query.value(14).toString() + " - " + query.value(13).toString());
337 recipeBox->setEnabled(true);
338 } else {
339 qDebug() << "Error getting product record" << record;
340 }
341 }
342
343
344 ProdOnDate::~ProdOnDate() {}
345
346
347 void ProdOnDate::edit(int recno)
348 {
349 EditProduct dialog(recno, this);
350 /* Signal from editor if a refresh is needed */
351 connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable()));
352 dialog.setModal(true);
353 dialog.exec();
354 }
355
356
357 void ProdOnDate::on_openButton_clicked()
358 {
359 if (record > 0)
360 edit(record);
361 }
362
363
364 void ProdOnDate::on_item_clicked(QTreeWidgetItem *item, int col)
365 {
366 record = -2; // Invalid
367
368 if (col == 0) {
369 if (item->isExpanded()) {
370 treeWidget->collapseItem(item);
371 } else {
372 treeWidget->expandItem(item);
373 }
374 } else if (col > 0) {
375 /*
376 * if a product line is selected then:
377 * item column 1 contains the product date,
378 * item column 2 contains the product code,
379 * item column 3 contains the product name,
380 * item column 4 contains the product record number.
381 */
382 if (item->text(4).toInt() > 0) {
383 if (record != item->text(4).toInt()) {
384 record = item->text(4).toInt();
385 showRecipe();
386 }
387 }
388 }
389 }
390
391
392 void ProdOnDate::on_item_doubleclicked(QTreeWidgetItem *item, int col)
393 {
394 if ((col > 0) && (item->text(4).toInt() > 0)) {
395 edit(item->text(4).toInt());
396 }
397 }
398
399

mercurial