File: KoInlineTextObjectManager.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 (278 lines) | stat: -rw-r--r-- 8,933 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
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
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2006-2009 Thomas Zander <zander@kde.org>
 * SPDX-FileCopyrightText: 2008 Thorsten Zachmann <zachmann@kde.org>
 * SPDX-FileCopyrightText: 2011 Boudewijn Rempt <boud@kogmbh.com>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */
#include "KoInlineTextObjectManager.h"

#include "InsertNamedVariableAction_p.h"
#include "InsertTextLocator_p.h"
#include "InsertTextReferenceAction_p.h"
#include "KoInlineCite.h"
#include "KoInlineNote.h"
#include "KoInlineObjectRegistry.h"
#include "KoTextLocator.h"

#include <QTextCursor>

KoInlineTextObjectManager::KoInlineTextObjectManager(QObject *parent)
    : QObject(parent)
    , m_lastObjectId(0)
    , m_variableManager(this)
{
}

KoInlineTextObjectManager::~KoInlineTextObjectManager() = default;

KoInlineObject *KoInlineTextObjectManager::inlineTextObject(const QTextCharFormat &format) const
{
    int id = format.intProperty(InlineInstanceId);
    if (id <= 0)
        return nullptr;
    return m_objects.value(id, 0);
}

KoInlineObject *KoInlineTextObjectManager::inlineTextObject(const QTextCursor &cursor) const
{
    return inlineTextObject(cursor.charFormat());
}

KoInlineObject *KoInlineTextObjectManager::inlineTextObject(int id) const
{
    return m_objects.value(id);
}

void KoInlineTextObjectManager::insertInlineObject(QTextCursor &cursor, KoInlineObject *object)
{
    QTextCharFormat oldCf = cursor.charFormat();
    // create a new format out of the old so that the current formatting is
    // also used for the inserted object.  KoVariables render text too ;)
    QTextCharFormat cf(oldCf);
    cf.setObjectType(QTextFormat::UserObject + 1);
    cf.setProperty(InlineInstanceId, ++m_lastObjectId);
    cursor.insertText(QString(QChar::ObjectReplacementCharacter), cf);
    object->setId(m_lastObjectId);
    object->setManager(this);
    object->setup();

    insertObject(object);

    // reset to use old format so that the InlineInstanceId is no longer set.
    cursor.setCharFormat(oldCf);
}

void KoInlineTextObjectManager::insertObject(KoInlineObject *object)
{
    m_objects.insert(object->id(), object);
    if (object->propertyChangeListener()) {
        m_listeners.append(object);
        QHash<int, QVariant>::ConstIterator i;
        for (i = m_properties.constBegin(); i != m_properties.constEnd(); ++i)
            object->propertyChanged((KoInlineObject::Property)(i.key()), i.value());
    }

    // reset to use old format so that the InlineInstanceId is no longer set.
}

void KoInlineTextObjectManager::addInlineObject(KoInlineObject *object)
{
    if (!object) {
        return;
    }

    int id = object->id();
    if (id == -1) {
        object->setId(++m_lastObjectId);
        object->setManager(this);
        object->setup();
    } else {
        m_deletedObjects.remove(id);
    }
    insertObject(object);
}

void KoInlineTextObjectManager::removeInlineObject(KoInlineObject *object)
{
    if (!object) {
        return;
    }

    int id = object->id();
    m_objects.remove(id);
    m_deletedObjects[id] = object;
    m_listeners.removeAll(object);
}

void KoInlineTextObjectManager::setProperty(KoInlineObject::Property key, const QVariant &value)
{
    if (m_properties.contains(key)) {
        if (value == m_properties.value(key))
            return;
        m_properties.remove(key);
    }
    m_properties.insert(key, value);
    foreach (KoInlineObject *obj, m_listeners)
        obj->propertyChanged(key, value);
}

QVariant KoInlineTextObjectManager::property(KoInlineObject::Property key) const
{
    return m_properties.value(key);
}

int KoInlineTextObjectManager::intProperty(KoInlineObject::Property key) const
{
    if (!m_properties.contains(key))
        return 0;
    return m_properties.value(key).toInt();
}

bool KoInlineTextObjectManager::boolProperty(KoInlineObject::Property key) const
{
    if (!m_properties.contains(key))
        return false;
    return m_properties.value(key).toBool();
}

QString KoInlineTextObjectManager::stringProperty(KoInlineObject::Property key) const
{
    if (!m_properties.contains(key))
        return QString();
    return qvariant_cast<QString>(m_properties.value(key));
}

const KoVariableManager *KoInlineTextObjectManager::variableManager() const
{
    return &m_variableManager;
}

KoVariableManager *KoInlineTextObjectManager::variableManager()
{
    return &m_variableManager;
}

void KoInlineTextObjectManager::removeProperty(KoInlineObject::Property key)
{
    m_properties.remove(key);
}

QList<QAction *> KoInlineTextObjectManager::createInsertVariableActions(KoCanvasBase *host) const
{
    QList<QAction *> answer = KoInlineObjectRegistry::instance()->createInsertVariableActions(host);
    int i = 0;
    foreach (const QString &name, m_variableManager.variables()) {
        answer.insert(i++, new InsertNamedVariableAction(host, this, name));
    }

    answer.append(new InsertTextLocator(host));
    answer.append(new InsertTextReferenceAction(host, this));
    return answer;
}

QList<KoTextLocator *> KoInlineTextObjectManager::textLocators() const
{
    QList<KoTextLocator *> answers;
    foreach (KoInlineObject *object, m_objects) {
        KoTextLocator *tl = qobject_cast<KoTextLocator *>(object);
        if (tl)
            answers.append(tl);
    }
    return answers;
}

QList<KoInlineNote *> KoInlineTextObjectManager::endNotes() const
{
    QList<KoInlineNote *> answers;
    foreach (KoInlineObject *object, m_objects) {
        KoInlineNote *note = qobject_cast<KoInlineNote *>(object);
        if (note && note->type() == KoInlineNote::Endnote) {
            answers.append(note);
        }
    }
    return answers;
}

QMap<QString, KoInlineCite *> KoInlineTextObjectManager::citations(bool duplicatesEnabled) const
{
    QMap<QString, KoInlineCite *> answers;
    foreach (KoInlineObject *object, m_objects) {
        KoInlineCite *cite = qobject_cast<KoInlineCite *>(object);
        if (cite && (cite->type() == KoInlineCite::Citation || (duplicatesEnabled && cite->type() == KoInlineCite::ClonedCitation))) {
            answers.insert(cite->identifier(), cite);
        }
    }
    return answers;
}

QList<KoInlineCite *> KoInlineTextObjectManager::citationsSortedByPosition(bool duplicatesEnabled, QTextBlock block) const
{
    QList<KoInlineCite *> answers;

    while (block.isValid()) {
        QString text = block.text();
        int pos = text.indexOf(QChar::ObjectReplacementCharacter);

        while (pos >= 0 && pos <= block.length()) {
            QTextCursor cursor(block);
            cursor.setPosition(block.position() + pos);
            cursor.setPosition(cursor.position() + 1, QTextCursor::KeepAnchor);

            KoInlineCite *cite = qobject_cast<KoInlineCite *>(this->inlineTextObject(cursor));

            if (cite && (cite->type() == KoInlineCite::Citation || (duplicatesEnabled && cite->type() == KoInlineCite::ClonedCitation))) {
                answers.append(cite);
            }
            pos = text.indexOf(QChar::ObjectReplacementCharacter, pos + 1);
        }
        block = block.next();
    }

    return answers;
}

void KoInlineTextObjectManager::documentInformationUpdated(const QString &info, const QString &data)
{
    if (info == "title")
        setProperty(KoInlineObject::Title, data);
    else if (info == "description")
        setProperty(KoInlineObject::Description, data);
    else if (info == "comments")
        setProperty(KoInlineObject::Comments, data);
    else if (info == "subject")
        setProperty(KoInlineObject::Subject, data);
    else if (info == "keyword")
        setProperty(KoInlineObject::Keywords, data);
    else if (info == "creator")
        setProperty(KoInlineObject::AuthorName, data);
    else if (info == "initial")
        setProperty(KoInlineObject::AuthorInitials, data);
    else if (info == "title")
        setProperty(KoInlineObject::SenderTitle, data);
    else if (info == "email")
        setProperty(KoInlineObject::SenderEmail, data);
    else if (info == "telephone")
        setProperty(KoInlineObject::SenderPhonePrivate, data);
    else if (info == "telephone-work")
        setProperty(KoInlineObject::SenderPhoneWork, data);
    else if (info == "fax")
        setProperty(KoInlineObject::SenderFax, data);
    else if (info == "country")
        setProperty(KoInlineObject::SenderCountry, data);
    else if (info == "postal-code")
        setProperty(KoInlineObject::SenderPostalCode, data);
    else if (info == "city")
        setProperty(KoInlineObject::SenderCity, data);
    else if (info == "street")
        setProperty(KoInlineObject::SenderStreet, data);
    else if (info == "position")
        setProperty(KoInlineObject::SenderPosition, data);
    else if (info == "company")
        setProperty(KoInlineObject::SenderCompany, data);
}

QList<KoInlineObject *> KoInlineTextObjectManager::inlineTextObjects() const
{
    return m_objects.values();
}