File: KoSection.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 (234 lines) | stat: -rw-r--r-- 6,695 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
/*
 *  SPDX-FileCopyrightText: 2011 Boudewijn Rempt <boud@valdyas.org>
 *  SPDX-FileCopyrightText: 2014-2015 Denis Kuplyakov <dener.kup@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "KoSection.h"

#include <KoSectionEnd.h>
#include <KoSectionModel.h>
#include <KoSectionStyle.h>
#include <KoShapeSavingContext.h>
#include <KoTextDocument.h>
#include <KoTextInlineRdf.h>
#include <KoTextSharedLoadingData.h>
#include <KoXmlNS.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>

#include <QTextBlock>

#include "TextDebug.h"

class KoSectionPrivate
{
public:
    explicit KoSectionPrivate(const QTextCursor &cursor, const QString &_name, KoSection *_parent)
        : document(cursor.block().document())
        , name(_name)
        , sectionStyle(nullptr)
        , boundingCursorStart(cursor)
        , boundingCursorEnd(cursor)
        , parent(_parent)
        , inlineRdf(nullptr)
    {
    }

    const QTextDocument *document;

    QString condition;
    QString display;
    QString name;
    QString text_protected;
    QString protection_key;
    QString protection_key_digest_algorithm;
    QString style_name;
    KoSectionStyle *sectionStyle;

    QScopedPointer<KoSectionEnd> sectionEnd; ///< pointer to the corresponding section end
    int level; ///< level of the section in document, root sections have 0 level
    QTextCursor boundingCursorStart; ///< This cursor points to the start of the section
    QTextCursor boundingCursorEnd; ///< This cursor points to the end of the section (excluding paragraph symbol)

    // Boundings explanation:
    //
    // |S|e|c|t|i|o|n|...|t|e|x|t|P|
    // ^                         ^
    // |--- Start                |-- End

    QVector<KoSection *> children; ///< List of the section's childrens
    KoSection *parent; ///< Parent of the section

    KoTextInlineRdf *inlineRdf; ///< Handling associated RDF
};

KoSection::KoSection(const QTextCursor &cursor, const QString &name, KoSection *parent)
    : d_ptr(new KoSectionPrivate(cursor, name, parent))
{
    Q_D(KoSection);

    d->boundingCursorStart.setKeepPositionOnInsert(true); // Start cursor should stay on place
    d->boundingCursorEnd.setKeepPositionOnInsert(false); // and end one should move forward

    if (parent) {
        d->level = parent->level() + 1;
    } else {
        d->level = 0;
    }
}

KoSection::~KoSection()
{
    // Here scoped pointer will delete sectionEnd
}

QString KoSection::name() const
{
    Q_D(const KoSection);
    return d->name;
}

QPair<int, int> KoSection::bounds() const
{
    Q_D(const KoSection);
    return QPair<int, int>(d->boundingCursorStart.position(), d->boundingCursorEnd.position());
}

int KoSection::level() const
{
    Q_D(const KoSection);
    return d->level;
}

bool KoSection::loadOdf(const KoXmlElement &element, KoTextSharedLoadingData *sharedData, bool stylesDotXml)
{
    Q_D(KoSection);
    // check whether we really are a section
    if (element.namespaceURI() == KoXmlNS::text && element.localName() == "section") {
        // get all the attributes
        d->condition = element.attributeNS(KoXmlNS::text, "condition");
        d->display = element.attributeNS(KoXmlNS::text, "display");

        if (d->display == "condition" && d->condition.isEmpty()) {
            warnText << "Section display is set to \"condition\", but condition is empty.";
        }

        QString newName = element.attributeNS(KoXmlNS::text, "name");
        if (!KoTextDocument(d->document).sectionModel()->setName(this, newName)) {
            warnText << "Section name \"" << newName << "\" must be unique or is invalid. Resetting it to " << name();
        }

        d->text_protected = element.attributeNS(KoXmlNS::text, "text-protected");
        d->protection_key = element.attributeNS(KoXmlNS::text, "protection-key");
        d->protection_key_digest_algorithm = element.attributeNS(KoXmlNS::text, "protection-key-algorithm");
        d->style_name = element.attributeNS(KoXmlNS::text, "style-name", "");

        if (!d->style_name.isEmpty()) {
            d->sectionStyle = sharedData->sectionStyle(d->style_name, stylesDotXml);
        }

        // lets handle associated xml:id
        if (element.hasAttribute("id")) {
            KoTextInlineRdf *inlineRdf = new KoTextInlineRdf(const_cast<QTextDocument *>(d->document), this);
            if (inlineRdf->loadOdf(element)) {
                d->inlineRdf = inlineRdf;
            } else {
                delete inlineRdf;
                inlineRdf = nullptr;
            }
        }

        return true;
    }
    return false;
}

void KoSection::saveOdf(KoShapeSavingContext &context) const
{
    Q_D(const KoSection);
    KoXmlWriter *writer = &context.xmlWriter();
    Q_ASSERT(writer);
    writer->startElement("text:section", false);

    if (!d->condition.isEmpty())
        writer->addAttribute("text:condition", d->condition);
    if (!d->display.isEmpty())
        writer->addAttribute("text:display", d->condition);
    if (!d->name.isEmpty())
        writer->addAttribute("text:name", d->name);
    if (!d->text_protected.isEmpty())
        writer->addAttribute("text:text-protected", d->text_protected);
    if (!d->protection_key.isEmpty())
        writer->addAttribute("text:protection-key", d->protection_key);
    if (!d->protection_key_digest_algorithm.isEmpty()) {
        writer->addAttribute("text:protection-key-digest-algorithm", d->protection_key_digest_algorithm);
    }
    if (!d->style_name.isEmpty())
        writer->addAttribute("text:style-name", d->style_name);

    if (d->inlineRdf) {
        d->inlineRdf->saveOdf(context, writer);
    }
}

void KoSection::setSectionEnd(KoSectionEnd *sectionEnd)
{
    Q_D(KoSection);
    d->sectionEnd.reset(sectionEnd);
}

void KoSection::setName(const QString &name)
{
    Q_D(KoSection);
    d->name = name;
}

void KoSection::setLevel(int level)
{
    Q_D(KoSection);
    d->level = level;
}

void KoSection::setKeepEndBound(bool state)
{
    Q_D(KoSection);
    d->boundingCursorEnd.setKeepPositionOnInsert(state);
}

KoSection *KoSection::parent() const
{
    Q_D(const KoSection);
    return d->parent;
}

QVector<KoSection *> KoSection::children() const
{
    Q_D(const KoSection);
    return d->children;
}

void KoSection::insertChild(int childIdx, KoSection *section)
{
    Q_D(KoSection);
    d->children.insert(childIdx, section);
}

void KoSection::removeChild(int childIdx)
{
    Q_D(KoSection);
    d->children.remove(childIdx);
}

KoTextInlineRdf *KoSection::inlineRdf() const
{
    Q_D(const KoSection);
    return d->inlineRdf;
}

void KoSection::setInlineRdf(KoTextInlineRdf *inlineRdf)
{
    Q_D(KoSection);
    d->inlineRdf = inlineRdf;
}