1 /* This file is part of the KDE project
2  * Copyright (C) 2014 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 "TemplateVariantsModel.h"
20 #include <QColor>
21 #include <QDebug>
22 #include <KIconLoader>
23 #include <kicontheme.h>
24 struct TemplateVariant {
TemplateVariantTemplateVariant25     TemplateVariant()
26     {}
27 
28     QString name;
29     QColor color;
30     QString thumbnail;
31     QString swatch;
32     QString url;
33 };
34 
35 class TemplateVariantsModel::Private
36 {
37 public:
Private()38     Private() {}
~Private()39     ~Private()
40     {
41         qDeleteAll(entries);
42     }
43 
44     QList<TemplateVariant*> entries;
45 };
46 
TemplateVariantsModel(QObject * parent)47 TemplateVariantsModel::TemplateVariantsModel(QObject* parent)
48     : QAbstractListModel(parent)
49     , d(new Private())
50 {
51     QHash<int, QByteArray> roles;
52     roles[NameRole] = "text";
53     roles[ColorRole] = "color";
54     roles[ThumbnailRole] = "thumbnail";
55     roles[SwatchRole] = "swatch";
56     roles[UrlRole] = "url";
57     setRoleNames(roles);
58 }
59 
~TemplateVariantsModel()60 TemplateVariantsModel::~TemplateVariantsModel()
61 {
62     delete d;
63 }
64 
data(const QModelIndex & index,int role) const65 QVariant TemplateVariantsModel::data(const QModelIndex& index, int role) const
66 {
67     QVariant result;
68 
69     if(index.isValid() && index.row() > -1 && index.row() < d->entries.count())
70     {
71         TemplateVariant* entry = d->entries.at(index.row());
72         switch(role)
73         {
74             case NameRole:
75                 result = entry->name;
76                 break;
77             case ColorRole:
78                 result = entry->color;
79                 break;
80             case ThumbnailRole:
81                 result = entry->thumbnail;
82                 break;
83             case SwatchRole:
84                 result = entry->swatch;
85                 break;
86             case UrlRole:
87                 result = entry->url;
88                 break;
89             default:
90                 break;
91         }
92     }
93 
94     return result;
95 }
96 
rowCount(const QModelIndex & parent) const97 int TemplateVariantsModel::rowCount(const QModelIndex& parent) const
98 {
99     if(parent.isValid() || d->entries.count() == 1)
100         return 0;
101     return d->entries.count();
102 }
103 
addVariant(QString name,QString color,QString swatch,QString picture,QString file)104 void TemplateVariantsModel::addVariant(QString name, QString color, QString swatch, QString picture, QString file)
105 {
106     TemplateVariant* entry = new TemplateVariant();
107     d->entries.append(entry);
108 
109     entry->name = name;
110     entry->color = QColor(color);
111     entry->swatch = swatch;
112     entry->url = file;
113 
114     if(picture.at(0) == QChar('/') || picture.at(1) == QChar(':')) {
115         entry->thumbnail = picture;
116     }
117     else {
118         entry->thumbnail = KIconLoader::global()->iconPath(picture, KIconLoader::Desktop, true);
119     }
120 }
121 
firstIndex()122 QModelIndex TemplateVariantsModel::firstIndex()
123 {
124     return createIndex(0, 0);
125 }
126