| 1 |
/*************************************************************************** |
| 2 |
view.cpp |
| 3 |
------------------- |
| 4 |
View class for application |
| 5 |
------------------- |
| 6 |
Copyright 2006-2008 David Johnson |
| 7 |
Please see the header file for copyright and license information |
| 8 |
***************************************************************************/ |
| 9 |
|
| 10 |
#include <QHeaderView> |
| 11 |
#include <QTableView> |
| 12 |
#include <QLocale> |
| 13 |
|
| 14 |
#include "data.h" |
| 15 |
#include "recipe.h" |
| 16 |
#include "resource.h" |
| 17 |
|
| 18 |
#include "graindelegate.h" |
| 19 |
#include "grainmodel.h" |
| 20 |
#include "hopdelegate.h" |
| 21 |
#include "hopmodel.h" |
| 22 |
#include "miscdelegate.h" |
| 23 |
#include "miscmodel.h" |
| 24 |
#include "notepage.h" |
| 25 |
#include "view.h" |
| 26 |
|
| 27 |
////////////////////////////////////////////////////////////////////////////// |
| 28 |
// Construction, Destruction // |
| 29 |
////////////////////////////////////////////////////////////////////////////// |
| 30 |
|
| 31 |
////////////////////////////////////////////////////////////////////////////// |
| 32 |
// View() |
| 33 |
// ------ |
| 34 |
// Constructor |
| 35 |
|
| 36 |
View::View(QWidget *parent, Recipe *recipe) |
| 37 |
: QWidget(parent), recipe_(recipe), grainmodel_(0), hopmodel_(0), |
| 38 |
miscmodel_(0), notepage_(0) |
| 39 |
{ |
| 40 |
ui.setupUi(this); |
| 41 |
|
| 42 |
// get current font information |
| 43 |
QFont bold(font()); |
| 44 |
bold.setBold(true); |
| 45 |
QFontMetrics fm(font()); |
| 46 |
unsigned mh = (unsigned)(fm.lineSpacing() * 1.5); |
| 47 |
unsigned mw = fm.width('M'); |
| 48 |
|
| 49 |
// additional setup |
| 50 |
ui.ogreclabel->setFont(bold); |
| 51 |
ui.ogrec->setFont(bold); |
| 52 |
ui.ibureclabel->setFont(bold); |
| 53 |
ui.iburec->setFont(bold); |
| 54 |
ui.srmreclabel->setFont(bold); |
| 55 |
ui.srmrec->setFont(bold); |
| 56 |
ui.abvlabel->setFont(bold); |
| 57 |
ui.abv->setFont(bold); |
| 58 |
ui.abwlabel->setFont(bold); |
| 59 |
ui.abw->setFont(bold); |
| 60 |
ui.fglabel->setFont(bold); |
| 61 |
ui.fg->setFont(bold); |
| 62 |
|
| 63 |
// grain page |
| 64 |
QWidget *widget = new QWidget(); |
| 65 |
grainpage.setupUi(widget); |
| 66 |
ui.ingredients->addTab(widget, tr("&Grains")); |
| 67 |
|
| 68 |
grainmodel_ = new GrainModel(this, recipe_->grains()); |
| 69 |
grainpage.view->setModel(grainmodel_); |
| 70 |
QItemDelegate *delegate = new GrainDelegate(this); |
| 71 |
grainpage.view->setItemDelegate(delegate); |
| 72 |
|
| 73 |
grainpage.view->verticalHeader()->setDefaultSectionSize(mh); |
| 74 |
grainpage.view->verticalHeader()->hide(); |
| •75 |
grainpage.view->horizontalHeader()->setSectionsClickable(true); |
| 76 |
grainpage.view->horizontalHeader()->setHighlightSections(false); |
| 77 |
|
| 78 |
grainpage.view->setColumnWidth(GrainModel::NAME, 20*mw); |
| 79 |
grainpage.view->setColumnWidth(GrainModel::WEIGHT, 8*mw); |
| 80 |
grainpage.view->setColumnWidth(GrainModel::EXTRACT, 8*mw); |
| 81 |
grainpage.view->setColumnWidth(GrainModel::COLOR, 8*mw); |
| 82 |
grainpage.view->setColumnWidth(GrainModel::TYPE, 8*mw); |
| 83 |
grainpage.view->setColumnWidth(GrainModel::USE, 8*mw); |
| 84 |
|
| 85 |
// hop page |
| 86 |
widget = new QWidget(); |
| 87 |
hoppage.setupUi(widget); |
| 88 |
ui.ingredients->addTab(widget, tr("&Hops")); |
| 89 |
|
| 90 |
hopmodel_ = new HopModel(this, recipe_->hops()); |
| 91 |
hoppage.view->setModel(hopmodel_); |
| 92 |
delegate = new HopDelegate(this); |
| 93 |
hoppage.view->setItemDelegate(delegate); |
| 94 |
|
| 95 |
hoppage.view->verticalHeader()->setDefaultSectionSize(mh); |
| 96 |
hoppage.view->verticalHeader()->hide(); |
| •97 |
hoppage.view->horizontalHeader()->setSectionsClickable(true); |
| 98 |
hoppage.view->horizontalHeader()->setHighlightSections(false); |
| 99 |
|
| 100 |
hoppage.view->setColumnWidth(HopModel::NAME, 20*mw); |
| 101 |
hoppage.view->setColumnWidth(HopModel::WEIGHT, 8*mw); |
| 102 |
hoppage.view->setColumnWidth(HopModel::ALPHA, 8*mw); |
| 103 |
hoppage.view->setColumnWidth(HopModel::TIME, 8*mw); |
| 104 |
hoppage.view->setColumnWidth(HopModel::TYPE, 8*mw); |
| 105 |
|
| 106 |
// misc page |
| 107 |
widget = new QWidget(); |
| 108 |
miscpage.setupUi(widget); |
| 109 |
ui.ingredients->addTab(widget, tr("&Miscellaneous")); |
| 110 |
|
| 111 |
miscmodel_ = new MiscModel(this, recipe_->miscs()); |
| 112 |
miscpage.view->setModel(miscmodel_); |
| 113 |
delegate = new MiscDelegate(this); |
| 114 |
miscpage.view->setItemDelegate(delegate); |
| 115 |
|
| 116 |
miscpage.view->verticalHeader()->setDefaultSectionSize(mh); |
| 117 |
miscpage.view->verticalHeader()->hide(); |
| •118 |
miscpage.view->horizontalHeader()->setSectionsClickable(true); |
| 119 |
miscpage.view->horizontalHeader()->setHighlightSections(false); |
| 120 |
|
| 121 |
miscpage.view->setColumnWidth(MiscModel::NAME, 20*mw); |
| 122 |
miscpage.view->setColumnWidth(MiscModel::QUANTITY, 8*mw); |
| 123 |
miscpage.view->setColumnWidth(MiscModel::TYPE, 8*mw); |
| 124 |
miscpage.view->horizontalHeader()->setStretchLastSection(true); |
| 125 |
|
| 126 |
// note page |
| 127 |
notepage_ = new NotePage(0, recipe_); |
| 128 |
ui.ingredients->addTab(notepage_, tr("&Notes")); |
| 129 |
|
| 130 |
// widget connections |
| 131 |
connect(grainmodel_, SIGNAL(modified()), |
| 132 |
this, SLOT(modelModified())); |
| 133 |
connect(grainpage.addbutton, SIGNAL(clicked()), |
| 134 |
grainpage.view, SLOT(addIngredient())); |
| 135 |
connect(grainpage.removebutton, SIGNAL(clicked()), |
| 136 |
grainpage.view, SLOT(removeIngredient())); |
| 137 |
|
| 138 |
connect(hopmodel_, SIGNAL(modified()), |
| 139 |
this, SLOT(modelModified())); |
| 140 |
connect(hoppage.addbutton, SIGNAL(clicked()), |
| 141 |
hoppage.view, SLOT(addIngredient())); |
| 142 |
connect(hoppage.removebutton, SIGNAL(clicked()), |
| 143 |
hoppage.view, SLOT(removeIngredient())); |
| 144 |
|
| 145 |
connect(miscmodel_, SIGNAL(modified()), |
| 146 |
this, SLOT(modelModified())); |
| 147 |
connect(miscpage.addbutton, SIGNAL(clicked()), |
| 148 |
miscpage.view, SLOT(addIngredient())); |
| 149 |
connect(miscpage.removebutton, SIGNAL(clicked()), |
| 150 |
miscpage.view, SLOT(removeIngredient())); |
| 151 |
|
| 152 |
connect(ui.titleedit, SIGNAL(textChanged(const QString &)), |
| 153 |
this, SLOT(setTitle(const QString &))); |
| 154 |
connect(ui.stylecombo, SIGNAL(activated(const QString &)), |
| 155 |
this, SLOT(setStyle(const QString &))); |
| 156 |
connect(ui.breweredit, SIGNAL(textChanged(const QString &)), |
| 157 |
this, SLOT(setBrewer(const QString &))); |
| 158 |
connect(ui.sizespin, SIGNAL(valueChanged(double)), |
| 159 |
this, SLOT(setSize(double))); |
| 160 |
|
| 161 |
connect(recipe_, SIGNAL(recipeChanged()), this, SLOT(flush())); |
| 162 |
connect(recipe_, SIGNAL(recipeModified()), this, SLOT(refresh())); |
| 163 |
|
| 164 |
// start with new view |
| 165 |
flush(); |
| 166 |
} |
| 167 |
|
| 168 |
View::~View() { ; } |
| 169 |
|
| 170 |
////////////////////////////////////////////////////////////////////////////// |
| 171 |
// Slot Implementations // |
| 172 |
////////////////////////////////////////////////////////////////////////////// |
| 173 |
|
| 174 |
////////////////////////////////////////////////////////////////////////////// |
| 175 |
// setTitle() |
| 176 |
// ---------- |
| 177 |
// set the recipe title |
| 178 |
|
| 179 |
void View::setTitle(const QString &title) |
| 180 |
{ |
| 181 |
recipe_->setTitle(title); |
| 182 |
} |
| 183 |
|
| 184 |
////////////////////////////////////////////////////////////////////////////// |
| 185 |
// setStyle() |
| 186 |
// ---------- |
| 187 |
// set the recipe style |
| 188 |
|
| 189 |
void View::setStyle(const QString &style) |
| 190 |
{ |
| 191 |
recipe_->setStyle(style); |
| 192 |
} |
| 193 |
|
| 194 |
////////////////////////////////////////////////////////////////////////////// |
| 195 |
// Brewer() |
| 196 |
// -------- |
| 197 |
// set the recipeCombo brewer |
| 198 |
|
| 199 |
void View::setBrewer(const QString &brewer) |
| 200 |
{ |
| 201 |
recipe_->setBrewer(brewer); |
| 202 |
} |
| 203 |
|
| 204 |
////////////////////////////////////////////////////////////////////////////// |
| 205 |
// setSize() |
| 206 |
// --------- |
| 207 |
// set the recipe size |
| 208 |
|
| 209 |
void View::setSize(double size) |
| 210 |
{ |
| 211 |
recipe_->setSize(Volume(size, recipe_->size().unit())); |
| 212 |
} |
| 213 |
|
| 214 |
/////////////////////////////////////////////////////////////////////////////// |
| 215 |
// Miscellaneous // |
| 216 |
/////////////////////////////////////////////////////////////////////////////// |
| 217 |
|
| 218 |
////////////////////////////////////////////////////////////////////////////// |
| 219 |
// modelModified() |
| 220 |
// --------------- |
| 221 |
// One of the models has been modified |
| 222 |
|
| 223 |
void View::modelModified() |
| 224 |
{ |
| 225 |
recipe_->recalc(); |
| 226 |
recipe_->setModified(true); |
| 227 |
} |
| 228 |
|
| 229 |
////////////////////////////////////////////////////////////////////////////// |
| 230 |
// flush() |
| 231 |
// ------- |
| 232 |
// flush/reset the entire view |
| 233 |
|
| 234 |
void View::flush() |
| 235 |
{ |
| 236 |
// save the modified flag to prevent side effects of setting widgets |
| 237 |
bool oldmod = recipe_->modified(); |
| 238 |
recipe_->blockSignals(true); |
| 239 |
|
| 240 |
// update recipe widgets |
| 241 |
ui.titleedit->setText(recipe_->title()); |
| 242 |
ui.breweredit->setText(recipe_->brewer()); |
| 243 |
|
| 244 |
ui.sizespin->setValue(recipe_->size().amount()); |
| 245 |
ui.sizespin->setSuffix(" " + recipe_->size().unit().name()); |
| 246 |
|
| 247 |
// set style combo to style |
| 248 |
ui.stylecombo->clear(); |
| 249 |
ui.stylecombo->addItems(Data::instance()->stylesList()); |
| 250 |
int index = ui.stylecombo->findText(recipe_->style().name(), |
| 251 |
Qt::MatchExactly); |
| 252 |
if (index >= 0) { |
| 253 |
ui.stylecombo->setCurrentIndex(index); |
| 254 |
} else { |
| 255 |
ui.stylecombo->addItem(recipe_->style().name()); |
| 256 |
} |
| 257 |
|
| 258 |
// restore modified flag |
| 259 |
recipe_->setModified(oldmod); |
| 260 |
recipe_->blockSignals(false); |
| 261 |
|
| 262 |
// reset ingredient models |
| 263 |
grainmodel_->flush(); |
| 264 |
hopmodel_->flush(); |
| 265 |
miscmodel_->flush(); |
| 266 |
notepage_->refresh(); |
| 267 |
|
| 268 |
grainpage.view->sortByColumn(0, Qt::AscendingOrder); |
| 269 |
hoppage.view->sortByColumn(0, Qt::AscendingOrder); |
| 270 |
miscpage.view->sortByColumn(0, Qt::AscendingOrder); |
| 271 |
|
| 272 |
// update style widgets |
| 273 |
refresh(); |
| 274 |
} |
| 275 |
|
| 276 |
////////////////////////////////////////////////////////////////////////////// |
| 277 |
// refresh() |
| 278 |
// --------- |
| 279 |
// refresh just the characteristics |
| 280 |
|
| 281 |
void View::refresh() |
| 282 |
{ |
| 283 |
const Style &style = recipe_->style(); |
| 284 |
QLocale locale = QLocale::system(); |
| 285 |
|
| 286 |
// beer style has changed, so update labels in stylelayout_ |
| 287 |
ui.stylebox->setTitle(tr("%1 characteristics", |
| 288 |
"Beer style characteristics, e.g. Generic ale") |
| 289 |
.arg(ui.stylecombo->currentText())); |
| 290 |
|
| 291 |
ui.ogmin->setText(locale.toString(style.OGLow(), 'f', 3)); |
| 292 |
ui.ogmax->setText(locale.toString(style.OGHi(), 'f', 3)); |
| 293 |
ui.ogrec->setText(locale.toString(recipe_->OG(), 'f', 3)); |
| 294 |
|
| 295 |
ui.ibumin->setText(locale.toString(style.IBULow())); |
| 296 |
ui.ibumax->setText(locale.toString(style.IBUHi())); |
| 297 |
ui.iburec->setText(locale.toString(recipe_->IBU())); |
| 298 |
|
| 299 |
ui.srmmin->setText(locale.toString(style.SRMLow()) + Resource::DEGREE); |
| 300 |
ui.srmmax->setText(locale.toString(style.SRMHi()) + Resource::DEGREE); |
| 301 |
ui.srmrec->setText(locale.toString(recipe_->SRM()) + Resource::DEGREE); |
| 302 |
|
| 303 |
ui.fg->setText(locale.toString(recipe_->FGEstimate(), 'f', 3)); |
| 304 |
ui.abv->setText(QString(tr("%1%")) |
| 305 |
.arg(locale.toString(recipe_->ABV() * 100.0, 'f', 1))); |
| 306 |
ui.abw->setText(QString(tr("%1%")) |
| 307 |
.arg(locale.toString(recipe_->ABW() * 100.0, 'f', 1))); |
| 308 |
} |