File: TextDocumentStructureModel.cpp

package info (click to toggle)
krita 1%3A3.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 430,944 kB
  • ctags: 68,203
  • sloc: cpp: 798,916; xml: 7,448; ansic: 3,844; perl: 434; sh: 140; makefile: 20
file content (295 lines) | stat: -rw-r--r-- 8,476 bytes parent folder | download | duplicates (3)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file is part of the Calligra project, made within the KDE community.
 *
 * Copyright (C) 2013 Friedrich W. H. Kossebau <friedrich@kogmbh.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <TextDocumentStructureModel.h>

// KDE
#include <QDebug>
// Qt
#include <QTextDocument>
#include <QTextFrame>
#include <QTextBlock>
#include <QTextCursor>

NodeData NodeData::fromBlock(int blockNumber)
{
    NodeData nodeData;
    nodeData.type = Block;
    nodeData.blockNumber = blockNumber;
    return nodeData;
}

NodeData NodeData::fromFrame(QTextFrame *frame)
{
    NodeData nodeData;
    nodeData.type = Frame;
    nodeData.frame = frame;
    return nodeData;
}

TextDocumentStructureModel::TextDocumentStructureModel(QObject *parent)
    : QAbstractItemModel(parent)
{
    connect(this, SIGNAL(modelReset()), SLOT(onModelReset()));
}

TextDocumentStructureModel::~TextDocumentStructureModel()
{
}

int TextDocumentStructureModel::columnCount(const QModelIndex &index) const
{
    Q_UNUSED(index);

    return endColumn;
}

int TextDocumentStructureModel::rowCount(const QModelIndex &index) const
{
    qDebug() << "-------------------------- index:" << index << m_textDocument;
    if (!m_textDocument) {
        return 0;
    }

    if (!index.isValid()) {
        // one root frame
        return 1;
    }

    Q_ASSERT(index.internalId() < uint(m_nodeDataTable.count()));

    const NodeData &nodeData = m_nodeDataTable.at(index.internalId());

    if (nodeData.type == NodeData::Frame) {
        QTextFrame *frame = nodeData.frame;

        // count frames and blocks
        int count = 0;
        for (QTextFrame::iterator iterator = frame->begin(); !iterator.atEnd(); ++iterator) {
            ++count;
        }
        return count;
    }

    // should be a block then, no childs for now
    return 0;
}

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

    Q_ASSERT(index.internalId() < uint(m_nodeDataTable.count()));

    const NodeData &nodeData = m_nodeDataTable.at(index.internalId());

    switch (role) {
    case Qt::DisplayRole: {
        if (nodeData.type == NodeData::Frame) {
            QTextFrame *frame = nodeData.frame;
            return QLatin1String(frame->metaObject()->className());
        }
        // else should be a block
        return QLatin1String("Block");
    }
    }

    return QVariant();
}

QModelIndex TextDocumentStructureModel::parent(const QModelIndex &index) const
{
    qDebug() << "-------------------------- index:" << index << m_textDocument;
    if (!m_textDocument || ! index.isValid()) {
        return QModelIndex();
    }

    Q_ASSERT(index.internalId() < uint(m_nodeDataTable.count()));

    const NodeData &nodeData = m_nodeDataTable.at(index.internalId());

    QTextFrame *parentFrame;
    if (nodeData.type == NodeData::Frame) {
        parentFrame = nodeData.frame->parentFrame();
    } else {
        QTextBlock block = m_textDocument->findBlockByNumber(nodeData.blockNumber);
        Q_ASSERT(block.isValid());
        // QTextBlock's API has no option to query the parentframe, so get it via a cursor
        QTextCursor cursor(block);
        parentFrame = cursor.currentFrame();
    }

    if (!parentFrame) {
        return QModelIndex();
    }

    QTextFrame *grandParentFrame = parentFrame->parentFrame();
    // parent is root frame?
    if (!grandParentFrame) {
        Q_ASSERT(parentFrame == m_textDocument->rootFrame());
        return createIndex(0, 0, static_cast<quintptr>(0));
    }

    // find position of parentFrame
    bool posFound = false;
    int row = 0;
    for (QTextFrame::iterator iterator = grandParentFrame->begin(); !iterator.atEnd(); ++iterator) {
        if (iterator.currentFrame() == parentFrame) {
            posFound = true;
            break;
        }
        ++row;
    }
    Q_ASSERT(posFound); Q_UNUSED(posFound);
    return createIndex(row, 0, frameIndex(parentFrame));
}

QModelIndex TextDocumentStructureModel::index(int row, int column, const QModelIndex &parentIndex) const
{
    qDebug() << "-------------------------- row:" << row << "column:" << column << "index:" << parentIndex << m_textDocument;
    if (!m_textDocument) {
        return QModelIndex();
    }

    if (!parentIndex.isValid()) {
        return createIndex(row, column, static_cast<quintptr>(0));
    }

    Q_ASSERT(parentIndex.internalId() < uint(m_nodeDataTable.count()));

    const NodeData &nodeData = m_nodeDataTable.at(parentIndex.internalId());
    // can be only frame for now
    Q_ASSERT(nodeData.type == NodeData::Frame);

    QTextFrame *parentFrame = nodeData.frame;
    int index = -1;
    int count = 0;
    for (QTextFrame::iterator iterator = parentFrame->begin(); !iterator.atEnd(); ++iterator) {
        if (count == row) {
            QTextFrame *frame = iterator.currentFrame();
            if (frame) {
                index = frameIndex(frame);
                break;
            } else {
                QTextBlock block = iterator.currentBlock();
                if (block.isValid()) {
                    index = blockIndex(block);
                    break;
                }
            }
        }
        ++count;
    }

    Q_ASSERT(index != -1);
    return createIndex(row, column, index);
}

bool TextDocumentStructureModel::hasChildren(const QModelIndex &parentIndex) const
{
    qDebug() << "-------------------------- parentIndex:" << parentIndex << m_textDocument;
    if (!m_textDocument) {
        return false;
    }
    // there is one root children
    if (!parentIndex.isValid()) {
        return true;
    }

    Q_ASSERT(parentIndex.internalId() < uint(m_nodeDataTable.count()));

    const NodeData &nodeData = m_nodeDataTable.at(parentIndex.internalId());

    if (nodeData.type == NodeData::Frame) {
        return (! nodeData.frame->begin().atEnd());
    }
    // no children with a block
    return false;
}

void TextDocumentStructureModel::setTextDocument(QTextDocument *textDocument)
{
    if (m_textDocument) {
        m_textDocument->disconnect(this);
    }

    m_textDocument = textDocument;

    if (m_textDocument) {
        connect(m_textDocument, SIGNAL(contentsChanged()), SLOT(onContentsChanged()));
    }

    reset();
}

int TextDocumentStructureModel::blockIndex(const QTextBlock &block) const
{
    int index;

    const int blockNumber = block.blockNumber();
    QHash<int, int>::ConstIterator it = m_blockNumberTable.constFind(blockNumber);
    if (it == m_blockNumberTable.constEnd()) {
        index = m_nodeDataTable.count();
        m_blockNumberTable.insert(blockNumber, index);
        m_nodeDataTable.append(NodeData::fromBlock(blockNumber));
    } else {
        index = it.value();
    }

    return index;
}

int TextDocumentStructureModel::frameIndex(QTextFrame *frame) const
{
    int index;

    QHash<QTextFrame *, int>::ConstIterator it = m_frameTable.constFind(frame);
    if (it == m_frameTable.constEnd()) {
        index = m_nodeDataTable.count();
        m_frameTable.insert(frame, index);
        m_nodeDataTable.append(NodeData::fromFrame(frame));
    } else {
        index = it.value();
    }

    return index;
}

void TextDocumentStructureModel::onContentsChanged()
{
    reset();
}

void TextDocumentStructureModel::onModelReset()
{
    qDebug() << "-------------------------- " << m_textDocument;
    m_nodeDataTable.clear();
    m_blockNumberTable.clear();
    m_frameTable.clear();

    // prefill table with root node
    if (m_textDocument) {
        QTextFrame *rootFrame = m_textDocument->rootFrame();
        m_frameTable.insert(rootFrame, 0);
        m_nodeDataTable.append(NodeData::fromFrame(rootFrame));
    }
}