~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

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