Implemented setData() and setItemData() functions of the model for later use

This commit is contained in:
Scharel Clemens 2020-01-12 15:46:54 +01:00
parent 21cccf2f6e
commit 73adff15ca
3 changed files with 99 additions and 2 deletions

View file

@ -34,8 +34,10 @@ NotesApi::~NotesApi() {
disconnect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
disconnect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>)));
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) {

View file

@ -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<int>{ IdRole });
}
break;
}
case ModifiedRole: {
double modified = value.toDouble(&retval);
if (retval) {
m_notes[index.row()].setModified(modified);
emit dataChanged(index, index, QVector<int>{ ModifiedRole });
}
break;
}
case TitleRole: {
QString title = value.toString();
if (!title.isEmpty()) {
m_notes[index.row()].setTitle(title);
emit dataChanged(index, index, QVector<int>{ TitleRole });
retval = true;
}
break;
}
case CategoryRole: {
QString category = value.toString();
if (!category.isEmpty()) {
m_notes[index.row()].setCategory(category);
emit dataChanged(index, index, QVector<int>{ CategoryRole });
retval = true;
}
break;
}
case ContentRole: {
QString content = value.toString();
if (!content.isEmpty()) {
m_notes[index.row()].setContent(content);
emit dataChanged(index, index, QVector<int>{ ContentRole });
retval = true;
}
break;
}
case FavoriteRole: {
bool favorite = value.toBool();
m_notes[index.row()].setFavorite(favorite);
emit dataChanged(index, index, QVector<int>{ FavoriteRole });
retval = true;
break;
}
case EtagRole: {
QString etag = value.toString();
if (!etag.isEmpty()) {
m_notes[index.row()].setEtag(etag);
emit dataChanged(index, index, QVector<int>{ EtagRole });
retval = true;
}
break;
}
case ErrorRole: {
bool error = value.toBool();
m_notes[index.row()].setError(error);
emit dataChanged(index, index, QVector<int>{ ErrorRole });
retval = true;
break;
}
case ErrorMessageRole: {
QString errorMessage = value.toString();
if (!errorMessage.isEmpty()) {
m_notes[index.row()].setErrorMessage(errorMessage);
emit dataChanged(index, index, QVector<int>{ ErrorMessageRole });
retval = true;
}
break;
} }
}
return retval;
}
QMap<int, QVariant> NotesModel::itemData(const QModelIndex &index) const {
QMap<int, QVariant> map;
if (!index.isValid()) return map;
@ -205,3 +288,13 @@ QMap<int, QVariant> NotesModel::itemData(const QModelIndex &index) const {
}
return map;
}
bool NotesModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) {
bool retval = true;
QMapIterator<int, QVariant> role(roles);
while (role.hasNext()) {
role.next();
retval &= setData(index, role.value(), role.key());
}
return retval;
}

View file

@ -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<int, QVariant> itemData(const QModelIndex &index) const;
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
protected:
//void addNote(const QJsonValue &note);