File indexing completed on 2024-05-12 16:34:59
0001 /* This file is part of the Calligra project, made within the KDE community. 0002 * 0003 * Copyright (C) 2013,2016 Friedrich W. H. Kossebau <friedrich@kogmbh.com> 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef TEXTDOCUMENTSTRUCTUREMODEL_H 0022 #define TEXTDOCUMENTSTRUCTUREMODEL_H 0023 0024 #include <QAbstractItemModel> 0025 #include <QVector> 0026 #include <QHash> 0027 #include <QPointer> 0028 0029 class QTextDocument; 0030 class QTextFrame; 0031 class QTextBlock; 0032 class QTextLine; 0033 0034 struct NodeData 0035 { 0036 enum Type {Frame, Block, Line}; 0037 0038 Type type; 0039 union { 0040 QTextFrame *frame; 0041 int blockNumber; 0042 }; 0043 int lineNumber; 0044 0045 static NodeData fromFrame(QTextFrame *frame); 0046 static NodeData fromBlock(int blockNumber); 0047 static NodeData fromLine(int blockNumber, int lineNumber); 0048 }; 0049 Q_DECLARE_TYPEINFO(NodeData, Q_MOVABLE_TYPE); 0050 0051 struct BlockData 0052 { 0053 explicit BlockData(int _nodeIndex) : nodeIndex(_nodeIndex) {} 0054 0055 int nodeIndex; 0056 QHash<int, int> lineNumberTable; 0057 }; 0058 Q_DECLARE_TYPEINFO(BlockData, Q_MOVABLE_TYPE); 0059 0060 0061 class TextDocumentStructureModel : public QAbstractItemModel 0062 { 0063 Q_OBJECT 0064 0065 enum Columns { 0066 nameColumn = 0, 0067 endColumn 0068 }; 0069 0070 public: 0071 explicit TextDocumentStructureModel(QObject *parent = 0); 0072 ~TextDocumentStructureModel() override; 0073 0074 public: // QAbstractItemModel API 0075 QModelIndex index(int row, int column, const QModelIndex &parentIndex) const override; 0076 QModelIndex parent(const QModelIndex &index) const override; 0077 int rowCount(const QModelIndex &index) const override; 0078 int columnCount(const QModelIndex &index) const override; 0079 QVariant data(const QModelIndex &index, int role) const override; 0080 bool hasChildren(const QModelIndex &parent) const override; 0081 0082 public: 0083 void setTextDocument(QTextDocument *textDocument); 0084 0085 private Q_SLOTS: 0086 void onContentsChanged(); 0087 void onModelReset(); 0088 0089 private: 0090 int lineIndex(const QTextBlock &block, const QTextLine &line) const; 0091 int blockIndex(const QTextBlock &block) const; 0092 int frameIndex(QTextFrame *frame) const; 0093 0094 private: 0095 QPointer<QTextDocument> m_textDocument; 0096 0097 mutable QVector<NodeData> m_nodeDataTable; 0098 0099 mutable QHash<int, BlockData> m_blockNumberTable; 0100 mutable QHash<QTextFrame*, int> m_frameTable; 0101 }; 0102 0103 #endif // TEXTDOCUMENTSTRUCTUREMODEL_H