~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to MiscTableModel.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
 
 * MiscTableModel.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 <QAbstractItemModel>
21
 
#include <QWidget>
22
 
#include <QModelIndex>
23
 
#include <QVariant>
24
 
#include <Qt>
25
 
#include <QString>
26
 
#include <QItemDelegate>
27
 
#include <QStyleOptionViewItem>
28
 
#include <QComboBox>
29
 
#include <QLineEdit>
30
 
#include <vector>
31
 
#include <iostream>
32
 
#include "misc.h"
33
 
#include "observable.h"
34
 
#include "MiscTableModel.h"
35
 
#include "unit.h"
36
 
#include "brewtarget.h"
37
 
 
38
 
MiscTableModel::MiscTableModel(MiscTableWidget* parent)
39
 
   : QAbstractTableModel(parent), MultipleObserver()
40
 
{
41
 
   miscObs.clear();
42
 
   parentTableWidget = parent;
43
 
}
44
 
 
45
 
void MiscTableModel::addMisc(Misc* misc)
46
 
{
47
 
   unsigned int i;
48
 
   // Check to see if it's already in the list.
49
 
   for( i = 0; i < miscObs.size(); ++i )
50
 
      if( miscObs[i] == misc )
51
 
         return;
52
 
   
53
 
   miscObs.push_back(misc);
54
 
   addObserved(misc);
55
 
   reset();
56
 
   
57
 
   if( parentTableWidget )
58
 
   {
59
 
      parentTableWidget->resizeColumnsToContents();
60
 
      parentTableWidget->resizeRowsToContents();
61
 
   }
62
 
}
63
 
 
64
 
// Returns true when misc is successfully found and removed.
65
 
bool MiscTableModel::removeMisc(Misc* misc)
66
 
{
67
 
   std::vector<Misc*>::iterator iter;
68
 
   
69
 
   for( iter=miscObs.begin(); iter != miscObs.end(); iter++ )
70
 
      if( *iter == misc )
71
 
      {
72
 
         miscObs.erase(iter);
73
 
         removeObserved(misc);
74
 
         reset();
75
 
 
76
 
         if( parentTableWidget )
77
 
         {
78
 
            parentTableWidget->resizeColumnsToContents();
79
 
            parentTableWidget->resizeRowsToContents();
80
 
         }
81
 
         
82
 
         return true;
83
 
      }
84
 
   
85
 
   return false;
86
 
}
87
 
 
88
 
void MiscTableModel::removeAll()
89
 
{
90
 
   unsigned int i;
91
 
 
92
 
   for( i = 0; i < miscObs.size(); ++i )
93
 
      removeObserved(miscObs[i]);
94
 
 
95
 
   miscObs.clear();
96
 
   reset();
97
 
}
98
 
 
99
 
int MiscTableModel::rowCount(const QModelIndex& /*parent*/) const
100
 
{
101
 
   return miscObs.size();
102
 
}
103
 
 
104
 
int MiscTableModel::columnCount(const QModelIndex& /*parent*/) const
105
 
{
106
 
   return 5;
107
 
}
108
 
 
109
 
QVariant MiscTableModel::data( const QModelIndex& index, int role ) const
110
 
{
111
 
   Misc* row;
112
 
   
113
 
   // Ensure the row is ok.
114
 
   if( index.row() >= (int)miscObs.size() )
115
 
   {
116
 
      std::cerr << "Bad model index. row = " << index.row() << std::endl;
117
 
      return QVariant();
118
 
   }
119
 
   else
120
 
      row = miscObs[index.row()];
121
 
   
122
 
   // Make sure we only respond to the DisplayRole role.
123
 
   if( role != Qt::DisplayRole )
124
 
      return QVariant();
125
 
   
126
 
   // Deal with the column and return the right data.
127
 
   if( index.column() == MISCNAMECOL )
128
 
   {
129
 
      return QVariant(row->getName().c_str());
130
 
   }
131
 
   else if( index.column() == MISCTYPECOL )
132
 
   {
133
 
      return QVariant(row->getType().c_str());
134
 
   }
135
 
   else if( index.column() == MISCUSECOL )
136
 
   {
137
 
      return QVariant(row->getUse().c_str());
138
 
   }
139
 
   else if( index.column() == MISCTIMECOL )
140
 
   {
141
 
      return QVariant( Brewtarget::displayAmount(row->getTime(), Units::minutes) );
142
 
   }
143
 
   else if( index.column() == MISCAMOUNTCOL )
144
 
   {
145
 
      return QVariant( Brewtarget::displayAmount(row->getAmount(), row->getAmountIsWeight()? (Unit*)Units::kilograms : (Unit*)Units::liters ) );
146
 
   }
147
 
   else
148
 
   {
149
 
      std::cerr << "Bad model index. column = " << index.column() << std::endl;
150
 
      return QVariant();
151
 
   }
152
 
}
153
 
 
154
 
QVariant MiscTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
155
 
{
156
 
   if( orientation == Qt::Horizontal && role == Qt::DisplayRole )
157
 
   {
158
 
      switch( section )
159
 
      {
160
 
         case MISCNAMECOL:
161
 
            return QVariant("Name");
162
 
         case MISCTYPECOL:
163
 
            return QVariant("Type");
164
 
         case MISCUSECOL:
165
 
            return QVariant("Use");
166
 
         case MISCTIMECOL:
167
 
            return QVariant("Time");
168
 
         case MISCAMOUNTCOL:
169
 
            return QVariant("Amount");
170
 
         default:
171
 
            return QVariant();
172
 
      }
173
 
   }
174
 
   else
175
 
      return QVariant();
176
 
}
177
 
 
178
 
Qt::ItemFlags MiscTableModel::flags(const QModelIndex& /*index*/ ) const
179
 
{
180
 
   return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
181
 
          Qt::ItemIsEnabled;
182
 
}
183
 
 
184
 
bool MiscTableModel::setData( const QModelIndex& index, const QVariant& value, int /*role*/ )
185
 
{
186
 
   Misc *row;
187
 
   int col;
188
 
   QString tmpStr;
189
 
   
190
 
   if( index.row() >= (int)miscObs.size() )
191
 
      return false;
192
 
   else
193
 
      row = miscObs[index.row()];
194
 
   
195
 
   col = index.column();
196
 
   
197
 
   if( col == MISCNAMECOL )
198
 
   {
199
 
      if( value.canConvert(QVariant::String) )
200
 
      {
201
 
         tmpStr = value.toString();
202
 
         row->setName(tmpStr.toStdString());
203
 
      }
204
 
      else
205
 
         return false;
206
 
   }
207
 
   else if( col == MISCTYPECOL )
208
 
   {
209
 
      if( value.canConvert(QVariant::String) )
210
 
      {
211
 
         tmpStr = value.toString();
212
 
         row->setType(tmpStr.toStdString());
213
 
      }
214
 
      else
215
 
         return false;
216
 
   }
217
 
   else if( col == MISCUSECOL )
218
 
   {
219
 
      if( value.canConvert(QVariant::String) )
220
 
      {
221
 
         tmpStr = value.toString();
222
 
         row->setUse(tmpStr.toStdString());
223
 
      }
224
 
      else
225
 
         return false;
226
 
   }
227
 
   else if( col == MISCTIMECOL )
228
 
   {
229
 
      if( value.canConvert(QVariant::String) )
230
 
         row->setTime( Unit::qstringToSI(value.toString()) );
231
 
      else
232
 
         return false;
233
 
   }
234
 
   else if( col == MISCAMOUNTCOL )
235
 
   {
236
 
      if( value.canConvert(QVariant::String) )
237
 
         row->setAmount( Unit::qstringToSI(value.toString()) );
238
 
      else
239
 
         return false;
240
 
   }
241
 
   else
242
 
      return false;
243
 
   
244
 
   emit dataChanged( index, index );
245
 
   return true;
246
 
}
247
 
 
248
 
void MiscTableModel::notify(Observable* notifier, QVariant info) // Gets called when an observable changes.
249
 
{
250
 
   int i;
251
 
   
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) );
256
 
}
257
 
 
258
 
Misc* MiscTableModel::getMisc(unsigned int i)
259
 
{
260
 
   return miscObs[i];
261
 
}
262
 
 
263
 
//======================CLASS MiscItemDelegate===========================
264
 
 
265
 
MiscItemDelegate::MiscItemDelegate(QObject* parent)
266
 
        : QItemDelegate(parent)
267
 
{
268
 
}
269
 
        
270
 
QWidget* MiscItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const
271
 
{
272
 
   if( index.column() == MISCTYPECOL )
273
 
   {
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);
282
 
      return box;
283
 
   }
284
 
   else if( index.column() == MISCUSECOL )
285
 
   {
286
 
      QComboBox *box = new QComboBox(parent);
287
 
 
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);
294
 
      return box;
295
 
   }
296
 
   else
297
 
      return new QLineEdit(parent);
298
 
}
299
 
 
300
 
void MiscItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
301
 
{
302
 
   int column = index.column();
303
 
   
304
 
   if( column == MISCTYPECOL ||  column == MISCUSECOL )
305
 
   {
306
 
      QComboBox* box = (QComboBox*)editor;
307
 
      QString text = index.model()->data(index, Qt::DisplayRole).toString();
308
 
      
309
 
      int index = box->findText(text);
310
 
      box->setCurrentIndex(index);
311
 
   }
312
 
   else
313
 
   {
314
 
      QLineEdit* line = (QLineEdit*)editor;
315
 
      
316
 
      line->setText(index.model()->data(index, Qt::DisplayRole).toString());
317
 
   }
318
 
   
319
 
}
320
 
 
321
 
void MiscItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
322
 
{
323
 
   int column = index.column();
324
 
   if( column == MISCTYPECOL || column == MISCUSECOL )
325
 
   {
326
 
      QComboBox* box = (QComboBox*)editor;
327
 
      QString value = box->currentText();
328
 
      
329
 
      model->setData(index, value, Qt::EditRole);
330
 
   }
331
 
   else
332
 
   {
333
 
      QLineEdit* line = (QLineEdit*)editor;
334
 
      
335
 
      model->setData(index, line->text(), Qt::EditRole);
336
 
   }
337
 
}
338
 
 
339
 
void MiscItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
340
 
{
341
 
   editor->setGeometry(option.rect);
342
 
}
343