1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-14 11:18:47 +02:00

147 lines
3.6 KiB
C++
Raw Normal View History

2016-12-10 22:39:53 +00:00
#include "foldermodel.h"
2017-01-01 11:33:14 +01:00
#include "uuid.h"
2017-01-07 10:40:13 +01:00
#include "dispatcher.h"
2016-12-10 22:39:53 +00:00
using namespace jop;
2017-01-12 21:33:56 +01:00
FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
2017-01-07 10:40:13 +01:00
connect(&dispatcher(), SIGNAL(folderCreated(QString)), this, SLOT(dispatcher_folderCreated(QString)));
connect(&dispatcher(), SIGNAL(folderUpdated(QString)), this, SLOT(dispatcher_folderUpdated(QString)));
connect(&dispatcher(), SIGNAL(folderDeleted(QString)), this, SLOT(dispatcher_folderDeleted(QString)));
2017-01-11 15:18:56 +01:00
connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted()));
2016-12-10 22:39:53 +00:00
}
BaseModel* FolderModel::atIndex(int index) const {
if (cacheSize()) {
if (index < 0 || index >= cacheSize()) {
2017-01-01 11:33:14 +01:00
qWarning() << "Invalid folder index:" << index;
return NULL;
2017-01-01 11:33:14 +01:00
}
return cacheGet(index);
2017-01-01 11:33:14 +01:00
}
cacheClear();
2017-01-01 11:33:14 +01:00
2017-01-01 23:20:06 +01:00
cache_ = Folder::all(orderBy_);
2017-01-01 11:33:14 +01:00
if (!cacheSize()) {
2017-01-01 11:33:14 +01:00
qWarning() << "Invalid folder index:" << index;
return NULL;
2017-01-01 11:33:14 +01:00
} else {
return atIndex(index);
}
}
2017-01-13 21:19:47 +01:00
//QString FolderModel::indexToId(int index) const {
// return data(this->index(index), IdRole).toString();
//}
2016-12-31 10:48:18 +01:00
int FolderModel::idToIndex(const QString &id) const {
2017-01-01 11:33:14 +01:00
int count = this->rowCount();
for (int i = 0; i < count; i++) {
Folder* folder = (Folder*)atIndex(i);
if (folder->idString() == id) return i;
2017-01-01 11:33:14 +01:00
}
return -1;
}
2017-01-13 21:19:47 +01:00
//QString FolderModel::lastInsertId() const {
// return lastInsertId_;
//}
2016-12-31 10:48:18 +01:00
bool FolderModel::setTitle(int index, const QVariant &value, int role) {
2016-12-29 20:19:00 +01:00
return setData(this->index(index), value, role);
}
2016-12-31 10:48:18 +01:00
void FolderModel::addData(const QString &title) {
2017-01-01 23:20:06 +01:00
Folder folder;
folder.setValue("title", title);
2017-01-01 23:20:06 +01:00
if (!folder.save()) return;
2017-01-01 11:33:14 +01:00
lastInsertId_ = folder.id().toString();
2017-01-01 11:33:14 +01:00
}
void FolderModel::deleteData(const int index) {
Folder* folder = (Folder*)atIndex(index);
if (!folder) return;
folder->dispose();
2016-12-10 22:39:53 +00:00
}
2017-01-07 10:40:13 +01:00
int FolderModel::baseModelCount() const {
return Folder::count();
}
BaseModel *FolderModel::cacheGet(int index) const {
return cache_[index].get();
}
void FolderModel::cacheSet(int index, BaseModel* baseModel) const {
Folder* folder = static_cast<Folder*>(baseModel);
cache_[index] = std::unique_ptr<Folder>(folder);
}
bool FolderModel::cacheIsset(int index) const {
return index > 0 && cache_.size() > index;
}
void FolderModel::cacheClear() const {
cache_.clear();
}
int FolderModel::cacheSize() const {
return cache_.size();
}
2017-01-09 14:34:06 +01:00
// TODO: instead of clearing the whole cache every time, the individual items
// could be created/updated/deleted
2017-01-07 10:40:13 +01:00
void FolderModel::dispatcher_folderCreated(const QString &folderId) {
qDebug() << "FolderModel Folder created" << folderId;
cacheClear();
2017-01-07 10:40:13 +01:00
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 FolderModel::dispatcher_folderUpdated(const QString &folderId) {
qDebug() << "FolderModel Folder udpated" << folderId;
2017-01-09 14:34:06 +01:00
cacheClear();
2017-01-09 14:34:06 +01:00
QVector<int> roles;
roles << Qt::DisplayRole;
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
2017-01-07 10:40:13 +01:00
}
void FolderModel::dispatcher_folderDeleted(const QString &folderId) {
qDebug() << "FolderModel Folder deleted" << folderId;
2017-01-09 22:14:32 +01:00
int index = idToIndex(folderId);
if (index < 0) return;
cacheClear();
2017-01-09 22:14:32 +01:00
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
2017-01-07 10:40:13 +01:00
}
2017-01-11 15:18:56 +01:00
void FolderModel::dispatcher_allFoldersDeleted() {
qDebug() << "FolderModel All folders deleted";
cacheClear();
2017-01-11 15:18:56 +01:00
beginResetModel();
endResetModel();
}