diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.cpp b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp index 150f6e7d39..6c3819b50e 100755 --- a/QtClient/JoplinQtClient/models/abstractlistmodel.cpp +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp @@ -56,22 +56,17 @@ int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(par // return atIndex(index.row()); //} -BaseModel AbstractListModel::newBaseModel() const { - qFatal("AbstractListModel::baseModel() not implemented"); - return BaseModel(); -} - int AbstractListModel::baseModelCount() const { qFatal("AbstractListModel::baseModelCount() not implemented"); return 0; } -BaseModel AbstractListModel::cacheGet(int index) const { +BaseModel* AbstractListModel::cacheGet(int index) const { qFatal("AbstractListModel::cacheGet() not implemented"); - return BaseModel(); + return NULL; } -void AbstractListModel::cacheSet(int index, const BaseModel &baseModel) { +void AbstractListModel::cacheSet(int index, BaseModel* baseModel) const { qFatal("AbstractListModel::cacheSet() not implemented"); } @@ -80,10 +75,15 @@ bool AbstractListModel::cacheIsset(int index) const { return false; } -void AbstractListModel::cacheClear() { +void AbstractListModel::cacheClear() const { qFatal("AbstractListModel::cacheClear() not implemented"); } +int AbstractListModel::cacheSize() const { + qFatal("AbstractListModel::cacheSize() not implemented"); + return -1; +} + void AbstractListModel::showVirtualItem() { virtualItemShown_ = true; beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1); diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.h b/QtClient/JoplinQtClient/models/abstractlistmodel.h index 31c8f38aeb..567052e1e1 100755 --- a/QtClient/JoplinQtClient/models/abstractlistmodel.h +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.h @@ -26,13 +26,16 @@ public: protected: - virtual BaseModel newBaseModel() const; virtual int baseModelCount() const; - virtual BaseModel cacheGet(int index) const; - virtual void cacheSet(int index, const BaseModel& baseModel); + // All these methods are const because we want to be able to clear the + // cache or set values from any method including const ones. + // http://stackoverflow.com/a/4248661/561309 + virtual BaseModel* cacheGet(int index) const; + virtual void cacheSet(int index, BaseModel* baseModel) const; virtual bool cacheIsset(int index) const; - virtual void cacheClear(); + virtual void cacheClear() const; + virtual int cacheSize() const; private: diff --git a/QtClient/JoplinQtClient/models/foldermodel.cpp b/QtClient/JoplinQtClient/models/foldermodel.cpp index d1bb944621..9f923ccdd8 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.cpp +++ b/QtClient/JoplinQtClient/models/foldermodel.cpp @@ -12,26 +12,6 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") { } QVariant FolderModel::data(const QModelIndex & index, int role) const { - - //QVector> folders; -// std::unique_ptr f(new Folder()); -// f->setValue("title", QString("ancd")); -// BaseModel* b = static_cast(f.get()); -// qDebug() << b->value("title").toString(); -// Folder* f2 = static_cast(b); -// qDebug() << "*" << f2->value("title").toString(); - - -// std::unique_ptr baseModel(f.release()); -// qDebug() << "££££££££££££££££££££££££££££££££" << baseModel->value("title").toString(); -// std::unique_ptr f2(baseModel.release()); -// qDebug() << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << f2->value("title").toString(); - - - //f->setValue("title", "abcd"); - - - Folder* folder = NULL; if (virtualItemShown() && index.row() == rowCount() - 1) { @@ -60,7 +40,7 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r if (role == Qt::EditRole) { folder->setValue("title", value); if (!folder->save()) return false; - cache_.clear(); + cacheClear(); return true; } @@ -69,20 +49,20 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r } Folder* FolderModel::atIndex(int index) const { - if (cache_.size()) { - if (index < 0 || index >= cache_.size()) { + if (cacheSize()) { + if (index < 0 || index >= cacheSize()) { qWarning() << "Invalid folder index:" << index; return NULL; } - return cache_[index].get(); + return (Folder*)cacheGet(index); } - cache_.clear(); + cacheClear(); cache_ = Folder::all(orderBy_); - if (!cache_.size()) { + if (!cacheSize()) { qWarning() << "Invalid folder index:" << index; return NULL; } else { @@ -94,30 +74,30 @@ Folder* FolderModel::atIndex(const QModelIndex &index) const { return atIndex(index.row()); } -BaseModel FolderModel::newBaseModel() const { - return Folder(); -} - int FolderModel::baseModelCount() const { return Folder::count(); } -//BaseModel FolderModel::cacheGet(int index) const { -// return cache_[index]; -//} +BaseModel *FolderModel::cacheGet(int index) const { + return cache_[index].get(); +} -//void FolderModel::cacheSet(int index, const BaseModel &baseModel) { -//// Folder f(baseModel); -//// cache_[index] = f; -//} +void FolderModel::cacheSet(int index, BaseModel* baseModel) const { + Folder* folder = static_cast(baseModel); + cache_[index] = std::unique_ptr(folder); +} -//bool FolderModel::cacheIsset(int index) const { -// return index > 0 && cache_.size() > index; -//} +bool FolderModel::cacheIsset(int index) const { + return index > 0 && cache_.size() > index; +} -//void FolderModel::cacheClear() { -// cache_.clear(); -//} +void FolderModel::cacheClear() const { + cache_.clear(); +} + +int FolderModel::cacheSize() const { + return cache_.size(); +} QString FolderModel::indexToId(int index) const { return data(this->index(index), IdRole).toString(); @@ -160,7 +140,7 @@ void FolderModel::deleteData(const int index) { void FolderModel::dispatcher_folderCreated(const QString &folderId) { qDebug() << "FolderModel Folder created" << folderId; - cache_.clear(); + cacheClear(); int from = 0; int to = rowCount() - 1; @@ -179,7 +159,7 @@ void FolderModel::dispatcher_folderCreated(const QString &folderId) { void FolderModel::dispatcher_folderUpdated(const QString &folderId) { qDebug() << "FolderModel Folder udpated" << folderId; - cache_.clear(); + cacheClear(); QVector roles; roles << Qt::DisplayRole; @@ -192,7 +172,7 @@ void FolderModel::dispatcher_folderDeleted(const QString &folderId) { int index = idToIndex(folderId); if (index < 0) return; - cache_.clear(); + cacheClear(); beginRemoveRows(QModelIndex(), index, index); endRemoveRows(); @@ -200,7 +180,7 @@ void FolderModel::dispatcher_folderDeleted(const QString &folderId) { void FolderModel::dispatcher_allFoldersDeleted() { qDebug() << "FolderModel All folders deleted"; - cache_.clear(); + cacheClear(); beginResetModel(); endResetModel(); } diff --git a/QtClient/JoplinQtClient/models/foldermodel.h b/QtClient/JoplinQtClient/models/foldermodel.h index ffed606947..9cd9110a65 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.h +++ b/QtClient/JoplinQtClient/models/foldermodel.h @@ -25,18 +25,18 @@ public: protected: - BaseModel newBaseModel() const; + //BaseModel newBaseModel() const; int baseModelCount() const; -// virtual BaseModel cacheGet(int index) const; -// virtual void cacheSet(int index, const BaseModel& baseModel); -// virtual bool cacheIsset(int index) const; -// virtual void cacheClear(); + BaseModel* cacheGet(int index) const; + void cacheSet(int index, BaseModel *baseModel) const; + bool cacheIsset(int index) const; + void cacheClear() const; + int cacheSize() const; private: QList folders_; QString orderBy_; - //mutable QVector cache_; mutable std::vector> cache_; QString lastInsertId_;