Warning, file /office/calligra/libs/rdf/KoSemanticStylesheet.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library 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 GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 #include "KoSemanticStylesheet.h" 0020 0021 #include "KoDocumentRdf.h" 0022 #include "KoChangeTrackerDisabledRAII.h" 0023 // main 0024 #include <KoDocument.h> 0025 #include <KoTextDocument.h> 0026 #include <KoTextEditor.h> 0027 // KF5 0028 #include <kdebug.h> 0029 // Qt 0030 #include <QCoreApplication> 0031 0032 0033 class KoSemanticStylesheetPrivate 0034 { 0035 public: 0036 QString m_uuid; 0037 QString m_name; 0038 QString m_templateString; 0039 QString m_type; 0040 bool m_isMutable; 0041 0042 KoSemanticStylesheetPrivate(const QString &uuid, const QString &name, const QString &templateString, 0043 const QString &type = QLatin1String("System"), bool isMutable = false) 0044 : 0045 m_uuid(uuid), 0046 m_name(name), 0047 m_templateString(templateString), 0048 m_type(type), 0049 m_isMutable(isMutable) 0050 {} 0051 }; 0052 0053 0054 KoSemanticStylesheet::KoSemanticStylesheet(const QString &uuid, 0055 const QString &name, 0056 const QString &templateString, 0057 const QString &type, bool isMutable) 0058 : QObject(QCoreApplication::instance()) 0059 , d (new KoSemanticStylesheetPrivate (uuid,name,templateString,type,isMutable)) 0060 { 0061 } 0062 0063 KoSemanticStylesheet::~KoSemanticStylesheet() 0064 { 0065 delete d; 0066 } 0067 0068 QString KoSemanticStylesheet::uuid() const 0069 { 0070 return d->m_uuid; 0071 } 0072 0073 QString KoSemanticStylesheet::name() const 0074 { 0075 return d->m_name; 0076 } 0077 0078 QString KoSemanticStylesheet::templateString() const 0079 { 0080 return d->m_templateString; 0081 } 0082 0083 QString KoSemanticStylesheet::type() const 0084 { 0085 return d->m_type; 0086 } 0087 0088 0089 bool KoSemanticStylesheet::isMutable() const 0090 { 0091 return d->m_isMutable; 0092 } 0093 0094 void KoSemanticStylesheet::name(const QString &v) 0095 { 0096 if (d->m_isMutable) { 0097 emit nameChanging(hKoSemanticStylesheet(this), d->m_name, v); 0098 d->m_name = v; 0099 } 0100 } 0101 0102 void KoSemanticStylesheet::templateString(const QString &v) 0103 { 0104 if (d->m_isMutable) { 0105 d->m_templateString = v; 0106 } 0107 } 0108 0109 0110 void KoSemanticStylesheet::format(hKoRdfSemanticItem obj, KoTextEditor *editor, const QString &xmlid) 0111 { 0112 Q_ASSERT(obj); 0113 Q_ASSERT(editor); 0114 kDebug(30015) << "formatting obj:" << obj << " name:" << obj->name(); 0115 kDebug(30015) << "xmlid:" << xmlid << " editor:" << editor << " sheet-name:" << name(); 0116 const KoDocumentRdf *rdf = obj->documentRdf(); 0117 Q_ASSERT(rdf); 0118 Q_ASSERT(editor); 0119 QPair<int, int> p; 0120 if (xmlid.size()) { 0121 p = rdf->findExtent(xmlid); 0122 } else { 0123 p = rdf->findExtent(editor); 0124 } 0125 int startpos = p.first + 1; 0126 int endpos = p.second; 0127 if (!endpos) { 0128 kDebug(30015) << "format() invalid range, skipping! start:" << startpos << " end:" << endpos; 0129 return; 0130 } 0131 KoTextDocument ktd(editor->document()); 0132 KoChangeTrackerDisabledRAII disableChangeTracker(ktd.changeTracker()); 0133 editor->beginEditBlock(); 0134 editor->setPosition(startpos, QTextCursor::MoveAnchor); 0135 editor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endpos - startpos); 0136 QString oldText = editor->selectedText(); 0137 if (editor->hasSelection()) { 0138 editor->deleteChar(); // deletes the selection 0139 } 0140 editor->setPosition(startpos, QTextCursor::MoveAnchor); 0141 kDebug(30015) << "formatting start:" << startpos << " end:" << endpos; 0142 kDebug(30015) << "semantic item oldText:" << oldText; 0143 QString data = templateString(); 0144 QMap<QString, QString> m; 0145 m["%NAME%"] = obj->name(); 0146 obj->setupStylesheetReplacementMapping(m); 0147 0148 for (QMap<QString, QString>::iterator mi = m.begin(); mi != m.end(); ++mi) { 0149 QString k = mi.key(); 0150 QString v = mi.value(); 0151 data.replace(k, v); 0152 } 0153 // make sure there is something in the replacement other than commas and spaces 0154 QString tmpstring = data; 0155 tmpstring.remove(' '); 0156 tmpstring.remove(','); 0157 if (!tmpstring.size()) { 0158 kDebug(30015) << "stylesheet results in empty data, using name() instead"; 0159 data = name(); 0160 } 0161 kDebug(30015) << "Updating with new formatting:" << data; 0162 editor->insertText(data); 0163 editor->setPosition(startpos, QTextCursor::MoveAnchor); 0164 editor->endEditBlock(); 0165 } 0166 0167 QString KoSemanticStylesheet::stylesheetTypeSystem() 0168 { 0169 return QLatin1String("System"); 0170 } 0171 0172 QString KoSemanticStylesheet::stylesheetTypeUser() 0173 { 0174 return QLatin1String("User"); 0175 } 0176