1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-03 23:50:33 +02:00
Files
joplin/QtClient/JoplinQtClient/models/notemodel.cpp

150 lines
3.6 KiB
C++
Raw Normal View History

2016-12-11 16:09:39 +00:00
#include "notemodel.h"
2017-01-14 00:04:52 +01:00
#include "dispatcher.h"
2016-12-11 16:09:39 +00:00
namespace jop {
NoteModel::NoteModel() : AbstractListModel() {
2017-01-12 17:59:19 +01:00
folderId_ = "";
orderBy_ = "title";
2017-01-14 00:04:52 +01:00
connect(&dispatcher(), SIGNAL(noteCreated(QString)), this, SLOT(dispatcher_noteCreated(QString)), Qt::QueuedConnection);
connect(&dispatcher(), SIGNAL(noteUpdated(QString)), this, SLOT(dispatcher_noteUpdated(QString)), Qt::QueuedConnection);
connect(&dispatcher(), SIGNAL(noteDeleted(QString)), this, SLOT(dispatcher_noteDeleted(QString)), Qt::QueuedConnection);
2016-12-11 16:09:39 +00:00
}
2017-01-21 11:47:24 +00:00
const Note *NoteModel::atIndex(int index) const {
if (folderId_ == "") return NULL;
if (index < 0 || index >= rowCount()) return NULL;
2017-01-12 17:59:19 +01:00
if (cache_.isset(index)) return cache_.get(index);
2016-12-12 22:33:33 +00:00
2017-01-12 17:59:19 +01:00
std::vector<int> indexes = cache_.availableBufferAround(index, 32);
if (!indexes.size()) {
qCritical() << "Couldn't acquire buffer"; // "Cannot happen"
return NULL;
2017-01-12 17:59:19 +01:00
}
2016-12-12 22:33:33 +00:00
2017-01-12 17:59:19 +01:00
int from = indexes[0];
int to = indexes[indexes.size() - 1];
2016-12-12 22:33:33 +00:00
2017-02-06 20:20:30 +00:00
// Folder folder = this->folder();
// qDebug() << "NoteModel: cache recreated";
// std::vector<std::unique_ptr<Note>> notes = folder.notes(orderBy_, to - from + 1, from);
// int noteIndex = from;
// for (int i = 0; i < notes.size(); i++) {
// cache_.set(noteIndex, notes[i].release());
// noteIndex++;
// }
2017-01-12 17:59:19 +01:00
return cache_.get(index);
2016-12-11 16:09:39 +00:00
}
void NoteModel::setFolderId(const QString &v) {
2017-01-12 17:59:19 +01:00
if (v == folderId_) return;
beginResetModel();
cache_.clear();
2016-12-12 22:33:33 +00:00
folderId_ = v;
2017-01-12 17:59:19 +01:00
endResetModel();
2016-12-12 22:33:33 +00:00
}
2017-02-06 20:20:30 +00:00
//Folder NoteModel::folder() const {
// Folder folder;
// //if (folderId_ == "") return folder;
// folder.load(folderId_);
// return folder;
//}
2016-12-12 22:33:33 +00:00
int NoteModel::idToIndex(const QString &id) const {
2017-01-21 11:36:50 +00:00
std::vector<int> indexes = cache_.indexes();
2017-02-07 19:42:35 +00:00
for (size_t i = 0; i < indexes.size(); i++) {
2017-01-21 11:36:50 +00:00
Note* note = cache_.get(indexes[i]);
if (note->idString() == id) return indexes[i];
}
2017-02-06 20:20:30 +00:00
return 0;
//Folder f = this->folder();
//return f.noteIndexById(orderBy_, id);
2017-01-12 19:11:58 +01:00
}
2017-01-14 00:04:52 +01:00
void NoteModel::addData(const QString &title) {
Note note;
note.setValue("title", title);
note.setValue("parent_id", folderId_);
if (!note.save()) return;
lastInsertId_ = note.idString();
}
2017-01-21 11:36:50 +00:00
void NoteModel::deleteData(int index) {
Note* note = (Note*)atIndex(index);
if (!note) return;
note->dispose();
}
int NoteModel::baseModelCount() const {
2017-02-06 20:20:30 +00:00
return 0;
//return folder().noteCount();
2016-12-11 16:09:39 +00:00
}
2017-01-21 11:47:24 +00:00
const BaseModel *NoteModel::cacheGet(int index) const {
return static_cast<BaseModel*>(cache_.get(index));
}
void NoteModel::cacheSet(int index, BaseModel *baseModel) const {
cache_.set(index, static_cast<Note*>(baseModel));
}
bool NoteModel::cacheIsset(int index) const {
return cache_.isset(index);
}
void NoteModel::cacheClear() const {
2017-01-31 21:20:13 +00:00
qDebug() << "NoteModel::cacheClear()";
cache_.clear();
}
2017-01-14 00:04:52 +01:00
void NoteModel::dispatcher_noteCreated(const QString &noteId) {
2017-01-31 21:20:13 +00:00
qDebug() << "NoteModel note created" << noteId;
2017-01-14 00:04:52 +01:00
cacheClear();
int from = 0;
int to = rowCount() - 1;
QVector<int> roles;
roles << Qt::DisplayRole;
// Necessary to make sure a new item is added to the view, even
// though it might not be positioned there due to sorting
beginInsertRows(QModelIndex(), to, to);
endInsertRows();
emit dataChanged(this->index(from), this->index(to), roles);
}
void NoteModel::dispatcher_noteUpdated(const QString &noteId) {
2017-01-21 11:36:50 +00:00
qDebug() << "NoteModel note udpated" << noteId;
2017-01-14 00:04:52 +01:00
cacheClear();
QVector<int> roles;
roles << Qt::DisplayRole;
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
}
void NoteModel::dispatcher_noteDeleted(const QString &noteId) {
2017-01-21 11:36:50 +00:00
qDebug() << "NoteModel note deleted" << noteId;
2017-01-14 00:04:52 +01:00
int index = idToIndex(noteId);
2017-01-21 11:36:50 +00:00
qDebug() << "index" << index;
2017-01-14 00:04:52 +01:00
if (index < 0) return;
cacheClear();
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
}
}