File: BibliographyGenerator.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 (196 lines) | stat: -rw-r--r-- 7,788 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
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2011 Smit Patel <smitpatel24@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include "BibliographyGenerator.h"
#include "KoInlineCite.h"

#include <KLocalizedString>
#include <KoInlineTextObjectManager.h>
#include <KoOdfBibliographyConfiguration.h>
#include <KoParagraphStyle.h>
#include <KoStyleManager.h>
#include <KoTextDocument.h>

#include <QTextFrame>

#include <algorithm>

BibliographyGenerator::BibliographyGenerator(QTextDocument *bibDocument, const QTextBlock &block, KoBibliographyInfo *bibInfo)
    : QObject(bibDocument)
    , m_bibDocument(bibDocument)
    , m_bibInfo(bibInfo)
    , m_block(block)
{
    Q_ASSERT(bibDocument);
    Q_ASSERT(bibInfo);

    m_bibInfo->setGenerator(this);

    bibDocument->setUndoRedoEnabled(false);
    generate();
}

BibliographyGenerator::~BibliographyGenerator()
{
    delete m_bibInfo;
}

static bool compare_on(const QVector<SortKeyPair> &sortKeys, int keyIndex, KoInlineCite *c1, KoInlineCite *c2)
{
    if (keyIndex == sortKeys.size())
        return false;
    else if (sortKeys[keyIndex].second == Qt::AscendingOrder) {
        if (c1->dataField(sortKeys[keyIndex].first) < c2->dataField(sortKeys[keyIndex].first))
            return true;
        else if (c1->dataField(sortKeys[keyIndex].first) > c2->dataField(sortKeys[keyIndex].first))
            return false;
    } else if (sortKeys[keyIndex].second == Qt::DescendingOrder) {
        if (c1->dataField(sortKeys[keyIndex].first) < c2->dataField(sortKeys[keyIndex].first))
            return false;
        else if (c1->dataField(sortKeys[keyIndex].first) > c2->dataField(sortKeys[keyIndex].first))
            return true;
    } else
        return compare_on(sortKeys, keyIndex + 1, c1, c2);

    return false;
}

static QList<KoInlineCite *> sort(QList<KoInlineCite *> cites, const QVector<SortKeyPair> &keys)
{
    QVector<SortKeyPair> sortKeys = keys;
    sortKeys << QPair<QString, Qt::SortOrder>("identifier", Qt::AscendingOrder);
    std::sort(cites.begin(), cites.end(), [sortKeys](KoInlineCite *c1, KoInlineCite *c2) {
        return compare_on(sortKeys, 0, c1, c2);
    });
    return cites;
}

void BibliographyGenerator::generate()
{
    if (!m_bibInfo)
        return;

    QTextCursor cursor = m_bibDocument->rootFrame()->lastCursorPosition();
    cursor.setPosition(m_bibDocument->rootFrame()->firstPosition(), QTextCursor::KeepAnchor);
    cursor.beginEditBlock();

    KoStyleManager *styleManager = KoTextDocument(m_block.document()).styleManager();

    if (!m_bibInfo->m_indexTitleTemplate.text.isNull()) {
        KoParagraphStyle *titleStyle = styleManager->paragraphStyle(m_bibInfo->m_indexTitleTemplate.styleId);
        if (!titleStyle) {
            titleStyle = styleManager->defaultBibliographyTitleStyle();
            m_bibInfo->m_indexTitleTemplate.styleName = titleStyle->name();
        }

        QTextBlock titleTextBlock = cursor.block();
        titleStyle->applyStyle(titleTextBlock);

        cursor.insertText(m_bibInfo->m_indexTitleTemplate.text);
        cursor.insertBlock();
    }

    QTextCharFormat savedCharFormat = cursor.charFormat();

    QList<KoInlineCite *> citeList;
    if (KoTextDocument(m_block.document()).styleManager()->bibliographyConfiguration()->sortByPosition()) {
        citeList = KoTextDocument(m_block.document()).inlineTextObjectManager()->citationsSortedByPosition(false, m_block.document()->firstBlock());
    } else {
        KoTextDocument *doc = new KoTextDocument(m_block.document());
        citeList = sort(doc->inlineTextObjectManager()->citationsSortedByPosition(false, m_block.document()->firstBlock()),
                        KoTextDocument(m_block.document()).styleManager()->bibliographyConfiguration()->sortKeys());
    }

    foreach (KoInlineCite *cite, citeList) {
        KoParagraphStyle *bibTemplateStyle = nullptr;
        BibliographyEntryTemplate bibEntryTemplate;
        if (m_bibInfo->m_entryTemplate.contains(cite->bibliographyType())) {
            bibEntryTemplate = m_bibInfo->m_entryTemplate[cite->bibliographyType()];

            bibTemplateStyle = styleManager->paragraphStyle(bibEntryTemplate.styleId);
            if (bibTemplateStyle == nullptr) {
                bibTemplateStyle = styleManager->defaultBibliographyEntryStyle(bibEntryTemplate.bibliographyType);
                bibEntryTemplate.styleName = bibTemplateStyle->name();
            }
        } else {
            continue;
        }

        cursor.insertBlock(QTextBlockFormat(), QTextCharFormat());

        QTextBlock bibEntryTextBlock = cursor.block();
        bibTemplateStyle->applyStyle(bibEntryTextBlock);
        bool spanEnabled = false; // true if data field is not empty

        foreach (IndexEntry *entry, bibEntryTemplate.indexEntries) {
            switch (entry->name) {
            case IndexEntry::BIBLIOGRAPHY: {
                IndexEntryBibliography *indexEntry = static_cast<IndexEntryBibliography *>(entry);
                cursor.insertText(QString(((spanEnabled) ? " " : "")).append(cite->dataField(indexEntry->dataField)));
                spanEnabled = !cite->dataField(indexEntry->dataField).isEmpty();
                break;
            }
            case IndexEntry::SPAN: {
                if (spanEnabled) {
                    IndexEntrySpan *span = static_cast<IndexEntrySpan *>(entry);
                    cursor.insertText(span->text);
                }
                break;
            }
            case IndexEntry::TAB_STOP: {
                IndexEntryTabStop *tabEntry = static_cast<IndexEntryTabStop *>(entry);

                cursor.insertText("\t");

                QTextBlockFormat blockFormat = cursor.blockFormat();
                QList<QVariant> tabList;
                if (tabEntry->m_position == "MAX") {
                    tabEntry->tab.position = m_maxTabPosition;
                } else {
                    tabEntry->tab.position = tabEntry->m_position.toDouble();
                }
                tabList.append(QVariant::fromValue<KoText::Tab>(tabEntry->tab));
                blockFormat.setProperty(KoParagraphStyle::TabPositions, QVariant::fromValue<QList<QVariant>>(tabList));
                cursor.setBlockFormat(blockFormat);
                break;
            }
            default: {
                break;
            }
            }
        } // foreach
    }
    cursor.setCharFormat(savedCharFormat); // restore the cursor char format
}

QMap<QString, BibliographyEntryTemplate> BibliographyGenerator::defaultBibliographyEntryTemplates()
{
    QMap<QString, BibliographyEntryTemplate> entryTemplates;
    foreach (const QString &bibType, KoOdfBibliographyConfiguration::bibTypes) {
        BibliographyEntryTemplate bibEntryTemplate;

        // Now creating default IndexEntries for all BibliographyEntryTemplates
        IndexEntryBibliography *identifier = new IndexEntryBibliography(QString());
        IndexEntryBibliography *author = new IndexEntryBibliography(QString());
        IndexEntryBibliography *title = new IndexEntryBibliography(QString());
        IndexEntryBibliography *year = new IndexEntryBibliography(QString());
        IndexEntrySpan *firstSpan = new IndexEntrySpan(QString());
        IndexEntrySpan *otherSpan = new IndexEntrySpan(QString());

        identifier->dataField = "identifier";
        author->dataField = "author";
        title->dataField = "title";
        year->dataField = "year";
        firstSpan->text = ":";
        otherSpan->text = ",";

        bibEntryTemplate.bibliographyType = bibType;
        bibEntryTemplate.indexEntries = {identifier, firstSpan, author, otherSpan, title, otherSpan, year};

        entryTemplates[bibType] = bibEntryTemplate;
    }
    return entryTemplates;
}