1 /* This file is part of the KDE project
2 * Copyright (C) 2012 Dan Leinir Turthra Jensen <admin@leinir.dk>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "PaletteModel.h"
20
21 #include <resources/KoColorSet.h>
22 #include <KoResourceServerAdapter.h>
23 #include <KoResourceServerProvider.h>
24 #include <KisResourceServerProvider.h>
25
26 class PaletteModel::Private {
27 public:
Private(QObject * q)28 Private(QObject* q)
29 : currentSet(0)
30 {
31 KoResourceServer<KoColorSet>* rServer = KoResourceServerProvider::instance()->paletteServer();
32 serverAdaptor = new KoResourceServerAdapter<KoColorSet>(rServer, q);
33 serverAdaptor->connectToResourceServer();
34 }
35 KoResourceServerAdapter<KoColorSet>* serverAdaptor;
36 KoColorSet* currentSet;
37 };
38
PaletteModel(QObject * parent)39 PaletteModel::PaletteModel(QObject *parent)
40 : QAbstractListModel(parent)
41 , d(new Private(this))
42 {
43 }
44
~PaletteModel()45 PaletteModel::~PaletteModel()
46 {
47 delete d;
48 }
49
roleNames() const50 QHash<int, QByteArray> PaletteModel::roleNames() const
51 {
52 QHash<int, QByteArray> roles;
53 roles[ImageRole] = "image";
54 roles[TextRole] = "text";
55
56 return roles;
57 }
58
rowCount(const QModelIndex & parent) const59 int PaletteModel::rowCount(const QModelIndex &parent) const
60 {
61 if (parent.isValid())
62 return 0;
63 return d->serverAdaptor->resources().count();
64 }
65
data(const QModelIndex & index,int role) const66 QVariant PaletteModel::data(const QModelIndex &index, int role) const
67 {
68 QVariant result;
69 if (index.isValid())
70 {
71 switch(role)
72 {
73 case ImageRole:
74 result = "../images/help-about.png";
75 break;
76 case TextRole:
77 result = d->serverAdaptor->resources().at(index.row())->name();
78 break;
79 default:
80 break;
81 }
82 }
83 return result;
84 }
85
headerData(int section,Qt::Orientation orientation,int role) const86 QVariant PaletteModel::headerData(int section, Qt::Orientation orientation, int role) const
87 {
88 Q_UNUSED(orientation);
89 QVariant result;
90 if (section == 0)
91 {
92 switch(role)
93 {
94 case ImageRole:
95 result = QString("Thumbnail");
96 break;
97 case TextRole:
98 result = QString("Name");
99 break;
100 default:
101 break;
102 }
103 }
104 return result;
105 }
106
itemActivated(int index)107 void PaletteModel::itemActivated(int index)
108 {
109 QList<KoResource*> resources = d->serverAdaptor->resources();
110 if (index >= 0 && index < resources.count())
111 {
112 d->currentSet = dynamic_cast<KoColorSet*>(resources.at(index));
113 emit colorSetChanged();
114 }
115 }
116
colorSet() const117 QObject* PaletteModel::colorSet() const
118 {
119 return d->currentSet;
120 }
121
122