mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-24 08:12:24 +02:00
Refactoring to make cache read-only
This commit is contained in:
parent
b3e5b4f091
commit
2298c8fb5c
@ -11,7 +11,7 @@ int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(par
|
||||
}
|
||||
|
||||
QVariant AbstractListModel::data(const QModelIndex & index, int role) const {
|
||||
BaseModel* model = NULL;
|
||||
const BaseModel* model = NULL;
|
||||
|
||||
if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||
if (role == Qt::DisplayRole) return "Untitled";
|
||||
@ -31,25 +31,27 @@ QVariant AbstractListModel::data(const QModelIndex & index, int role) const {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
BaseModel* AbstractListModel::atIndex(int index) const {
|
||||
const BaseModel *AbstractListModel::atIndex(int index) const {
|
||||
qFatal("AbstractListModel::atIndex() not implemented");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BaseModel* AbstractListModel::atIndex(const QModelIndex &index) const {
|
||||
const BaseModel* AbstractListModel::atIndex(const QModelIndex &index) const {
|
||||
return atIndex(index.row());
|
||||
}
|
||||
|
||||
bool AbstractListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
||||
BaseModel* model = atIndex(index.row());
|
||||
const BaseModel* model = atIndex(index.row());
|
||||
if (!model) return false;
|
||||
|
||||
if (role == TitleRole) {
|
||||
BaseModel temp;
|
||||
temp.clone(*model);
|
||||
qDebug() << model->value("title").toString();
|
||||
|
||||
temp.setValue("title", value.toString());
|
||||
if (!temp.save()) return false;
|
||||
cacheClear();
|
||||
return true;
|
||||
|
||||
// model->setValue("title", value.toString());
|
||||
// if (!model->save()) return false;
|
||||
// cacheClear();
|
||||
@ -69,7 +71,7 @@ int AbstractListModel::baseModelCount() const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BaseModel* AbstractListModel::cacheGet(int index) const {
|
||||
const BaseModel *AbstractListModel::cacheGet(int index) const {
|
||||
qFatal("AbstractListModel::cacheGet() not implemented");
|
||||
return NULL;
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ public:
|
||||
AbstractListModel();
|
||||
int rowCount(const QModelIndex & parent = QModelIndex()) const;
|
||||
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||
virtual BaseModel* atIndex(int index) const;
|
||||
BaseModel* atIndex(const QModelIndex &index) const;
|
||||
virtual const BaseModel* atIndex(int index) const;
|
||||
const BaseModel* atIndex(const QModelIndex &index) const;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
||||
|
||||
protected:
|
||||
@ -33,7 +33,7 @@ protected:
|
||||
// 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 const BaseModel* cacheGet(int index) const;
|
||||
virtual void cacheSet(int index, BaseModel* baseModel) const;
|
||||
virtual bool cacheIsset(int index) const;
|
||||
virtual void cacheClear() const;
|
||||
|
@ -10,9 +10,7 @@ using namespace jop;
|
||||
QMap<int, QVector<BaseModel::Field>> BaseModel::tableFields_;
|
||||
QHash<QString, QVariant> BaseModel::cache_;
|
||||
|
||||
BaseModel::BaseModel() {
|
||||
isNew_ = -1;
|
||||
}
|
||||
BaseModel::BaseModel() : table_(jop::UndefinedTable), isNew_(-1) {}
|
||||
|
||||
QStringList BaseModel::changedFields() const {
|
||||
QStringList output;
|
||||
@ -178,8 +176,7 @@ bool BaseModel::dispose() {
|
||||
}
|
||||
|
||||
Table BaseModel::table() const {
|
||||
qFatal("BaseModel::table() must be overriden");
|
||||
return jop::UndefinedTable;
|
||||
return table_;
|
||||
}
|
||||
|
||||
QString BaseModel::primaryKey() const {
|
||||
@ -419,6 +416,7 @@ void BaseModel::clone(const BaseModel &baseModel) {
|
||||
values_ = baseModel.values_;
|
||||
changedFields_.clear();
|
||||
isNew_ = false;
|
||||
table_ = baseModel.table_;
|
||||
}
|
||||
|
||||
QString BaseModel::tableName(Table t) {
|
||||
|
@ -46,7 +46,7 @@ public:
|
||||
virtual bool save(bool trackChanges = true);
|
||||
virtual bool dispose();
|
||||
|
||||
virtual Table table() const;
|
||||
Table table() const;
|
||||
virtual QString primaryKey() const;
|
||||
virtual bool primaryKeyIsUuid() const;
|
||||
virtual bool trackChanges() const;
|
||||
@ -84,6 +84,7 @@ protected:
|
||||
QHash<QString, bool> changedFields_;
|
||||
QHash<QString, Value> values_;
|
||||
int isNew_;
|
||||
jop::Table table_;
|
||||
|
||||
static QVariant cacheGet(const QString& key);
|
||||
static void cacheSet(const QString& key, const QVariant& value);
|
||||
|
@ -3,8 +3,12 @@
|
||||
|
||||
using namespace jop;
|
||||
|
||||
Table Change::table() const {
|
||||
return jop::ChangesTable;
|
||||
//Table Change::table() const {
|
||||
// return jop::ChangesTable;
|
||||
//}
|
||||
|
||||
Change::Change() : BaseModel() {
|
||||
table_ = jop::ChangesTable;
|
||||
}
|
||||
|
||||
QVector<Change> Change::all(int limit) {
|
||||
|
@ -13,8 +13,9 @@ public:
|
||||
|
||||
enum Type { Undefined, Create, Update, Delete };
|
||||
|
||||
Table table() const;
|
||||
//Table table() const;
|
||||
|
||||
Change();
|
||||
static QVector<Change> all(int limit = 100);
|
||||
static QVector<Change> mergedChanges(const QVector<Change> &changes);
|
||||
static void disposeByItemId(const QString& itemId);
|
||||
|
@ -3,12 +3,14 @@
|
||||
|
||||
namespace jop {
|
||||
|
||||
Folder::Folder() : Item() {}
|
||||
|
||||
Table Folder::table() const {
|
||||
return jop::FoldersTable;
|
||||
Folder::Folder() : Item() {
|
||||
table_ = jop::FoldersTable;
|
||||
}
|
||||
|
||||
//Table Folder::table() const {
|
||||
// return jop::FoldersTable;
|
||||
//}
|
||||
|
||||
bool Folder::primaryKeyIsUuid() const {
|
||||
return true;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public:
|
||||
static int count();
|
||||
static std::vector<std::unique_ptr<Folder>> all(const QString& orderBy);
|
||||
|
||||
Table table() const;
|
||||
//Table table() const;
|
||||
bool primaryKeyIsUuid() const;
|
||||
bool trackChanges() const;
|
||||
int noteCount() const;
|
||||
|
@ -20,7 +20,7 @@ FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
|
||||
connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted()), Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
BaseModel* FolderModel::atIndex(int index) const {
|
||||
const BaseModel *FolderModel::atIndex(int index) const {
|
||||
if (cache_.size()) {
|
||||
if (index < 0 || index >= cache_.size()) {
|
||||
qWarning() << "Invalid folder index:" << index;
|
||||
@ -78,7 +78,7 @@ int FolderModel::baseModelCount() const {
|
||||
return Folder::count();
|
||||
}
|
||||
|
||||
BaseModel *FolderModel::cacheGet(int index) const {
|
||||
const BaseModel *FolderModel::cacheGet(int index) const {
|
||||
return cache_[index].get();
|
||||
}
|
||||
|
||||
|
@ -17,12 +17,12 @@ public:
|
||||
|
||||
FolderModel();
|
||||
void addFolder(Folder* folder);
|
||||
BaseModel* atIndex(int index) const;
|
||||
const BaseModel* atIndex(int index) const;
|
||||
|
||||
protected:
|
||||
|
||||
int baseModelCount() const;
|
||||
BaseModel* cacheGet(int index) const;
|
||||
const BaseModel *cacheGet(int index) const;
|
||||
void cacheSet(int index, BaseModel *baseModel) const;
|
||||
bool cacheIsset(int index) const;
|
||||
void cacheClear() const;
|
||||
|
@ -2,12 +2,14 @@
|
||||
|
||||
using namespace jop;
|
||||
|
||||
Note::Note() : Item() {}
|
||||
|
||||
Table Note::table() const {
|
||||
return jop::NotesTable;
|
||||
Note::Note() : Item() {
|
||||
table_ = jop::NotesTable;
|
||||
}
|
||||
|
||||
//Table Note::table() const {
|
||||
// return jop::NotesTable;
|
||||
//}
|
||||
|
||||
bool Note::primaryKeyIsUuid() const {
|
||||
return true;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ class Note : public Item {
|
||||
public:
|
||||
|
||||
Note();
|
||||
Table table() const;
|
||||
//Table table() const;
|
||||
bool primaryKeyIsUuid() const;
|
||||
bool trackChanges() const;
|
||||
|
||||
|
@ -12,7 +12,7 @@ NoteModel::NoteModel() : AbstractListModel() {
|
||||
connect(&dispatcher(), SIGNAL(noteDeleted(QString)), this, SLOT(dispatcher_noteDeleted(QString)), Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
Note *NoteModel::atIndex(int index) const {
|
||||
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);
|
||||
@ -83,7 +83,7 @@ int NoteModel::baseModelCount() const {
|
||||
return folder().noteCount();
|
||||
}
|
||||
|
||||
BaseModel* NoteModel::cacheGet(int index) const {
|
||||
const BaseModel *NoteModel::cacheGet(int index) const {
|
||||
return static_cast<BaseModel*>(cache_.get(index));
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ class NoteModel : public AbstractListModel {
|
||||
public:
|
||||
|
||||
NoteModel();
|
||||
Note* atIndex(int index) const;
|
||||
const Note* atIndex(int index) const;
|
||||
void setFolderId(const QString& v);
|
||||
Folder folder() const;
|
||||
|
||||
@ -32,7 +32,7 @@ public slots:
|
||||
protected:
|
||||
|
||||
int baseModelCount() const;
|
||||
BaseModel* cacheGet(int index) const;
|
||||
const BaseModel* cacheGet(int index) const;
|
||||
void cacheSet(int index, BaseModel *baseModel) const;
|
||||
bool cacheIsset(int index) const;
|
||||
void cacheClear() const;
|
||||
|
Loading…
Reference in New Issue
Block a user