File: KoSemanticStylesheetsEditor.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (338 lines) | stat: -rw-r--r-- 12,678 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* This file is part of the KDE project
   Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoSemanticStylesheetsEditor.h"

#include "ui_KoSemanticStylesheetsEditor.h"

#include "KoRdfSemanticItem.h"
#include "KoRdfSemanticItemRegistry.h"
#include "KoDocumentRdf.h"

#include <kdebug.h>

class KoSemanticStylesheetsEditor::Private
{
public:
    Ui::KoSemanticStylesheetsEditor *m_ui;
    KoDocumentRdf *m_rdf;
    QTreeWidgetItem *m_systemSheetsParentItem;
    QTreeWidgetItem *m_userSheetsParentItem;
    QMap<QString, QTreeWidgetItem*> m_systemSheetsItems;
    QMap<QString, QTreeWidgetItem*> m_userSheetsItems;
    Private()
            : m_systemSheetsParentItem(0)
            , m_userSheetsParentItem(0) {}
    ~Private() {
	    delete m_ui;
    }
};

enum {
    ColName = 0,
    ColSemobj = 1,
    ColVar = 0,
    ColDesc = 1
};

class KoSemanticStylesheetWidgetItem : public QTreeWidgetItem
{
public:
    KoDocumentRdf *m_rdf;
    hKoSemanticStylesheet m_ss;
    hKoRdfSemanticItem m_si;
    KoSemanticStylesheetWidgetItem(KoDocumentRdf *rdf,
                                   hKoSemanticStylesheet ss,
                                   hKoRdfSemanticItem si,
                                   QTreeWidgetItem *parent,
                                   int type = Type)
            : QTreeWidgetItem(parent, type)
            , m_rdf(rdf)
            , m_ss(ss)
            , m_si(si)
    {
    }
    KoSemanticStylesheetWidgetItem(KoDocumentRdf *rdf,
                                   hKoRdfSemanticItem si,
                                   QTreeWidgetItem *parent,
                                   int type = Type)
        : QTreeWidgetItem(parent, type)
        , m_rdf(rdf)
        , m_ss(0)
        , m_si(si)
    {
    }

    virtual void setData(int column, int role, const QVariant &value)
    {
        if (m_ss && m_ss->isMutable() && column == ColName) {
            QString newName = value.toString();
            kDebug(30015) << "update to value:" << newName;
            m_ss->name(newName);
            QVariant v = m_ss->name();
            QTreeWidgetItem::setData(column, role, v);
        } else {
            QTreeWidgetItem::setData(column, role, value);
        }
    }
};


KoSemanticStylesheetsEditor::KoSemanticStylesheetsEditor(QWidget *parent, KoDocumentRdf *rdf)
        : KDialog(parent),
        d(new Private())
{
    d->m_rdf = rdf;
    QWidget *widget = new QWidget(this);
    setMainWidget(widget);
    d->m_ui = new Ui::KoSemanticStylesheetsEditor();
    d->m_ui->setupUi(widget);
    d->m_ui->newStylesheet->setEnabled(false);
    d->m_ui->deleteStylesheet->setEnabled(false);
    connect(d->m_ui->newStylesheet, SIGNAL(clicked()),
            this, SLOT(newStylesheet()));
    connect(d->m_ui->deleteStylesheet, SIGNAL(clicked()),
            this, SLOT(deleteStylesheet()));
    connect(d->m_ui->stylesheets, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
            this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
    d->m_ui->stylesheets->setSelectionBehavior(QAbstractItemView::SelectRows);
    d->m_ui->stylesheets->sortItems(ColName, Qt::DescendingOrder);
    connect(d->m_ui->definition, SIGNAL(textChanged()),
            this, SLOT(definitionChanged()));
    // setup system/user parent treewidgets
    d->m_systemSheetsParentItem = new QTreeWidgetItem(d->m_ui->stylesheets);
    d->m_systemSheetsParentItem->setText(ColName, i18n("System"));
    d->m_systemSheetsParentItem->setText(ColSemobj, "");
    d->m_ui->stylesheets->expandItem(d->m_systemSheetsParentItem);

    QTreeWidgetItem *treewidget = 0;
    treewidget = d->m_systemSheetsParentItem;
    const QStringList classNames = KoRdfSemanticItemRegistry::instance()->classNames();
    foreach (const QString &semanticClass, classNames) {
        hKoRdfSemanticItem si = rdf->createSemanticItem(semanticClass, this);
        KoSemanticStylesheetWidgetItem *item = new KoSemanticStylesheetWidgetItem(rdf, si, treewidget);
        item->setText(ColName, semanticClass);
        d->m_systemSheetsItems[semanticClass] = item;
        d->m_ui->stylesheets->expandItem(item);
    }
    d->m_userSheetsParentItem = new QTreeWidgetItem(d->m_ui->stylesheets);
    d->m_userSheetsParentItem->setText(ColName, i18n("User"));
    d->m_userSheetsParentItem->setText(ColSemobj, "");
    d->m_ui->stylesheets->expandItem(d->m_userSheetsParentItem);

    treewidget = d->m_userSheetsParentItem;
    foreach (const QString &semanticClass, classNames) {
        hKoRdfSemanticItem si = rdf->createSemanticItem(semanticClass, this);
        KoSemanticStylesheetWidgetItem* item = new KoSemanticStylesheetWidgetItem(rdf, si, treewidget);
        item->setText(ColName, semanticClass);
        d->m_userSheetsItems[semanticClass] = item;
        d->m_ui->stylesheets->expandItem(item);
    }

    // initialize stylesheets tree
    foreach (const QString &semanticClass, classNames) {
        kDebug(30015) << "semanticClass:" << semanticClass;
        hKoRdfSemanticItem p = rdf->createSemanticItem(semanticClass, this);
        setupStylesheetsItems(semanticClass, p, p->stylesheets(), d->m_systemSheetsItems);
        setupStylesheetsItems(semanticClass, p, p->userStylesheets(), d->m_userSheetsItems, true);
    }
    connect(d->m_ui->variables, SIGNAL(itemActivated(QTableWidgetItem*)),
            this, SLOT(onVariableActivated(QTableWidgetItem*)));
    d->m_ui->variables->horizontalHeader()->setResizeMode(ColVar, QHeaderView::ResizeToContents);
    d->m_ui->variables->horizontalHeader()->setResizeMode(ColDesc, QHeaderView::ResizeToContents);
}

KoSemanticStylesheetsEditor::~KoSemanticStylesheetsEditor()
{
}

void KoSemanticStylesheetsEditor::setupStylesheetsItems(const QString& semanticClass,
                                                        hKoRdfSemanticItem si,
                                                        const QList<hKoSemanticStylesheet> &ssl,
                                                        const QMap<QString, QTreeWidgetItem*> &m,
                                                        bool editable)
{
    foreach (hKoSemanticStylesheet ss, ssl) {
        kDebug(30015) << "ss:" << ss->name();
        QTreeWidgetItem *parent = m[semanticClass];
        KoSemanticStylesheetWidgetItem *item = new KoSemanticStylesheetWidgetItem(d->m_rdf, ss, si, parent);
        item->setText(ColName, ss->name());
        item->setText(ColSemobj, semanticClass);
        if (editable) {
            item->setFlags(item->flags() | Qt::ItemIsEditable);
        }
    }
}

void KoSemanticStylesheetsEditor::slotOk()
{
}

void KoSemanticStylesheetsEditor::maskButtonsDependingOnCurrentItem(QTreeWidgetItem *current)
{
    bool newEnabled = true;
    bool delEnabled = true;
    for (QMap< QString, QTreeWidgetItem* >::iterator iter = d->m_userSheetsItems.begin();
            iter != d->m_userSheetsItems.end(); ++iter) {
        if (iter.value() == current) {
            delEnabled = false;
        }
    }
    if (d->m_userSheetsParentItem == current) {
        newEnabled = false;
        delEnabled = false;
    } else {
        while (current) {
            kDebug(30015) << "current:" << current->text(ColName);
            if (d->m_systemSheetsParentItem == current) {
                newEnabled = false;
                delEnabled = false;
                break;
            }
            current = current->parent();
        }
    }
    d->m_ui->newStylesheet->setEnabled(newEnabled);
    d->m_ui->deleteStylesheet->setEnabled(delEnabled);
}

void KoSemanticStylesheetsEditor::definitionChanged()
{
    if (KoSemanticStylesheetWidgetItem *ssitem
            = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) {
        if (!ssitem->m_ss) {
            return;
        }
        QString desc = d->m_ui->definition->toPlainText();
        kDebug(30015) << "ss:" << ssitem->m_ss->name() << " desc:" << desc;
        ssitem->m_ss->templateString(desc);
    }
}

struct SignalBlockerRAII {
    QObject *obj;
    bool oldval;
    SignalBlockerRAII(QObject *o)
            : obj(o) {
        oldval = obj->blockSignals(true);
    }
    ~SignalBlockerRAII() {
        obj->blockSignals(oldval);
    }
};


void KoSemanticStylesheetsEditor::currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    SignalBlockerRAII _blocker1(d->m_ui->definition);
    kDebug(30015) << "current:" << current;
    kDebug(30015) << "previous:" << previous;
    maskButtonsDependingOnCurrentItem(current);
    if (previous) {
        QString desc = d->m_ui->definition->toPlainText();
        kDebug(30015) << "desc:" << desc;
        if (KoSemanticStylesheetWidgetItem *ssitem
                = dynamic_cast<KoSemanticStylesheetWidgetItem*>(previous)) {
            kDebug(30015) << "ssitem, ss?:" << ssitem->m_ss;

            if (ssitem->m_ss) {
                ssitem->m_ss->templateString(desc);
            }
        }
    }
    if (KoSemanticStylesheetWidgetItem *ssitem
            = dynamic_cast<KoSemanticStylesheetWidgetItem*>(current)) {
        d->m_ui->definition->setPlainText(QString());
        d->m_ui->variables->clear();
        if (!ssitem->m_ss) {
            return;
        }
        d->m_ui->definition->setPlainText(ssitem->m_ss->templateString());
        // update the list of available variables
        QTableWidget *v = d->m_ui->variables;
        v->clear();
        int row = 0;
        QMap<QString, QString> m;
        ssitem->m_si->setupStylesheetReplacementMapping(m);
        QMap< QString, QString >::const_iterator mi = m.constBegin();
        QMap< QString, QString >::const_iterator me = m.constEnd();
        for (; mi != me; ++mi) {
            QTableWidgetItem *item = 0;
            QString varName = mi.key();
            QString desc("t");
            v->setRowCount(row + 1);
            item = new QTableWidgetItem(varName);
            v->setItem(row, ColVar, item);
            item = new QTableWidgetItem(desc);
            v->setItem(row, ColDesc, item);
            ++row;
        }
    }
}

void KoSemanticStylesheetsEditor::newStylesheet()
{
    kDebug(30015);
    if (KoSemanticStylesheetWidgetItem *ssitem
            = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) {
        if (ssitem->m_ss) {
            ssitem = dynamic_cast<KoSemanticStylesheetWidgetItem*>(ssitem->parent());
            if (!ssitem) {
                return;
            }
        }
        kDebug(30015) << ssitem;
        hKoRdfSemanticItem si = ssitem->m_si;
        hKoSemanticStylesheet ss = si->createUserStylesheet(
                                     QString("new stylesheet %1").arg(QDateTime::currentDateTime().toTime_t()), "");
        QTreeWidgetItem *parent = ssitem;
        KoSemanticStylesheetWidgetItem* item =
            new KoSemanticStylesheetWidgetItem(d->m_rdf, ss, si, parent);
        item->setText(ColName, ss->name());
        item->setFlags(item->flags() | Qt::ItemIsEditable);

        d->m_ui->stylesheets->setCurrentItem(item);
        d->m_ui->stylesheets->scrollToItem(item);
    }
}

void KoSemanticStylesheetsEditor::deleteStylesheet()
{
    kDebug(30015);
    if (KoSemanticStylesheetWidgetItem *ssitem
            = dynamic_cast<KoSemanticStylesheetWidgetItem*>(d->m_ui->stylesheets->currentItem())) {
        if (!ssitem->m_ss) {
            return;
        }
        hKoSemanticStylesheet sheet = ssitem->m_ss;
        ssitem->m_ss = 0;
        ssitem->m_si->destroyUserStylesheet(sheet);
        ssitem->parent()->removeChild(ssitem);
    }
}

void KoSemanticStylesheetsEditor::onVariableActivated(QTableWidgetItem* item)
{
    QTableWidgetItem *vitem = d->m_ui->variables->item(item->row(), ColVar);
    QString vtext = vitem->text();
    QTextCursor cursor(d->m_ui->definition->textCursor());
    cursor.insertText(vtext);
}

#include <KoSemanticStylesheetsEditor.moc>