MauiKit TextEditor

linenumbermodel.cpp
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include "linenumbermodel.h"
5
6#include <QDebug>
7
8/*!
9 When using an integer model based on the line count of the editor,
10 any changes in that line count cause all delegates to be destroyed
11 and recreated. That's inefficient, so instead, we add/remove model
12 items as necessary ourselves, based on the lineCount property.
13*/
14LineNumberModel::LineNumberModel(QObject *parent)
15 : QAbstractListModel(parent)
16{
17}
18
19int LineNumberModel::lineCount() const
20{
21 return m_lineCount;
22}
23
24void LineNumberModel::setLineCount(int lineCount)
25{
26 if (lineCount < 0) {
27 qWarning() << "lineCount must be greater than zero";
28 return;
29 }
30
31 if (m_lineCount == lineCount)
32 return;
33
34 if (m_lineCount < lineCount) {
35 beginInsertRows(QModelIndex(), m_lineCount, lineCount - 1);
36 m_lineCount = lineCount;
38 } else if (m_lineCount > lineCount) {
39 beginRemoveRows(QModelIndex(), lineCount, m_lineCount - 1);
40 m_lineCount = lineCount;
42 }
43
44 Q_EMIT lineCountChanged();
45}
46
47int LineNumberModel::rowCount(const QModelIndex &) const
48{
49 return m_lineCount;
50}
51
52QVariant LineNumberModel::data(const QModelIndex &index, int role) const
53{
54 if (!checkIndex(index) || role != Qt::DisplayRole)
55 return QVariant();
56
57 return index.row();
58}
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int row() const const
Q_EMITQ_EMIT
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Aug 30 2024 11:55:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.