Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement setData for QSListModel #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QSTableModel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "qstablemodel.h"
94 changes: 94 additions & 0 deletions checkindex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* MIT License

Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// This is borrowed from https://github.com/KDAB/kdabtv/tree/master/Qt-Widgets-and-more/CheckIndex
// as i did not yet find a possibility to add it as "external" this stays a copy. :-(

#pragma once

#define CHECK_rowCount(index) Q_ASSERT(checkIndex(index))

#define CHECK_columnCount(index) Q_ASSERT(checkIndex(index))

#define CHECK_data(index) \
if (qobject_cast<const QAbstractTableModel *>(this) \
|| qobject_cast<const QAbstractListModel *>(this)) \
Q_ASSERT(checkIndex(index, \
QAbstractItemModel::CheckIndexOption::IndexIsValid \
| QAbstractItemModel::CheckIndexOption::ParentIsInvalid)); \
else \
Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid))

#define CHECK_setData(index) CHECK_data(index)

#define CHECK_headerData(section, orientation) \
Q_ASSERT(section >= 0); \
if (orientation == Qt::Horizontal) \
Q_ASSERT(section < columnCount({})); \
else \
Q_ASSERT(section < rowCount({}));

#define CHECK_setHeaderData(section, orientation) CHECK_headerData(section, orientation)

#define CHECK_flags(index) Q_ASSERT(checkIndex(index))

// If you get an assert for row or column being zero and rowCount() and columnCount() is zero too,
// then there is a chance that it is a subclass of QAbstractProxyModel::headerData making this out
// of bound call. The cure is to implement headerData in the subclass of QAbstractProxyModel, and
// while doing so /not/ use mapToSource for finding the row or column in the source model.
#define CHECK_index(row, column, parent) \
Q_ASSERT(row >= 0); \
Q_ASSERT(column >= 0); \
Q_ASSERT(row < rowCount(parent)); \
Q_ASSERT(column < columnCount(parent)); \
Q_ASSERT(checkIndex(parent));

#define CHECK_parent(parent) \
Q_ASSERT(checkIndex(parent, QAbstractItemModel::CheckIndexOption::DoNotUseParent));

#define CHECK_insertRows(row, count, parent) \
Q_ASSERT(checkIndex(parent)); \
Q_ASSERT(row >= 0); \
Q_ASSERT(row <= rowCount(parent)); \
Q_ASSERT(count > 0)

// Technically it might be OK to call removeRows with count == 0, so you might
// consider taking that out and making it a check in your code instead.
#define CHECK_removeRows(row, count, parent) \
Q_ASSERT(checkIndex(parent)); \
Q_ASSERT(row >= 0); \
Q_ASSERT(count > 0); \
Q_ASSERT(row <= rowCount(parent) - count)

#define CHECK_insertColumns(column, count, parent) \
Q_ASSERT(checkIndex(parent)); \
Q_ASSERT(column >= 0); \
Q_ASSERT(column <= columnCount(parent)); \
Q_ASSERT(count > 0)

// Same as removeRows above.
#define CHECK_removeColumns(column, count, parent) \
Q_ASSERT(checkIndex(parent)); \
Q_ASSERT(column >= 0); \
Q_ASSERT(count > 0); \
Q_ASSERT(column <= columnCount(parent) - count)
55 changes: 54 additions & 1 deletion qsjsonlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ It will emit signals of insertion, removal and moving automatically.
QSJsonListModel::QSJsonListModel(QObject *parent) : QSListModel(parent)
{
componentCompleted = false;
connect(this, &QAbstractItemModel::rowsInserted, this, &QSJsonListModel::handleRowsInserted);
connect(this, &QAbstractItemModel::rowsMoved, this, &QSJsonListModel::handleRowsMoved);
connect(this, &QAbstractItemModel::rowsRemoved, this, &QSJsonListModel::handleRowsRemoved);
connect(this, &QAbstractItemModel::dataChanged, this, &QSJsonListModel::handleDataChanged);
}

/*! \qmlproperty string JsonListModel::keyField
Expand Down Expand Up @@ -129,7 +133,6 @@ void QSJsonListModel::setSource(const QVariantList &source)
}

emit sourceChanged();

}

/*! \qmlproperty array JsonListModel::fields
Expand Down Expand Up @@ -165,6 +168,25 @@ void QSJsonListModel::setFields(const QStringList &roleNames)
emit fieldsChanged();
}

/*! \qmlproperty array JsonListModel::synchronizeModelChangesToSource

Propagate the changes in the Model back to the source property
Default is false.
*/

bool QSJsonListModel::synchronizeModelChangesToSource() const
{
return m_synchronizeModelChangesToSource;
}

void QSJsonListModel::setSynchronizeModelChangesToSource(bool synchronizeModelChangesToSource)
{
if (m_synchronizeModelChangesToSource == synchronizeModelChangesToSource)
return;
m_synchronizeModelChangesToSource = synchronizeModelChangesToSource;
emit synchronizeModelChangesToSourceChanged();
}

void QSJsonListModel::classBegin()
{

Expand All @@ -179,6 +201,30 @@ void QSJsonListModel::componentComplete()
}
}

void QSJsonListModel::handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
if (synchronizeModelChangesToSource())
syncStorageToSource();
}

void QSJsonListModel::handleRowsInserted(const QModelIndex &parent, int first, int last)
{
if (synchronizeModelChangesToSource())
syncStorageToSource();
}

void QSJsonListModel::handleRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
{
if (synchronizeModelChangesToSource())
syncStorageToSource();
}

void QSJsonListModel::handleRowsRemoved(const QModelIndex &parent, int first, int last)
{
if (synchronizeModelChangesToSource())
syncStorageToSource();
}

void QSJsonListModel::sync()
{
QSDiffRunner runner;
Expand All @@ -190,3 +236,10 @@ void QSJsonListModel::sync()
runner.patch(this, patches);
}
}

void QSJsonListModel::syncStorageToSource()
{
// don't call setSource as this invokes a unneccessary sync
m_source = storage();
emit sourceChanged();
}
29 changes: 16 additions & 13 deletions qsjsonlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,48 @@ class QSJsonListModel : public QSListModel, public QQmlParserStatus
Q_PROPERTY(QString keyField READ keyField WRITE setKeyField NOTIFY keyFieldChanged)
Q_PROPERTY(QVariantList source READ source WRITE setSource NOTIFY sourceChanged)
Q_PROPERTY(QStringList fields READ fields WRITE setFields NOTIFY fieldsChanged)
Q_PROPERTY(bool synchronizeModelChangesToSource READ synchronizeModelChangesToSource WRITE setSynchronizeModelChangesToSource NOTIFY synchronizeModelChangesToSourceChanged)

Q_INTERFACES(QQmlParserStatus)

public:
explicit QSJsonListModel(QObject *parent = 0);

QString keyField() const;

void setKeyField(const QString &keyField);

QVariantList source() const;

void setSource(const QVariantList &source);

QStringList fields() const;

void setFields(const QStringList &roleNames);

signals:
bool synchronizeModelChangesToSource() const;
void setSynchronizeModelChangesToSource(bool synchronizeModelChangesToSource);

signals:
void keyFieldChanged();

void sourceChanged();

void fieldsChanged();

public slots:
void synchronizeModelChangesToSourceChanged();

protected:
virtual void classBegin();
virtual void componentComplete();
virtual void classBegin() override;
virtual void componentComplete() override;

private slots:
void handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
void handleRowsInserted(const QModelIndex &parent, int first, int last);
void handleRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row);
void handleRowsRemoved(const QModelIndex &parent, int first, int last);

private:
void sync();
void syncStorageToSource();

QString m_keyField;

QVariantList m_source;

QStringList m_fields;

bool m_synchronizeModelChangesToSource = false;
bool componentCompleted;
};
34 changes: 32 additions & 2 deletions qslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ QSListModel::QSListModel(QObject *parent) :

int QSListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Q_UNUSED(parent)
return count();
}

Expand All @@ -83,6 +83,36 @@ QVariant QSListModel::data(const QModelIndex &index, int role) const
return v;
}

/*! \fn bool QSListModel::setData(const QModelIndex &index, const QVariant &value, int role)

Sets the role data for the item at index to value.
Returns true if successful; otherwise returns false.
The dataChanged() signal should be emitted if the data was successfully set.
*/
bool QSListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() < 0 || index.row() >= m_storage.size())
return false;

if (!m_roles.contains(role))
{
return false;
}

auto item = m_storage[index.row()].toMap();

if(item[m_roles[role]] == value)
{
// value was already correct, no need to update / emit change signal.
return true;
}

item[m_roles[role]] = value;
m_storage[index.row()] = item;
emit dataChanged(index, index, {role});
return true;
}

/*! \fn void QSListModel::append(const QVariantMap &value)

Append an items at the end of list
Expand Down Expand Up @@ -329,7 +359,7 @@ void QSListModel::set(int idx, QVariantMap data)
while (iter.hasNext()) {
iter.next();
if (!original.contains(iter.key()) ||
original[iter.key()] != iter.value()) {
original[iter.key()] != iter.value()) {

if (m_rolesLookup.contains(iter.key())) {
roles << m_rolesLookup[iter.key()];
Expand Down
22 changes: 11 additions & 11 deletions qslistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class QSListModel : public QAbstractListModel, public QSPatchable
Q_PROPERTY(int count READ count NOTIFY countChanged)

public:
explicit QSListModel(QObject *parent = 0);
explicit QSListModel(QObject *parent = nullptr);

int rowCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent) const override;

QVariant data(const QModelIndex &index, int role) const;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;

int count() const;


QHash<int, QByteArray> roleNames() const;
QHash<int, QByteArray> roleNames() const override;

void setRoleNames(const QVariantMap& value);

Expand All @@ -38,7 +38,7 @@ class QSListModel : public QAbstractListModel, public QSPatchable

QVariantList storage() const;

virtual void insert(int index, const QVariantList &value);
virtual void insert(int index, const QVariantList &value) override;


public slots:
Expand All @@ -49,23 +49,23 @@ public slots:

void clear();

virtual void move(int from, int to, int count = 1);
virtual void move(int from, int to, int count = 1) override;

virtual void remove(int i , int count = 1);
virtual void remove(int i , int count = 1) override;

int indexOf(QString field,QVariant value) const;

QVariantMap get(int i) const;

void setProperty(int index,QString property ,QVariant value);

void set(int index,QVariantMap data);
void set(int index,QVariantMap data) override;



signals:
void countChanged();

public slots:

private:

QHash<int, QByteArray> m_roles;
Expand Down
Loading