File: KoTableOfContentsGeneratorInfo.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 (251 lines) | stat: -rw-r--r-- 11,670 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
/*
 * SPDX-FileCopyrightText: 2011 Lukáš Tvrdý <lukas.tvrdy@ixonos.com>
 * SPDX-FileCopyrightText: 2011 Ko GmbH <cbo@kogmbh.com>
 * SPDX-FileCopyrightText: 2011 Gopalakrishna Bhat A <gopalakbhat@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include "KoTableOfContentsGeneratorInfo.h"

#include <KoParagraphStyle.h>
#include <KoTextSharedLoadingData.h>
#include <KoXmlNS.h>
#include <KoXmlReader.h>
#include <KoXmlWriter.h>

int KoTableOfContentsGeneratorInfo::styleNameToStyleId(KoTextSharedLoadingData *sharedLoadingData, const QString &styleName)
{
    // find styleId of a style based on its style:name property
    KoParagraphStyle *style = sharedLoadingData->paragraphStyle(styleName, true);
    if (style) {
        return style->styleId();
    }

    // if the previous way of finding styles fails fall back on using style:display-name property of a style
    QList<KoParagraphStyle *> paragraphStyles = sharedLoadingData->paragraphStyles(true);
    QList<KoParagraphStyle *>::const_iterator iter = paragraphStyles.constBegin();

    for (; iter != paragraphStyles.constEnd(); ++iter) {
        if ((*iter)->name() == styleName) {
            return (*iter)->styleId();
        }
    }

    return 0;
}

KoTableOfContentsGeneratorInfo::KoTableOfContentsGeneratorInfo(bool generateEntryTemplate)
    : m_indexScope("document")
    , m_outlineLevel(10)
    , m_relativeTabStopPosition(true)
    , m_useIndexMarks(true)
    , m_useIndexSourceStyles(false)
    , m_useOutlineLevel(true)
{
    // index-title-template
    // m_indexTitleTemplate.text = "title";
    if (generateEntryTemplate) {
        // table-of-content-entry-template
        for (int level = 1; level <= m_outlineLevel; level++) {
            TocEntryTemplate tocEntryTemplate;
            tocEntryTemplate.outlineLevel = level;

            // index-entry-link-start
            IndexEntryLinkStart *link = new IndexEntryLinkStart(QString());
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(link));

            // index-entry-chapter
            // use null String if the style name is not present, it means that we inherit it from the parent
            IndexEntryChapter *entryChapter = new IndexEntryChapter(QString());
            entryChapter->display = "number";
            entryChapter->outlineLevel = level;
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryChapter));

            // index-entry-text
            IndexEntryText *entryText = new IndexEntryText(QString());
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryText));

            // index-entry-tab-stop
            IndexEntryTabStop *entryTabStop = new IndexEntryTabStop(QString());
            entryTabStop->tab.type = QTextOption::RightTab;
            entryTabStop->setPosition("");
            entryTabStop->tab.leaderText = '.';
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryTabStop));

            // index-entry-page-number
            IndexEntryPageNumber *entryPageNumber = new IndexEntryPageNumber(QString());
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryPageNumber));

            // index-entry-link-end
            IndexEntryLinkEnd *linkend = new IndexEntryLinkEnd(QString());
            tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(linkend));

            m_entryTemplate.append(tocEntryTemplate);
        }
    }
}

KoTableOfContentsGeneratorInfo::~KoTableOfContentsGeneratorInfo()
{
    foreach (const TocEntryTemplate &entryTemplate, m_entryTemplate) {
        qDeleteAll(entryTemplate.indexEntries);
    }
}

void KoTableOfContentsGeneratorInfo::loadOdf(KoTextSharedLoadingData *sharedLoadingData, const KoXmlElement &element)
{
    Q_ASSERT(element.localName() == "table-of-content-source" && element.namespaceURI() == KoXmlNS::text);

    foreach (const TocEntryTemplate &entryTemplate, m_entryTemplate) {
        qDeleteAll(entryTemplate.indexEntries);
    }
    m_entryTemplate.clear();

    m_indexScope = element.attribute("index-scope", "document"); // enum {document, chapter}
    m_outlineLevel = element.attribute("outline-level", "1").toInt();
    m_relativeTabStopPosition = element.attribute("relative-tab-stop-position", "true") == "true";
    m_useIndexMarks = element.attribute("use-index-marks", "f") == "true";
    m_useIndexSourceStyles = element.attribute("use-index-source-styles", "false") == "true";
    m_useOutlineLevel = element.attribute("use-outline-level", "true") == "true";

    // three other children to visit
    KoXmlElement p;
    forEachElement(p, element)
    {
        if (p.namespaceURI() != KoXmlNS::text) {
            continue;
        }

        // first child
        if (p.localName() == "index-title-template") {
            m_indexTitleTemplate.styleName = p.attribute("style-name");
            m_indexTitleTemplate.styleId = styleNameToStyleId(sharedLoadingData, m_indexTitleTemplate.styleName);
            m_indexTitleTemplate.text = p.text();
            // second child
        } else if (p.localName() == "table-of-content-entry-template") {
            TocEntryTemplate tocEntryTemplate;
            tocEntryTemplate.outlineLevel = p.attribute("outline-level").toInt();
            tocEntryTemplate.styleName = p.attribute("style-name");
            tocEntryTemplate.styleId = styleNameToStyleId(sharedLoadingData, tocEntryTemplate.styleName);

            KoXmlElement indexEntry;
            forEachElement(indexEntry, p)
            {
                if (indexEntry.namespaceURI() != KoXmlNS::text) {
                    continue;
                }

                if (indexEntry.localName() == "index-entry-chapter") {
                    // use null String if the style name is not present, it means that we inherit it from the parent
                    IndexEntryChapter *entryChapter = new IndexEntryChapter(indexEntry.attribute("style-name", QString()));

                    // display can be "name", "number", "number-and-name", "plain-number" or "plain-number-and-name"
                    // "number" is default according the specs ODF v1.2
                    entryChapter->display = indexEntry.attribute("display", "number");
                    entryChapter->outlineLevel = indexEntry.attribute("outline-level", QString::number(tocEntryTemplate.outlineLevel)).toInt();
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryChapter));

                } else if (indexEntry.localName() == "index-entry-text") {
                    IndexEntryText *entryText = new IndexEntryText(indexEntry.attribute("style-name", QString()));
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryText));

                } else if (indexEntry.localName() == "index-entry-page-number") {
                    IndexEntryPageNumber *entryPageNumber = new IndexEntryPageNumber(indexEntry.attribute("style-name", QString()));
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryPageNumber));

                } else if (indexEntry.localName() == "index-entry-span") {
                    IndexEntrySpan *entrySpan = new IndexEntrySpan(indexEntry.attribute("style-name", QString()));
                    entrySpan->text = indexEntry.text();
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entrySpan));

                } else if (indexEntry.localName() == "index-entry-tab-stop") {
                    IndexEntryTabStop *entryTabStop = new IndexEntryTabStop(indexEntry.attribute("style-name", QString()));

                    QString type = indexEntry.attribute("type", "right"); // left or right
                    if (type == "left") {
                        entryTabStop->tab.type = QTextOption::LeftTab;
                    } else {
                        entryTabStop->tab.type = QTextOption::RightTab;
                    }
                    entryTabStop->setPosition(indexEntry.attribute("position", QString()));
                    entryTabStop->tab.leaderText = indexEntry.attribute("leader-char", ".");
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(entryTabStop));

                } else if (indexEntry.localName() == "index-entry-link-start") {
                    IndexEntryLinkStart *link = new IndexEntryLinkStart(indexEntry.attribute("style-name", QString()));
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(link));
                } else if (indexEntry.localName() == "index-entry-link-end") {
                    IndexEntryLinkEnd *link = new IndexEntryLinkEnd(indexEntry.attribute("style-name", QString()));
                    tocEntryTemplate.indexEntries.append(static_cast<IndexEntry *>(link));
                }
            }
            m_entryTemplate.append(tocEntryTemplate);

            // third child
        } else if (p.localName() == "index-source-styles" && p.namespaceURI() == KoXmlNS::text) {
            IndexSourceStyles indexStyles;
            indexStyles.outlineLevel = p.attribute("outline-level").toInt();

            IndexSourceStyle indexStyle;
            KoXmlElement sourceElement;
            forEachElement(sourceElement, p)
            {
                if (sourceElement.localName() == "index-source-style") {
                    indexStyle.styleName = sourceElement.attribute("style-name");
                    indexStyle.styleId = styleNameToStyleId(sharedLoadingData, indexStyle.styleName);
                    indexStyles.styles.append(indexStyle);
                }
            }
            m_indexSourceStyles.append(indexStyles);
        }
    } // forEachElement
}

void KoTableOfContentsGeneratorInfo::saveOdf(KoXmlWriter *writer) const
{
    writer->startElement("text:table-of-content-source");
    writer->addAttribute("text:index-scope", m_indexScope);
    writer->addAttribute("text:outline-level", m_outlineLevel);
    writer->addAttribute("text:relative-tab-stop-position", m_relativeTabStopPosition);
    writer->addAttribute("text:use-index-marks", m_useIndexMarks);
    writer->addAttribute("text:use-index-source-styles", m_useIndexSourceStyles);
    writer->addAttribute("text:use-outline-level", m_useOutlineLevel);

    m_indexTitleTemplate.saveOdf(writer);

    foreach (const TocEntryTemplate &entry, m_entryTemplate) {
        entry.saveOdf(writer);
    }

    foreach (const IndexSourceStyles &sourceStyle, m_indexSourceStyles) {
        sourceStyle.saveOdf(writer);
    }

    writer->endElement(); // text:table-of-content-source
}

KoTableOfContentsGeneratorInfo *KoTableOfContentsGeneratorInfo::clone()
{
    KoTableOfContentsGeneratorInfo *newToCInfo = new KoTableOfContentsGeneratorInfo(false);
    newToCInfo->m_entryTemplate.clear();
    newToCInfo->m_name = QString(m_name);
    newToCInfo->m_styleName = QString(m_styleName);
    newToCInfo->m_indexScope = QString(m_indexScope);
    newToCInfo->m_outlineLevel = m_outlineLevel;
    newToCInfo->m_relativeTabStopPosition = m_relativeTabStopPosition;
    newToCInfo->m_useIndexMarks = m_useIndexMarks;
    newToCInfo->m_useIndexSourceStyles = m_useIndexSourceStyles;
    newToCInfo->m_useOutlineLevel = m_useOutlineLevel;
    newToCInfo->m_indexTitleTemplate = m_indexTitleTemplate;

    foreach (const TocEntryTemplate &tocTemplate, m_entryTemplate) {
        newToCInfo->m_entryTemplate.append(tocTemplate);
    }

    foreach (const IndexSourceStyles &indexSourceStyles, m_indexSourceStyles) {
        newToCInfo->m_indexSourceStyles.append(indexSourceStyles);
    }

    return newToCInfo;
}