Warning, file /office/calligra/libs/text/KoInlineTextObjectManager.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) 2006-2009 Thomas Zander <zander@kde.org> 0003 * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org> 0004 * Copyright (c) 2011 Boudewijn Rempt <boud@kogmbh.com> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 #include "KoInlineTextObjectManager.h" 0022 0023 #include "InsertNamedVariableAction_p.h" 0024 #include "InsertTextReferenceAction_p.h" 0025 #include "InsertTextLocator_p.h" 0026 #include "KoInlineObjectRegistry.h" 0027 #include "KoTextLocator.h" 0028 #include "KoInlineNote.h" 0029 #include "KoInlineCite.h" 0030 0031 #include <QTextCursor> 0032 0033 KoInlineTextObjectManager::KoInlineTextObjectManager(QObject *parent) 0034 : QObject(parent), 0035 m_lastObjectId(0), 0036 m_variableManager(this) 0037 { 0038 } 0039 0040 KoInlineTextObjectManager::~KoInlineTextObjectManager() 0041 { 0042 } 0043 0044 KoInlineObject *KoInlineTextObjectManager::inlineTextObject(const QTextCharFormat &format) const 0045 { 0046 int id = format.intProperty(InlineInstanceId); 0047 if (id <= 0) 0048 return 0; 0049 return m_objects.value(id, 0); 0050 } 0051 0052 KoInlineObject *KoInlineTextObjectManager::inlineTextObject(const QTextCursor &cursor) const 0053 { 0054 return inlineTextObject(cursor.charFormat()); 0055 } 0056 0057 KoInlineObject *KoInlineTextObjectManager::inlineTextObject(int id) const 0058 { 0059 return m_objects.value(id); 0060 } 0061 0062 void KoInlineTextObjectManager::insertInlineObject(QTextCursor& cursor, KoInlineObject *object) 0063 { 0064 QTextCharFormat oldCf = cursor.charFormat(); 0065 // create a new format out of the old so that the current formatting is 0066 // also used for the inserted object. KoVariables render text too ;) 0067 QTextCharFormat cf(oldCf); 0068 cf.setObjectType(QTextFormat::UserObject + 1); 0069 cf.setProperty(InlineInstanceId, ++m_lastObjectId); 0070 cursor.insertText(QString(QChar::ObjectReplacementCharacter), cf); 0071 object->setId(m_lastObjectId); 0072 object->setManager(this); 0073 object->setup(); 0074 0075 insertObject(object); 0076 0077 // reset to use old format so that the InlineInstanceId is no longer set. 0078 cursor.setCharFormat(oldCf); 0079 } 0080 0081 void KoInlineTextObjectManager::insertObject(KoInlineObject *object) 0082 { 0083 m_objects.insert(object->id(), object); 0084 if (object->propertyChangeListener()) { 0085 m_listeners.append(object); 0086 QHash<int, QVariant>::ConstIterator i; 0087 for (i = m_properties.constBegin(); i != m_properties.constEnd(); ++i) 0088 object->propertyChanged((KoInlineObject::Property)(i.key()), i.value()); 0089 } 0090 0091 // reset to use old format so that the InlineInstanceId is no longer set. 0092 } 0093 0094 void KoInlineTextObjectManager::addInlineObject(KoInlineObject* object) 0095 { 0096 if (!object) { 0097 return; 0098 } 0099 0100 int id = object->id(); 0101 if (id == -1) { 0102 object->setId(++m_lastObjectId); 0103 object->setManager(this); 0104 object->setup(); 0105 } 0106 else { 0107 m_deletedObjects.remove(id); 0108 } 0109 insertObject(object); 0110 } 0111 0112 void KoInlineTextObjectManager::removeInlineObject(KoInlineObject *object) 0113 { 0114 if (!object) { 0115 return; 0116 } 0117 0118 int id = object->id(); 0119 m_objects.remove(id); 0120 m_deletedObjects[id] = object; 0121 m_listeners.removeAll(object); 0122 } 0123 0124 void KoInlineTextObjectManager::setProperty(KoInlineObject::Property key, const QVariant &value) 0125 { 0126 if (m_properties.contains(key)) { 0127 if (value == m_properties.value(key)) 0128 return; 0129 m_properties.remove(key); 0130 } 0131 m_properties.insert(key, value); 0132 foreach (KoInlineObject *obj, m_listeners) 0133 obj->propertyChanged(key, value); 0134 } 0135 0136 QVariant KoInlineTextObjectManager::property(KoInlineObject::Property key) const 0137 { 0138 return m_properties.value(key); 0139 } 0140 0141 int KoInlineTextObjectManager::intProperty(KoInlineObject::Property key) const 0142 { 0143 if (!m_properties.contains(key)) 0144 return 0; 0145 return m_properties.value(key).toInt(); 0146 } 0147 0148 bool KoInlineTextObjectManager::boolProperty(KoInlineObject::Property key) const 0149 { 0150 if (!m_properties.contains(key)) 0151 return false; 0152 return m_properties.value(key).toBool(); 0153 } 0154 0155 QString KoInlineTextObjectManager::stringProperty(KoInlineObject::Property key) const 0156 { 0157 if (!m_properties.contains(key)) 0158 return QString(); 0159 return qvariant_cast<QString>(m_properties.value(key)); 0160 } 0161 0162 const KoVariableManager *KoInlineTextObjectManager::variableManager() const 0163 { 0164 return &m_variableManager; 0165 } 0166 0167 KoVariableManager *KoInlineTextObjectManager::variableManager() 0168 { 0169 return &m_variableManager; 0170 } 0171 0172 void KoInlineTextObjectManager::removeProperty(KoInlineObject::Property key) 0173 { 0174 m_properties.remove(key); 0175 } 0176 0177 QList<QAction*> KoInlineTextObjectManager::createInsertVariableActions(KoCanvasBase *host) const 0178 { 0179 QList<QAction *> answer = KoInlineObjectRegistry::instance()->createInsertVariableActions(host); 0180 int i = 0; 0181 foreach(const QString & name, m_variableManager.variables()) { 0182 answer.insert(i++, new InsertNamedVariableAction(host, this, name)); 0183 } 0184 0185 answer.append(new InsertTextLocator(host)); 0186 answer.append(new InsertTextReferenceAction(host, this)); 0187 return answer; 0188 } 0189 0190 QList<KoTextLocator*> KoInlineTextObjectManager::textLocators() const 0191 { 0192 QList<KoTextLocator*> answers; 0193 foreach(KoInlineObject *object, m_objects) { 0194 KoTextLocator *tl = dynamic_cast<KoTextLocator*>(object); 0195 if (tl) 0196 answers.append(tl); 0197 } 0198 return answers; 0199 } 0200 0201 QList<KoInlineNote*> KoInlineTextObjectManager::endNotes() const 0202 { 0203 QList<KoInlineNote*> answers; 0204 foreach(KoInlineObject* object, m_objects) { 0205 KoInlineNote* note = dynamic_cast<KoInlineNote*>(object); 0206 if (note && note->type() == KoInlineNote::Endnote) { 0207 answers.append(note); 0208 } 0209 } 0210 return answers; 0211 } 0212 0213 QMap<QString, KoInlineCite*> KoInlineTextObjectManager::citations(bool duplicatesEnabled) const 0214 { 0215 QMap<QString, KoInlineCite*> answers; 0216 foreach(KoInlineObject* object, m_objects) { 0217 KoInlineCite* cite = dynamic_cast<KoInlineCite*>(object); 0218 if (cite && (cite->type() == KoInlineCite::Citation || 0219 (duplicatesEnabled && cite->type() == KoInlineCite::ClonedCitation))) { 0220 answers.insert(cite->identifier(), cite); 0221 } 0222 } 0223 return answers; 0224 } 0225 0226 QList<KoInlineCite*> KoInlineTextObjectManager::citationsSortedByPosition(bool duplicatesEnabled, QTextBlock block) const 0227 { 0228 QList<KoInlineCite*> answers; 0229 0230 while (block.isValid()) { 0231 QString text = block.text(); 0232 int pos = text.indexOf(QChar::ObjectReplacementCharacter); 0233 0234 while (pos >= 0 && pos <= block.length() ) { 0235 QTextCursor cursor(block); 0236 cursor.setPosition(block.position() + pos); 0237 cursor.setPosition(cursor.position() + 1, QTextCursor::KeepAnchor); 0238 0239 KoInlineCite *cite = dynamic_cast<KoInlineCite*>(this->inlineTextObject(cursor)); 0240 0241 if (cite && (cite->type() == KoInlineCite::Citation || 0242 (duplicatesEnabled && cite->type() == KoInlineCite::ClonedCitation))) { 0243 answers.append(cite); 0244 } 0245 pos = text.indexOf(QChar::ObjectReplacementCharacter, pos + 1); 0246 } 0247 block = block.next(); 0248 } 0249 0250 return answers; 0251 } 0252 0253 void KoInlineTextObjectManager::documentInformationUpdated(const QString &info, const QString &data) 0254 { 0255 if (info == "title") 0256 setProperty(KoInlineObject::Title, data); 0257 else if (info == "description") 0258 setProperty(KoInlineObject::Description, data); 0259 else if (info == "comments") 0260 setProperty(KoInlineObject::Comments, data); 0261 else if (info == "subject") 0262 setProperty(KoInlineObject::Subject, data); 0263 else if (info == "keyword") 0264 setProperty(KoInlineObject::Keywords, data); 0265 else if (info == "creator") 0266 setProperty(KoInlineObject::AuthorName, data); 0267 else if (info == "initial") 0268 setProperty(KoInlineObject::AuthorInitials, data); 0269 else if (info == "title") 0270 setProperty(KoInlineObject::SenderTitle, data); 0271 else if (info == "email") 0272 setProperty(KoInlineObject::SenderEmail, data); 0273 else if (info == "telephone") 0274 setProperty(KoInlineObject::SenderPhonePrivate, data); 0275 else if (info == "telephone-work") 0276 setProperty(KoInlineObject::SenderPhoneWork, data); 0277 else if (info == "fax") 0278 setProperty(KoInlineObject::SenderFax, data); 0279 else if (info == "country") 0280 setProperty(KoInlineObject::SenderCountry, data); 0281 else if (info == "postal-code") 0282 setProperty(KoInlineObject::SenderPostalCode, data); 0283 else if (info == "city") 0284 setProperty(KoInlineObject::SenderCity, data); 0285 else if (info == "street") 0286 setProperty(KoInlineObject::SenderStreet, data); 0287 else if (info == "position") 0288 setProperty(KoInlineObject::SenderPosition, data); 0289 else if (info == "company") 0290 setProperty(KoInlineObject::SenderCompany, data); 0291 } 0292 0293 QList<KoInlineObject*> KoInlineTextObjectManager::inlineTextObjects() const 0294 { 0295 return m_objects.values(); 0296 }