Debian Codegraph @

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