forked from Brewtarget/brewtarget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
2573 lines (2161 loc) · 81.4 KB
/
MainWindow.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MainWindow.cpp is part of Brewtarget, and is Copyright the following
* authors 2009-2014
* - A.J. Drobnich <[email protected]>
* - Dan Cavanagh <[email protected]>
* - David Grundberg <[email protected]>
* - Kregg K <[email protected]>
* - Maxime Lavigne <[email protected]>
* - Mik Firestone <[email protected]>
* - Philip Greggory Lee <[email protected]>
* - plut0nium
* - Samuel Östling <[email protected]>
* - Ted Wright
*
* 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 <QWidget>
#include <QMainWindow>
#include <QMessageBox>
#include <QToolButton>
#include <QSize>
#include <QtGui>
#include <QString>
#include <QFileDialog>
#include <QIcon>
#include <QPixmap>
#include <QList>
#include <QVector>
#include <QVBoxLayout>
#include <QDomDocument>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include <QDomNodeList>
#include <QDomNode>
#include <QDomElement>
#include <QInputDialog>
#include <QLineEdit>
#include <QUrl>
#include <QDesktopServices>
#include <QNetworkReply>
#include <QAction>
#include <QLinearGradient>
#include <QBrush>
#include <QPen>
#include <QDesktopWidget>
#include "Algorithms.h"
#include "MashStepEditor.h"
#include "MashStepTableModel.h"
#include "mash.h"
#include "MashEditor.h"
#include "brewtarget.h"
#include "FermentableEditor.h"
#include "MiscEditor.h"
#include "HopEditor.h"
#include "YeastEditor.h"
#include "YeastTableModel.h"
#include "MiscTableModel.h"
#include "style.h"
#include "recipe.h"
#include "MainWindow.h"
#include "AboutDialog.h"
#include "database.h"
#include "YeastDialog.h"
#include "config.h"
#include "unit.h"
#include "ScaleRecipeTool.h"
#include "HopTableModel.h"
#include "BtDigitWidget.h"
#include "FermentableTableModel.h"
#include "BrewNoteWidget.h"
#include "EquipmentEditor.h"
#include "FermentableDialog.h"
#include "HopDialog.h"
#include "InventoryFormatter.h"
#include "MashWizard.h"
#include "MiscDialog.h"
#include "StyleEditor.h"
#include "OptionDialog.h"
#include "OgAdjuster.h"
#include "ConverterTool.h"
#include "HydrometerTool.h"
#include "TimerMainDialog.h"
#include "RecipeFormatter.h"
#include "PrimingDialog.h"
#include "StrikeWaterDialog.h"
#include "RefractoDialog.h"
#include "MashDesigner.h"
#include "PitchDialog.h"
#include "fermentable.h"
#include "yeast.h"
#include "brewnote.h"
#include "equipment.h"
#include "FermentableTableModel.h"
#include "FermentableSortFilterProxyModel.h"
#include "HopTableModel.h"
#include "HopSortFilterProxyModel.h"
#include "MiscTableModel.h"
#include "MiscSortFilterProxyModel.h"
#include "YeastSortFilterProxyModel.h"
#include "EquipmentListModel.h"
#include "StyleListModel.h"
#include "MashListModel.h"
#include "StyleSortFilterProxyModel.h"
#include "NamedMashEditor.h"
#include "BtDatePopup.h"
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
#include <memory>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
// Need to call this to get all the widgets added (I think).
setupUi(this);
/* PLEASE DO NOT REMOVE.
This code is left here, commented out, intentionally. The only way I can
test internationalization is by forcing the locale manually. I am tired
of having to figure this out every time I need to test.
PLEASE DO NOT REMOVE.
QLocale german(QLocale::German,QLocale::Germany);
QLocale::setDefault(german);
*/
// If the database doesn't load, we bail
if (! Database::instance().loadSuccessful() )
exit(1);
// Set the window title.
setWindowTitle( QString("Brewtarget - %1").arg(VERSIONSTRING) );
// Null out the recipe
recipeObs = 0;
// Set up the printer
printer = new QPrinter;
printer->setPageSize(QPrinter::Letter);
setupCSS();
// initialize all of the dialog windows
setupDialogs();
// initialize the ranged sliders
setupRanges();
// the dialogs have to be setup before this is called
setupComboBoxes();
// do all the work to configure the tables models and their proxies
setupTables();
// Create the keyboard shortcuts
setupShortCuts();
// Once more with the context menus too
setupContextMenu();
// Breaks the naming convention, doesn't it?
restoreSavedState();
// Connect slots to triggered() signals
setupTriggers();
// Connect slots to clicked() signals
setupClicks();
// connect slots to activate() signals
setupActivate();
// connect signal/slots for labels
setupLabels();
// connect signal slots for the text editors
setupTextEdit();
// connect the remaining labels
setupLabels();
// and (finally) set up the drag/drop parts
setupDrops();
// No connections from the database yet? Oh FSM, that probably means I'm
// doing it wrong again.
connect( &(Database::instance()), SIGNAL( deletedSignal(BrewNote*)), this, SLOT( closeBrewNote(BrewNote*)));
}
// Setup the keyboard shortcuts
void MainWindow::setupShortCuts()
{
actionNewRecipe->setShortcut(QKeySequence::New);
actionCopy_Recipe->setShortcut(QKeySequence::Copy);
actionDeleteSelected->setShortcut(QKeySequence::Delete);
}
// Any manipulation of CSS for the MainWindow should be in here
void MainWindow::setupCSS()
{
// Different palettes for some text. This is all done via style sheets now.
QColor wPalette = tabWidget_recipeView->palette().color(QPalette::Active,QPalette::Base);
goodSS = QString( "QLineEdit:read-only { color: #008800; background: %1 }").arg(wPalette.name());
lowSS = QString( "QLineEdit:read-only { color: #0000D0; background: %1 }").arg(wPalette.name());
highSS = QString( "QLineEdit:read-only { color: #D00000; background: %1 }").arg(wPalette.name());
boldSS = QString( "QLineEdit:read-only { font: bold 12px; color: #000000; background: %1 }").arg(wPalette.name());
// The bold style sheet doesn't change, so set it here once.
lineEdit_boilSg->setStyleSheet(boldSS);
}
// Any dialogs should be initialized in here. That should include any initial
// configurations as well
void MainWindow::setupDialogs()
{
dialog_about = new AboutDialog(this);
equipEditor = new EquipmentEditor(this);
singleEquipEditor = new EquipmentEditor(this, true);
fermDialog = new FermentableDialog(this);
fermEditor = new FermentableEditor(this);
hopDialog = new HopDialog(this);
hopEditor = new HopEditor(this);
mashEditor = new MashEditor(this);
mashStepEditor = new MashStepEditor(this);
mashWizard = new MashWizard(this);
miscDialog = new MiscDialog(this);
miscEditor = new MiscEditor(this);
styleEditor = new StyleEditor(this);
singleStyleEditor = new StyleEditor(this,true);
yeastDialog = new YeastDialog(this);
yeastEditor = new YeastEditor(this);
optionDialog = new OptionDialog(this);
recipeScaler = new ScaleRecipeTool(this);
recipeFormatter = new RecipeFormatter(this);
ogAdjuster = new OgAdjuster(this);
converterTool = new ConverterTool(this);
hydrometerTool = new HydrometerTool(this);
timerMainDialog = new TimerMainDialog(this);
primingDialog = new PrimingDialog(this);
strikeWaterDialog = new StrikeWaterDialog(this);
refractoDialog = new RefractoDialog(this);
mashDesigner = new MashDesigner(this);
pitchDialog = new PitchDialog(this);
btDatePopup = new BtDatePopup(this);
// Set up the fileOpener dialog.
fileOpener = new QFileDialog(this, tr("Open"), QDir::homePath(), tr("BeerXML files (*.xml)"));
fileOpener->setAcceptMode(QFileDialog::AcceptOpen);
fileOpener->setFileMode(QFileDialog::ExistingFiles);
fileOpener->setViewMode(QFileDialog::List);
// Set up the fileSaver dialog.
fileSaver = new QFileDialog(this, tr("Save"), QDir::homePath(), tr("BeerXML files (*.xml)") );
fileSaver->setAcceptMode(QFileDialog::AcceptSave);
fileSaver->setFileMode(QFileDialog::AnyFile);
fileSaver->setViewMode(QFileDialog::List);
fileSaver->setDefaultSuffix(QString("xml"));
}
// Configures the range widgets for the bubbles
void MainWindow::setupRanges()
{
styleRangeWidget_og->setRange(1.000, 1.120);
styleRangeWidget_og->setPrecision(3);
styleRangeWidget_og->setTickMarks(0.010, 2);
styleRangeWidget_fg->setRange(1.000, 1.030);
styleRangeWidget_fg->setPrecision(3);
styleRangeWidget_fg->setTickMarks(0.010, 2);
styleRangeWidget_abv->setRange(0.0, 15.0);
styleRangeWidget_abv->setPrecision(1);
styleRangeWidget_abv->setTickMarks(1, 2);
styleRangeWidget_ibu->setRange(0.0, 120.0);
styleRangeWidget_ibu->setPrecision(1);
styleRangeWidget_ibu->setTickMarks(10, 2);
const int srmMax = 50;
styleRangeWidget_srm->setRange(0.0, static_cast<double>(srmMax));
styleRangeWidget_srm->setPrecision(1);
styleRangeWidget_srm->setTickMarks(10, 2);
// Need to change appearance of color slider
{
// The styleRangeWidget_srm should display beer color in the background
QLinearGradient grad( 0,0, 1,0 );
grad.setCoordinateMode(QGradient::ObjectBoundingMode);
for( int i=0; i <= srmMax; ++i )
{
double srm = i;
grad.setColorAt( srm/static_cast<double>(srmMax), Algorithms::srmToColor(srm));
}
styleRangeWidget_srm->setBackgroundBrush(grad);
// The styleRangeWidget_srm should display a "window" to show acceptable colors for the style
styleRangeWidget_srm->setPreferredRangeBrush(QColor(0,0,0,0));
styleRangeWidget_srm->setPreferredRangePen(QPen(Qt::black, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
// Half-height "tick" for color marker
grad = QLinearGradient( 0,0, 0,1 );
grad.setCoordinateMode(QGradient::ObjectBoundingMode);
grad.setColorAt( 0, QColor(255,255,255,255) );
grad.setColorAt( 0.49, QColor(255,255,255,255) );
grad.setColorAt( 0.50, QColor(255,255,255,0) );
grad.setColorAt( 1, QColor(255,255,255,0) );
styleRangeWidget_srm->setMarkerBrush(grad);
}
}
// Any new combo boxes, along with their list models, should be initialized
// here
void MainWindow::setupComboBoxes()
{
// Set equipment combo box model.
equipmentListModel = new EquipmentListModel(equipmentComboBox);
equipmentComboBox->setModel(equipmentListModel);
// Set the style combo box
styleListModel = new StyleListModel(styleComboBox);
styleProxyModel = new StyleSortFilterProxyModel(styleComboBox);
styleProxyModel->setDynamicSortFilter(true);
styleProxyModel->setSourceModel(styleListModel);
styleComboBox->setModel(styleProxyModel);
// Set the mash combo box
mashListModel = new MashListModel(mashComboBox);
mashComboBox->setModel(mashListModel);
// Nothing to say.
namedMashEditor = new NamedMashEditor(this, mashStepEditor);
// I don't think this is used yet
singleNamedMashEditor = new NamedMashEditor(this,mashStepEditor,true);
}
// Anything creating new tables models, filter proxies and configuring the two
// should go in here
void MainWindow::setupTables()
{
// Set table models.
// Fermentables
fermTableModel = new FermentableTableModel(fermentableTable);
fermTableProxy = new FermentableSortFilterProxyModel(fermentableTable,false);
fermTableProxy->setSourceModel(fermTableModel);
fermentableTable->setItemDelegate(new FermentableItemDelegate(fermentableTable));
fermentableTable->setModel(fermTableProxy);
// Make the fermentable table show grain percentages in row headers.
fermTableModel->setDisplayPercentages(true);
// Double clicking the name column pops up an edit dialog for the selected item
connect( fermentableTable, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
MainWindow::editSelectedFermentable();
});
// Hops
hopTableModel = new HopTableModel(hopTable);
hopTableProxy = new HopSortFilterProxyModel(hopTable, false);
hopTableProxy->setSourceModel(hopTableModel);
hopTable->setItemDelegate(new HopItemDelegate(hopTable));
hopTable->setModel(hopTableProxy);
// Hop table show IBUs in row headers.
hopTableModel->setShowIBUs(true);
connect( hopTable, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
MainWindow::editSelectedHop();
});
// Misc
miscTableModel = new MiscTableModel(miscTable);
miscTableProxy = new MiscSortFilterProxyModel(miscTable,false);
miscTableProxy->setSourceModel(miscTableModel);
miscTable->setItemDelegate(new MiscItemDelegate(miscTable));
miscTable->setModel(miscTableProxy);
connect( miscTable, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
MainWindow::editSelectedMisc();
});
// Yeast
yeastTableModel = new YeastTableModel(yeastTable);
yeastTableProxy = new YeastSortFilterProxyModel(yeastTable,false);
yeastTableProxy->setSourceModel(yeastTableModel);
yeastTable->setItemDelegate(new YeastItemDelegate(yeastTable));
yeastTable->setModel(yeastTableProxy);
connect( yeastTable, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
MainWindow::editSelectedYeast();
});
// Mashes
mashStepTableModel = new MashStepTableModel(mashStepTableWidget);
mashStepTableWidget->setItemDelegate(new MashStepItemDelegate());
mashStepTableWidget->setModel(mashStepTableModel);
connect( mashStepTableWidget, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
MainWindow::editSelectedMashStep();
});
// Enable sorting in the main tables.
fermentableTable->horizontalHeader()->setSortIndicator( FERMAMOUNTCOL, Qt::DescendingOrder );
fermentableTable->setSortingEnabled(true);
fermTableProxy->setDynamicSortFilter(true);
hopTable->horizontalHeader()->setSortIndicator( HOPTIMECOL, Qt::DescendingOrder );
hopTable->setSortingEnabled(true);
hopTableProxy->setDynamicSortFilter(true);
miscTable->horizontalHeader()->setSortIndicator( MISCUSECOL, Qt::DescendingOrder );
miscTable->setSortingEnabled(true);
miscTableProxy->setDynamicSortFilter(true);
yeastTable->horizontalHeader()->setSortIndicator( YEASTNAMECOL, Qt::DescendingOrder );
yeastTable->setSortingEnabled(true);
yeastTableProxy->setDynamicSortFilter(true);
}
// Anything resulting in a restoreState() should go in here
void MainWindow::restoreSavedState()
{
QDesktopWidget *desktop = QApplication::desktop();
// If we saved a size the last time we ran, use it
if ( Brewtarget::hasOption("geometry"))
{
restoreGeometry(Brewtarget::option("geometry").toByteArray());
restoreState(Brewtarget::option("windowState").toByteArray());
}
else
{
// otherwise, guess a reasonable size at 1/4 of the screen.
int width = desktop->width();
int height = desktop->height();
this->resize(width/2,height/2);
}
// If we saved the selected recipe name the last time we ran, select it and show it.
if (Brewtarget::hasOption("recipeKey"))
{
int key = Brewtarget::option("recipeKey").toInt();
recipeObs = Database::instance().recipe( key );
QModelIndex rIdx = treeView_recipe->findElement(recipeObs);
setRecipe(recipeObs);
setTreeSelection(rIdx);
}
else
{
QList<Recipe*> recs = Database::instance().recipes();
if( recs.size() > 0 )
setRecipe( recs[0] );
}
//UI restore state
if (Brewtarget::hasOption("MainWindow/splitter_vertical_State"))
splitter_vertical->restoreState(Brewtarget::option("MainWindow/splitter_vertical_State").toByteArray());
if (Brewtarget::hasOption("MainWindow/splitter_horizontal_State"))
splitter_horizontal->restoreState(Brewtarget::option("MainWindow/splitter_horizontal_State").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_recipe_headerState"))
treeView_recipe->header()->restoreState(Brewtarget::option("MainWindow/treeView_recipe_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_style_headerState"))
treeView_style->header()->restoreState(Brewtarget::option("MainWindow/treeView_style_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_equip_headerState"))
treeView_equip->header()->restoreState(Brewtarget::option("MainWindow/treeView_equip_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_ferm_headerState"))
treeView_ferm->header()->restoreState(Brewtarget::option("MainWindow/treeView_ferm_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_hops_headerState"))
treeView_hops->header()->restoreState(Brewtarget::option("MainWindow/treeView_hops_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_misc_headerState"))
treeView_misc->header()->restoreState(Brewtarget::option("MainWindow/treeView_misc_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/treeView_yeast_headerState"))
treeView_yeast->header()->restoreState(Brewtarget::option("MainWindow/treeView_yeast_headerState").toByteArray());
if (Brewtarget::hasOption("MainWindow/mashStepTableWidget_headerState"))
mashStepTableWidget->horizontalHeader()->restoreState(Brewtarget::option("MainWindow/mashStepTableWidget_headerState").toByteArray());
}
// anything with a SIGNAL of triggered() should go in here.
void MainWindow::setupTriggers()
{
// actions
connect( actionExit, &QAction::triggered, this, &QWidget::close );
connect( actionAbout_BrewTarget, &QAction::triggered, dialog_about, &QWidget::show );
connect( actionNewRecipe, &QAction::triggered, this, &MainWindow::newRecipe );
connect( actionImport_Recipes, &QAction::triggered, this, &MainWindow::importFiles );
connect( actionExportRecipe, &QAction::triggered, this, &MainWindow::exportRecipe );
connect( actionEquipments, &QAction::triggered, equipEditor, &QWidget::show );
connect( actionMashs, &QAction::triggered, namedMashEditor, &QWidget::show );
connect( actionStyles, &QAction::triggered, styleEditor, &QWidget::show );
connect( actionFermentables, &QAction::triggered, fermDialog, &QWidget::show );
connect( actionHops, &QAction::triggered, hopDialog, &QWidget::show );
connect( actionMiscs, &QAction::triggered, miscDialog, &QWidget::show );
connect( actionYeasts, &QAction::triggered, yeastDialog, &QWidget::show );
connect( actionOptions, &QAction::triggered, optionDialog, &OptionDialog::show );
connect( actionManual, &QAction::triggered, this, &MainWindow::openManual );
connect( actionScale_Recipe, &QAction::triggered, recipeScaler, &QWidget::show );
connect( action_recipeToTextClipboard, &QAction::triggered, recipeFormatter, &RecipeFormatter::toTextClipboard );
connect( actionConvert_Units, &QAction::triggered, converterTool, &QWidget::show );
connect( actionHydrometer_Temp_Adjustment, &QAction::triggered, hydrometerTool, &QWidget::show );
connect( actionOG_Correction_Help, &QAction::triggered, ogAdjuster, &QWidget::show );
connect( actionCopy_Recipe, &QAction::triggered, this, &MainWindow::copyRecipe );
connect( actionPriming_Calculator, &QAction::triggered, primingDialog, &QWidget::show );
connect( actionStrikeWater_Calculator, &QAction::triggered, strikeWaterDialog, &QWidget::show );
connect( actionRefractometer_Tools, &QAction::triggered, refractoDialog, &QWidget::show );
connect( actionPitch_Rate_Calculator, &QAction::triggered, this, &MainWindow::showPitchDialog);
connect( actionMergeDatabases, &QAction::triggered, this, &MainWindow::updateDatabase );
connect( actionTimers, &QAction::triggered, timerMainDialog, &QWidget::show );
connect( actionDeleteSelected, &QAction::triggered, this, &MainWindow::deleteSelected );
// postgresql cannot backup or restore yet. I would like to find some way
// around this, but for now just disable
if ( Brewtarget::dbType() == Brewtarget::PGSQL ) {
actionBackup_Database->setEnabled(false);
actionRestore_Database->setEnabled(false);
label_Brewtarget->setToolTip( recipeFormatter->getLabelToolTip());
}
else {
connect( actionBackup_Database, &QAction::triggered, this, &MainWindow::backup );
connect( actionRestore_Database, &QAction::triggered, this, &MainWindow::restoreFromBackup );
}
// Printing signals/slots.
// Refactoring is good. It's like a rye saison fermenting away
connect(actionRecipePrint, &QAction::triggered, [this]() {
print([this](QPrinter* printer) {
recipeFormatter->print(
printer, RecipeFormatter::PRINT);
});
});
connect(actionRecipePreview, &QAction::triggered, [this]() {
recipeFormatter->print(printer, RecipeFormatter::PREVIEW);
});
connect(actionRecipeHTML, &QAction::triggered, this, [this]() {
exportHTML([this](QFile* file) {
recipeFormatter->print(printer, RecipeFormatter::HTML, file);
});
});
connect(actionRecipeBBCode, &QAction::triggered, [this]() {
QApplication::clipboard()->setText(recipeFormatter->getBBCodeFormat());
});
connect(actionBrewdayPrint, &QAction::triggered, [this]() {
print([this](QPrinter* printer) {
brewDayScrollWidget->print(
printer, BrewDayScrollWidget::PRINT);
});
});
connect(actionBrewdayPreview, &QAction::triggered, [this]() {
brewDayScrollWidget->print(printer, RecipeFormatter::PREVIEW);
});
connect(actionBrewdayHTML, &QAction::triggered, this, [this]() {
exportHTML([this](QFile* file) {
brewDayScrollWidget->print(
printer, BrewDayScrollWidget::PRINT);
});
});
connect(actionInventoryPrint, &QAction::triggered, [this]() {
print(
[](QPrinter* printer) { InventoryFormatter::print(printer); });
});
connect(actionInventoryPreview, &QAction::triggered,
[]() { InventoryFormatter::printPreview(); });
connect(actionInventoryHTML, &QAction::triggered, [this]() {
exportHTML(
[](QFile* file) { InventoryFormatter::exportHTML(file); });
});
}
// anything with a SIGNAL of clicked() should go in here.
void MainWindow::setupClicks()
{
connect( equipmentButton, &QAbstractButton::clicked, this, &MainWindow::showEquipmentEditor);
connect( styleButton, &QAbstractButton::clicked, this, &MainWindow::showStyleEditor );
connect( mashButton, &QAbstractButton::clicked, mashEditor, &MashEditor::showEditor );
connect( pushButton_addFerm, &QAbstractButton::clicked, fermDialog, &QWidget::show );
connect( pushButton_addHop, &QAbstractButton::clicked, hopDialog, &QWidget::show );
connect( pushButton_addMisc, &QAbstractButton::clicked, miscDialog, &QWidget::show );
connect( pushButton_addYeast, &QAbstractButton::clicked, yeastDialog, &QWidget::show );
connect( pushButton_removeFerm, &QAbstractButton::clicked, this, &MainWindow::removeSelectedFermentable );
connect( pushButton_removeHop, &QAbstractButton::clicked, this, &MainWindow::removeSelectedHop );
connect( pushButton_removeMisc, &QAbstractButton::clicked, this, &MainWindow::removeSelectedMisc );
connect( pushButton_removeYeast, &QAbstractButton::clicked, this, &MainWindow::removeSelectedYeast );
connect( pushButton_editFerm, &QAbstractButton::clicked, this, &MainWindow::editSelectedFermentable );
connect( pushButton_editMisc, &QAbstractButton::clicked, this, &MainWindow::editSelectedMisc );
connect( pushButton_editHop, &QAbstractButton::clicked, this, &MainWindow::editSelectedHop );
connect( pushButton_editYeast, &QAbstractButton::clicked, this, &MainWindow::editSelectedYeast );
connect( pushButton_editMash, &QAbstractButton::clicked, mashEditor, &MashEditor::showEditor );
connect( pushButton_addMashStep, &QAbstractButton::clicked, this, &MainWindow::addMashStep );
connect( pushButton_removeMashStep, &QAbstractButton::clicked, this, &MainWindow::removeSelectedMashStep );
connect( pushButton_editMashStep, &QAbstractButton::clicked, this, &MainWindow::editSelectedMashStep );
connect( pushButton_mashWizard, &QAbstractButton::clicked, mashWizard, &MashWizard::show );
connect( pushButton_saveMash, &QAbstractButton::clicked, this, &MainWindow::saveMash );
connect( pushButton_mashDes, &QAbstractButton::clicked, mashDesigner, &MashDesigner::show );
connect( pushButton_mashUp, &QAbstractButton::clicked, this, &MainWindow::moveSelectedMashStepUp );
connect( pushButton_mashDown, &QAbstractButton::clicked, this, &MainWindow::moveSelectedMashStepDown );
connect( pushButton_mashRemove, &QAbstractButton::clicked, this, &MainWindow::removeMash );
}
// anything with a SIGNAL of activated() should go in here.
void MainWindow::setupActivate()
{
connect( equipmentComboBox, SIGNAL( activated(int) ), this, SLOT(updateRecipeEquipment()) );
connect( styleComboBox, SIGNAL( activated(int) ), this, SLOT(updateRecipeStyle()) );
connect( mashComboBox, SIGNAL( activated(int) ), this, SLOT(updateRecipeMash()) );
}
// anything with either an editingFinished() or a textModified() should go in
// here
void MainWindow::setupTextEdit()
{
connect( lineEdit_name, &QLineEdit::editingFinished, this, &MainWindow::updateRecipeName );
connect( lineEdit_batchSize, &BtLineEdit::textModified, this, &MainWindow::updateRecipeBatchSize );
connect( lineEdit_boilSize, &BtLineEdit::textModified, this, &MainWindow::updateRecipeBoilSize );
connect( lineEdit_boilTime, &BtLineEdit::textModified, this, &MainWindow::updateRecipeBoilTime );
connect( lineEdit_efficiency, &BtLineEdit::textModified, this, &MainWindow::updateRecipeEfficiency );
}
// anything using a BtLabel::labelChanged signal should go in here
void MainWindow::setupLabels()
{
// These are the sliders. I need to consider these harder, but small steps
connect(oGLabel, &BtLabel::labelChanged,
this, &MainWindow::redisplayLabel);
connect(fGLabel, &BtLabel::labelChanged,
this, &MainWindow::redisplayLabel);
connect(colorSRMLabel, &BtLabel::labelChanged,
this, &MainWindow::redisplayLabel);
}
// anything with a BtTabWidget::set* signal should go in here
void MainWindow::setupDrops()
{
// drag and drop. maybe
connect( tabWidget_recipeView, &BtTabWidget::setRecipe,
this, &MainWindow::setRecipe);
connect( tabWidget_recipeView, &BtTabWidget::setEquipment,
this, &MainWindow::droppedRecipeEquipment);
connect( tabWidget_recipeView, &BtTabWidget::setStyle,
this, &MainWindow::droppedRecipeStyle);
connect( tabWidget_ingredients, &BtTabWidget::setFermentables,
this, &MainWindow::droppedRecipeFermentable);
connect( tabWidget_ingredients, &BtTabWidget::setHops,
this, &MainWindow::droppedRecipeHop);
connect( tabWidget_ingredients, &BtTabWidget::setMiscs,
this, &MainWindow::droppedRecipeMisc);
connect( tabWidget_ingredients, &BtTabWidget::setYeasts,
this, &MainWindow::droppedRecipeYeast);
}
void MainWindow::deleteSelected()
{
QModelIndexList selected;
BtTreeView* active = qobject_cast<BtTreeView*>(tabWidget_Trees->currentWidget()->focusWidget());
// This happens after startup when nothing is selected
if (!active)
return;
active->deleteSelected(active->selectionModel()->selectedRows());
// This should be fixed to find the first nonfolder object in the tree
QModelIndex first = active->first();
if ( first.isValid() )
{
if (active->type(first) == BtTreeItem::RECIPE)
setRecipe(treeView_recipe->recipe(first));
setTreeSelection(first);
}
}
void MainWindow::treeActivated(const QModelIndex &index)
{
Equipment *kit;
Fermentable *ferm;
Hop* h;
Misc *m;
Yeast *y;
Style *s;
QObject* calledBy = sender();
BtTreeView* active;
// Not sure how this could happen, but better safe the sigsegv'd
if ( calledBy == 0 )
return;
active = qobject_cast<BtTreeView*>(calledBy);
// If the sender cannot be morphed into a BtTreeView object
if ( active == 0 )
return;
switch( active->type(index))
{
case BtTreeItem::RECIPE:
setRecipe(treeView_recipe->recipe(index));
break;
case BtTreeItem::EQUIPMENT:
kit = active->equipment(index);
if ( kit )
{
singleEquipEditor->setEquipment(kit);
singleEquipEditor->show();
}
break;
case BtTreeItem::FERMENTABLE:
ferm = active->fermentable(index);
if ( ferm )
{
fermEditor->setFermentable(ferm);
fermEditor->show();
}
break;
case BtTreeItem::HOP:
h = active->hop(index);
if (h)
{
hopEditor->setHop(h);
hopEditor->show();
}
break;
case BtTreeItem::MISC:
m = active->misc(index);
if (m)
{
miscEditor->setMisc(m);
miscEditor->show();
}
break;
case BtTreeItem::STYLE:
s = active->style(index);
if ( s )
{
singleStyleEditor->setStyle(s);
singleStyleEditor->show();
}
break;
case BtTreeItem::YEAST:
y = active->yeast(index);
if (y)
{
yeastEditor->setYeast(y);
yeastEditor->show();
}
break;
case BtTreeItem::BREWNOTE:
setBrewNoteByIndex(index);
break;
case BtTreeItem::FOLDER: // default behavior is fine, but no warning
break;
default:
Brewtarget::logW(QString("MainWindow::treeActivated Unknown type %1.").arg(treeView_recipe->type(index)));
}
treeView_recipe->setCurrentIndex(index);
}
void MainWindow::setBrewNoteByIndex(const QModelIndex &index)
{
BrewNoteWidget* ni;
BrewNote* bNote = treeView_recipe->brewNote(index);
if ( ! bNote )
return;
// HERE
// This is some clean up work. REMOVE FROM HERE TO THERE
if ( bNote->projPoints() < 15 )
{
double pnts = bNote->projPoints();
bNote->setProjPoints(pnts);
}
if ( bNote->effIntoBK_pct() < 10 )
{
bNote->calculateEffIntoBK_pct();
bNote->calculateBrewHouseEff_pct();
}
// THERE
Recipe* parent = Database::instance().getParentRecipe(bNote);
// I think this means a brew note for a different recipe has been selected.
// We need to select that recipe, which will clear the current tabs
if ( parent != recipeObs )
setRecipe(parent);
ni = findBrewNoteWidget(bNote);
if ( ! ni )
{
ni = new BrewNoteWidget(tabWidget_recipeView);
ni->setBrewNote(bNote);
}
tabWidget_recipeView->addTab(ni,bNote->brewDate_short());
tabWidget_recipeView->setCurrentWidget(ni);
}
BrewNoteWidget* MainWindow::findBrewNoteWidget(BrewNote* b)
{
for (int i = 0; i < tabWidget_recipeView->count(); ++i)
{
if (tabWidget_recipeView->widget(i)->objectName() == "BrewNoteWidget")
{
BrewNoteWidget* ni = qobject_cast<BrewNoteWidget*>(tabWidget_recipeView->widget(i));
if ( ni->isBrewNote(b) )
return ni;
}
}
return 0;
}
void MainWindow::setBrewNote(BrewNote* bNote)
{
QString tabname;
BrewNoteWidget* ni = findBrewNoteWidget(bNote);
if ( ni )
{
tabWidget_recipeView->setCurrentWidget(ni);
return;
}
ni = new BrewNoteWidget(tabWidget_recipeView);
ni->setBrewNote(bNote);
tabWidget_recipeView->addTab(ni,bNote->brewDate_short());
tabWidget_recipeView->setCurrentWidget(ni);
}
// Can handle null recipes.
void MainWindow::setRecipe(Recipe* recipe)
{
int tabs = 0;
// Don't like void pointers.
if( recipe == 0 )
return;
// Make sure this MainWindow is paying attention...
if( recipeObs )
disconnect( recipeObs, 0, this, 0 );
recipeObs = recipe;
recStyle = recipe->style();
recEquip = recipe->equipment();
if( recStyle )
{
styleRangeWidget_og->setPreferredRange(Brewtarget::displayRange(recStyle, tab_recipe, "og", Brewtarget::DENSITY ));
styleRangeWidget_fg->setPreferredRange(Brewtarget::displayRange(recStyle, tab_recipe, "fg", Brewtarget::DENSITY ));
styleRangeWidget_abv->setPreferredRange(recStyle->abvMin_pct(), recStyle->abvMax_pct());
styleRangeWidget_ibu->setPreferredRange(recStyle->ibuMin(), recStyle->ibuMax());
styleRangeWidget_srm->setPreferredRange(Brewtarget::displayRange(recStyle, tab_recipe, "color_srm", Brewtarget::COLOR ));
}
// Reset all previous recipe shit.
fermTableModel->observeRecipe(recipe);
hopTableModel->observeRecipe(recipe);
miscTableModel->observeRecipe(recipe);
yeastTableModel->observeRecipe(recipe);
mashStepTableModel->setMash(recipeObs->mash());
// Clean out any brew notes
tabWidget_recipeView->setCurrentIndex(0);
// Start closing from the right (highest index) down. Anything else dumps
// core in the most unpleasant of fashions
tabs = tabWidget_recipeView->count() - 1;
for (int i = tabs; i >= 0; --i)
{
if (tabWidget_recipeView->widget(i)->objectName() == "BrewNoteWidget")
tabWidget_recipeView->removeTab(i);
}
// Tell some of our other widgets to observe the new recipe.
mashWizard->setRecipe(recipe);
brewDayScrollWidget->setRecipe(recipe);
equipmentListModel->observeRecipe(recipe);
recipeFormatter->setRecipe(recipe);
ogAdjuster->setRecipe(recipe);
recipeExtrasWidget->setRecipe(recipe);
mashDesigner->setRecipe(recipe);
equipmentButton->setRecipe(recipe);
singleEquipEditor->setEquipment(recEquip);
styleButton->setRecipe(recipe);
singleStyleEditor->setStyle(recStyle);
mashEditor->setMash(recipeObs->mash());
mashEditor->setEquipment(recEquip);
mashButton->setMash(recipeObs->mash());
recipeScaler->setRecipe(recipeObs);
// If you don't connect this late, every previous set of an attribute
// causes this signal to be slotted, which then causes showChanges() to be
// called.
connect( recipeObs, SIGNAL(changed(QMetaProperty,QVariant)), this, SLOT(changed(QMetaProperty,QVariant)) );
showChanges();
}
void MainWindow::changed(QMetaProperty prop, QVariant value)
{
QString propName(prop.name());
if( propName == "equipment" )
{
Equipment* newRecEquip = qobject_cast<Equipment*>(BeerXMLElement::extractPtr(value));
recEquip = newRecEquip;
singleEquipEditor->setEquipment(recEquip);
}
else if( propName == "style" )
{
//recStyle = recipeObs->style();
recStyle = qobject_cast<Style*>(BeerXMLElement::extractPtr(value));
singleStyleEditor->setStyle(recStyle);
}
showChanges(&prop);
}
void MainWindow::updateDensitySlider(QString attribute, RangedSlider* slider, double max)
{
Unit::unitDisplay dispUnit = (Unit::unitDisplay)Brewtarget::option(attribute, Unit::noUnit, "tab_recipe", Brewtarget::UNIT).toInt();
if ( dispUnit == Unit::noUnit )
dispUnit = Brewtarget::densityUnit == Brewtarget::PLATO ? Unit::displayPlato : Unit::displaySg;
slider->setPreferredRange(Brewtarget::displayRange(recStyle, tab_recipe, attribute, Brewtarget::DENSITY));
slider->setRange( Brewtarget::displayRange(tab_recipe, attribute, 1.000, max, Brewtarget::DENSITY ));
if ( dispUnit == Unit::displayPlato )
{
slider->setPrecision(1);
slider->setTickMarks(2,5);
}
else
{
slider->setPrecision(3);
slider->setTickMarks(0.010, 2);
}
}
void MainWindow::updateColorSlider(QString attribute, RangedSlider* slider)
{
Unit::unitDisplay dispUnit = (Unit::unitDisplay)Brewtarget::option(attribute, Unit::noUnit, "tab_recipe", Brewtarget::UNIT).toInt();
if ( dispUnit == Unit::noUnit )
dispUnit = Brewtarget::colorUnit == Brewtarget::SRM ? Unit::displaySrm : Unit::displayEbc;
slider->setPreferredRange(Brewtarget::displayRange(recStyle, tab_recipe, attribute,Brewtarget::COLOR));
slider->setRange(Brewtarget::displayRange(tab_recipe, attribute, 1, 44, Brewtarget::COLOR) );
slider->setTickMarks( dispUnit == Unit::displaySrm ? 10 : 40, 2);
}
void MainWindow::showChanges(QMetaProperty* prop)
{
if( recipeObs == 0 )
return;
bool updateAll = (prop == 0);
QString propName;
if( prop )
{
propName = prop->name();
}
// May St. Stevens preserve me
lineEdit_name->setText(recipeObs->name());
lineEdit_batchSize->setText(recipeObs);
lineEdit_boilSize->setText(recipeObs);
lineEdit_efficiency->setText(recipeObs);
lineEdit_boilTime->setText(recipeObs);
lineEdit_name->setCursorPosition(0);
lineEdit_batchSize->setCursorPosition(0);
lineEdit_boilSize->setCursorPosition(0);
lineEdit_efficiency->setCursorPosition(0);
lineEdit_boilTime->setCursorPosition(0);
lineEdit_calcBatchSize->setText(recipeObs);
lineEdit_calcBoilSize->setText(recipeObs);
// Color manipulation
if( 0.95*recipeObs->batchSize_l() <= recipeObs->finalVolume_l() && recipeObs->finalVolume_l() <= 1.05*recipeObs->batchSize_l() )
lineEdit_calcBatchSize->setStyleSheet(goodSS);
else if( recipeObs->finalVolume_l() < 0.95*recipeObs->batchSize_l() )
lineEdit_calcBatchSize->setStyleSheet(lowSS);
else
lineEdit_calcBatchSize->setStyleSheet(highSS);
if( 0.95*recipeObs->boilSize_l() <= recipeObs->boilVolume_l() && recipeObs->boilVolume_l() <= 1.05*recipeObs->boilSize_l() )
lineEdit_calcBoilSize->setStyleSheet(goodSS);
else if( recipeObs->boilVolume_l() < 0.95* recipeObs->boilSize_l() )
lineEdit_calcBoilSize->setStyleSheet(lowSS);
else
lineEdit_calcBoilSize->setStyleSheet(highSS);
lineEdit_boilSg->setText(recipeObs);