1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-29 21:21:15 +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

@ -78,11 +78,6 @@ 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);

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

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

@ -1,16 +1,18 @@
#include "notemodel.h"
jop::NoteModel::NoteModel() : QAbstractListModel() {
namespace jop {
NoteModel::NoteModel() : QAbstractListModel() {
folderId_ = "";
orderBy_ = "title";
}
int jop::NoteModel::rowCount(const QModelIndex &parent) const {
int NoteModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
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());
if (!note) return "";
@ -22,7 +24,7 @@ QVariant jop::NoteModel::data(const QModelIndex &index, int role) const {
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 (index < 0 || index >= rowCount()) return NULL;
if (cache_.isset(index)) return cache_.get(index);
@ -48,7 +50,7 @@ jop::Note *jop::NoteModel::atIndex(int index) const {
return cache_.get(index);
}
void jop::NoteModel::setFolderId(const QString &v) {
void NoteModel::setFolderId(const QString &v) {
if (v == folderId_) return;
beginResetModel();
cache_.clear();
@ -56,24 +58,24 @@ void jop::NoteModel::setFolderId(const QString &v) {
endResetModel();
}
jop::Folder jop::NoteModel::folder() const {
Folder NoteModel::folder() const {
Folder folder;
if (folderId_ == "") return folder;
folder.load(folderId_);
return folder;
}
QString jop::NoteModel::indexToId(int index) const {
QString NoteModel::indexToId(int index) const {
Note* note = atIndex(index);
return note->idString();
}
int jop::NoteModel::idToIndex(const QString &id) const {
int NoteModel::idToIndex(const QString &id) const {
Folder f = this->folder();
return f.noteIndexById(orderBy_, id);
}
QHash<int, QByteArray> jop::NoteModel::roleNames() const {
QHash<int, QByteArray> NoteModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
// roles[TitleRole] = "title";
// 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();
}
}

@ -37,6 +37,10 @@ protected:
QHash<int, QByteArray> roleNames() 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: