Debian Codegraph @

src/grainmodel.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /***************************************************************************
2 grainmodel.cpp
3 -------------------
4 Grain model
5 -------------------
6 Copyright 2006-2008, David Johnson
7 Please see the header file for copyright and license information
8 ***************************************************************************/
9
10 #include <QApplication>
11 #include <QMessageBox>
12 #include <QPair>
13
14 #include "data.h"
15 #include "recipe.h"
16 #include "resource.h"
17 #include "grainmodel.h"
18
19 using namespace Resource;
20
21 //////////////////////////////////////////////////////////////////////////////
22 // GrainModel()
23 // ------------
24 // Constructor
25
26 // TODO: add "unique" flag, disallowing duplicates (for DatabaseTool)
27
28 GrainModel::GrainModel(QObject *parent, GrainList *list)
29 : QAbstractTableModel(parent), list_(list)
30 {}
31
32 GrainModel::~GrainModel(){}
33
34 //////////////////////////////////////////////////////////////////////////////
35 // flush()
36 // -------
37 // Reset the model
•38 // The reset() method is obsolete with Qt5.
39
40 void GrainModel::flush()
41 {
•42 // reset();
43 }
44
45 //////////////////////////////////////////////////////////////////////////////
46 // data()
47 // ------
48 // Return data at index
49
50 QVariant GrainModel::data(const QModelIndex &index, int role) const
51 {
52 if (!index.isValid()) return QVariant();
53 if (index.row() >= list_->count()) return QVariant();
54
55 // row is the entry in the QList
56 const Grain &grain = list_->at(index.row());
57
58 // column is the grain "field"
59 if (role == Qt::DisplayRole) {
60 switch (index.column()) {
61 case NAME:
62 return grain.name();
63 case WEIGHT:
64 return grain.weight().toString(3);
65 case EXTRACT:
66 return QString::number(grain.extract(), 'f', 3);
67 case COLOR:
68 return QString::number(grain.color(), 'f', 1) + Resource::DEGREE;
69 case TYPE:
70 return grain.type();
71 case USE:
72 return grain.use();
73 default:
74 return QVariant();
75 }
76 } else if (role == Qt::EditRole) {
77 switch (index.column()) {
78 case NAME:
79 return grain.name();
80 case WEIGHT: {
81 // return converted quantity
82 Weight weight = grain.weight();
83 weight.convert(Data::instance()->defaultGrainUnit());
84 return weight.amount();
85 }
86 case EXTRACT:
87 return grain.extract();
88 case COLOR:
89 return grain.color();
90 case TYPE:
91 return grain.type();
92 case USE:
93 return grain.use();
94 default:
95 return QVariant();
96 }
97 } else if (role == Qt::TextAlignmentRole) {
98 switch (index.column()) {
99 case NAME:
100 case TYPE:
101 case USE:
102 return Qt::AlignLeft;
103 case WEIGHT:
104 case EXTRACT:
105 case COLOR:
106 default:
107 return Qt::AlignRight;
108 }
109 } else {
110 return QVariant();
111 }
112 }
113
114 //////////////////////////////////////////////////////////////////////////////
115 // setData()
116 // ---------
117 // Set data at index
118
119 bool GrainModel::setData(const QModelIndex &index,
120 const QVariant &value, int role)
121 {
122 static bool deleting = false;
123
124 Grain grain;
125 QString name;
126 int row = index.row();
127 int column = index.column();
128
129 if (!index.isValid()) return false;
130 if (role != Qt::EditRole) return false;
131 if (row >= list_->count()) return false;
132
133 // grab existing grain
134 grain = list_->value(row);
135
136 switch (column) {
137 case NAME:
138 // editing name as several special cases
139 name = value.toString();
140
141 // deleting name deletes ingredient
142 if (name.isEmpty()) {
143 if (row >= list_->count()) return false; // empty
144
145 // TODO: for some reason this gets entered recursively...
146 if (deleting) return false;
147 deleting = true;
148
149 int status = QMessageBox::question(QApplication::activeWindow(),
150 TITLE + tr(" - Delete?"),
151 tr("Do you wish to remove this entry?"),
152 QMessageBox::Yes | QMessageBox::Cancel);
153 if (status == QMessageBox::Yes) {
154 // remove grain
155 beginRemoveRows(index.parent(), row, row);
156
157 list_->removeAt(row);
158 emit modified();
159 endRemoveRows();
160
161 deleting = false;
162 return true;
163 } else {
164 // ignore
165 deleting = false;
166 return false;
167 }
168 }
169
170 // no change, nothing to do
171 if (name == list_->at(row).name()) {
172 return false;
173 }
174
175 // changed name
176 grain.setName(name);
177 if (Data::instance()->hasGrain(name)) {
178 Grain newgrain = Data::instance()->grain(name);
179 // we don't override weight
180 grain.setExtract(newgrain.extract());
181 grain.setColor(newgrain.color());
182 grain.setType(newgrain.type());
183 grain.setUse(newgrain.use());
184 }
185 break;
186
187 case WEIGHT:
188 grain.setWeight(Weight(value.toDouble(),
189 Data::instance()->defaultGrainUnit()));
190 break;
191
192 case EXTRACT:
193 grain.setExtract(value.toDouble());
194 break;
195
196 case COLOR:
197 grain.setColor(value.toDouble());
198 break;
199
200 case TYPE:
201 grain.setType(value.toString());
202 break;
203
204 case USE:
205 grain.setUse(value.toString());
206 break;
207
208 default:
209 return false;
210 }
211
212 list_->replace(row, grain);
213 emit modified();
214
215 // whole row may have changed
216 emit dataChanged(index.sibling(row, NAME),
217 index.sibling(row, USE));
218
219 return true;
220 }
221
222 //////////////////////////////////////////////////////////////////////////////
223 // insertRows()
224 // ------------
225 // Insert rows into table
226
227 bool GrainModel::insertRows(int row, int count, const QModelIndex&)
228 {
229 if (count != 1) return false; // only insert one row at a time
230 if ((row < 0) || (row >= list_->count())) row = list_->count();
231
232 Grain grain = Data::instance()->grain(tr("Generic"));
233
234 beginInsertRows(QModelIndex(), row, row);
235 list_->insert(row, grain);
236 emit modified();
237 endInsertRows();
238 return true;
239 }
240
241 //////////////////////////////////////////////////////////////////////////////
242 // removeRows()
243 // ------------
244 // Remove rows from table
245
246 bool GrainModel::removeRows(int row, int count, const QModelIndex&)
247 {
248 if (count != 1) return false; // only remove one row at a time
249 if ((row < 0) || (row >= list_->count())) return false;
250
251 int status = QMessageBox::question(QApplication::activeWindow(),
252 TITLE + tr(" - Delete?"),
253 tr("Do you wish to remove this entry?"),
254 QMessageBox::Yes | QMessageBox::Cancel);
255 if (status == QMessageBox::Cancel) {
256 return false;
257 }
258
259 beginRemoveRows(QModelIndex(), row, row);
260 list_->removeAt(row);
261 emit modified();
262 endRemoveRows();
263 return true;
264 }
265
266 //////////////////////////////////////////////////////////////////////////////
267 // headerData()
268 // ------------
269 // Return header information
270
271 QVariant GrainModel::headerData(int section, Qt::Orientation orientation,
272 int role) const
273 {
274 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
275 switch (section) {
276 case NAME:
277 return tr("Grain");
278 case WEIGHT:
279 return tr("Weight");
280 case EXTRACT:
281 return tr("Extract");
282 case COLOR:
283 return tr("Color");
284 case TYPE:
285 return tr("Type");
286 case USE:
287 return tr("Use");
288 default:
289 return QVariant();
290 }
291 }
292
293 return QVariant();
294 }
295
296 //////////////////////////////////////////////////////////////////////////////
297 // flags()
298 // ------
299 // Return flags at index
300
301 Qt::ItemFlags GrainModel::flags(const QModelIndex &index) const
302 {
303 if (!index.isValid()) return Qt::ItemIsEnabled;
304
305 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
306 }
307
308 //////////////////////////////////////////////////////////////////////////////
309 // rowCount()
310 // ----------
311 // Return number of rows of data
312
313 int GrainModel::rowCount(const QModelIndex &) const
314 {
315 return list_->count();
316 }
317
318 //////////////////////////////////////////////////////////////////////////////
319 // columnCount()
320 // -------------
321 // Return number of columns of data
322
323 int GrainModel::columnCount(const QModelIndex &) const
324 {
325 return COUNT;
326 }
327
328 //////////////////////////////////////////////////////////////////////////////
329 // sort()
330 // ------
331 // Sort by column
332
333 void GrainModel::sort(int column, Qt::SortOrder order)
334 {
335 QList<QPair<QString,Grain> > sortlist;
336
337 foreach(Grain grain, *list_) {
338 QString field;
339 switch (column) {
340 case NAME:
341 field = grain.name();
342 break;
343 case WEIGHT:
344 field = QString::number(grain.weight().amount()).rightJustified(8,'0');
345 break;
346 case EXTRACT:
347 field = QString::number(grain.extract(),'f',3).rightJustified(8,'0');
348 break;
349 case COLOR:
350 field = QString::number(grain.color(),'f',1).rightJustified(8,'0');
351 break;
352 case TYPE:
353 field = grain.type();
354 break;
355 case USE:
356 default:
357 field = grain.use();
358 break;
359 }
360 sortlist.append(QPair<QString,Grain>(field, grain));
361 }
362
363 // sort list
364 qSort(sortlist.begin(), sortlist.end());
365
366 emit layoutAboutToBeChanged();
367
368 // create new list
369 list_->clear();
370 QPair<QString,Grain> pair;
371 foreach(pair, sortlist) {
372 if (order == Qt::AscendingOrder)
373 list_->append(pair.second);
374 else
375 list_->prepend(pair.second);
376 }
377
378 emit layoutChanged();
379 }