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 "TemplatesModel.h"
0020 #include "TemplateVariantsModel.h"
0021 #include <KoTemplateTree.h>
0022 #include <KoTemplateGroup.h>
0023 #include <KoTemplate.h>
0024 #include <KWFactory.h>
0025 #include <KWDocument.h>
0026 #include <part/KPrFactory.h>
0027 #include <part/KPrDocument.h>
0028 
0029 struct TemplateEntry {
0030     TemplateEntry()
0031         : variants(new TemplateVariantsModel())
0032     {}
0033     ~TemplateEntry()
0034     {
0035         variants->deleteLater();
0036     }
0037 
0038     QString title;
0039     QString description;
0040     TemplateVariantsModel* variants;
0041 };
0042 
0043 class TemplatesModel::Private {
0044 public:
0045     Private()
0046         : showWide(false)
0047     {}
0048 
0049     QString templateType;
0050     bool showWide;
0051     QList<TemplateEntry*> entries;
0052 
0053     void refresh()
0054     {
0055         qDeleteAll(entries);
0056         entries.clear();
0057 
0058         KoTemplateTree* tree(0);
0059         if(templateType == WORDS_MIME_TYPE)
0060             tree = new KoTemplateTree("calligrawords/templates/", true);
0061         else if(templateType == STAGE_MIME_TYPE)
0062             tree = new KoTemplateTree("calligrastage/templates/", true);
0063         if(!tree)
0064             return;
0065 
0066         Q_FOREACH(const KoTemplateGroup* group , tree->groups()) {
0067             Q_FOREACH(const KoTemplate* tmplate, group->templates()) {
0068                 if(tmplate->wide() != showWide)
0069                     continue;
0070 
0071                 QString title = tmplate->name();
0072 
0073                 TemplateEntry* found(0);
0074                 Q_FOREACH(TemplateEntry* otherEntry, entries) {
0075                     if(otherEntry->title == title) {
0076                         found = otherEntry;
0077                         break;
0078                     }
0079                 }
0080                 TemplateEntry* entry(0);
0081                 if(found) {
0082                     entry = found;
0083                 }
0084                 else {
0085                     entry = new TemplateEntry();
0086                     entry->title = title;
0087                     entry->description = tmplate->description();
0088                     entries.append(entry);
0089                 }
0090 
0091                 entry->variants->addVariant(tmplate->variantName(),
0092                                             tmplate->color(),
0093                                             tmplate->swatch(),
0094                                             tmplate->thumbnail().isEmpty() ? tmplate->picture() : tmplate->thumbnail(),
0095                                             tmplate->file());
0096             }
0097         }
0098     }
0099 };
0100 
0101 TemplatesModel::TemplatesModel(QObject* parent)
0102     : QAbstractListModel(parent)
0103     , d(new Private())
0104 {
0105     QHash<int, QByteArray> roles;
0106     roles[TitleRole] = "text";
0107     roles[DescriptionRole] = "description";
0108     roles[ColorRole] = "color";
0109     roles[ThumbnailRole] = "thumbnail";
0110     roles[UrlRole] = "url";
0111     roles[VariantCountRole] = "variantCount";
0112     roles[VariantsRole] = "variants";
0113     setRoleNames(roles);
0114 }
0115 
0116 TemplatesModel::~TemplatesModel()
0117 {
0118     delete d;
0119 }
0120 
0121 QVariant TemplatesModel::data(const QModelIndex& index, int role) const
0122 {
0123     QVariant result;
0124 
0125     if(index.isValid() && index.row() > -1 && index.row() < d->entries.count())
0126     {
0127         TemplateEntry* entry = d->entries.at(index.row());
0128         switch(role)
0129         {
0130             case TitleRole:
0131                 result = entry->title;
0132                 break;
0133             case DescriptionRole:
0134                 result = entry->description;
0135                 break;
0136             case ColorRole:
0137                 result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::ColorRole);
0138                 break;
0139             case ThumbnailRole:
0140                 result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::ThumbnailRole);
0141                 break;
0142             case UrlRole:
0143                 result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::UrlRole);
0144                 break;
0145             case VariantCountRole:
0146                 result = entry->variants->rowCount();
0147                 break;
0148             case VariantsRole:
0149                 result = QVariant::fromValue<QObject*>(entry->variants);
0150                 break;
0151             default:
0152                 break;
0153         }
0154     }
0155 
0156     return result;
0157 }
0158 
0159 int TemplatesModel::rowCount(const QModelIndex& parent) const
0160 {
0161     if(parent.isValid())
0162         return 0;
0163     return d->entries.count();
0164 }
0165 
0166 QString TemplatesModel::templateType() const
0167 {
0168     return d->templateType;
0169 }
0170 
0171 void TemplatesModel::setTemplateType(const QString& newType)
0172 {
0173     d->templateType = newType;
0174     emit templateTypeChanged();
0175     qApp->processEvents();
0176     beginResetModel();
0177     d->refresh();
0178     endResetModel();
0179 }
0180 
0181 bool TemplatesModel::showWide() const
0182 {
0183     return d->showWide;
0184 }
0185 
0186 void TemplatesModel::setShowWide(const bool& newValue)
0187 {
0188     d->showWide = newValue;
0189     emit showWideChanged();
0190     qApp->processEvents();
0191     beginResetModel();
0192     d->refresh();
0193     endResetModel();
0194 }