File: KoSectionModel.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 (217 lines) | stat: -rw-r--r-- 6,310 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
#include "KoSectionModel.h"

#include <KLocalizedString>
#include <KoTextDocument.h>
#include <climits>

KoSectionModel::KoSectionModel(QTextDocument *doc)
    : QAbstractItemModel()
    , m_doc(doc)
{
    KoTextDocument(m_doc).setSectionModel(this);
}

KoSectionModel::~KoSectionModel()
{
    foreach (KoSection *sec, m_registeredSections) {
        delete sec; // This will delete associated KoSectionEnd in KoSection destructor
    }
}

KoSection *KoSectionModel::createSection(const QTextCursor &cursor, KoSection *parent, const QString &name)
{
    if (!isValidNewName(name)) {
        return nullptr;
    }

    KoSection *result = new KoSection(cursor, name, parent);

    // Lets find our number in parent's children by cursor position
    QVector<KoSection *> children = (parent ? parent->children() : m_rootSections);
    int childrenId = children.size();
    for (int i = 0; i < children.size(); i++) {
        if (cursor.position() < children[i]->bounds().first) {
            childrenId = i;
            break;
        }
    }
    // We need to place link from parent to children in childId place
    // Also need to find corresponding index and declare operations in terms of model
    insertToModel(result, childrenId);

    return result;
}

KoSection *KoSectionModel::createSection(const QTextCursor &cursor, KoSection *parent)
{
    return createSection(cursor, parent, possibleNewName());
}

KoSectionEnd *KoSectionModel::createSectionEnd(KoSection *section)
{
    return new KoSectionEnd(section);
}

KoSection *KoSectionModel::sectionAtPosition(int pos) const
{
    // TODO: Rewrite it by traversing Model as tree
    KoSection *result = nullptr;
    int level = -1; // Seeking the section with maximum level
    QHash<QString, KoSection *>::ConstIterator it = m_sectionNames.begin();
    for (; it != m_sectionNames.end(); ++it) {
        QPair<int, int> bounds = it.value()->bounds();
        if (bounds.first > pos || bounds.second < pos) {
            continue;
        }

        if (it.value()->level() > level) {
            result = it.value();
            level = it.value()->level();
        }
    }

    return result;
}

bool KoSectionModel::isValidNewName(const QString &name) const
{
    return (m_sectionNames.constFind(name) == m_sectionNames.constEnd());
}

QString KoSectionModel::possibleNewName()
{
    QString newName;
    int i = m_registeredSections.count();
    do {
        i++;
        newName = i18nc("new numbered section name", "New section %1", i);
    } while (!isValidNewName(newName));

    return newName;
}

bool KoSectionModel::setName(KoSection *section, const QString &name)
{
    if (section->name() == name || isValidNewName(name)) {
        section->setName(name);
        // TODO: we don't have name in columns, but need something to notify views about change
        Q_EMIT dataChanged(m_modelIndex[section], m_modelIndex[section]);
        return true;
    }
    return false;
}

void KoSectionModel::allowMovingEndBound()
{
    QSet<KoSection *>::ConstIterator it = m_registeredSections.constBegin();
    for (; it != m_registeredSections.constEnd(); ++it) {
        (*it)->setKeepEndBound(false);
    }
}

int KoSectionModel::findRowOfChild(KoSection *section) const
{
    QVector<KoSection *> lookOn;
    if (!section->parent()) {
        lookOn = m_rootSections;
    } else {
        lookOn = section->parent()->children();
    }

    int result = lookOn.indexOf(section);
    Q_ASSERT(result != -1);
    return result;
}

QModelIndex KoSectionModel::index(int row, int column, const QModelIndex &parentIdx) const
{
    if (!hasIndex(row, column, parentIdx)) {
        return QModelIndex();
    }

    if (!parentIdx.isValid()) {
        return createIndex(row, column, m_rootSections[row]);
    }

    KoSection *parent = static_cast<KoSection *>(parentIdx.internalPointer());
    return createIndex(row, column, parent->children().at(row));
}

QModelIndex KoSectionModel::parent(const QModelIndex &child) const
{
    if (!child.isValid()) {
        return QModelIndex();
    }

    KoSection *section = static_cast<KoSection *>(child.internalPointer());
    KoSection *parent = section->parent();
    if (parent) {
        return createIndex(findRowOfChild(parent), 0, parent);
    }
    return QModelIndex();
}

int KoSectionModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return m_rootSections.size();
    }
    return static_cast<KoSection *>(parent.internalPointer())->children().size();
}

int KoSectionModel::columnCount(const QModelIndex & /*parent*/) const
{
    return 1;
}

QVariant KoSectionModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (index.column() == 0 && role == PointerRole) {
        QVariant v;
        v.setValue(static_cast<KoSection *>(index.internalPointer()));
        return v;
    }
    return QVariant();
}

void KoSectionModel::insertToModel(KoSection *section, int childIdx)
{
    Q_ASSERT(isValidNewName(section->name()));

    KoSection *parent = section->parent();
    if (parent) { // Inserting to some section
        beginInsertRows(m_modelIndex[parent], childIdx, childIdx);
        parent->insertChild(childIdx, section);
        endInsertRows();
        m_modelIndex[section] = QPersistentModelIndex(index(childIdx, 0, m_modelIndex[parent]));
    } else { // It will be root section
        beginInsertRows(QModelIndex(), childIdx, childIdx);
        m_rootSections.insert(childIdx, section);
        endInsertRows();
        m_modelIndex[section] = QPersistentModelIndex(index(childIdx, 0, QModelIndex()));
    }

    m_registeredSections.insert(section);
    m_sectionNames[section->name()] = section;
}

void KoSectionModel::deleteFromModel(KoSection *section)
{
    KoSection *parent = section->parent();
    int childIdx = findRowOfChild(section);
    if (parent) { // Deleting non root section
        beginRemoveRows(m_modelIndex[parent], childIdx, childIdx);
        parent->removeChild(childIdx);
        endRemoveRows();
    } else { // Deleting root section
        beginRemoveRows(QModelIndex(), childIdx, childIdx);
        m_rootSections.remove(childIdx);
        endRemoveRows();
    }
    m_modelIndex.remove(section);
    m_sectionNames.remove(section->name());
}