From 40d3a80737d0560fb03a8de83860dc567053a96f Mon Sep 17 00:00:00 2001 From: Dominik Holland Date: Tue, 9 Oct 2018 11:23:58 +0200 Subject: Add helper functions to the autogenerated pagingmodel backend implementations These helper functions can be used to alter a pagingmodel from QML and script it in a meaningful way without the need to care about the internal paging logic. Change-Id: Iaca947ab738701a93288e845df08ae4d37ec32df Reviewed-by: Robert Griebl --- .../pagingmodel.cpp.tpl | 45 ++++++++++++++++++++++ .../templates_backend_simulator/pagingmodel.h.tpl | 16 ++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.cpp.tpl b/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.cpp.tpl index 060a377..68433f6 100644 --- a/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.cpp.tpl +++ b/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.cpp.tpl @@ -42,6 +42,7 @@ {{class}}::{{class}}(QObject* parent) : QIviPagingModelInterface(parent) { + qRegisterMetaType(); m_list = {{property|default_value}}; } @@ -85,3 +86,47 @@ void {{class}}::fetchData(const QUuid &identifier, int start, int count) emit dataFetched(identifier, list, start, max < m_list.count()); } + +void {{class}}::insert(int index, const {{property.type.nested}} &item) +{ + m_list.insert(index, item); + + emit dataChanged(QUuid(), { QVariant::fromValue(item) }, index, 0); +} + +void {{class}}::remove(int index) +{ + m_list.removeAt(index); + + emit dataChanged(QUuid(), QVariantList(), index, 1); +} + +void {{class}}::move(int currentIndex, int newIndex) +{ + int min = qMin(currentIndex, newIndex); + int max = qMax(currentIndex, newIndex); + + m_list.move(currentIndex, newIndex); + QVariantList variantList; + for (int i = min; i <= max; i++) + variantList.append(QVariant::fromValue(m_list.at(i))); + + emit dataChanged(QUuid(), variantList, min, max - min + 1); +} + +void {{class}}::reset() +{ + emit dataChanged(QUuid(), QVariantList(), 0, m_list.count()); + m_list.clear(); +} + +void {{class}}::update(int index, const {{property.type.nested}} &item) +{ + m_list[index] = item; + emit dataChanged(QUuid(), { QVariant::fromValue(item) }, index, 1); +} + +const {{property.type.nested}} &{{class}}::at(int index) const +{ + return m_list.at(index); +} diff --git a/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.h.tpl b/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.h.tpl index 5d664bf..e274ce8 100644 --- a/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.h.tpl +++ b/src/tools/ivigenerator/templates_backend_simulator/pagingmodel.h.tpl @@ -49,11 +49,19 @@ public: explicit {{class}}(QObject *parent = nullptr); ~{{class}}(); - void initialize() override; - void registerInstance(const QUuid &identifier) override; - void unregisterInstance(const QUuid &identifier) override; + Q_INVOKABLE void initialize() override; + Q_INVOKABLE void registerInstance(const QUuid &identifier) override; + Q_INVOKABLE void unregisterInstance(const QUuid &identifier) override; - void fetchData(const QUuid &identifier, int start, int count) override; + Q_INVOKABLE void fetchData(const QUuid &identifier, int start, int count) override; + +public Q_SLOTS: + void insert(int index, const {{property.type.nested}} &item); + void remove(int index); + void move(int currentIndex, int newIndex); + void reset(); + void update(int index, const {{property.type.nested}} &item); + const {{property.type.nested}} &at(int index) const; private: QList<{{property.type.nested}}> m_list; -- cgit v1.2.3