File indexing completed on 2024-05-05 17:04:27

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  *  This program is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include "TemplateVariantsModel.h"
0020 #include <QColor>
0021 #include <QDebug>
0022 #include <KIconLoader>
0023 #include <kicontheme.h>
0024 struct TemplateVariant {
0025     TemplateVariant()
0026     {}
0027 
0028     QString name;
0029     QColor color;
0030     QString thumbnail;
0031     QString swatch;
0032     QString url;
0033 };
0034 
0035 class TemplateVariantsModel::Private
0036 {
0037 public:
0038     Private() {}
0039     ~Private()
0040     {
0041         qDeleteAll(entries);
0042     }
0043 
0044     QList<TemplateVariant*> entries;
0045 };
0046 
0047 TemplateVariantsModel::TemplateVariantsModel(QObject* parent)
0048     : QAbstractListModel(parent)
0049     , d(new Private())
0050 {
0051     QHash<int, QByteArray> roles;
0052     roles[NameRole] = "text";
0053     roles[ColorRole] = "color";
0054     roles[ThumbnailRole] = "thumbnail";
0055     roles[SwatchRole] = "swatch";
0056     roles[UrlRole] = "url";
0057     setRoleNames(roles);
0058 }
0059 
0060 TemplateVariantsModel::~TemplateVariantsModel()
0061 {
0062     delete d;
0063 }
0064 
0065 QVariant TemplateVariantsModel::data(const QModelIndex& index, int role) const
0066 {
0067     QVariant result;
0068 
0069     if(index.isValid() && index.row() > -1 && index.row() < d->entries.count())
0070     {
0071         TemplateVariant* entry = d->entries.at(index.row());
0072         switch(role)
0073         {
0074             case NameRole:
0075                 result = entry->name;
0076                 break;
0077             case ColorRole:
0078                 result = entry->color;
0079                 break;
0080             case ThumbnailRole:
0081                 result = entry->thumbnail;
0082                 break;
0083             case SwatchRole:
0084                 result = entry->swatch;
0085                 break;
0086             case UrlRole:
0087                 result = entry->url;
0088                 break;
0089             default:
0090                 break;
0091         }
0092     }
0093 
0094     return result;
0095 }
0096 
0097 int TemplateVariantsModel::rowCount(const QModelIndex& parent) const
0098 {
0099     if(parent.isValid() || d->entries.count() == 1)
0100         return 0;
0101     return d->entries.count();
0102 }
0103 
0104 void TemplateVariantsModel::addVariant(QString name, QString color, QString swatch, QString picture, QString file)
0105 {
0106     TemplateVariant* entry = new TemplateVariant();
0107     d->entries.append(entry);
0108 
0109     entry->name = name;
0110     entry->color = QColor(color);
0111     entry->swatch = swatch;
0112     entry->url = file;
0113 
0114     if(picture.at(0) == QChar('/') || picture.at(1) == QChar(':')) {
0115         entry->thumbnail = picture;
0116     }
0117     else {
0118         entry->thumbnail = KIconLoader::global()->iconPath(picture, KIconLoader::Desktop, true);
0119     }
0120 }
0121 
0122 QModelIndex TemplateVariantsModel::firstIndex()
0123 {
0124     return createIndex(0, 0);
0125 }