From 8f2250199efc2d43f584d11dc220742bc2092d6a Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Sun, 17 Jun 2018 15:18:14 +0200 Subject: [PATCH] model cleanups --- src/imports/mvvmquick/settingsentrymodel.cpp | 26 +++++++++++++++-- .../mvvmquick/settingssectionmodel.cpp | 11 +++++++- src/mvvmdatasynccore/accountmodel.cpp | 20 ++++++++++++- src/mvvmdatasynccore/exchangedevicesmodel.cpp | 28 +++++++++++++++---- 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/imports/mvvmquick/settingsentrymodel.cpp b/src/imports/mvvmquick/settingsentrymodel.cpp index 0bb8df3..b322ce6 100644 --- a/src/imports/mvvmquick/settingsentrymodel.cpp +++ b/src/imports/mvvmquick/settingsentrymodel.cpp @@ -36,6 +36,10 @@ void SettingsEntryModel::setup(const SettingsElements::Section §ion, Setting int SettingsEntryModel::rowCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -44,8 +48,13 @@ int SettingsEntryModel::rowCount(const QModelIndex &parent) const QVariant SettingsEntryModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) - return QVariant(); +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return {}; +#else + if(!index.isValid()) + return {}; +#endif auto entry = _entries.value(index.row()); switch (role) { @@ -75,7 +84,14 @@ QVariant SettingsEntryModel::data(const QModelIndex &index, int role) const bool SettingsEntryModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (!index.isValid() || role != SettingsValueRole) +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return false; +#else + if(!index.isValid()) + return false; +#endif + if(role != SettingsValueRole) return false; _viewModel->saveValue(_entries.value(index.row()).key, value); @@ -99,6 +115,10 @@ QHash SettingsEntryModel::roleNames() const Qt::ItemFlags SettingsEntryModel::flags(const QModelIndex &index) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return Qt::NoItemFlags; +#endif return QAbstractListModel::flags(index) | Qt::ItemIsEditable; } diff --git a/src/imports/mvvmquick/settingssectionmodel.cpp b/src/imports/mvvmquick/settingssectionmodel.cpp index a6f11fd..96d47b8 100644 --- a/src/imports/mvvmquick/settingssectionmodel.cpp +++ b/src/imports/mvvmquick/settingssectionmodel.cpp @@ -35,6 +35,10 @@ void SettingsSectionModel::setup(const SettingsElements::Setup &setup) int SettingsSectionModel::rowCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -43,8 +47,13 @@ int SettingsSectionModel::rowCount(const QModelIndex &parent) const QVariant SettingsSectionModel::data(const QModelIndex &index, int role) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return {}; +#else if (!index.isValid()) - return QVariant(); + return {}; +#endif switch (role) { case Qt::DisplayRole: diff --git a/src/mvvmdatasynccore/accountmodel.cpp b/src/mvvmdatasynccore/accountmodel.cpp index 2aa8962..2d90877 100644 --- a/src/mvvmdatasynccore/accountmodel.cpp +++ b/src/mvvmdatasynccore/accountmodel.cpp @@ -80,6 +80,10 @@ QVariant AccountModel::headerData(int section, Qt::Orientation orientation, int int AccountModel::rowCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -88,6 +92,10 @@ int AccountModel::rowCount(const QModelIndex &parent) const int AccountModel::columnCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -96,8 +104,13 @@ int AccountModel::columnCount(const QModelIndex &parent) const QVariant AccountModel::data(const QModelIndex &index, int role) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return {}; +#else if (!index.isValid()) - return QVariant(); + return {}; +#endif switch (index.column()) { case 0: @@ -131,8 +144,13 @@ QHash AccountModel::roleNames() const bool AccountModel::removeDevice(const QModelIndex &index) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return false; +#else if (!index.isValid()) return false; +#endif else { d->accountManager->removeDevice(d->devices.value(index.row())); return true; diff --git a/src/mvvmdatasynccore/exchangedevicesmodel.cpp b/src/mvvmdatasynccore/exchangedevicesmodel.cpp index 3717c64..1baaa6e 100644 --- a/src/mvvmdatasynccore/exchangedevicesmodel.cpp +++ b/src/mvvmdatasynccore/exchangedevicesmodel.cpp @@ -44,10 +44,15 @@ void ExchangeDevicesModel::setup(QtDataSync::UserExchangeManager *exchangeManage UserInfo ExchangeDevicesModel::infoAt(const QModelIndex &index) const { - if(index.isValid()) - return d->devices.value(index.row()); - else +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return {}; +#else + if(!index.isValid()) return {}; +#endif + else + return d->devices.value(index.row()); } QVariant ExchangeDevicesModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -83,6 +88,10 @@ QVariant ExchangeDevicesModel::headerData(int section, Qt::Orientation orientati int ExchangeDevicesModel::rowCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -91,6 +100,10 @@ int ExchangeDevicesModel::rowCount(const QModelIndex &parent) const int ExchangeDevicesModel::columnCount(const QModelIndex &parent) const { +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(parent, QAbstractItemModel::CheckIndexOption::ParentIsInvalid)) + return 0; +#endif if (parent.isValid()) return 0; else @@ -99,8 +112,13 @@ int ExchangeDevicesModel::columnCount(const QModelIndex &parent) const QVariant ExchangeDevicesModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) - return QVariant(); +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + if(!checkIndex(index, QAbstractItemModel::CheckIndexOption::ParentIsInvalid | QAbstractItemModel::CheckIndexOption::IndexIsValid)) + return {}; +#else + if(!index.isValid()) + return {}; +#endif switch (index.column()) { case 0: