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 #include "linemodel.h"
0005 
0006 LineModel::LineModel(QObject *parent)
0007     : QAbstractListModel(parent)
0008 {
0009 }
0010 
0011 QQuickTextDocument *LineModel::document() const
0012 {
0013     return m_document;
0014 }
0015 
0016 void LineModel::setDocument(QQuickTextDocument *document)
0017 {
0018     if (document == m_document) {
0019         return;
0020     }
0021 
0022     m_document = document;
0023     Q_EMIT documentChanged();
0024 
0025     resetModel();
0026 }
0027 
0028 QVariant LineModel::data(const QModelIndex &index, int role) const
0029 {
0030     if (!index.isValid()) {
0031         return {};
0032     }
0033 
0034     const auto &row = index.row();
0035     if (row < 0 || row > rowCount()) {
0036         return {};
0037     }
0038 
0039     if (role == LineHeightRole) {
0040         auto textDoc = m_document->textDocument();
0041         return int(textDoc->documentLayout()->blockBoundingRect(textDoc->findBlockByNumber(row)).height());
0042     }
0043     return {};
0044 }
0045 
0046 int LineModel::rowCount(const QModelIndex &parent) const
0047 {
0048     Q_UNUSED(parent);
0049     if (m_document == nullptr) {
0050         return 0;
0051     }
0052     return m_document->textDocument()->blockCount();
0053 }
0054 
0055 QHash<int, QByteArray> LineModel::roleNames() const
0056 {
0057     return {{LineHeightRole, "docLineHeight"}};
0058 }
0059 
0060 void LineModel::resetModel()
0061 {
0062     beginResetModel();
0063     endResetModel();
0064 }