File: KoVariableManager.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 (206 lines) | stat: -rw-r--r-- 6,746 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
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
 * SPDX-FileCopyrightText: 2008, 2011 Sebastian Sauer <mail@dipe.org>
 * SPDX-FileCopyrightText: 2011 Robert Mathias Marmorstein <robert@narnia.homeunix.com>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */
#include "KoVariableManager.h"

#include "KoInlineTextObjectManager.h"
#include "KoNamedVariable.h"
#include <KoXmlNS.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>

class KoVariableManagerPrivate
{
public:
    KoVariableManagerPrivate()
        : lastId(KoInlineObject::VariableManagerStart)
    {
    }
    KoInlineTextObjectManager *inlineObjectManager;
    QHash<QString, int> variableMapping;
    QHash<int, QString> userTypes;
    QStringList variableNames;
    QStringList userVariableNames;
    int lastId;
};

KoVariableManager::KoVariableManager(KoInlineTextObjectManager *inlineObjectManager)
    : d(new KoVariableManagerPrivate)
{
    d->inlineObjectManager = inlineObjectManager;
}

KoVariableManager::~KoVariableManager()
{
    delete d;
}

void KoVariableManager::setValue(const QString &name, const QString &value, const QString &type)
{
    int key;
    // we store the mapping from name to key
    if (d->variableMapping.contains(name)) {
        key = d->variableMapping.value(name);
    } else {
        key = d->lastId++;
        d->variableMapping.insert(name, key);
        if (type.isEmpty()) {
            Q_ASSERT(!d->variableNames.contains(name));
            d->variableNames.append(name);
        } else {
            Q_ASSERT(!d->userVariableNames.contains(name));
            d->userVariableNames.append(name);
        }
    }
    if (!type.isEmpty()) {
        d->userTypes.insert(key, type);
    }
    // the variable manager stores the actual value of the variable.
    d->inlineObjectManager->setProperty(static_cast<KoInlineObject::Property>(key), value);
    Q_EMIT valueChanged();
}

QString KoVariableManager::value(const QString &name) const
{
    int key = d->variableMapping.value(name);
    if (key == 0) {
        return QString();
    }
    return d->inlineObjectManager->stringProperty(static_cast<KoInlineObject::Property>(key));
}

QString KoVariableManager::userType(const QString &name) const
{
    int key = d->variableMapping.value(name);
    if (key == 0) {
        return QString();
    }
    QHash<int, QString>::const_iterator it = d->userTypes.constFind(key);
    if (it == d->userTypes.constEnd()) {
        return QString();
    }
    return it.value();
}

void KoVariableManager::remove(const QString &name)
{
    int key = d->variableMapping.value(name);
    if (key == 0) {
        return;
    }
    d->variableMapping.remove(name);
    d->userTypes.remove(key);
    d->variableNames.removeOne(name);
    d->userVariableNames.removeOne(name);
    d->inlineObjectManager->removeProperty(static_cast<KoInlineObject::Property>(key));
}

KoVariable *KoVariableManager::createVariable(const QString &name) const
{
    int key = d->variableMapping.value(name);
    if (key == 0) {
        return nullptr;
    }
    return new KoNamedVariable(static_cast<KoInlineObject::Property>(key), name);
}

QList<QString> KoVariableManager::variables() const
{
    return d->variableNames;
}

QList<QString> KoVariableManager::userVariables() const
{
    return d->userVariableNames;
}

#include "TextDebug.h"

void KoVariableManager::loadOdf(const KoXmlElement &bodyElement)
{
    KoXmlElement element = KoXml::namedItemNS(bodyElement, KoXmlNS::text, "user-field-decls", KoXmlTextContentPrelude);
    if (element.isNull())
        return;
    KoXmlElement e;
    forEachElement(e, element)
    {
        if (e.namespaceURI() != KoXmlNS::text || e.localName() != "user-field-decl")
            continue;
        const QString name = e.attributeNS(KoXmlNS::text, "name");
        QString type = e.attributeNS(KoXmlNS::office, "value-type");
        QString value;
        if (type == "string") {
            if (e.hasAttributeNS(KoXmlNS::office, "string-value"))
                value = e.attributeNS(KoXmlNS::office, "string-value");
            else // if the string-value is not present then the content defines the value
                value = e.toText().data();
        } else if (type == "boolean") {
            value = e.attributeNS(KoXmlNS::office, "boolean-value");
        } else if (type == "currency") {
            value = e.attributeNS(KoXmlNS::office, "currency");
        } else if (type == "date") {
            value = e.attributeNS(KoXmlNS::office, "date-value");
        } else if (type == "float") {
            value = e.attributeNS(KoXmlNS::office, "value");
        } else if (type == "percentage") {
            value = e.attributeNS(KoXmlNS::office, "value");
        } else if (type == "time") {
            value = e.attributeNS(KoXmlNS::office, "time-value");
        } else if (type == "void") {
            value = e.attributeNS(KoXmlNS::office, "value");
        } else if (e.hasAttributeNS(KoXmlNS::text, "formula")) {
            type = "formula";
            value = e.attributeNS(KoXmlNS::text, "formula");
        } else {
            warnText << "Unknown user-field-decl value-type=" << type;
            continue;
        }
        setValue(name, value, type);
    }
}

void KoVariableManager::saveOdf(KoXmlWriter *bodyWriter)
{
    if (userVariables().isEmpty()) {
        return;
    }
    bodyWriter->startElement("text:user-field-decls");
    foreach (const QString &name, userVariables()) {
        bodyWriter->startElement("text:user-field-decl");
        bodyWriter->addAttribute("text:name", name);
        QByteArray tag;
        QString type = userType(name);
        if (type == "formula") {
            tag = "text:formula";
        } else {
            if (type == "string") {
                tag = "office:string-value";
            } else if (type == "boolean") {
                tag = "office:boolean-value";
            } else if (type == "currency") {
                tag = "office:boolean-value";
            } else if (type == "date") {
                tag = "office:date-value";
            } else if (type == "float") {
                tag = "office:value";
            } else if (type == "percentage") {
                tag = "office:value";
            } else if (type == "time") {
                tag = "office:time-value";
            } else if (type == "void") {
                tag = "office:value";
            } else {
                tag = "office:string-value";
                type = "string";
            }
            bodyWriter->addAttribute("office:value-type", type);
        }
        bodyWriter->addAttribute(tag, value(name));
        bodyWriter->endElement();
    }
    bodyWriter->endElement();
}