1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-01 21:24:45 +02:00

Refactoring to make notemodel and foldermodel share the same base class

This commit is contained in:
Laurent Cozic 2017-01-13 22:23:09 +01:00
parent 41d44a3d5c
commit a853eab10b
5 changed files with 34 additions and 24 deletions

View File

@ -78,11 +78,6 @@ void AbstractListModel::cacheClear() const {
qFatal("AbstractListModel::cacheClear() not implemented"); qFatal("AbstractListModel::cacheClear() not implemented");
} }
int AbstractListModel::cacheSize() const {
qFatal("AbstractListModel::cacheSize() not implemented");
return -1;
}
void AbstractListModel::showVirtualItem() { void AbstractListModel::showVirtualItem() {
virtualItemShown_ = true; virtualItemShown_ = true;
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1); beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);

View File

@ -38,7 +38,6 @@ protected:
virtual void cacheSet(int index, BaseModel* baseModel) const; virtual void cacheSet(int index, BaseModel* baseModel) const;
virtual bool cacheIsset(int index) const; virtual bool cacheIsset(int index) const;
virtual void cacheClear() const; virtual void cacheClear() const;
virtual int cacheSize() const;
private: private:

View File

@ -12,8 +12,8 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
} }
BaseModel* FolderModel::atIndex(int index) const { BaseModel* FolderModel::atIndex(int index) const {
if (cacheSize()) { if (cache_.size()) {
if (index < 0 || index >= cacheSize()) { if (index < 0 || index >= cache_.size()) {
qWarning() << "Invalid folder index:" << index; qWarning() << "Invalid folder index:" << index;
return NULL; return NULL;
} }
@ -25,7 +25,7 @@ BaseModel* FolderModel::atIndex(int index) const {
cache_ = Folder::all(orderBy_); cache_ = Folder::all(orderBy_);
if (!cacheSize()) { if (!cache_.size()) {
qWarning() << "Invalid folder index:" << index; qWarning() << "Invalid folder index:" << index;
return NULL; return NULL;
} else { } else {
@ -90,10 +90,6 @@ void FolderModel::cacheClear() const {
cache_.clear(); cache_.clear();
} }
int FolderModel::cacheSize() const {
return cache_.size();
}
// TODO: instead of clearing the whole cache every time, the individual items // TODO: instead of clearing the whole cache every time, the individual items
// could be created/updated/deleted // could be created/updated/deleted

View File

@ -1,16 +1,18 @@
#include "notemodel.h" #include "notemodel.h"
jop::NoteModel::NoteModel() : QAbstractListModel() { namespace jop {
NoteModel::NoteModel() : QAbstractListModel() {
folderId_ = ""; folderId_ = "";
orderBy_ = "title"; orderBy_ = "title";
} }
int jop::NoteModel::rowCount(const QModelIndex &parent) const { int NoteModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent); Q_UNUSED(parent);
return folder().noteCount(); return folder().noteCount();
} }
QVariant jop::NoteModel::data(const QModelIndex &index, int role) const { QVariant NoteModel::data(const QModelIndex &index, int role) const {
Note* note = atIndex(index.row()); Note* note = atIndex(index.row());
if (!note) return ""; if (!note) return "";
@ -22,7 +24,7 @@ QVariant jop::NoteModel::data(const QModelIndex &index, int role) const {
return QVariant(note->value("title").toString()); return QVariant(note->value("title").toString());
} }
jop::Note *jop::NoteModel::atIndex(int index) const { Note *NoteModel::atIndex(int index) const {
if (folderId_ == "") return NULL; if (folderId_ == "") return NULL;
if (index < 0 || index >= rowCount()) return NULL; if (index < 0 || index >= rowCount()) return NULL;
if (cache_.isset(index)) return cache_.get(index); if (cache_.isset(index)) return cache_.get(index);
@ -48,7 +50,7 @@ jop::Note *jop::NoteModel::atIndex(int index) const {
return cache_.get(index); return cache_.get(index);
} }
void jop::NoteModel::setFolderId(const QString &v) { void NoteModel::setFolderId(const QString &v) {
if (v == folderId_) return; if (v == folderId_) return;
beginResetModel(); beginResetModel();
cache_.clear(); cache_.clear();
@ -56,24 +58,24 @@ void jop::NoteModel::setFolderId(const QString &v) {
endResetModel(); endResetModel();
} }
jop::Folder jop::NoteModel::folder() const { Folder NoteModel::folder() const {
Folder folder; Folder folder;
if (folderId_ == "") return folder; if (folderId_ == "") return folder;
folder.load(folderId_); folder.load(folderId_);
return folder; return folder;
} }
QString jop::NoteModel::indexToId(int index) const { QString NoteModel::indexToId(int index) const {
Note* note = atIndex(index); Note* note = atIndex(index);
return note->idString(); return note->idString();
} }
int jop::NoteModel::idToIndex(const QString &id) const { int NoteModel::idToIndex(const QString &id) const {
Folder f = this->folder(); Folder f = this->folder();
return f.noteIndexById(orderBy_, id); return f.noteIndexById(orderBy_, id);
} }
QHash<int, QByteArray> jop::NoteModel::roleNames() const { QHash<int, QByteArray> NoteModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
// roles[TitleRole] = "title"; // roles[TitleRole] = "title";
// roles[UuidRole] = "uuid"; // roles[UuidRole] = "uuid";
@ -81,6 +83,20 @@ QHash<int, QByteArray> jop::NoteModel::roleNames() const {
} }
//int jop::NoteModel::baseModelCount() const { BaseModel* NoteModel::cacheGet(int index) const {
return (BaseModel*)cache_.get(index);
}
//} void NoteModel::cacheSet(int index, BaseModel *baseModel) const {
cache_.set(index, baseModel);
}
bool NoteModel::cacheIsset(int index) const {
return cache_.isset(index);
}
void NoteModel::cacheClear() const {
cache_.clear();
}
}

View File

@ -37,6 +37,10 @@ protected:
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
int baseModelCount() const; int baseModelCount() const;
BaseModel* cacheGet(int index) const;
void cacheSet(int index, BaseModel *baseModel) const;
bool cacheIsset(int index) const;
void cacheClear() const;
private: private: