File: TemplatesModel.cpp

package info (click to toggle)
calligra 1%3A25.04.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 309,164 kB
  • sloc: cpp: 667,890; xml: 126,105; perl: 2,724; python: 2,497; yacc: 1,817; ansic: 1,326; sh: 1,223; lex: 1,107; javascript: 495; makefile: 24
file content (189 lines) | stat: -rw-r--r-- 5,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "TemplatesModel.h"
#include "TemplateVariantsModel.h"

#include <QApplication>

#include <KWDocument.h>
#include <KWFactory.h>
#include <KoTemplate.h>
#include <KoTemplateGroup.h>
#include <KoTemplateTree.h>
#include <part/KPrDocument.h>
#include <part/KPrFactory.h>

struct TemplateEntry {
    TemplateEntry()
        : variants(new TemplateVariantsModel())
    {
    }
    ~TemplateEntry()
    {
        variants->deleteLater();
    }

    QString title;
    QString description;
    TemplateVariantsModel *variants;
};

class TemplatesModel::Private
{
public:
    Private()
        : showWide(false)
    {
    }

    QString templateType;
    bool showWide;
    QList<TemplateEntry *> entries;

    void refresh()
    {
        qDeleteAll(entries);
        entries.clear();

        KoTemplateTree *tree(nullptr);
        if (templateType == WORDS_MIME_TYPE)
            tree = new KoTemplateTree("calligrawords/templates/", true);
        else if (templateType == STAGE_MIME_TYPE)
            tree = new KoTemplateTree("calligrastage/templates/", true);
        if (!tree)
            return;

        Q_FOREACH (const KoTemplateGroup *group, tree->groups()) {
            Q_FOREACH (const KoTemplate *tmplate, group->templates()) {
                if (tmplate->wide() != showWide)
                    continue;

                QString title = tmplate->name();

                TemplateEntry *found(nullptr);
                Q_FOREACH (TemplateEntry *otherEntry, entries) {
                    if (otherEntry->title == title) {
                        found = otherEntry;
                        break;
                    }
                }
                TemplateEntry *entry(nullptr);
                if (found) {
                    entry = found;
                } else {
                    entry = new TemplateEntry();
                    entry->title = title;
                    entry->description = tmplate->description();
                    entries.append(entry);
                }

                entry->variants->addVariant(tmplate->variantName(),
                                            tmplate->color(),
                                            tmplate->swatch(),
                                            tmplate->thumbnail().isEmpty() ? tmplate->picture() : tmplate->thumbnail(),
                                            tmplate->file());
            }
        }
    }
};

TemplatesModel::TemplatesModel(QObject *parent)
    : QAbstractListModel(parent)
    , d(new Private())
{
}

TemplatesModel::~TemplatesModel()
{
    delete d;
}

QHash<int, QByteArray> TemplatesModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TitleRole] = "text";
    roles[DescriptionRole] = "description";
    roles[ColorRole] = "color";
    roles[ThumbnailRole] = "thumbnail";
    roles[UrlRole] = "url";
    roles[VariantCountRole] = "variantCount";
    roles[VariantsRole] = "variants";
    return roles;
}

QVariant TemplatesModel::data(const QModelIndex &index, int role) const
{
    QVariant result;

    if (index.isValid() && index.row() > -1 && index.row() < d->entries.count()) {
        TemplateEntry *entry = d->entries.at(index.row());
        switch (role) {
        case TitleRole:
            result = entry->title;
            break;
        case DescriptionRole:
            result = entry->description;
            break;
        case ColorRole:
            result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::ColorRole);
            break;
        case ThumbnailRole:
            result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::ThumbnailRole);
            break;
        case UrlRole:
            result = entry->variants->data(entry->variants->firstIndex(), TemplateVariantsModel::UrlRole);
            break;
        case VariantCountRole:
            result = entry->variants->rowCount();
            break;
        case VariantsRole:
            result = QVariant::fromValue<QObject *>(entry->variants);
            break;
        default:
            break;
        }
    }

    return result;
}

int TemplatesModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return d->entries.count();
}

QString TemplatesModel::templateType() const
{
    return d->templateType;
}

void TemplatesModel::setTemplateType(const QString &newType)
{
    d->templateType = newType;
    emit templateTypeChanged();
    qApp->processEvents();
    beginResetModel();
    d->refresh();
    endResetModel();
}

bool TemplatesModel::showWide() const
{
    return d->showWide;
}

void TemplatesModel::setShowWide(const bool &newValue)
{
    d->showWide = newValue;
    emit showWideChanged();
    qApp->processEvents();
    beginResetModel();
    d->refresh();
    endResetModel();
}