2
* FermentableTableModel.cpp is part of Brewtarget, and is Copyright Philip G. Lee
3
* (rocketman768@gmail.com), 2009.
5
* Brewtarget is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Brewtarget is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include <QAbstractTableModel>
20
#include <QAbstractItemModel>
21
#include <QAbstractItemView>
23
#include <QModelIndex>
26
#include <QItemEditorFactory>
30
#include "brewtarget.h"
40
#include "observable.h"
41
#include "fermentable.h"
42
#include "FermentableTableModel.h"
45
//=====================CLASS FermentableTableModel==============================
46
FermentableTableModel::FermentableTableModel(FermentableTableWidget* parent)
47
: QAbstractTableModel(parent), MultipleObserver()
50
parentTableWidget = parent;
53
displayPercentages = false;
56
void FermentableTableModel::addFermentable(Fermentable* ferm)
58
std::vector<Fermentable*>::iterator iter;
60
//Check to see if it's already in the list
61
for( iter=fermObs.begin(); iter != fermObs.end(); iter++ )
65
fermObs.push_back(ferm);
67
totalFermMass_kg += ferm->getAmount_kg();
68
reset(); // Tell everybody that the table has changed.
72
parentTableWidget->resizeColumnsToContents();
73
parentTableWidget->resizeRowsToContents();
77
bool FermentableTableModel::removeFermentable(Fermentable* ferm)
79
std::vector<Fermentable*>::iterator iter;
81
for( iter=fermObs.begin(); iter != fermObs.end(); iter++ )
86
totalFermMass_kg -= ferm->getAmount_kg();
87
reset(); // Tell everybody the table has changed.
91
parentTableWidget->resizeColumnsToContents();
92
parentTableWidget->resizeRowsToContents();
101
void FermentableTableModel::removeAll()
105
for( i = 0; i < fermObs.size(); ++i )
106
removeObserved(fermObs[i]);
109
totalFermMass_kg = 0;
113
void FermentableTableModel::updateTotalGrains()
115
unsigned int i, size;
117
totalFermMass_kg = 0;
119
size = fermObs.size();
120
for( i = 0; i < size; ++i )
121
totalFermMass_kg += fermObs[i]->getAmount_kg();
124
void FermentableTableModel::setDisplayPercentages(bool var)
126
displayPercentages = var;
130
void FermentableTableModel::notify(Observable* notifier, QVariant info)
134
// Find the notifier in the list
135
for( i = 0; i < (int)fermObs.size(); ++i )
137
if( notifier == fermObs[i] )
140
emit dataChanged( QAbstractItemModel::createIndex(i, 0),
141
QAbstractItemModel::createIndex(i, FERMNUMCOLS));
150
int FermentableTableModel::rowCount(const QModelIndex& /*parent*/) const
152
return fermObs.size();
155
int FermentableTableModel::columnCount(const QModelIndex& /*parent*/) const
160
QVariant FermentableTableModel::data( const QModelIndex& index, int role ) const
164
// Ensure the row is ok.
165
if( index.row() >= (int)fermObs.size() )
167
std::cerr << "Bad model index. row = " << index.row() << std::endl;
171
row = fermObs[index.row()];
173
switch( index.column() )
176
if( role == Qt::DisplayRole )
177
return QVariant(row->getName().c_str());
181
if( role == Qt::DisplayRole )
182
return QVariant(row->getType().c_str());
186
if( role == Qt::DisplayRole )
187
return QVariant( Brewtarget::displayAmount(row->getAmount_kg(), Units::kilograms) );
190
case FERMISMASHEDCOL:
191
if( role == Qt::CheckStateRole )
192
return QVariant( row->getIsMashed() ? Qt::Checked : Qt::Unchecked);
193
else if( role == Qt::DisplayRole )
194
return row->getIsMashed() ? QString("Mashed") : QString ("Not mashed");
198
if( role == Qt::DisplayRole )
199
return QVariant( Brewtarget::displayAmount(row->getYield_pct(), 0) );
203
if( role == Qt::DisplayRole )
204
return QVariant( Brewtarget::displayAmount(row->getColor_srm(), 0) );
208
std::cerr << "Bad column: " << index.column() << std::endl;
213
QVariant FermentableTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
215
if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
220
return QVariant("Name");
222
return QVariant("Type");
224
return QVariant("Amount");
225
case FERMISMASHEDCOL:
226
return QVariant("Mashed");
228
return QVariant("Yield %");
230
return QVariant("Color (SRM)");
232
std::cerr << "Bad column: " << section << std::endl;
236
else if( displayPercentages && orientation == Qt::Vertical && role == Qt::DisplayRole )
238
return QVariant( QString("%1\%").arg( (double)100.0 *fermObs[section]->getAmount_kg()/totalFermMass_kg, 0, 'f', 0 ) );
244
Qt::ItemFlags FermentableTableModel::flags(const QModelIndex& index ) const
246
Qt::ItemFlags defaults = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
248
if( index.column() == FERMISMASHEDCOL )
249
return (defaults | Qt::ItemIsUserCheckable);
251
return (defaults | Qt::ItemIsEditable);
254
bool FermentableTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
258
if( index.row() >= (int)fermObs.size() )
263
row = fermObs[index.row()];
265
switch( index.column() )
268
if( value.canConvert(QVariant::String))
270
row->setName(value.toString().toStdString());
276
if( value.canConvert(QVariant::String) )
278
row->setType(value.toString().toStdString());
284
if( value.canConvert(QVariant::String) )
286
row->setAmount_kg( Unit::qstringToSI(value.toString()) );
291
case FERMISMASHEDCOL:
292
if( role == Qt::CheckStateRole && value.canConvert(QVariant::Int) )
294
row->setIsMashed( ((Qt::CheckState)value.toInt()) == Qt::Checked );
300
if( value.canConvert(QVariant::String) )
302
row->setYield_pct( Unit::qstringToSI(value.toString()) );
308
if( value.canConvert(QVariant::String) )
310
row->setColor_srm( Unit::qstringToSI(value.toString()) );
316
std::cerr << "Bad column: " << index.column() << std::endl;
321
Fermentable* FermentableTableModel::getFermentable(unsigned int i)
326
//======================CLASS FermentableItemDelegate===========================
328
FermentableItemDelegate::FermentableItemDelegate(QObject* parent)
329
: QStyledItemDelegate(parent)
331
//connect( this, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), this, SLOT(destroyWidget(QWidget*, QAbstractItemDelegate::EndEditHint)) );
334
void FermentableItemDelegate::destroyWidget(QWidget* widget, QAbstractItemDelegate::EndEditHint hint)
340
QWidget* FermentableItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
342
if( index.column() == FERMTYPECOL )
344
QComboBox *box = new QComboBox(parent);
346
box->addItem("Grain");
347
box->addItem("Sugar");
348
box->addItem("Extract");
349
box->addItem("Dry Extract");
350
box->addItem("Adjunct");
351
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
354
else if( index.column() == FERMISMASHEDCOL )
356
QCheckBox* box = new QCheckBox(parent);
357
box->setFocusPolicy(Qt::StrongFocus);
358
box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
361
QWidget* displayWidget = (((FermentableTableModel*)(index.model()))->parentTableWidget)->indexWidget(index);
362
if( displayWidget != 0 )
363
box->move(displayWidget->pos());
364
***Didn't work at all***/
367
QRect rect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignLeft, box->sizeHint(), option.rect);
368
std::cerr << "option.rect " << option.rect.x() << " " << option.rect.y() << " " << option.rect.width() << " " << option.rect.height() << std::endl;
369
std::cerr << "rect " << rect.x() << " " << rect.y() << " " << rect.width() << " " << rect.height() << std::endl;
370
box->move(rect.topRight());
371
***Didn't really do much either***/
375
//return QItemDelegate::createEditor(parent, option, index);
376
//return itemEditorFactory()->createEditor(QVariant::Bool, parent);
379
return new QLineEdit(parent);
382
void FermentableItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
384
if( index.column() == FERMTYPECOL )
386
QComboBox* box = (QComboBox*)editor;
387
QString text = index.model()->data(index, Qt::DisplayRole).toString();
389
int index = box->findText(text);
390
box->setCurrentIndex(index);
392
else if( index.column() == FERMISMASHEDCOL )
394
QCheckBox* checkBox = (QCheckBox*)editor;
395
Qt::CheckState checkState = (Qt::CheckState)index.model()->data(index, Qt::CheckStateRole).toInt();
397
checkBox->setCheckState( checkState );
401
QLineEdit* line = (QLineEdit*)editor;
403
line->setText(index.model()->data(index, Qt::DisplayRole).toString());
408
void FermentableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
410
if( index.column() == FERMTYPECOL )
412
QComboBox* box = (QComboBox*)editor;
413
QString value = box->currentText();
415
model->setData(index, value, Qt::EditRole);
417
else if( index.column() == FERMISMASHEDCOL )
419
QCheckBox* checkBox = (QCheckBox*)editor;
420
bool checked = (checkBox->checkState() == Qt::Checked);
422
model->setData(index, checked, Qt::EditRole);
426
QLineEdit* line = (QLineEdit*)editor;
428
model->setData(index, line->text(), Qt::EditRole);
433
void FermentableItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
435
if( index.column() == FERMISMASHEDCOL )
437
QItemDelegate::drawCheck(painter, option, option.rect, (Qt::CheckState)index.model()->data(index, Qt::CheckStateRole).toInt() );
441
QString str = index.model()->data(index, Qt::DisplayRole).toString();
442
QItemDelegate::drawDisplay(painter, option, option.rect, str);
447
void FermentableItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
449
editor->setGeometry(option.rect);