2
* HopTableModel.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>
22
#include <QModelIndex>
25
#include <QItemDelegate>
26
#include <QStyleOptionViewItem>
36
#include "observable.h"
37
#include "HopTableModel.h"
39
#include "brewtarget.h"
41
HopTableModel::HopTableModel(HopTableWidget* parent)
42
: QAbstractTableModel(parent), MultipleObserver()
45
parentTableWidget = parent;
50
void HopTableModel::addHop(Hop* hop)
52
std::vector<Hop*>::iterator iter;
54
//Check to see if it's already in the list
55
for( iter=hopObs.begin(); iter != hopObs.end(); iter++ )
59
hopObs.push_back(hop);
61
reset(); // Tell everybody that the table has changed.
63
if( parentTableWidget )
65
parentTableWidget->resizeColumnsToContents();
66
parentTableWidget->resizeRowsToContents();
70
void HopTableModel::setShowIBUs( bool var )
75
void HopTableModel::setRecipe( Recipe* rec )
78
removeObserved(recObs);
87
bool HopTableModel::removeHop(Hop* hop)
89
std::vector<Hop*>::iterator iter;
91
for( iter=hopObs.begin(); iter != hopObs.end(); iter++ )
96
reset(); // Tell everybody the table has changed.
98
if( parentTableWidget )
100
parentTableWidget->resizeColumnsToContents();
101
parentTableWidget->resizeRowsToContents();
110
void HopTableModel::removeAll()
114
for( i = 0; i < hopObs.size(); ++i )
115
removeObserved(hopObs[i]);
121
void HopTableModel::notify(Observable* notifier, QVariant info)
125
if( notifier == recObs )
126
emit headerDataChanged( Qt::Vertical, 0, hopObs.size() );
128
// Find the notifier in the list
129
for( i = 0; i < (int)hopObs.size(); ++i )
131
if( notifier == hopObs[i] )
132
emit dataChanged( QAbstractItemModel::createIndex(i, 0),
133
QAbstractItemModel::createIndex(i, HOPNUMCOLS));
137
int HopTableModel::rowCount(const QModelIndex& /*parent*/) const
139
return hopObs.size();
142
int HopTableModel::columnCount(const QModelIndex& /*parent*/) const
147
QVariant HopTableModel::data( const QModelIndex& index, int role ) const
151
// Ensure the row is ok.
152
if( index.row() >= (int)hopObs.size() )
154
std::cerr << "Bad model index. row = " << index.row() << std::endl;
158
row = hopObs[index.row()];
160
// Make sure we only respond to the DisplayRole role.
161
if( role != Qt::DisplayRole )
164
switch( index.column() )
167
return QVariant(row->getName().c_str());
169
return QVariant( Brewtarget::displayAmount(row->getAlpha_pct(), 0) );
171
return QVariant( Brewtarget::displayAmount(row->getAmount_kg(), Units::kilograms) );
173
return QVariant(row->getUse().c_str());
175
return QVariant( Brewtarget::displayAmount(row->getTime_min(), Units::minutes) );
177
std::cerr << "Bad column: " << index.column() << std::endl;
182
QVariant HopTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
184
if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
189
return QVariant("Name");
191
return QVariant("Alpha %");
193
return QVariant("Amount");
195
return QVariant("Use");
197
return QVariant("Time");
199
std::cerr << "Bad column: " << section << std::endl;
203
else if( showIBUs && recObs && orientation == Qt::Vertical && role == Qt::DisplayRole )
205
double ibus = recObs->getIBUFromHop( section );
206
return QVariant( QString("%1 IBU").arg( ibus, 0, 'f', 1 ) );
212
Qt::ItemFlags HopTableModel::flags(const QModelIndex& /*index*/ ) const
214
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
218
bool HopTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
222
if( index.row() >= (int)hopObs.size() || role != Qt::EditRole )
225
row = hopObs[index.row()];
227
switch( index.column() )
230
if( value.canConvert(QVariant::String))
232
row->setName(value.toString().toStdString());
238
if( value.canConvert(QVariant::String) )
240
row->setAlpha_pct( Unit::qstringToSI(value.toString()) );
246
if( value.canConvert(QVariant::String) )
248
row->setAmount_kg( Unit::qstringToSI(value.toString()) );
254
if( value.canConvert(QVariant::String) )
256
row->setUse(value.toString().toStdString());
262
if( value.canConvert(QVariant::String) )
264
row->setTime_min( Unit::qstringToSI(value.toString()) );
270
std::cerr << "Bad column: " << index.column() << std::endl;
275
Hop* HopTableModel::getHop(unsigned int i)
280
//==========================CLASS HopItemDelegate===============================
282
HopItemDelegate::HopItemDelegate(QObject* parent)
283
: QItemDelegate(parent)
287
QWidget* HopItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex &index) const
289
if( index.column() == HOPUSECOL )
291
QComboBox *box = new QComboBox(parent);
293
box->addItem("Boil");
294
box->addItem("Dry Hop");
295
box->addItem("Mash");
296
box->addItem("First Wort");
297
box->addItem("Aroma");
298
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
303
return new QLineEdit(parent);
306
void HopItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
308
if( index.column() == HOPUSECOL )
310
QComboBox* box = (QComboBox*)editor;
311
QString text = index.model()->data(index, Qt::DisplayRole).toString();
313
int index = box->findText(text);
314
box->setCurrentIndex(index);
318
QLineEdit* line = (QLineEdit*)editor;
320
line->setText(index.model()->data(index, Qt::DisplayRole).toString());
325
void HopItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
327
if( index.column() == HOPUSECOL )
329
QComboBox* box = (QComboBox*)editor;
330
QString value = box->currentText();
332
model->setData(index, value, Qt::EditRole);
336
QLineEdit* line = (QLineEdit*)editor;
338
model->setData(index, line->text(), Qt::EditRole);
342
void HopItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& /*index*/) const
344
editor->setGeometry(option.rect);