File: stylemodel.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (389 lines) | stat: -rw-r--r-- 10,349 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/***************************************************************************
  stylemodel.cpp
  -------------------
  Style model
  -------------------
  Copyright 2006-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QApplication>
#include <QMessageBox>
#include <QPair>

#include "data.h"
#include "stylemodel.h"

using namespace Resource;

//////////////////////////////////////////////////////////////////////////////
// StyleModel()
// ------------
// Constructor

StyleModel::StyleModel(QObject *parent, StyleList *list)
    : QAbstractTableModel(parent), list_(list)
{}

StyleModel::~StyleModel(){}

//////////////////////////////////////////////////////////////////////////////
// flush()
// -------
// Reset the model

void StyleModel::flush()
{
    reset();
}

//////////////////////////////////////////////////////////////////////////////
// data()
// ------
// Return data at index

QVariant StyleModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();
    if (index.row() >= list_->count()) return QVariant();

    // row is the entry in the QList
    const Style &style = list_->at(index.row());

    // column is the style "field"
    if (role == Qt::DisplayRole) {
        switch (index.column()) {
          case NAME:
              return style.name();
          case OGLOW:
              return QString::number(style.oglow_, 'f', 3);
          case OGHI:
              return QString::number(style.oghi_, 'f', 3);
          case FGLOW:
              return QString::number(style.fglow_, 'f', 3);
          case FGHI:
              return QString::number(style.fghi_, 'f', 3);
          case IBULOW:
              return QString::number(style.ibulow_);
          case IBUHI:
              return QString::number(style.ibuhi_);
          case SRMLOW:
              return QString::number(style.srmlow_);
          case SRMHI:
              return QString::number(style.srmhi_);
          default:
              return QVariant();
        }
    } else if (role == Qt::EditRole) {
        switch (index.column()) {
          case NAME:
              return style.name();
          case OGLOW:
              return style.oglow_;
          case OGHI:
              return style.oghi_;
          case FGLOW:
              return style.fglow_;
          case FGHI:
              return style.fghi_;
          case IBULOW:
              return style.ibulow_;
          case IBUHI:
              return style.ibuhi_;
          case SRMLOW:
              return style.srmlow_;
          case SRMHI:
              return style.srmhi_;
          default:
              return QVariant();
        }
    } else if (role == Qt::TextAlignmentRole) {
        switch (index.column()) {
          case NAME:
              return Qt::AlignLeft;
          case OGLOW:
          case OGHI:
          case FGLOW:
          case FGHI:
          case IBULOW:
          case IBUHI:
          case SRMLOW:
          case SRMHI:
          default:
              return Qt::AlignRight;
        }
    } else {
        return QVariant();
    }
}

//////////////////////////////////////////////////////////////////////////////
// setData()
// ---------
// Set data at index

bool StyleModel::setData(const QModelIndex &index,
                       const QVariant &value, int role)
{
    static bool deleting = false;

    Style style;
    QString name;
    int row = index.row();
    int column = index.column();

    if (!index.isValid()) return false;
    if (role != Qt::EditRole) return false;
    if ((row >= list_->count())
        && (column != NAME)) return false;

    // grab existing style
    if (row < list_->count()) style = list_->value(row);

    switch (column) {
      case NAME:
          // editing name as several special cases
          name = value.toString();

          // deleting name deletes style
          if (name.isEmpty()) {
              if (row >= list_->count()) return false; // empty

              // TODO: for some reason this gets entered recursively...
              if (deleting) return false;
              deleting = true;

              // remove style
              beginRemoveRows(index.parent(), row, row);

              list_->removeAt(row);
              emit modified();
              endRemoveRows();

              deleting = false;
              return true;
          }

          // changed name
          style.setName(name);
          break;

      case OGLOW:
          style.oglow_ = value.toDouble();
          break;

      case OGHI:
          style.oghi_ = value.toDouble();
          break;

      case FGLOW:
          style.fglow_ = value.toDouble();
          break;

      case FGHI:
          style.fghi_ = value.toDouble();
          break;

      case IBULOW:
          style.ibulow_ = value.toUInt();
          break;

      case IBUHI:
          style.ibuhi_ = value.toUInt();
          break;

      case SRMLOW:
          style.srmlow_ = value.toUInt();
          break;

      case SRMHI:
          style.srmhi_ = value.toUInt();
          break;

      default:
          return false;
    }

    list_->replace(row, style);
    emit modified();

    // whole row may have changed
    emit dataChanged(index.sibling(row, NAME),
                     index.sibling(row, SRMHI));

    return true;
}

//////////////////////////////////////////////////////////////////////////////
// insertRows()
// ------------
// Insert rows into table

bool StyleModel::insertRows(int row, int count, const QModelIndex&)
{
    if (count != 1) return false; // only insert one row at a time
    if ((row < 0) || (row >= list_->count())) row = list_->count();

    Style style = Data::instance()->style(tr("Generic"));

    beginInsertRows(QModelIndex(), row, row);
    list_->insert(row, style);
    emit modified();
    endInsertRows();
    return true;
}

//////////////////////////////////////////////////////////////////////////////
// removeRows()
// ------------
// Remove rows from table

bool StyleModel::removeRows(int row, int count, const QModelIndex&)
{
    if (count != 1) return false; // only remove one row at a time
    if ((row < 0) || (row >= list_->count())) return false;

    int status = QMessageBox::question(QApplication::activeWindow(),
                               TITLE + tr(" - Delete?"),
                               tr("Do you wish to remove this entry?"),
                               QMessageBox::Yes | QMessageBox::Cancel);
    if (status == QMessageBox::Cancel) {
        return false;
    }

    beginRemoveRows(QModelIndex(), row, row);
    list_->removeAt(row);
    emit modified();
    endRemoveRows();
    return true;
}

//////////////////////////////////////////////////////////////////////////////
// headerData()
// ------------
// Return header information

QVariant StyleModel::headerData(int section, Qt::Orientation orientation,
                                int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
          case NAME:
              return tr("Style");
          case OGLOW:
              return tr("Min. OG");
          case OGHI:
              return tr("Max. OG");
          case FGLOW:
              return tr("Min. FG");
          case FGHI:
              return tr("Max. FG");
          case IBULOW:
              return tr("Min. IBU");
          case IBUHI:
              return tr("Max. IBU");
          case SRMLOW:
              return tr("Min. SRM");
          case SRMHI:
              return tr("Max. SRM");
           default:
              return QVariant();
        }
    }

    return QVariant();
}

//////////////////////////////////////////////////////////////////////////////
// flags()
// ------
// Return flags at index

Qt::ItemFlags StyleModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) return Qt::ItemIsEnabled;

    return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}

//////////////////////////////////////////////////////////////////////////////
// rowCount()
// ----------
// Return number of rows of data

int StyleModel::rowCount(const QModelIndex &) const
{
    return list_->count();
}

//////////////////////////////////////////////////////////////////////////////
// columnCount()
// -------------
// Return number of columns of data

int StyleModel::columnCount(const QModelIndex &) const
{
    return COUNT;
}

//////////////////////////////////////////////////////////////////////////////
// sort()
// ------
// Sort by column

void StyleModel::sort(int column, Qt::SortOrder order)
{
    QList<QPair<QString,Style> > sortlist;

    foreach(Style style, *list_) {
        QString field;
        switch (column) {
          case NAME:
              field = style.name();
              break;
          case OGLOW:
              field = QString::number(style.oglow_).rightJustified(6,'0');
              break;
          case OGHI:
              field = QString::number(style.oghi_).rightJustified(6,'0');
              break;
          case FGLOW:
              field = QString::number(style.fglow_).rightJustified(6,'0');
              break;
          case FGHI:
              field = QString::number(style.fghi_).rightJustified(6,'0');
              break;
          case IBULOW:
              field = QString::number(style.ibulow_).rightJustified(6,'0');
              break;
          case IBUHI:
              field = QString::number(style.ibuhi_).rightJustified(6,'0');
              break;
          case SRMLOW:
              field = QString::number(style.srmlow_).rightJustified(6,'0');
              break;
          case SRMHI:
              field = QString::number(style.srmhi_).rightJustified(6,'0');
              break;
          default:
              field = QString();
              break;
        }
        sortlist.append(QPair<QString,Style>(field, style));
    }

    // sort list
    qSort(sortlist.begin(), sortlist.end());

    // create new list
    list_->clear();
    QPair<QString,Style> pair;
    foreach(pair, sortlist) {
        if (order == Qt::AscendingOrder)
            list_->append(pair.second);
        else
            list_->prepend(pair.second);
    }

    emit layoutChanged();
}