2
* MashStepTableModel.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>
21
#include <QModelIndex>
24
#include <QItemDelegate>
31
#include "observable.h"
32
#include "MashStepTableModel.h"
34
#include "brewtarget.h"
36
MashStepTableModel::MashStepTableModel(MashStepTableWidget* parent)
37
: QAbstractTableModel(parent), Observer()
39
parentTableWidget = parent;
43
void MashStepTableModel::setMash( Mash* m )
47
reset(); // Tell everybody that the table has changed.
49
if( parentTableWidget )
51
parentTableWidget->resizeColumnsToContents();
52
parentTableWidget->resizeRowsToContents();
56
/***************************************************
57
void MashStepTableModel::addMashStep(MashStep* step)
59
std::vector<MashStep*>::iterator iter;
61
//Check to see if it's already in the list
62
for( iter=stepObs.begin(); iter != stepObs.end(); iter++ )
66
stepObs.push_back(step);
68
reset(); // Tell everybody that the table has changed.
70
if( parentTableWidget )
72
parentTableWidget->resizeColumnsToContents();
73
parentTableWidget->resizeRowsToContents();
77
bool MashStepTableModel::removeMashStep(MashStep* step)
79
std::vector<MashStep*>::iterator iter;
81
for( iter=stepObs.begin(); iter != stepObs.end(); iter++ )
86
reset(); // Tell everybody the table has changed.
88
if( parentTableWidget )
90
parentTableWidget->resizeColumnsToContents();
91
parentTableWidget->resizeRowsToContents();
100
void MashStepTableModel::removeAll()
104
for( i = 0; i < stepObs.size(); ++i )
105
removeObserved(stepObs[i]);
111
MashStep* MashStepTableModel::getMashStep(unsigned int i)
115
*************************************************************/
117
void MashStepTableModel::notify(Observable* notifier, QVariant info)
122
if( notifier != mashObs )
125
i = info.toInt(&ok); // mashObs' info says which MashStep changed.
129
emit dataChanged( QAbstractItemModel::createIndex(i, 0),
130
QAbstractItemModel::createIndex(i, MASHSTEPNUMCOLS));
135
if( parentTableWidget )
137
parentTableWidget->resizeColumnsToContents();
138
parentTableWidget->resizeRowsToContents();
142
int MashStepTableModel::rowCount(const QModelIndex& /*parent*/) const
145
return mashObs->getNumMashSteps();
150
int MashStepTableModel::columnCount(const QModelIndex& /*parent*/) const
152
return MASHSTEPNUMCOLS;
155
QVariant MashStepTableModel::data( const QModelIndex& index, int role ) const
162
// Ensure the row is ok.
163
if( index.row() >= (int)(mashObs->getNumMashSteps()) )
165
std::cerr << "Bad model index. row = " << index.row() << std::endl;
169
row = mashObs->getMashStep(index.row());
171
// Make sure we only respond to the DisplayRole role.
172
if( role != Qt::DisplayRole )
175
switch( index.column() )
177
case MASHSTEPNAMECOL:
178
return QVariant(row->getName().c_str());
179
case MASHSTEPTYPECOL:
180
return QVariant(row->getType().c_str());
181
case MASHSTEPAMOUNTCOL:
182
return QVariant( Brewtarget::displayAmount(row->getInfuseAmount_l(), Units::liters) );
183
case MASHSTEPTEMPCOL:
184
return QVariant( Brewtarget::displayAmount(row->getStepTemp_c(), Units::celsius) );
185
case MASHSTEPTIMECOL:
186
return QVariant( Brewtarget::displayAmount(row->getStepTime_min(), Units::minutes) );
188
std::cerr << "Bad column: " << index.column() << std::endl;
193
QVariant MashStepTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
195
if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
199
case MASHSTEPNAMECOL:
200
return QVariant("Name");
201
case MASHSTEPTYPECOL:
202
return QVariant("Type");
203
case MASHSTEPAMOUNTCOL:
204
return QVariant("Amount");
205
case MASHSTEPTEMPCOL:
206
return QVariant("Temp");
207
case MASHSTEPTIMECOL:
208
return QVariant("Time");
217
Qt::ItemFlags MashStepTableModel::flags(const QModelIndex& /*index*/ ) const
219
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
223
bool MashStepTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
230
if( index.row() >= (int)(mashObs->getNumMashSteps()) || role != Qt::EditRole )
233
row = mashObs->getMashStep(index.row());
235
switch( index.column() )
237
case MASHSTEPNAMECOL:
238
if( value.canConvert(QVariant::String))
240
row->setName(value.toString().toStdString());
245
case MASHSTEPTYPECOL:
246
if( value.canConvert(QVariant::String) )
248
row->setType(value.toString().toStdString());
253
case MASHSTEPAMOUNTCOL:
254
if( value.canConvert(QVariant::String) )
256
row->setInfuseAmount_l( Unit::qstringToSI(value.toString()) );
261
case MASHSTEPTEMPCOL:
262
if( value.canConvert(QVariant::String) )
264
row->setStepTemp_c( Unit::qstringToSI(value.toString()) );
265
row->setEndTemp_c( Unit::qstringToSI(value.toString()) );
270
case MASHSTEPTIMECOL:
271
if( value.canConvert(QVariant::String) )
273
row->setStepTime_min( Unit::qstringToSI(value.toString()) );
283
//==========================CLASS MashStepItemDelegate===============================
285
MashStepItemDelegate::MashStepItemDelegate(QObject* parent)
286
: QItemDelegate(parent)
290
QWidget* MashStepItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
292
if( index.column() == MASHSTEPTYPECOL )
294
QComboBox *box = new QComboBox(parent);
296
box->addItem("Infusion");
297
box->addItem("Temperature");
298
box->addItem("Decoction");
299
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
304
return new QLineEdit(parent);
307
void MashStepItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
309
if( index.column() == MASHSTEPTYPECOL )
311
QComboBox* box = (QComboBox*)editor;
312
QString text = index.model()->data(index, Qt::DisplayRole).toString();
314
int index = box->findText(text);
315
box->setCurrentIndex(index);
319
QLineEdit* line = (QLineEdit*)editor;
321
line->setText(index.model()->data(index, Qt::DisplayRole).toString());
326
void MashStepItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
328
if( index.column() == MASHSTEPTYPECOL )
330
QComboBox* box = (QComboBox*)editor;
331
QString value = box->currentText();
333
model->setData(index, value, Qt::EditRole);
337
QLineEdit* line = (QLineEdit*)editor;
339
model->setData(index, line->text(), Qt::EditRole);
343
void MashStepItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& /*index*/) const
345
editor->setGeometry(option.rect);