| 1 | /*************************************************************************** |
| 2 | configure.cpp |
| 3 | ------------------- |
| 4 | Application configuration dialog |
| 5 | ------------------- |
| 6 | Copyright 2006-2008 David Johnson |
| 7 | Please see the header file for copyright and licehnse information |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QDialogButtonBox> |
| 11 | #include <QStyleFactory> |
| 12 | |
| 13 | #include "configure.h" |
| 14 | #include "data.h" |
| 15 | #include "quantity.h" |
| 16 | #include "resource.h" |
| 17 | |
| 18 | using namespace Resource; |
| 19 | |
| 20 | enum { |
| 21 | GENERAL_PAGE = 0, |
| 22 | CALC_PAGE, |
| 23 | RECIPE_PAGE |
| 24 | }; |
| 25 | |
| 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | // Construction, Destruction // |
| 28 | ////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | Configure::Configure(QWidget* parent) |
| 31 | : QDialog(parent), calc_(0), general_(0), recipe_(0) |
| 32 | { |
| 33 | setWindowTitle(TITLE + tr(" - Configure")); |
| 34 | |
| 35 | // tab widget |
| 36 | tabwidget_ = new QTabWidget(this); |
| 37 | |
| 38 | // buttons |
| 39 | buttonbox_ = new QDialogButtonBox(Qt::Horizontal, this); |
| 40 | buttonbox_->addButton(QDialogButtonBox::Ok); |
| 41 | buttonbox_->addButton(QDialogButtonBox::Cancel); |
| 42 | buttonbox_->addButton(QDialogButtonBox::Apply); |
| 43 | buttonbox_->addButton(QDialogButtonBox::Reset); |
| 44 | |
| 45 | connect(buttonbox_, SIGNAL(accepted()), this, SLOT(accept())); |
| 46 | connect(buttonbox_, SIGNAL(rejected()), this, SLOT(reject())); |
| 47 | connect(buttonbox_, SIGNAL(clicked(QAbstractButton*)), |
| 48 | this, SLOT(slotClicked(QAbstractButton*))); |
| 49 | |
| 50 | // pages |
| 51 | QWidget *page = new QWidget(); |
| 52 | general_ = new Ui::GeneralConfig(); |
| 53 | general_->setupUi(page); |
| 54 | tabwidget_->insertTab(GENERAL_PAGE, page, tr("&General")); |
| 55 | page = new QWidget(); |
| 56 | calc_ = new Ui::CalcConfig(); |
| 57 | calc_->setupUi(page); |
| 58 | tabwidget_->insertTab(CALC_PAGE, page, tr("&Calculations")); |
| 59 | page = new QWidget(); |
| 60 | recipe_ = new Ui::RecipeConfig(); |
| 61 | recipe_->setupUi(page); |
| 62 | tabwidget_->insertTab(RECIPE_PAGE, page, tr("&Recipe")); |
| 63 | |
| 64 | // additional setup |
| 65 | recipe_->batch-> |
| 66 | setSuffix(" " + Data::instance()->defaultSize().unit().symbol()); |
| 67 | connect(calc_->units, SIGNAL(activated(const QString&)), |
| 68 | this, SLOT(convertBatchSpin(const QString&))); |
| 69 | |
| 70 | QVBoxLayout *mainlayout = new QVBoxLayout(); |
| 71 | mainlayout->addWidget(tabwidget_); |
| 72 | mainlayout->addWidget(buttonbox_); |
| 73 | setLayout(mainlayout); |
| 74 | } |
| 75 | |
| 76 | Configure::~Configure() { ; } |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | // Miscellaneous // |
| 80 | ////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | // setState() |
| 84 | // ---------- |
| 85 | // Read in the config to get the states |
| 86 | |
| 87 | void Configure::setState(const ConfigState &state) |
| 88 | { |
| 89 | state_ = state; |
| 90 | setGeneralState(state_.general); |
| 91 | setCalcState(state_.calc); |
| 92 | setRecipeState(state_.recipe); |
| 93 | } |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | // setGeneralState() |
| 97 | // ----------------- |
| 98 | // Set the state for the general page |
| 99 | |
| 100 | void Configure::setGeneralState(const GenConfigState &state) |
| 101 | { |
| 102 | if (general_) { |
| 103 | general_->lookfeel->clear(); |
| 104 | general_->lookfeel->addItem(QString()); |
| 105 | general_->lookfeel->addItems(QStyleFactory::keys()); |
| 106 | general_->showsplash->setChecked(state.showsplash); |
| 107 | general_->autosave->setChecked(state.autosave); |
| 108 | general_->saveinterval->setValue(state.saveinterval); |
| 109 | general_->autobackup->setChecked(state.autobackup); |
| 110 | general_->loadlast->setChecked(state.loadlast); |
| 111 | general_->recentnum->setValue(state.recentnum); |
| 112 | |
| 113 | // set combo for lookfeel |
| 114 | int index; |
| 115 | if (state.lookfeel.isEmpty()) { |
| 116 | index = general_->lookfeel->findText(QApplication::style()->objectName(), |
| 117 | Qt::MatchExactly); |
| 118 | } else { |
| 119 | index = general_->lookfeel->findText(state.lookfeel, |
| 120 | Qt::MatchExactly); |
| 121 | } |
| 122 | general_->lookfeel->setCurrentIndex(index); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | ////////////////////////////////////////////////////////////////////////////// |
| 127 | // setCalcState() |
| 128 | // ----------------- |
| 129 | // Set the state for the calculation page |
| 130 | |
| 131 | void Configure::setCalcState(const CalcConfigState &state) |
| 132 | { |
| 133 | if (calc_) { |
| 134 | calc_->steepyield->setValue(state.steepyield); |
| 135 | calc_->efficiency->setValue(state.efficiency); |
| 136 | calc_->morey->setChecked(state.morey); |
| 137 | calc_->tinseth->setChecked(state.tinseth); |
| 138 | QStringList list = (QStringList() << UNIT_METRIC << UNIT_US); |
| 139 | calc_->units->clear(); |
| 140 | calc_->units->addItems(list); |
| 141 | |
| 142 | // set combo for units string |
| 143 | int index = calc_->units->findText(state.units, Qt::MatchExactly); |
| 144 | if (index >= 0) { |
| 145 | calc_->units->setCurrentIndex(index); |
| 146 | } else { |
| 147 | calc_->units->addItem(state.units); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | ////////////////////////////////////////////////////////////////////////////// |
| 153 | // setRecipeState() |
| 154 | // ----------------- |
| 155 | // Set the state for the recipe page |
| 156 | |
| 157 | void Configure::setRecipeState(const RecipeConfigState &state) |
| 158 | { |
| 159 | if (recipe_) { |
| 160 | recipe_->batch->setValue(state.batch); |
| 161 | recipe_->stylebox->clear(); |
| 162 | recipe_->stylebox->addItems(Data::instance()->stylesList()); |
| 163 | recipe_->hoptype->clear(); |
| 164 | recipe_->hoptype->addItems(Hop::typeStringList()); |
| 165 | |
| 166 | // set combo for style string |
| 167 | int index = recipe_->stylebox->findText(state.style, Qt::MatchExactly); |
| 168 | if (index >= 0) { |
| 169 | recipe_->stylebox->setCurrentIndex(index); |
| 170 | } else { |
| 171 | recipe_->stylebox->addItem(state.style); |
| 172 | } |
| 173 | // set combo for hoptype string |
| 174 | index = recipe_->hoptype->findText(state.hoptype, Qt::MatchExactly); |
| 175 | if (index >= 0) { |
| 176 | recipe_->hoptype->setCurrentIndex(index); |
| 177 | } else { |
| 178 | recipe_->hoptype->addItem(state.hoptype); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | ////////////////////////////////////////////////////////////////////////////// |
| 184 | // Slots // |
| 185 | ////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | // clicked() |
| 189 | // --------- |
| 190 | // QDialogButtonBox clicked |
| 191 | |
| 192 | void Configure::slotClicked(QAbstractButton *button) |
| 193 | { |
| 194 | switch (buttonbox_->buttonRole(button)) { |
| 195 | case QDialogButtonBox::ApplyRole: |
| 196 | apply(); |
| 197 | break; |
| 198 | case QDialogButtonBox::ResetRole: |
| 199 | defaults(); |
| 200 | break; |
| 201 | default: |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | ////////////////////////////////////////////////////////////////////////////// |
| 207 | // accept() |
| 208 | // -------- |
| 209 | // OK button was pressed |
| 210 | |
| 211 | void Configure::accept() |
| 212 | { |
| 213 | apply(); |
| 214 | QDialog::accept(); |
| 215 | emit configureAccept(); |
| 216 | } |
| 217 | |
| 218 | ////////////////////////////////////////////////////////////////////////////// |
| 219 | // defauls() |
| 220 | // --------- |
| 221 | // Set dialog to default values |
| 222 | |
| 223 | void Configure::defaults() |
| 224 | { |
| 225 | // only default the current page |
| 226 | switch (tabwidget_->currentIndex()) { |
| 227 | case GENERAL_PAGE: |
| 228 | setGeneralState(GenConfigState()); break; |
| 229 | case CALC_PAGE: |
| 230 | setCalcState(CalcConfigState()); break; |
| 231 | case RECIPE_PAGE: |
| 232 | setRecipeState(RecipeConfigState()); break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | ////////////////////////////////////////////////////////////////////////////// |
| 237 | // apply() |
| 238 | // ------- |
| 239 | // Apply values |
| 240 | |
| 241 | void Configure::apply() |
| 242 | { |
| 243 | if (general_) { |
| 244 | // general page |
| 245 | state_.general.lookfeel = general_->lookfeel->currentText(); |
| 246 | state_.general.showsplash = general_->showsplash->isChecked(); |
| 247 | state_.general.autosave = general_->autosave->isChecked(); |
| 248 | state_.general.saveinterval = general_->saveinterval->value(); |
| 249 | state_.general.autobackup = general_->autobackup->isChecked(); |
| 250 | state_.general.loadlast = general_->loadlast->isChecked(); |
| 251 | state_.general.recentnum = general_->recentnum->value(); |
| 252 | emit generalApply(state_.general); |
| 253 | } |
| 254 | |
| 255 | if (calc_) { |
| 256 | // calc page |
| 257 | state_.calc.steepyield = calc_->steepyield->value(); |
| 258 | state_.calc.efficiency = calc_->efficiency->value(); |
| 259 | state_.calc.morey = calc_->morey->isChecked(); |
| 260 | state_.calc.tinseth = calc_->tinseth->isChecked(); |
| 261 | state_.calc.units = calc_->units->currentText(); |
| 262 | emit calcApply(state_.calc); |
| 263 | } |
| 264 | |
| 265 | if (recipe_) { |
| 266 | // recipe page |
| 267 | state_.recipe.batch = recipe_->batch->value(); |
| 268 | state_.recipe.style = recipe_->stylebox->currentText(); |
| 269 | state_.recipe.hoptype = recipe_->hoptype->currentText(); |
| 270 | emit recipeApply(state_.recipe); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | ////////////////////////////////////////////////////////////////////////////// |
| 275 | // convertBatchSpin() |
| 276 | // ------------------ |
| 277 | // Set the appropriate suffice for the batch spinbox |
| 278 | |
| 279 | void Configure::convertBatchSpin(const QString &selection) |
| 280 | { |
| 281 | if (selection == Resource::UNIT_US) |
| 282 | recipe_->batch->setSuffix(" " + Volume::gallon.symbol()); |
| 283 | else if (selection == Resource::UNIT_METRIC) |
| 284 | recipe_->batch->setSuffix(" " + Volume::liter.symbol()); |
| 285 | else |
| 286 | recipe_->batch->setSuffix(""); |
| 287 | } |