File indexing completed on 2024-05-05 05:01:22
0001 // SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com> 0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0003 0004 #pragma once 0005 0006 #include <QAbstractListModel> 0007 #include <QAbstractTextDocumentLayout> 0008 #include <QQmlEngine> 0009 #include <QQuickTextDocument> 0010 #include <QTextBlock> 0011 #include <qtmetamacros.h> 0012 0013 /** 0014 * @class LineModel 0015 * 0016 * A model to provide line info for a QQuickTextDocument. 0017 * 0018 * @sa QQuickTextDocument 0019 */ 0020 class LineModel : public QAbstractListModel 0021 { 0022 Q_OBJECT 0023 QML_ELEMENT 0024 0025 /** 0026 * @brief The QQuickTextDocument that is being handled. 0027 */ 0028 Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged) 0029 0030 public: 0031 /** 0032 * @brief Defines the model roles. 0033 */ 0034 enum Roles { 0035 LineHeightRole = Qt::UserRole + 1, /**< The delegate type of the message. */ 0036 }; 0037 Q_ENUM(Roles) 0038 0039 explicit LineModel(QObject *parent = nullptr); 0040 0041 [[nodiscard]] QQuickTextDocument *document() const; 0042 void setDocument(QQuickTextDocument *document); 0043 0044 /** 0045 * @brief Get the given role value at the given index. 0046 * 0047 * @sa QAbstractItemModel::data 0048 */ 0049 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override; 0050 0051 /** 0052 * @brief Number of rows in the model. 0053 * 0054 * @sa QAbstractItemModel::rowCount 0055 */ 0056 [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0057 0058 /** 0059 * @brief Returns a mapping from Role enum values to role names. 0060 * 0061 * @sa Roles, QAbstractItemModel::roleNames() 0062 */ 0063 [[nodiscard]] QHash<int, QByteArray> roleNames() const override; 0064 0065 /** 0066 * @brief Reset the model. 0067 * 0068 * This needs to be called when the QQuickTextDocument container changes width 0069 * or height as this may change line heights due to wrapping. 0070 * 0071 * @sa QQuickTextDocument 0072 */ 0073 Q_INVOKABLE void resetModel(); 0074 0075 Q_SIGNALS: 0076 void documentChanged(); 0077 0078 private: 0079 QPointer<QQuickTextDocument> m_document = nullptr; 0080 };