File: configure.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 (287 lines) | stat: -rwxr-xr-x 9,415 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
/***************************************************************************
  configure.cpp
  -------------------
  Application configuration dialog
  -------------------
  Copyright 2006-2008 David Johnson
  Please see the header file for copyright and licehnse information
 ***************************************************************************/

#include <QDialogButtonBox>
#include <QStyleFactory>

#include "configure.h"
#include "data.h"
#include "quantity.h"
#include "resource.h"

using namespace Resource;

enum {
    GENERAL_PAGE = 0,
    CALC_PAGE,
    RECIPE_PAGE
};

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

Configure::Configure(QWidget* parent)
    : QDialog(parent), calc_(0), general_(0), recipe_(0)
{
    setWindowTitle(TITLE + tr(" - Configure"));

    // tab widget
    tabwidget_ = new QTabWidget(this);

    // buttons
    buttonbox_ = new QDialogButtonBox(Qt::Horizontal, this);
    buttonbox_->addButton(QDialogButtonBox::Ok);
    buttonbox_->addButton(QDialogButtonBox::Cancel);
    buttonbox_->addButton(QDialogButtonBox::Apply);
    buttonbox_->addButton(QDialogButtonBox::Reset);

    connect(buttonbox_, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonbox_, SIGNAL(rejected()), this, SLOT(reject()));
    connect(buttonbox_, SIGNAL(clicked(QAbstractButton*)),
            this, SLOT(slotClicked(QAbstractButton*)));

    // pages
    QWidget *page = new QWidget();
    general_ = new Ui::GeneralConfig();
    general_->setupUi(page);
    tabwidget_->insertTab(GENERAL_PAGE, page, tr("&General"));
    page = new QWidget();
    calc_ = new Ui::CalcConfig();
    calc_->setupUi(page);
    tabwidget_->insertTab(CALC_PAGE, page, tr("&Calculations"));
    page = new QWidget();
    recipe_ = new Ui::RecipeConfig();
    recipe_->setupUi(page);
    tabwidget_->insertTab(RECIPE_PAGE, page, tr("&Recipe"));

    // additional setup
    recipe_->batch->
        setSuffix(" " + Data::instance()->defaultSize().unit().symbol());
    connect(calc_->units, SIGNAL(activated(const QString&)),
            this, SLOT(convertBatchSpin(const QString&)));

    QVBoxLayout *mainlayout = new QVBoxLayout();
    mainlayout->addWidget(tabwidget_);
    mainlayout->addWidget(buttonbox_);
    setLayout(mainlayout);
}

Configure::~Configure() { ; }

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

//////////////////////////////////////////////////////////////////////////////
// setState()
// ----------
// Read in the config to get the states

void Configure::setState(const ConfigState &state)
{
    state_ = state;
    setGeneralState(state_.general);
    setCalcState(state_.calc);
    setRecipeState(state_.recipe);
}

//////////////////////////////////////////////////////////////////////////////
// setGeneralState()
// -----------------
// Set the state for the general page

void Configure::setGeneralState(const GenConfigState &state)
{
    if (general_) {
        general_->lookfeel->clear();
        general_->lookfeel->addItem(QString());
        general_->lookfeel->addItems(QStyleFactory::keys());
        general_->showsplash->setChecked(state.showsplash);
        general_->autosave->setChecked(state.autosave);
        general_->saveinterval->setValue(state.saveinterval);
        general_->autobackup->setChecked(state.autobackup);
        general_->loadlast->setChecked(state.loadlast);
        general_->recentnum->setValue(state.recentnum);

        // set combo for lookfeel
        int index;
        if (state.lookfeel.isEmpty()) {
            index = general_->lookfeel->findText(QApplication::style()->objectName(),
                                                 Qt::MatchExactly);
        } else {
            index = general_->lookfeel->findText(state.lookfeel,
                                                 Qt::MatchExactly);
        }
        general_->lookfeel->setCurrentIndex(index);
    }
}

//////////////////////////////////////////////////////////////////////////////
// setCalcState()
// -----------------
// Set the state for the calculation page

void Configure::setCalcState(const CalcConfigState &state)
{
    if (calc_) {
        calc_->steepyield->setValue(state.steepyield);
        calc_->efficiency->setValue(state.efficiency);
        calc_->morey->setChecked(state.morey);
        calc_->tinseth->setChecked(state.tinseth);
        QStringList list = (QStringList() << UNIT_METRIC << UNIT_US);
        calc_->units->clear();
        calc_->units->addItems(list);

        // set combo for units string
        int index = calc_->units->findText(state.units, Qt::MatchExactly);
        if (index >= 0) {
            calc_->units->setCurrentIndex(index);
        } else {
            calc_->units->addItem(state.units);
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// setRecipeState()
// -----------------
// Set the state for the recipe page

void Configure::setRecipeState(const RecipeConfigState &state)
{
    if (recipe_) {
        recipe_->batch->setValue(state.batch);
        recipe_->stylebox->clear();
        recipe_->stylebox->addItems(Data::instance()->stylesList());
        recipe_->hoptype->clear();
        recipe_->hoptype->addItems(Hop::typeStringList());

        // set combo for style string
        int index = recipe_->stylebox->findText(state.style, Qt::MatchExactly);
        if (index >= 0) {
            recipe_->stylebox->setCurrentIndex(index);
        } else {
            recipe_->stylebox->addItem(state.style);
        }
        // set combo for hoptype string
        index =  recipe_->hoptype->findText(state.hoptype, Qt::MatchExactly);
        if (index >= 0) {
            recipe_->hoptype->setCurrentIndex(index);
        } else {
            recipe_->hoptype->addItem(state.hoptype);
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// Slots                                                                    //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// clicked()
// ---------
// QDialogButtonBox clicked

void Configure::slotClicked(QAbstractButton *button)
{
    switch (buttonbox_->buttonRole(button)) {
      case QDialogButtonBox::ApplyRole:
          apply();
          break;
      case QDialogButtonBox::ResetRole:
          defaults();
          break;
      default:
          break;
    }
}

//////////////////////////////////////////////////////////////////////////////
// accept()
// --------
// OK button was pressed

void Configure::accept()
{
    apply();
    QDialog::accept();
    emit configureAccept();
}

//////////////////////////////////////////////////////////////////////////////
// defauls()
// ---------
// Set dialog to default values

void Configure::defaults()
{
    // only default the current page
    switch (tabwidget_->currentIndex()) {
      case GENERAL_PAGE:
          setGeneralState(GenConfigState()); break;
      case CALC_PAGE:
          setCalcState(CalcConfigState()); break;
      case RECIPE_PAGE:
          setRecipeState(RecipeConfigState()); break;
    }
}

//////////////////////////////////////////////////////////////////////////////
// apply()
// -------
// Apply values

void Configure::apply()
{
    if (general_) {
        // general page
        state_.general.lookfeel = general_->lookfeel->currentText();
        state_.general.showsplash = general_->showsplash->isChecked();
        state_.general.autosave = general_->autosave->isChecked();
        state_.general.saveinterval = general_->saveinterval->value();
        state_.general.autobackup = general_->autobackup->isChecked();
        state_.general.loadlast = general_->loadlast->isChecked();
        state_.general.recentnum = general_->recentnum->value();
        emit generalApply(state_.general);
    }

    if (calc_) {
        // calc page
        state_.calc.steepyield = calc_->steepyield->value();
        state_.calc.efficiency = calc_->efficiency->value();
        state_.calc.morey = calc_->morey->isChecked();
        state_.calc.tinseth = calc_->tinseth->isChecked();
        state_.calc.units = calc_->units->currentText();
        emit calcApply(state_.calc);
    }

    if (recipe_) {
        // recipe page
        state_.recipe.batch = recipe_->batch->value();
        state_.recipe.style = recipe_->stylebox->currentText();
        state_.recipe.hoptype = recipe_->hoptype->currentText();
        emit recipeApply(state_.recipe);
    }
}

//////////////////////////////////////////////////////////////////////////////
// convertBatchSpin()
// ------------------
// Set the appropriate suffice for the batch spinbox

void Configure::convertBatchSpin(const QString &selection)
{
    if (selection == Resource::UNIT_US)
        recipe_->batch->setSuffix(" " + Volume::gallon.symbol());
    else if (selection == Resource::UNIT_METRIC)
        recipe_->batch->setSuffix(" " + Volume::liter.symbol());
    else
        recipe_->batch->setSuffix("");
}