1 /*
2 * Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation;
7 * either version 2, or (at your option) any later version of the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "StatesModel.h"
21
22 #include <QPainter>
23 #include <QSvgRenderer>
24
25 #include <kcategorizedsortfilterproxymodel.h>
26
27 #include "State.h"
28 #include "StateCategory.h"
29 #include "StatesRegistry.h"
30
StatesModel()31 StatesModel::StatesModel()
32 {
33 foreach(const QString & catId, StatesRegistry::instance()->categorieIds()) {
34 foreach(const QString & stateId, StatesRegistry::instance()->stateIds(catId)) {
35 const State* state = StatesRegistry::instance()->state(catId, stateId);
36 Q_ASSERT(state);
37 m_states.push_back(state);
38 QImage image(32, 32, QImage::Format_ARGB32);
39 QPainter p(&image);
40 state->renderer()->render(&p, QRectF(0, 0, 32, 32));
41 m_icons.push_back(image);
42 }
43 }
44 Q_ASSERT(m_states.size() == m_icons.size());
45 }
46
rowCount(const QModelIndex & parent) const47 int StatesModel::rowCount(const QModelIndex & parent) const
48 {
49 Q_UNUSED(parent);
50 return m_states.size();
51 }
52
data(const QModelIndex & index,int role) const53 QVariant StatesModel::data(const QModelIndex & index, int role) const
54 {
55 if(index.isValid()) {
56 switch(role) {
57 case Qt::DisplayRole:
58 return m_states[index.row()]->name();
59 case Qt::DecorationRole:
60 return m_icons[index.row()];
61 case SortRole:
62 return m_states[index.row()]->priority();
63 case KCategorizedSortFilterProxyModel::CategorySortRole:
64 return QString(QString::number(m_states[index.row()]->category()->priority()) + m_states[index.row()]->category()->id());
65 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
66 return m_states[index.row()]->category()->name();
67 }
68 }
69 return QVariant();
70 }
71
stateAt(int index) const72 const State* StatesModel::stateAt(int index) const
73 {
74 Q_ASSERT(index >= 0 && index < m_states.count());
75 return m_states[index];
76 }
77
indexFor(const QString & catId,const QString & stateId) const78 QModelIndex StatesModel::indexFor(const QString& catId, const QString& stateId) const
79 {
80 for(int i = 0; i < m_states.count(); ++i) {
81 const State* state = m_states[i];
82 if(state->category()->id() == catId && state->id() == stateId) {
83 return index(i, 0, QModelIndex());
84 }
85 }
86 return QModelIndex();
87 }
88