forked from Brewtarget/brewtarget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrewDayScrollWidget.cpp
474 lines (393 loc) · 13.8 KB
/
BrewDayScrollWidget.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* BrewDayScrollWidget.cpp is part of Brewtarget, and is Copyright the following
* authors 2009-2014
* - Mik Firestone <[email protected]>
* - Philip Greggory Lee <[email protected]>
*
* Brewtarget 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.
*
* Brewtarget 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 "instruction.h"
#include "brewtarget.h"
#include "BrewDayScrollWidget.h"
#include "database.h"
#include "Html.h"
#include <QListWidgetItem>
#include <QPrinter>
#include <QPrintDialog>
#include <QDate>
#include <QVector>
#include <QDir>
#include "InstructionWidget.h"
#include "TimerWidget.h"
#include "style.h"
#include "equipment.h"
#include "mash.h"
BrewDayScrollWidget::BrewDayScrollWidget(QWidget* parent)
: QWidget(parent), doc(new QTextBrowser())
{
setupUi(this);
setObjectName("BrewDayScrollWidget");
recObs = 0;
connect( listWidget, &QListWidget::currentRowChanged, this, &BrewDayScrollWidget::showInstruction);
// connect( plainTextEdit, &QPlainTextEdit::textChanged, this, &BrewDayScrollWidget::saveInstruction );
connect(btTextEdit, &BtTextEdit::textModified, this, &BrewDayScrollWidget::saveInstruction);
connect( pushButton_insert, &QAbstractButton::clicked, this, &BrewDayScrollWidget::insertInstruction );
connect( pushButton_remove, &QAbstractButton::clicked, this, &BrewDayScrollWidget::removeSelectedInstruction );
connect( pushButton_up, &QAbstractButton::clicked, this, &BrewDayScrollWidget::pushInstructionUp );
connect( pushButton_down, &QAbstractButton::clicked, this, &BrewDayScrollWidget::pushInstructionDown );
connect( pushButton_generateInstructions, &QAbstractButton::clicked, this, &BrewDayScrollWidget::generateInstructions );
}
void BrewDayScrollWidget::saveInstruction()
{
recObs->instructions()[ listWidget->currentRow() ]->setDirections( btTextEdit->toPlainText() );
}
void BrewDayScrollWidget::showInstruction(int insNdx)
{
if( recObs == 0 )
return;
int size = recIns.size();
if( insNdx < 0 || insNdx >= size )
return;
// Block signals to avoid setPlainText() from triggering saveInstruction().
btTextEdit->setPlainText((recIns[insNdx])->directions());
}
void BrewDayScrollWidget::generateInstructions()
{
if( recObs == 0 )
return;
if(!btTextEdit->isEnabled())
btTextEdit->setEnabled(true);
recObs->generateInstructions();
}
QSize BrewDayScrollWidget::sizeHint() const
{
return QSize(0,0);
}
void BrewDayScrollWidget::removeSelectedInstruction()
{
if( recObs == 0 )
return;
int row = listWidget->currentRow();
if( row < 0 )
return;
recObs->remove(recIns[row]);
if(recIns.isEmpty())
{
btTextEdit->clear();
btTextEdit->setEnabled(false);
}
}
void BrewDayScrollWidget::pushInstructionUp()
{
if( recObs == 0 )
return;
int row = listWidget->currentRow();
if( row <= 0 )
return;
recObs->swapInstructions(recIns[row], recIns[row-1]);
listWidget->setCurrentRow(row-1);
}
void BrewDayScrollWidget::pushInstructionDown()
{
if( recObs == 0 )
return;
int row = listWidget->currentRow();
if( row >= listWidget->count() - 1 || row < 0 )
return;
recObs->swapInstructions(recIns[row], recIns[row+1]);
listWidget->setCurrentRow(row+1);
}
bool BrewDayScrollWidget::loadComplete(bool ok)
{
doc->print(printer);
return ok;
}
void BrewDayScrollWidget::print(QPrinter *mainPrinter,
int action, QFile* outFile)
{
QString pDoc;
if( recObs == 0 )
return;
/* Connect the webview's signal */
if ( action == PRINT )
{
printer = mainPrinter;
}
// Start building the document to be printed. The HTML doesn't work with
// the image since it is a compiled resource
pDoc = buildTitleTable( action != HTML );
pDoc += buildInstructionTable();
pDoc += buildFooterTable();
pDoc += tr("<h2>Notes</h2>");
if ( recObs->notes() != "" )
pDoc += QString("<div id=\"customNote\">%1</div>\n").arg(recObs->notes());
pDoc += "</body></html>";
doc->setHtml(pDoc);
if ( action == PREVIEW )
doc->show();
else if ( action == HTML )
{
QTextStream out(outFile);
out << pDoc;
outFile->close();
}
else
{
loadComplete(true);
}
}
void BrewDayScrollWidget::setRecipe(Recipe* rec)
{
// Disconnect old notifier.
if( recObs )
disconnect( recObs, &Recipe::changed, this, &BrewDayScrollWidget::acceptChanges );
recObs = rec;
connect( recObs, &Recipe::changed, this, &BrewDayScrollWidget::acceptChanges );
recIns = recObs->instructions();
foreach( Instruction* ins, recIns )
connect( ins, &Instruction::changed, this, &BrewDayScrollWidget::acceptInsChanges );
btTextEdit->clear();
if(recIns.isEmpty())
btTextEdit->setEnabled(false);
else
btTextEdit->setEnabled(true);
showChanges();
}
void BrewDayScrollWidget::insertInstruction()
{
if( recObs == 0 )
return;
if(!btTextEdit->isEnabled())
btTextEdit->setEnabled(true);
int pos = 0;
if(lineEdit_step->text().isEmpty())
pos = listWidget->count() + 1;
else
{
pos = lineEdit_step->text().toInt();
lineEdit_step->clear();
}
Instruction* ins = Database::instance().newInstruction(recObs);
pos = qBound(1, pos, recIns.size());
ins->setName(lineEdit_name->text());
lineEdit_name->clear();
recObs->insertInstruction( ins, pos );
listWidget->setCurrentRow(pos-1);
}
void BrewDayScrollWidget::acceptChanges(QMetaProperty prop, QVariant /*value*/)
{
if( recObs && QString(prop.name()) == "instructions" )
{
// An instruction has been added or deleted, so update internal list.
foreach( Instruction* ins, recIns )
disconnect( ins, 0, this, 0 );
recIns = recObs->instructions(); // Already sorted by instruction numbers.
foreach( Instruction* ins, recIns )
connect( ins, &Instruction::changed, this, &BrewDayScrollWidget::acceptInsChanges );
showChanges();
}
}
void BrewDayScrollWidget::acceptInsChanges(QMetaProperty prop, QVariant /*value*/)
{
QString propName = prop.name();
if( propName == "instructionNumber" )
{
// The order changed, so resort our internal list.
qSort( recIns.begin(), recIns.end(), insPtrLtByNumber );
showChanges();
}
else if( propName == "directions" )
{
// This will make the displayed text directions update.
listWidget->setCurrentRow( listWidget->currentRow() );
}
}
void BrewDayScrollWidget::clear()
{
listWidget->clear();
}
void BrewDayScrollWidget::showChanges()
{
clear();
if( recObs == 0 )
return;
repopulateListWidget();
}
void BrewDayScrollWidget::repopulateListWidget()
{
listWidget->clear();
if( recObs == 0 )
return;
foreach( Instruction* ins, recIns )
{
//QString text = tr("Step %1: %2").arg(i).arg(ins->name());
QString text = tr("Step %1: %2").arg(ins->instructionNumber()).arg(ins->name());
listWidget->addItem(new QListWidgetItem(text));
}
if( recIns.size() > 0 )
listWidget->setCurrentRow(0);
else
listWidget->setCurrentRow(-1);
}
static QString styleName(Style* style)
{
if ( ! style )
{
return "unknown";
}
else
{
return style->name();
}
}
static QString boilTime(Equipment* equipment)
{
if ( ! equipment )
{
return "unknown";
}
else
{
return Brewtarget::displayAmount(equipment->boilTime_min(), "tab_recipe", "boilTime_min", Units::minutes);
}
}
QString BrewDayScrollWidget::buildTitleTable(bool includeImage)
{
QString header;
QString body;
// Do the style sheet first
if (cssName == NULL)
cssName = ":/css/brewday.css";
header = Html::createHeader(BrewDayScrollWidget::tr("Brewday"), cssName);
body = QString("<h1>%1</h1>").arg(recObs->name());
if ( includeImage )
body += QString("<img src=\"%1\" />").arg("qrc:/images/title.svg");
// Build the top table
// Build the first row: Style and Date
body += "<table id=\"title\">";
body += QString("<tr><td class=\"left\">%1</td>")
.arg(tr("Style"));
body += QString("<td class=\"value\">%1</td>")
.arg(styleName(recObs->style()));
body += QString("<td class=\"right\">%1</td>")
.arg(tr("Date"));
body += QString("<td class=\"value\">%1</td></tr>")
.arg(QDate::currentDate().toString());
// second row: boil time and efficiency.
body += QString("<tr><td class=\"left\">%1</td><td class=\"value\">%2</td><td class=\"right\">%3</td><td class=\"value\">%4</td></tr>")
.arg(tr("Boil Time"))
.arg(boilTime(recObs->equipment()))
.arg(tr("Efficiency"))
.arg(Brewtarget::displayAmount(recObs->efficiency_pct(),0,0));
// third row: pre-Boil Volume and Preboil Gravity
body += QString("<tr><td class=\"left\">%1</td><td class=\"value\">%2</td><td class=\"right\">%3</td><td class=\"value\">%4</td></tr>")
.arg(tr("Boil Volume"))
.arg(Brewtarget::displayAmount(recObs->boilVolume_l(), "tab_recipe", "boilVolume_l", Units::liters,2))
.arg(tr("Preboil Gravity"))
.arg(Brewtarget::displayAmount(recObs->boilGrav(), "tab_recipe", "og", Units::sp_grav, 3));
// fourth row: Final volume and starting gravity
body += QString("<tr><td class=\"left\">%1</td><td class=\"value\">%2</td><td class=\"right\">%3</td><td class=\"value\">%4</td></tr>")
.arg(tr("Final Volume"))
.arg(Brewtarget::displayAmount(recObs->finalVolume_l(), "tab_recipe", "finalVolume_l", Units::liters,2))
.arg(tr("Starting Gravity"))
.arg(Brewtarget::displayAmount(recObs->og(), "tab_recipe", "og", Units::sp_grav, 3));
// fifth row: IBU and Final gravity
body += QString("<tr><td class=\"left\">%1</td><td class=\"value\">%2</td><td class=\"right\">%3</td><td class=\"value\">%4</tr>")
.arg(tr("IBU"))
.arg( Brewtarget::displayAmount(recObs->IBU(),0,1))
.arg(tr("Final Gravity"))
.arg(Brewtarget::displayAmount(recObs->fg(), "tab_recipe", "fg", Units::sp_grav, 3));
// sixth row: ABV and estimate calories
body += QString("<tr><td class=\"left\">%1</td><td class=\"value\">%2%</td><td class=\"right\">%3</td><td class=\"value\">%4</tr>")
.arg(tr("ABV"))
.arg( Brewtarget::displayAmount(recObs->ABV_pct(),0,1) )
.arg( Brewtarget::getVolumeUnitSystem() == SI ? tr("Estimated calories (per 33 cl)") : tr("Estimated calories (per 12 oz)"))
.arg( Brewtarget::displayAmount(Brewtarget::getVolumeUnitSystem() == SI ? recObs->calories33cl() : recObs->calories12oz(),0,0) );
body += "</table>";
return header + body;
}
QString BrewDayScrollWidget::buildInstructionTable()
{
QString middle;
int i, j, size;
middle += QString("<h2>%1</h2>").arg(tr("Instructions"));
middle += QString("<table id=\"steps\">");
middle += QString("<tr><th class=\"check\">%1</th><th class=\"time\">%2</th><th class=\"step\">%3</th></tr>")
.arg(tr("Completed"))
.arg(tr("Time"))
.arg(tr("Step"));
QList<Instruction*> instructions = recObs->instructions();
QList<MashStep*> mashSteps = recObs->mash()->mashSteps();
size = instructions.size();
for( i = 0; i < size; ++i )
{
QString stepTime, tmp;
QList<QString> reagents;
Instruction* ins = instructions[i];
if (ins->interval())
stepTime = Brewtarget::displayAmount(ins->interval(), Units::minutes, 0);
else
stepTime = "--";
tmp = "";
// TODO: comparing ins->name() with these untranslated strings means this
// doesn't work in other languages. Find a better way.
if ( ins->name() == tr("Add grains") )
reagents = recObs->getReagents( recObs->fermentables() );
else if ( ins->name() == tr("Heat water") )
reagents = recObs->getReagents( recObs->mash()->mashSteps() );
else
reagents = ins->reagents();
if ( reagents.size() > 1 )
{
tmp = QString("<ul>");
for ( j = 0; j < reagents.size(); j++ )
{
tmp += QString("<li>%1</li>")
.arg(reagents.at(j));
}
tmp += QString("</ul>");
}
else if ( reagents.size() == 1 )
{
tmp = reagents.at(0);
}
else
{
tmp = ins->directions();
}
QString altTag = i % 2 ? "alt" : "norm";
middle += QString("<tr class=\"%1\"><td class=\"check\"></td><td class=\"time\">%2</td><td align=\"step\">%3 : %4</td></tr>")
.arg(altTag)
.arg(stepTime)
.arg(ins->name())
.arg(tmp);
}
middle += "</table>";
return middle;
}
QString BrewDayScrollWidget::buildFooterTable()
{
QString bottom;
bottom = QString("<table id=\"notes\">");
bottom += QString("<tr><td class=\"left\">%1:</td><td class=\"value\"></td><td class=\"right\">%2:</td><td class=\"value\"></td></tr>")
.arg(tr("Actual PreBoil Volume"))
.arg(tr("Actual PreBoil Gravity"));
bottom += QString("<tr><td class=\"left\">%1:</td><td class=\"value\"></td><td class=\"right\">%2:</td><td class=\"value\"></td></tr>")
.arg(tr("PostBoil Volume"))
.arg(tr("PostBoil Gravity"));
bottom += QString("<tr><td class=\"left\">%1:</td><td class=\"value\"></tr>")
.arg(tr("Volume into fermenter"));
bottom += "</table>";
return bottom;
}