From 73adff15ca36b9c7e4d3e39ee0631978eb9d616a Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Sun, 12 Jan 2020 15:46:54 +0100 Subject: [PATCH] Implemented setData() and setItemData() functions of the model for later use --- src/notesapi.cpp | 6 ++- src/notesmodel.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++ src/notesmodel.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/notesapi.cpp b/src/notesapi.cpp index ea08632..7e1632f 100644 --- a/src/notesapi.cpp +++ b/src/notesapi.cpp @@ -34,8 +34,10 @@ NotesApi::~NotesApi() { disconnect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); disconnect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList)), this, SLOT(sslError(QNetworkReply*,QList))); m_jsonFile.close(); - delete mp_modelProxy; - delete mp_model; + if (mp_modelProxy) + delete mp_modelProxy; + if (mp_model) + delete mp_model; } void NotesApi::setSslVerify(bool verify) { diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 131f396..612ca93 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -195,6 +195,89 @@ QVariant NotesModel::data(const QModelIndex &index, int role) const { return QVariant(); } +bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role) { + bool retval = false; + if (index.isValid()) { + switch (role) { + case IdRole: { + double id = value.toDouble(&retval); + if (retval) { + m_notes[index.row()].setId(id); + emit dataChanged(index, index, QVector{ IdRole }); + } + break; + } + case ModifiedRole: { + double modified = value.toDouble(&retval); + if (retval) { + m_notes[index.row()].setModified(modified); + emit dataChanged(index, index, QVector{ ModifiedRole }); + } + break; + } + case TitleRole: { + QString title = value.toString(); + if (!title.isEmpty()) { + m_notes[index.row()].setTitle(title); + emit dataChanged(index, index, QVector{ TitleRole }); + retval = true; + } + break; + } + case CategoryRole: { + QString category = value.toString(); + if (!category.isEmpty()) { + m_notes[index.row()].setCategory(category); + emit dataChanged(index, index, QVector{ CategoryRole }); + retval = true; + } + break; + } + case ContentRole: { + QString content = value.toString(); + if (!content.isEmpty()) { + m_notes[index.row()].setContent(content); + emit dataChanged(index, index, QVector{ ContentRole }); + retval = true; + } + break; + } + case FavoriteRole: { + bool favorite = value.toBool(); + m_notes[index.row()].setFavorite(favorite); + emit dataChanged(index, index, QVector{ FavoriteRole }); + retval = true; + break; + } + case EtagRole: { + QString etag = value.toString(); + if (!etag.isEmpty()) { + m_notes[index.row()].setEtag(etag); + emit dataChanged(index, index, QVector{ EtagRole }); + retval = true; + } + break; + } + case ErrorRole: { + bool error = value.toBool(); + m_notes[index.row()].setError(error); + emit dataChanged(index, index, QVector{ ErrorRole }); + retval = true; + break; + } + case ErrorMessageRole: { + QString errorMessage = value.toString(); + if (!errorMessage.isEmpty()) { + m_notes[index.row()].setErrorMessage(errorMessage); + emit dataChanged(index, index, QVector{ ErrorMessageRole }); + retval = true; + } + break; + } } + } + return retval; +} + QMap NotesModel::itemData(const QModelIndex &index) const { QMap map; if (!index.isValid()) return map; @@ -205,3 +288,13 @@ QMap NotesModel::itemData(const QModelIndex &index) const { } return map; } + +bool NotesModel::setItemData(const QModelIndex &index, const QMap &roles) { + bool retval = true; + QMapIterator role(roles); + while (role.hasNext()) { + role.next(); + retval &= setData(index, role.value(), role.key()); + } + return retval; +} diff --git a/src/notesmodel.h b/src/notesmodel.h index 927d7c8..62eb5d8 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -63,7 +63,9 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual QVariant data(const QModelIndex &index, int role) const; + virtual bool setData(const QModelIndex &index, const QVariant &value, int role); QMap itemData(const QModelIndex &index) const; + virtual bool setItemData(const QModelIndex &index, const QMap &roles); protected: //void addNote(const QJsonValue ¬e);