From 31ded8c6a9abdbb890d3aec49be1ce3d11d52167 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 13 Jan 2017 10:36:27 +0100 Subject: [PATCH] refactoring to use vector of smart pointers --- QtClient/JoplinQtClient/MainPage.qml | 2 +- .../models/abstractlistmodel.cpp | 71 ++++++----- .../JoplinQtClient/models/abstractlistmodel.h | 7 +- .../JoplinQtClient/models/foldermodel.cpp | 111 ++++++------------ QtClient/JoplinQtClient/models/foldermodel.h | 8 +- 5 files changed, 77 insertions(+), 122 deletions(-) diff --git a/QtClient/JoplinQtClient/MainPage.qml b/QtClient/JoplinQtClient/MainPage.qml index 5acc4f091..a969c0e56 100755 --- a/QtClient/JoplinQtClient/MainPage.qml +++ b/QtClient/JoplinQtClient/MainPage.qml @@ -16,7 +16,7 @@ Item { list.model.addData(text) list.selectItemById(list.model.lastInsertId()); } else { - list.model.setData(index, text) + list.model.setTitle(index, text) } } diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.cpp b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp index 6c3819b50..d54b8028e 100755 --- a/QtClient/JoplinQtClient/models/abstractlistmodel.cpp +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp @@ -10,51 +10,50 @@ int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(par return baseModelCount() + (virtualItemShown() ? 1 : 0); } -//QVariant AbstractListModel::data(const QModelIndex & index, int role) const { -// BaseModel model = baseModel(); +QVariant AbstractListModel::data(const QModelIndex & index, int role) const { + BaseModel* model = NULL; -// if (virtualItemShown() && index.row() == rowCount() - 1) { -// model.setValue("title", BaseModel::Value(QString("Untitled"))); -// } else { -// model = atIndex(index.row()); -// } + if (virtualItemShown() && index.row() == rowCount() - 1) { + if (role == Qt::DisplayRole) return "Untitled"; + return ""; + } else { + model = atIndex(index.row()); + } -// if (role == Qt::DisplayRole) { -// return model.value("title").toQVariant(); -// } + if (role == Qt::DisplayRole) { + return model->value("title").toQVariant(); + } -// if (role == IdRole) { -// return model.id().toQVariant(); -// } + if (role == IdRole) { + return model->id().toQVariant(); + } -// return QVariant(); -//} + return QVariant(); +} -//BaseModel AbstractListModel::atIndex(int index) const { -// if (cache_.size()) { -// if (index < 0 || index >= cache_.size()) { -// qWarning() << "Invalid folder index:" << index; -// return Folder(); -// } +BaseModel* AbstractListModel::atIndex(int index) const { + qFatal("AbstractListModel::atIndex() not implemented"); + return NULL; +} -// return cache_[index]; -// } +BaseModel* AbstractListModel::atIndex(const QModelIndex &index) const { + return atIndex(index.row()); +} -// cache_.clear(); +bool AbstractListModel::setData(const QModelIndex &index, const QVariant &value, int role) { + BaseModel* model = atIndex(index.row()); + if (!model) return false; -// cache_ = Folder::all(orderBy_); + if (role == Qt::EditRole) { + model->setValue("title", value); + if (!model->save()) return false; + cacheClear(); + return true; + } -// if (!cache_.size()) { -// qWarning() << "Invalid folder index:" << index; -// return Folder(); -// } else { -// return atIndex(index); -// } -//} - -//BaseModel AbstractListModel::atIndex(const QModelIndex &index) const { -// return atIndex(index.row()); -//} + qWarning() << "Unsupported role" << role; + return false; +} int AbstractListModel::baseModelCount() const { qFatal("AbstractListModel::baseModelCount() not implemented"); diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.h b/QtClient/JoplinQtClient/models/abstractlistmodel.h index 567052e1e..a8d9a5bf3 100755 --- a/QtClient/JoplinQtClient/models/abstractlistmodel.h +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.h @@ -20,9 +20,10 @@ public: AbstractListModel(); int rowCount(const QModelIndex & parent = QModelIndex()) const; - //QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; -// BaseModel atIndex(int index) const; -// BaseModel atIndex(const QModelIndex &index) const; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + virtual BaseModel* atIndex(int index) const; + BaseModel* atIndex(const QModelIndex &index) const; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); protected: diff --git a/QtClient/JoplinQtClient/models/foldermodel.cpp b/QtClient/JoplinQtClient/models/foldermodel.cpp index 9f923ccdd..106a37ecd 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.cpp +++ b/QtClient/JoplinQtClient/models/foldermodel.cpp @@ -11,51 +11,14 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") { connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted())); } -QVariant FolderModel::data(const QModelIndex & index, int role) const { - Folder* folder = NULL; - - if (virtualItemShown() && index.row() == rowCount() - 1) { - if (role == Qt::DisplayRole) return "Untitled"; - return ""; - //folder->setValue("title", BaseModel::Value(QString("Untitled"))); - } else { - folder = atIndex(index.row()); - } - - if (role == Qt::DisplayRole) { - return folder->value("title").toQVariant(); - } - - if (role == IdRole) { - return folder->id().toQVariant(); - } - - return QVariant(); -} - -bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role) { - Folder* folder = atIndex(index.row()); - if (!folder) return false; - - if (role == Qt::EditRole) { - folder->setValue("title", value); - if (!folder->save()) return false; - cacheClear(); - return true; - } - - qWarning() << "Unsupported role" << role; - return false; -} - -Folder* FolderModel::atIndex(int index) const { +BaseModel* FolderModel::atIndex(int index) const { if (cacheSize()) { if (index < 0 || index >= cacheSize()) { qWarning() << "Invalid folder index:" << index; return NULL; } - return (Folder*)cacheGet(index); + return cacheGet(index); } cacheClear(); @@ -70,8 +33,39 @@ Folder* FolderModel::atIndex(int index) const { } } -Folder* FolderModel::atIndex(const QModelIndex &index) const { - return atIndex(index.row()); +QString FolderModel::indexToId(int index) const { + return data(this->index(index), IdRole).toString(); +} + +int FolderModel::idToIndex(const QString &id) const { + int count = this->rowCount(); + for (int i = 0; i < count; i++) { + Folder* folder = (Folder*)atIndex(i); + if (folder->idString() == id) return i; + } + return -1; +} + +QString FolderModel::lastInsertId() const { + return lastInsertId_; +} + +bool FolderModel::setTitle(int index, const QVariant &value, int role) { + return setData(this->index(index), value, role); +} + +void FolderModel::addData(const QString &title) { + Folder folder; + folder.setValue("title", title); + if (!folder.save()) return; + + lastInsertId_ = folder.id().toString(); +} + +void FolderModel::deleteData(const int index) { + Folder* folder = (Folder*)atIndex(index); + if (!folder) return; + folder->dispose(); } int FolderModel::baseModelCount() const { @@ -99,41 +93,6 @@ int FolderModel::cacheSize() const { return cache_.size(); } -QString FolderModel::indexToId(int index) const { - return data(this->index(index), IdRole).toString(); -} - -int FolderModel::idToIndex(const QString &id) const { - int count = this->rowCount(); - for (int i = 0; i < count; i++) { - Folder* folder = atIndex(i); - if (folder->idString() == id) return i; - } - return -1; -} - -QString FolderModel::lastInsertId() const { - return lastInsertId_; -} - -bool FolderModel::setData(int index, const QVariant &value, int role) { - return setData(this->index(index), value, role); -} - -void FolderModel::addData(const QString &title) { - Folder folder; - folder.setValue("title", title); - if (!folder.save()) return; - - lastInsertId_ = folder.id().toString(); -} - -void FolderModel::deleteData(const int index) { - Folder* folder = atIndex(index); - if (!folder) return; - folder->dispose(); -} - // TODO: instead of clearing the whole cache every time, the individual items // could be created/updated/deleted diff --git a/QtClient/JoplinQtClient/models/foldermodel.h b/QtClient/JoplinQtClient/models/foldermodel.h index 9cd9110a6..6ca41ac17 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.h +++ b/QtClient/JoplinQtClient/models/foldermodel.h @@ -18,14 +18,10 @@ public: FolderModel(); void addFolder(Folder* folder); - QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); - Folder* atIndex(int index) const; - Folder* atIndex(const QModelIndex &index) const; + BaseModel* atIndex(int index) const; protected: - //BaseModel newBaseModel() const; int baseModelCount() const; BaseModel* cacheGet(int index) const; void cacheSet(int index, BaseModel *baseModel) const; @@ -45,7 +41,7 @@ public slots: void addData(const QString& title); void deleteData(const int index); - bool setData(int index, const QVariant &value, int role = Qt::EditRole); + bool setTitle(int index, const QVariant &value, int role = Qt::EditRole); QString indexToId(int index) const; int idToIndex(const QString& id) const; QString lastInsertId() const;