File: view.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (308 lines) | stat: -rw-r--r-- 10,381 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
  view.cpp
  -------------------
  View class for application
  -------------------
  Copyright 2006-2008 David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QHeaderView>
#include <QTableView>
#include <QLocale>

#include "data.h"
#include "recipe.h"
#include "resource.h"

#include "graindelegate.h"
#include "grainmodel.h"
#include "hopdelegate.h"
#include "hopmodel.h"
#include "miscdelegate.h"
#include "miscmodel.h"
#include "notepage.h"
#include "view.h"

//////////////////////////////////////////////////////////////////////////////
// Construction, Destruction                                                //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// View()
// ------
// Constructor

View::View(QWidget *parent, Recipe *recipe)
    : QWidget(parent), recipe_(recipe), grainmodel_(0), hopmodel_(0),
      miscmodel_(0), notepage_(0)
{
    ui.setupUi(this);

    // get current font information
    QFont bold(font());
    bold.setBold(true);
    QFontMetrics fm(font());
    unsigned mh = (unsigned)(fm.lineSpacing() * 1.5);
    unsigned mw = fm.width('M');

    // additional setup
    ui.ogreclabel->setFont(bold);
    ui.ogrec->setFont(bold);
    ui.ibureclabel->setFont(bold);
    ui.iburec->setFont(bold);
    ui.srmreclabel->setFont(bold);
    ui.srmrec->setFont(bold);
    ui.abvlabel->setFont(bold);
    ui.abv->setFont(bold);
    ui.abwlabel->setFont(bold);
    ui.abw->setFont(bold);
    ui.fglabel->setFont(bold);
    ui.fg->setFont(bold);

    // grain page
    QWidget *widget = new QWidget();
    grainpage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Grains"));

    grainmodel_ = new GrainModel(this, recipe_->grains());
    grainpage.view->setModel(grainmodel_);
    QItemDelegate *delegate = new GrainDelegate(this);
    grainpage.view->setItemDelegate(delegate);

    grainpage.view->verticalHeader()->setDefaultSectionSize(mh);
    grainpage.view->verticalHeader()->hide();
    grainpage.view->horizontalHeader()->setClickable(true);
    grainpage.view->horizontalHeader()->setHighlightSections(false);

    grainpage.view->setColumnWidth(GrainModel::NAME, 20*mw);
    grainpage.view->setColumnWidth(GrainModel::WEIGHT, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::EXTRACT, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::COLOR, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::TYPE, 8*mw);
    grainpage.view->setColumnWidth(GrainModel::USE, 8*mw);

    // hop page
    widget = new QWidget();
    hoppage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Hops"));

    hopmodel_ = new HopModel(this, recipe_->hops());
    hoppage.view->setModel(hopmodel_);
    delegate = new HopDelegate(this);
    hoppage.view->setItemDelegate(delegate);

    hoppage.view->verticalHeader()->setDefaultSectionSize(mh);
    hoppage.view->verticalHeader()->hide();
    hoppage.view->horizontalHeader()->setClickable(true);
    hoppage.view->horizontalHeader()->setHighlightSections(false);

    hoppage.view->setColumnWidth(HopModel::NAME, 20*mw);
    hoppage.view->setColumnWidth(HopModel::WEIGHT, 8*mw);
    hoppage.view->setColumnWidth(HopModel::ALPHA, 8*mw);
    hoppage.view->setColumnWidth(HopModel::TIME, 8*mw);
    hoppage.view->setColumnWidth(HopModel::TYPE, 8*mw);

    // misc page
    widget = new QWidget();
    miscpage.setupUi(widget);
    ui.ingredients->addTab(widget, tr("&Miscellaneous"));

    miscmodel_ = new MiscModel(this, recipe_->miscs());
    miscpage.view->setModel(miscmodel_);
    delegate = new MiscDelegate(this);
    miscpage.view->setItemDelegate(delegate);

    miscpage.view->verticalHeader()->setDefaultSectionSize(mh);
    miscpage.view->verticalHeader()->hide();
    miscpage.view->horizontalHeader()->setClickable(true);
    miscpage.view->horizontalHeader()->setHighlightSections(false);

    miscpage.view->setColumnWidth(MiscModel::NAME, 20*mw);
    miscpage.view->setColumnWidth(MiscModel::QUANTITY, 8*mw);
    miscpage.view->setColumnWidth(MiscModel::TYPE, 8*mw);
    miscpage.view->horizontalHeader()->setStretchLastSection(true);

    // note page
    notepage_ = new NotePage(0, recipe_);
    ui.ingredients->addTab(notepage_, tr("&Notes"));

    // widget connections
    connect(grainmodel_, SIGNAL(modified()),
            this, SLOT(modelModified()));
    connect(grainpage.addbutton, SIGNAL(clicked()),
            grainpage.view, SLOT(addIngredient()));
    connect(grainpage.removebutton, SIGNAL(clicked()),
            grainpage.view, SLOT(removeIngredient()));

    connect(hopmodel_, SIGNAL(modified()),
            this, SLOT(modelModified()));
    connect(hoppage.addbutton, SIGNAL(clicked()),
            hoppage.view, SLOT(addIngredient()));
    connect(hoppage.removebutton, SIGNAL(clicked()),
            hoppage.view, SLOT(removeIngredient()));

    connect(miscmodel_, SIGNAL(modified()),
            this, SLOT(modelModified()));
    connect(miscpage.addbutton, SIGNAL(clicked()),
            miscpage.view, SLOT(addIngredient()));
    connect(miscpage.removebutton, SIGNAL(clicked()),
            miscpage.view, SLOT(removeIngredient()));

    connect(ui.titleedit, SIGNAL(textChanged(const QString &)),
            this, SLOT(setTitle(const QString &)));
    connect(ui.stylecombo, SIGNAL(activated(const QString &)),
            this, SLOT(setStyle(const QString &)));
    connect(ui.breweredit, SIGNAL(textChanged(const QString &)),
            this, SLOT(setBrewer(const QString &)));
    connect(ui.sizespin, SIGNAL(valueChanged(double)),
            this, SLOT(setSize(double)));

    connect(recipe_, SIGNAL(recipeChanged()), this, SLOT(flush()));
    connect(recipe_, SIGNAL(recipeModified()), this, SLOT(refresh()));

    // start with new view
    flush();
}

View::~View() { ; }

//////////////////////////////////////////////////////////////////////////////
// Slot Implementations                                                     //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// setTitle()
// ----------
// set the recipe title

void View::setTitle(const QString &title)
{
    recipe_->setTitle(title);
}

//////////////////////////////////////////////////////////////////////////////
// setStyle()
// ----------
// set the recipe style

void View::setStyle(const QString &style)
{
    recipe_->setStyle(style);
}

//////////////////////////////////////////////////////////////////////////////
// Brewer()
// --------
// set the recipeCombo brewer

void View::setBrewer(const QString &brewer)
{
    recipe_->setBrewer(brewer);
}

//////////////////////////////////////////////////////////////////////////////
// setSize()
// ---------
// set the recipe size

void View::setSize(double size)
{
    recipe_->setSize(Volume(size, recipe_->size().unit()));
}

///////////////////////////////////////////////////////////////////////////////
// Miscellaneous                                                             //
///////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// modelModified()
// ---------------
// One of the models has been modified

void View::modelModified()
{
    recipe_->recalc();
    recipe_->setModified(true);
}

//////////////////////////////////////////////////////////////////////////////
// flush()
// -------
// flush/reset the entire view

void View::flush()
{
    // save the modified flag to prevent side effects of setting widgets
    bool oldmod = recipe_->modified();
    recipe_->blockSignals(true);

    // update recipe widgets
    ui.titleedit->setText(recipe_->title());
    ui.breweredit->setText(recipe_->brewer());

    ui.sizespin->setValue(recipe_->size().amount());
    ui.sizespin->setSuffix(" " + recipe_->size().unit().name());

    // set style combo to style
    ui.stylecombo->clear();
    ui.stylecombo->addItems(Data::instance()->stylesList());
    int index = ui.stylecombo->findText(recipe_->style().name(),
                                        Qt::MatchExactly);
    if (index >= 0) {
        ui.stylecombo->setCurrentIndex(index);
    } else {
        ui.stylecombo->addItem(recipe_->style().name());
    }

    // restore modified flag
    recipe_->setModified(oldmod);
    recipe_->blockSignals(false);

    // reset ingredient models
    grainmodel_->flush();
    hopmodel_->flush();
    miscmodel_->flush();
    notepage_->refresh();

    grainpage.view->sortByColumn(0, Qt::AscendingOrder);
    hoppage.view->sortByColumn(0, Qt::AscendingOrder);
    miscpage.view->sortByColumn(0, Qt::AscendingOrder);

    // update style widgets
    refresh();
}

//////////////////////////////////////////////////////////////////////////////
// refresh()
// ---------
// refresh just the characteristics

void View::refresh()
{
    const Style &style = recipe_->style();
    QLocale locale = QLocale::system();

    // beer style has changed, so update labels in stylelayout_
    ui.stylebox->setTitle(tr("%1 characteristics",
                             "Beer style characteristics, e.g. Generic ale")
                          .arg(ui.stylecombo->currentText()));

    ui.ogmin->setText(locale.toString(style.OGLow(), 'f', 3));
    ui.ogmax->setText(locale.toString(style.OGHi(), 'f', 3));
    ui.ogrec->setText(locale.toString(recipe_->OG(), 'f', 3));

    ui.ibumin->setText(locale.toString(style.IBULow()));
    ui.ibumax->setText(locale.toString(style.IBUHi()));
    ui.iburec->setText(locale.toString(recipe_->IBU()));

    ui.srmmin->setText(locale.toString(style.SRMLow()) + Resource::DEGREE);
    ui.srmmax->setText(locale.toString(style.SRMHi()) + Resource::DEGREE);
    ui.srmrec->setText(locale.toString(recipe_->SRM()) + Resource::DEGREE);

    ui.fg->setText(locale.toString(recipe_->FGEstimate(), 'f', 3));
    ui.abv->setText(QString(tr("%1%"))
                    .arg(locale.toString(recipe_->ABV() * 100.0, 'f', 1)));
    ui.abw->setText(QString(tr("%1%"))
                    .arg(locale.toString(recipe_->ABW() * 100.0, 'f', 1)));
}