~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/MashStepTableModel.cpp

  • Committer: Philip Greggory Lee
  • Date: 2009-08-23 16:53:43 UTC
  • Revision ID: git-v1:f8d1a25135bd92f06c46c562293800e4faa42c61
Made a src/ and ui/ directory and moved everything.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * MashStepTableModel.cpp is part of Brewtarget, and is Copyright Philip G. Lee
 
3
 * (rocketman768@gmail.com), 2009.
 
4
 *
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
 */
 
18
 
 
19
#include <QAbstractTableModel>
 
20
#include <QWidget>
 
21
#include <QModelIndex>
 
22
#include <QVariant>
 
23
#include <Qt>
 
24
#include <QItemDelegate>
 
25
#include <QObject>
 
26
#include <QComboBox>
 
27
#include <QLineEdit>
 
28
#include <vector>
 
29
#include <iostream>
 
30
#include "mashstep.h"
 
31
#include "observable.h"
 
32
#include "MashStepTableModel.h"
 
33
#include "unit.h"
 
34
#include "brewtarget.h"
 
35
 
 
36
MashStepTableModel::MashStepTableModel(MashStepTableWidget* parent)
 
37
: QAbstractTableModel(parent), Observer()
 
38
{
 
39
   parentTableWidget = parent;
 
40
   mashObs = 0;
 
41
}
 
42
 
 
43
void MashStepTableModel::setMash( Mash* m )
 
44
{
 
45
   mashObs = m;
 
46
   setObserved(mashObs);
 
47
   reset(); // Tell everybody that the table has changed.
 
48
   
 
49
   if( parentTableWidget )
 
50
   {
 
51
      parentTableWidget->resizeColumnsToContents();
 
52
      parentTableWidget->resizeRowsToContents();
 
53
   }
 
54
}
 
55
 
 
56
/***************************************************
 
57
void MashStepTableModel::addMashStep(MashStep* step)
 
58
{
 
59
   std::vector<MashStep*>::iterator iter;
 
60
 
 
61
   //Check to see if it's already in the list
 
62
   for( iter=stepObs.begin(); iter != stepObs.end(); iter++ )
 
63
      if( *iter == step )
 
64
         return;
 
65
 
 
66
   stepObs.push_back(step);
 
67
   addObserved(step);
 
68
   reset(); // Tell everybody that the table has changed.
 
69
 
 
70
   if( parentTableWidget )
 
71
   {
 
72
      parentTableWidget->resizeColumnsToContents();
 
73
      parentTableWidget->resizeRowsToContents();
 
74
   }
 
75
}
 
76
 
 
77
bool MashStepTableModel::removeMashStep(MashStep* step)
 
78
{
 
79
   std::vector<MashStep*>::iterator iter;
 
80
 
 
81
   for( iter=stepObs.begin(); iter != stepObs.end(); iter++ )
 
82
      if( *iter == step )
 
83
      {
 
84
         stepObs.erase(iter);
 
85
         removeObserved(step);
 
86
         reset(); // Tell everybody the table has changed.
 
87
         
 
88
         if( parentTableWidget )
 
89
         {
 
90
            parentTableWidget->resizeColumnsToContents();
 
91
            parentTableWidget->resizeRowsToContents();
 
92
         }
 
93
         
 
94
         return true;
 
95
      }
 
96
 
 
97
   return false;
 
98
}
 
99
 
 
100
void MashStepTableModel::removeAll()
 
101
{
 
102
   unsigned int i;
 
103
 
 
104
   for( i = 0; i < stepObs.size(); ++i )
 
105
      removeObserved(stepObs[i]);
 
106
 
 
107
   stepObs.clear();
 
108
   reset();
 
109
}
 
110
 
 
111
MashStep* MashStepTableModel::getMashStep(unsigned int i)
 
112
{
 
113
   return stepObs[i];
 
114
}
 
115
*************************************************************/
 
116
 
 
117
void MashStepTableModel::notify(Observable* notifier, QVariant info)
 
118
{
 
119
   int i;
 
120
   bool ok = false;
 
121
   
 
122
   if( notifier != mashObs )
 
123
      return;
 
124
   
 
125
   i = info.toInt(&ok); // mashObs' info says which MashStep changed.
 
126
   
 
127
   if( ok )
 
128
   {
 
129
      emit dataChanged( QAbstractItemModel::createIndex(i, 0),
 
130
                        QAbstractItemModel::createIndex(i, MASHSTEPNUMCOLS));
 
131
   }
 
132
   else
 
133
      reset();
 
134
   
 
135
   if( parentTableWidget )
 
136
   {
 
137
      parentTableWidget->resizeColumnsToContents();
 
138
      parentTableWidget->resizeRowsToContents();
 
139
   }
 
140
}
 
141
 
 
142
int MashStepTableModel::rowCount(const QModelIndex& /*parent*/) const
 
143
{
 
144
   if( mashObs != 0 )
 
145
      return mashObs->getNumMashSteps();
 
146
   else
 
147
      return 0;
 
148
}
 
149
 
 
150
int MashStepTableModel::columnCount(const QModelIndex& /*parent*/) const
 
151
{
 
152
   return MASHSTEPNUMCOLS;
 
153
}
 
154
 
 
155
QVariant MashStepTableModel::data( const QModelIndex& index, int role ) const
 
156
{
 
157
   MashStep* row;
 
158
 
 
159
   if( mashObs == 0 )
 
160
      return QVariant();
 
161
   
 
162
   // Ensure the row is ok.
 
163
   if( index.row() >= (int)(mashObs->getNumMashSteps()) )
 
164
   {
 
165
      std::cerr << "Bad model index. row = " << index.row() << std::endl;
 
166
      return QVariant();
 
167
   }
 
168
   else
 
169
      row = mashObs->getMashStep(index.row());
 
170
 
 
171
   // Make sure we only respond to the DisplayRole role.
 
172
   if( role != Qt::DisplayRole )
 
173
      return QVariant();
 
174
 
 
175
   switch( index.column() )
 
176
   {
 
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) );
 
187
      default :
 
188
         std::cerr << "Bad column: " << index.column() << std::endl;
 
189
         return QVariant();
 
190
   }
 
191
}
 
192
 
 
193
QVariant MashStepTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
 
194
{
 
195
   if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
 
196
   {
 
197
      switch( section )
 
198
      {
 
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");
 
209
         default:
 
210
            return QVariant();
 
211
      }
 
212
   }
 
213
   else
 
214
      return QVariant();
 
215
}
 
216
 
 
217
Qt::ItemFlags MashStepTableModel::flags(const QModelIndex& /*index*/ ) const
 
218
{
 
219
   return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
 
220
          Qt::ItemIsEnabled;
 
221
}
 
222
 
 
223
bool MashStepTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
 
224
{
 
225
   MashStep *row;
 
226
 
 
227
   if( mashObs == 0 )
 
228
      return false;
 
229
   
 
230
   if( index.row() >= (int)(mashObs->getNumMashSteps()) || role != Qt::EditRole )
 
231
      return false;
 
232
   else
 
233
      row = mashObs->getMashStep(index.row());
 
234
 
 
235
   switch( index.column() )
 
236
   {
 
237
      case MASHSTEPNAMECOL:
 
238
         if( value.canConvert(QVariant::String))
 
239
         {
 
240
            row->setName(value.toString().toStdString());
 
241
            return true;
 
242
         }
 
243
         else
 
244
            return false;
 
245
      case MASHSTEPTYPECOL:
 
246
         if( value.canConvert(QVariant::String) )
 
247
         {
 
248
            row->setType(value.toString().toStdString());
 
249
            return true;
 
250
         }
 
251
         else
 
252
            return false;
 
253
      case MASHSTEPAMOUNTCOL:
 
254
         if( value.canConvert(QVariant::String) )
 
255
         {
 
256
            row->setInfuseAmount_l( Unit::qstringToSI(value.toString()) );
 
257
            return true;
 
258
         }
 
259
         else
 
260
            return false;
 
261
      case MASHSTEPTEMPCOL:
 
262
         if( value.canConvert(QVariant::String) )
 
263
         {
 
264
            row->setStepTemp_c( Unit::qstringToSI(value.toString()) );
 
265
            row->setEndTemp_c( Unit::qstringToSI(value.toString()) );
 
266
            return true;
 
267
         }
 
268
         else
 
269
            return false;
 
270
      case MASHSTEPTIMECOL:
 
271
         if( value.canConvert(QVariant::String) )
 
272
         {
 
273
            row->setStepTime_min( Unit::qstringToSI(value.toString()) );
 
274
            return true;
 
275
         }
 
276
         else
 
277
            return false;
 
278
      default:
 
279
         return false;
 
280
   }
 
281
}
 
282
 
 
283
//==========================CLASS MashStepItemDelegate===============================
 
284
 
 
285
MashStepItemDelegate::MashStepItemDelegate(QObject* parent)
 
286
        : QItemDelegate(parent)
 
287
{
 
288
}
 
289
 
 
290
QWidget* MashStepItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
 
291
{
 
292
   if( index.column() == MASHSTEPTYPECOL )
 
293
   {
 
294
      QComboBox *box = new QComboBox(parent);
 
295
 
 
296
      box->addItem("Infusion");
 
297
      box->addItem("Temperature");
 
298
      box->addItem("Decoction");
 
299
      box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
 
300
 
 
301
      return box;
 
302
   }
 
303
   else
 
304
      return new QLineEdit(parent);
 
305
}
 
306
 
 
307
void MashStepItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 
308
{
 
309
   if( index.column() == MASHSTEPTYPECOL )
 
310
   {
 
311
      QComboBox* box = (QComboBox*)editor;
 
312
      QString text = index.model()->data(index, Qt::DisplayRole).toString();
 
313
 
 
314
      int index = box->findText(text);
 
315
      box->setCurrentIndex(index);
 
316
   }
 
317
   else
 
318
   {
 
319
      QLineEdit* line = (QLineEdit*)editor;
 
320
 
 
321
      line->setText(index.model()->data(index, Qt::DisplayRole).toString());
 
322
   }
 
323
 
 
324
}
 
325
 
 
326
void MashStepItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
 
327
{
 
328
   if( index.column() == MASHSTEPTYPECOL )
 
329
   {
 
330
      QComboBox* box = (QComboBox*)editor;
 
331
      QString value = box->currentText();
 
332
 
 
333
      model->setData(index, value, Qt::EditRole);
 
334
   }
 
335
   else
 
336
   {
 
337
      QLineEdit* line = (QLineEdit*)editor;
 
338
 
 
339
      model->setData(index, line->text(), Qt::EditRole);
 
340
   }
 
341
}
 
342
 
 
343
void MashStepItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& /*index*/) const
 
344
{
 
345
   editor->setGeometry(option.rect);
 
346
}
 
347