2
* MiscTableModel.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>
26
#include <QItemDelegate>
27
#include <QStyleOptionViewItem>
33
#include "observable.h"
34
#include "MiscTableModel.h"
36
#include "brewtarget.h"
38
MiscTableModel::MiscTableModel(MiscTableWidget* parent)
39
: QAbstractTableModel(parent), MultipleObserver()
42
parentTableWidget = parent;
45
void MiscTableModel::addMisc(Misc* misc)
48
// Check to see if it's already in the list.
49
for( i = 0; i < miscObs.size(); ++i )
50
if( miscObs[i] == misc )
53
miscObs.push_back(misc);
57
if( parentTableWidget )
59
parentTableWidget->resizeColumnsToContents();
60
parentTableWidget->resizeRowsToContents();
64
// Returns true when misc is successfully found and removed.
65
bool MiscTableModel::removeMisc(Misc* misc)
67
std::vector<Misc*>::iterator iter;
69
for( iter=miscObs.begin(); iter != miscObs.end(); iter++ )
76
if( parentTableWidget )
78
parentTableWidget->resizeColumnsToContents();
79
parentTableWidget->resizeRowsToContents();
88
void MiscTableModel::removeAll()
92
for( i = 0; i < miscObs.size(); ++i )
93
removeObserved(miscObs[i]);
99
int MiscTableModel::rowCount(const QModelIndex& /*parent*/) const
101
return miscObs.size();
104
int MiscTableModel::columnCount(const QModelIndex& /*parent*/) const
109
QVariant MiscTableModel::data( const QModelIndex& index, int role ) const
113
// Ensure the row is ok.
114
if( index.row() >= (int)miscObs.size() )
116
std::cerr << "Bad model index. row = " << index.row() << std::endl;
120
row = miscObs[index.row()];
122
// Make sure we only respond to the DisplayRole role.
123
if( role != Qt::DisplayRole )
126
// Deal with the column and return the right data.
127
if( index.column() == MISCNAMECOL )
129
return QVariant(row->getName().c_str());
131
else if( index.column() == MISCTYPECOL )
133
return QVariant(row->getType().c_str());
135
else if( index.column() == MISCUSECOL )
137
return QVariant(row->getUse().c_str());
139
else if( index.column() == MISCTIMECOL )
141
return QVariant( Brewtarget::displayAmount(row->getTime(), Units::minutes) );
143
else if( index.column() == MISCAMOUNTCOL )
145
return QVariant( Brewtarget::displayAmount(row->getAmount(), row->getAmountIsWeight()? (Unit*)Units::kilograms : (Unit*)Units::liters ) );
149
std::cerr << "Bad model index. column = " << index.column() << std::endl;
154
QVariant MiscTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
156
if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
161
return QVariant("Name");
163
return QVariant("Type");
165
return QVariant("Use");
167
return QVariant("Time");
169
return QVariant("Amount");
178
Qt::ItemFlags MiscTableModel::flags(const QModelIndex& /*index*/ ) const
180
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
184
bool MiscTableModel::setData( const QModelIndex& index, const QVariant& value, int /*role*/ )
190
if( index.row() >= (int)miscObs.size() )
193
row = miscObs[index.row()];
195
col = index.column();
197
if( col == MISCNAMECOL )
199
if( value.canConvert(QVariant::String) )
201
tmpStr = value.toString();
202
row->setName(tmpStr.toStdString());
207
else if( col == MISCTYPECOL )
209
if( value.canConvert(QVariant::String) )
211
tmpStr = value.toString();
212
row->setType(tmpStr.toStdString());
217
else if( col == MISCUSECOL )
219
if( value.canConvert(QVariant::String) )
221
tmpStr = value.toString();
222
row->setUse(tmpStr.toStdString());
227
else if( col == MISCTIMECOL )
229
if( value.canConvert(QVariant::String) )
230
row->setTime( Unit::qstringToSI(value.toString()) );
234
else if( col == MISCAMOUNTCOL )
236
if( value.canConvert(QVariant::String) )
237
row->setAmount( Unit::qstringToSI(value.toString()) );
244
emit dataChanged( index, index );
248
void MiscTableModel::notify(Observable* notifier, QVariant info) // Gets called when an observable changes.
252
for( i = 0; i < (int)miscObs.size(); ++i )
253
if( notifier == miscObs[i] )
254
emit dataChanged( QAbstractItemModel::createIndex(i, 0),
255
QAbstractItemModel::createIndex(i, MISCNUMCOLS) );
258
Misc* MiscTableModel::getMisc(unsigned int i)
263
//======================CLASS MiscItemDelegate===========================
265
MiscItemDelegate::MiscItemDelegate(QObject* parent)
266
: QItemDelegate(parent)
270
QWidget* MiscItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const
272
if( index.column() == MISCTYPECOL )
274
QComboBox *box = new QComboBox(parent);
275
box->addItem("Spice");
276
box->addItem("Fining");
277
box->addItem("Water Agent");
278
box->addItem("Herb");
279
box->addItem("Flavor");
280
box->addItem("Other");
281
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
284
else if( index.column() == MISCUSECOL )
286
QComboBox *box = new QComboBox(parent);
288
box->addItem("Boil");
289
box->addItem("Mash");
290
box->addItem("Primary");
291
box->addItem("Secondary");
292
box->addItem("Bottling");
293
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
297
return new QLineEdit(parent);
300
void MiscItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
302
int column = index.column();
304
if( column == MISCTYPECOL || column == MISCUSECOL )
306
QComboBox* box = (QComboBox*)editor;
307
QString text = index.model()->data(index, Qt::DisplayRole).toString();
309
int index = box->findText(text);
310
box->setCurrentIndex(index);
314
QLineEdit* line = (QLineEdit*)editor;
316
line->setText(index.model()->data(index, Qt::DisplayRole).toString());
321
void MiscItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
323
int column = index.column();
324
if( column == MISCTYPECOL || column == MISCUSECOL )
326
QComboBox* box = (QComboBox*)editor;
327
QString value = box->currentText();
329
model->setData(index, value, Qt::EditRole);
333
QLineEdit* line = (QLineEdit*)editor;
335
model->setData(index, line->text(), Qt::EditRole);
339
void MiscItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
341
editor->setGeometry(option.rect);