Debian Codegraph @

src/grainmodel.cpp

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