Debian Codegraph @

src/stylemodel.cpp

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