Adapt LineModel to being autotested
This commit is contained in:
committed by
Tobias Fella
parent
53dc9c1944
commit
45cee495a5
@@ -212,10 +212,8 @@ void ModelTest::testLineModel()
|
|||||||
auto model = new LineModel();
|
auto model = new LineModel();
|
||||||
auto tester = new QAbstractItemModelTester(model);
|
auto tester = new QAbstractItemModelTester(model);
|
||||||
tester->setUseFetchMore(true);
|
tester->setUseFetchMore(true);
|
||||||
auto quickDocument = new QQuickTextDocument(new QQuickItem());
|
|
||||||
auto document = new QTextDocument();
|
auto document = new QTextDocument();
|
||||||
quickDocument->setTextDocument(document);
|
model->setDocument(document);
|
||||||
model->setDocument(quickDocument);
|
|
||||||
document->setPlainText(u"foo\nbar\n\nbaz"_s);
|
document->setPlainText(u"foo\nbar\n\nbaz"_s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ Components.AbstractMaximizeComponent {
|
|||||||
id: repeater
|
id: repeater
|
||||||
model: LineModel {
|
model: LineModel {
|
||||||
id: lineModel
|
id: lineModel
|
||||||
document: codeText.textDocument
|
Component.onCompleted: setDocument(codeText.textDocument)
|
||||||
}
|
}
|
||||||
delegate: QQC2.Label {
|
delegate: QQC2.Label {
|
||||||
id: label
|
id: label
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ Kirigami.Page {
|
|||||||
id: repeater
|
id: repeater
|
||||||
model: LineModel {
|
model: LineModel {
|
||||||
id: lineModel
|
id: lineModel
|
||||||
document: sourceTextArea.textDocument
|
Component.onCompleted: setDocument(sourceTextArea.textDocument)
|
||||||
}
|
}
|
||||||
delegate: QQC2.Label {
|
delegate: QQC2.Label {
|
||||||
id: label
|
id: label
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ Kirigami.Page {
|
|||||||
id: repeater
|
id: repeater
|
||||||
model: LineModel {
|
model: LineModel {
|
||||||
id: lineModel
|
id: lineModel
|
||||||
document: sourceTextArea.textDocument
|
Component.onCompleted: setDocument(sourceTextArea.textDocument)
|
||||||
}
|
}
|
||||||
delegate: QQC2.Label {
|
delegate: QQC2.Label {
|
||||||
id: label
|
id: label
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ QQC2.Control {
|
|||||||
id: repeater
|
id: repeater
|
||||||
model: LineModel {
|
model: LineModel {
|
||||||
id: lineModel
|
id: lineModel
|
||||||
document: codeText.textDocument
|
Component.onCompleted: setDocument(codeText.textDocument)
|
||||||
}
|
}
|
||||||
delegate: QQC2.Label {
|
delegate: QQC2.Label {
|
||||||
id: label
|
id: label
|
||||||
|
|||||||
@@ -8,12 +8,7 @@ LineModel::LineModel(QObject *parent)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
QQuickTextDocument *LineModel::document() const
|
void LineModel::setDocument(QTextDocument *document)
|
||||||
{
|
|
||||||
return m_document;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LineModel::setDocument(QQuickTextDocument *document)
|
|
||||||
{
|
{
|
||||||
if (document == m_document) {
|
if (document == m_document) {
|
||||||
return;
|
return;
|
||||||
@@ -25,6 +20,11 @@ void LineModel::setDocument(QQuickTextDocument *document)
|
|||||||
resetModel();
|
resetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LineModel::setDocument(QQuickTextDocument *document)
|
||||||
|
{
|
||||||
|
setDocument(document->textDocument());
|
||||||
|
}
|
||||||
|
|
||||||
QVariant LineModel::data(const QModelIndex &index, int role) const
|
QVariant LineModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
@@ -37,8 +37,7 @@ QVariant LineModel::data(const QModelIndex &index, int role) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (role == LineHeightRole) {
|
if (role == LineHeightRole) {
|
||||||
auto textDoc = m_document->textDocument();
|
return int(m_document->documentLayout()->blockBoundingRect(m_document->findBlockByNumber(row)).height());
|
||||||
return int(textDoc->documentLayout()->blockBoundingRect(textDoc->findBlockByNumber(row)).height());
|
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -49,7 +48,7 @@ int LineModel::rowCount(const QModelIndex &parent) const
|
|||||||
if (m_document == nullptr) {
|
if (m_document == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return m_document->textDocument()->blockCount();
|
return m_document->blockCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QByteArray> LineModel::roleNames() const
|
QHash<int, QByteArray> LineModel::roleNames() const
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <QQmlEngine>
|
#include <QQmlEngine>
|
||||||
#include <QQuickTextDocument>
|
#include <QQuickTextDocument>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
#include <qtmetamacros.h>
|
#include <QTextDocument>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class LineModel
|
* @class LineModel
|
||||||
@@ -22,11 +22,6 @@ class LineModel : public QAbstractListModel
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QML_ELEMENT
|
QML_ELEMENT
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The QQuickTextDocument that is being handled.
|
|
||||||
*/
|
|
||||||
Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Defines the model roles.
|
* @brief Defines the model roles.
|
||||||
@@ -38,8 +33,8 @@ public:
|
|||||||
|
|
||||||
explicit LineModel(QObject *parent = nullptr);
|
explicit LineModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
[[nodiscard]] QQuickTextDocument *document() const;
|
void setDocument(QTextDocument *document);
|
||||||
void setDocument(QQuickTextDocument *document);
|
Q_INVOKABLE void setDocument(QQuickTextDocument *document);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the given role value at the given index.
|
* @brief Get the given role value at the given index.
|
||||||
@@ -76,5 +71,5 @@ Q_SIGNALS:
|
|||||||
void documentChanged();
|
void documentChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPointer<QQuickTextDocument> m_document = nullptr;
|
QPointer<QTextDocument> m_document = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user