1 /* This file is part of the KDE project
2    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library 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 GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 #include "KoSemanticStylesheet.h"
20 
21 #include "KoDocumentRdf.h"
22 #include "KoChangeTrackerDisabledRAII.h"
23 // main
24 #include <KoDocument.h>
25 #include <KoTextDocument.h>
26 #include <KoTextEditor.h>
27 // KF5
28 #include <kdebug.h>
29 // Qt
30 #include <QCoreApplication>
31 
32 
33 class KoSemanticStylesheetPrivate
34 {
35 public:
36     QString m_uuid;
37     QString m_name;
38     QString m_templateString;
39     QString m_type;
40     bool m_isMutable;
41 
KoSemanticStylesheetPrivate(const QString & uuid,const QString & name,const QString & templateString,const QString & type=QLatin1String ("System"),bool isMutable=false)42     KoSemanticStylesheetPrivate(const QString &uuid, const QString &name, const QString &templateString,
43                                 const QString &type = QLatin1String("System"), bool isMutable = false)
44         :
45         m_uuid(uuid),
46         m_name(name),
47         m_templateString(templateString),
48         m_type(type),
49         m_isMutable(isMutable)
50         {}
51 };
52 
53 
KoSemanticStylesheet(const QString & uuid,const QString & name,const QString & templateString,const QString & type,bool isMutable)54 KoSemanticStylesheet::KoSemanticStylesheet(const QString &uuid,
55                                        const QString &name,
56                                        const QString &templateString,
57                                        const QString &type, bool isMutable)
58     : QObject(QCoreApplication::instance())
59     , d (new KoSemanticStylesheetPrivate (uuid,name,templateString,type,isMutable))
60 {
61 }
62 
~KoSemanticStylesheet()63 KoSemanticStylesheet::~KoSemanticStylesheet()
64 {
65     delete d;
66 }
67 
uuid() const68 QString KoSemanticStylesheet::uuid() const
69 {
70     return d->m_uuid;
71 }
72 
name() const73 QString KoSemanticStylesheet::name() const
74 {
75     return d->m_name;
76 }
77 
templateString() const78 QString KoSemanticStylesheet::templateString() const
79 {
80     return d->m_templateString;
81 }
82 
type() const83 QString KoSemanticStylesheet::type() const
84 {
85     return d->m_type;
86 }
87 
88 
isMutable() const89 bool KoSemanticStylesheet::isMutable() const
90 {
91     return d->m_isMutable;
92 }
93 
name(const QString & v)94 void KoSemanticStylesheet::name(const QString &v)
95 {
96     if (d->m_isMutable) {
97         emit nameChanging(hKoSemanticStylesheet(this), d->m_name, v);
98         d->m_name = v;
99     }
100 }
101 
templateString(const QString & v)102 void KoSemanticStylesheet::templateString(const QString &v)
103 {
104     if (d->m_isMutable) {
105         d->m_templateString = v;
106     }
107 }
108 
109 
format(hKoRdfSemanticItem obj,KoTextEditor * editor,const QString & xmlid)110 void KoSemanticStylesheet::format(hKoRdfSemanticItem obj, KoTextEditor *editor, const QString &xmlid)
111 {
112     Q_ASSERT(obj);
113     Q_ASSERT(editor);
114     kDebug(30015) << "formatting obj:" << obj << " name:" << obj->name();
115     kDebug(30015) << "xmlid:" << xmlid << " editor:" << editor << " sheet-name:" << name();
116     const KoDocumentRdf *rdf = obj->documentRdf();
117     Q_ASSERT(rdf);
118     Q_ASSERT(editor);
119     QPair<int, int> p;
120     if (xmlid.size()) {
121         p = rdf->findExtent(xmlid);
122     } else {
123         p = rdf->findExtent(editor);
124     }
125     int startpos = p.first + 1;
126     int endpos = p.second;
127     if (!endpos) {
128         kDebug(30015) << "format() invalid range, skipping! start:" << startpos << " end:" << endpos;
129         return;
130     }
131     KoTextDocument ktd(editor->document());
132     KoChangeTrackerDisabledRAII disableChangeTracker(ktd.changeTracker());
133     editor->beginEditBlock();
134     editor->setPosition(startpos, QTextCursor::MoveAnchor);
135     editor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endpos - startpos);
136     QString oldText = editor->selectedText();
137     if (editor->hasSelection()) {
138         editor->deleteChar(); // deletes the selection
139     }
140     editor->setPosition(startpos, QTextCursor::MoveAnchor);
141     kDebug(30015) << "formatting start:" << startpos << " end:" << endpos;
142     kDebug(30015) << "semantic item oldText:" << oldText;
143     QString data = templateString();
144     QMap<QString, QString> m;
145     m["%NAME%"] = obj->name();
146     obj->setupStylesheetReplacementMapping(m);
147 
148     for (QMap<QString, QString>::iterator mi = m.begin(); mi != m.end(); ++mi) {
149         QString k = mi.key();
150         QString v = mi.value();
151         data.replace(k, v);
152     }
153     // make sure there is something in the replacement other than commas and spaces
154     QString tmpstring = data;
155     tmpstring.remove(' ');
156     tmpstring.remove(',');
157     if (!tmpstring.size()) {
158         kDebug(30015) << "stylesheet results in empty data, using name() instead";
159         data = name();
160     }
161     kDebug(30015) << "Updating with new formatting:" << data;
162     editor->insertText(data);
163     editor->setPosition(startpos, QTextCursor::MoveAnchor);
164     editor->endEditBlock();
165 }
166 
stylesheetTypeSystem()167 QString KoSemanticStylesheet::stylesheetTypeSystem()
168 {
169     return QLatin1String("System");
170 }
171 
stylesheetTypeUser()172 QString KoSemanticStylesheet::stylesheetTypeUser()
173 {
174     return QLatin1String("User");
175 }
176 
177