1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-20 11:28:40 +02:00

162 lines
3.8 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"
2016-12-10 22:39:53 +00:00
using namespace jop;
2017-01-01 11:33:14 +01:00
FolderModel::FolderModel(Database &database) : QAbstractListModel(), db_(database), orderBy_("title") {
2016-12-31 10:48:18 +01:00
virtualItemShown_ = false;
2016-12-10 22:39:53 +00:00
}
int FolderModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent);
2017-01-01 23:20:06 +01:00
return Folder::count() + (virtualItemShown_ ? 1 : 0);
2016-12-10 22:39:53 +00:00
}
// NOTE: to lazy load - send back "Loading..." if item not currently loaded
// queue the item for loading.
// Then batch load them a bit later.
QVariant FolderModel::data(const QModelIndex & index, int role) const {
2016-12-31 10:48:18 +01:00
Folder folder;
if (virtualItemShown_ && index.row() == rowCount() - 1) {
folder.setValue("title", BaseModel::Value(QString("Untitled")));
2016-12-31 10:48:18 +01:00
} else {
2017-01-01 11:33:14 +01:00
folder = atIndex(index.row());
2016-12-31 10:48:18 +01:00
}
2016-12-10 22:39:53 +00:00
if (role == Qt::DisplayRole) {
return folder.value("title").toQVariant();
2016-12-10 22:39:53 +00:00
}
2016-12-11 16:09:39 +00:00
if (role == IdRole) {
return folder.id().toQVariant();
2016-12-11 16:09:39 +00:00
}
2016-12-10 22:39:53 +00:00
return QVariant();
}
2016-12-29 20:19:00 +01:00
bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role) {
2017-01-01 11:33:14 +01:00
Folder folder = atIndex(index.row());
2016-12-29 20:19:00 +01:00
if (role == Qt::EditRole) {
folder.setValue("title", value);
2017-01-01 23:20:06 +01:00
if (!folder.save()) return false;
2017-01-01 11:33:14 +01:00
cache_.clear();
QVector<int> roles;
roles << Qt::DisplayRole;
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
2016-12-29 20:19:00 +01:00
return true;
}
qWarning() << "Unsupported role" << role;
return false;
}
2017-01-01 11:33:14 +01:00
Folder FolderModel::atIndex(int index) const {
if (cache_.size()) {
if (index < 0 || index >= cache_.size()) {
qWarning() << "Invalid folder index:" << index;
return Folder();
}
return cache_[index];
}
cache_.clear();
2017-01-01 23:20:06 +01:00
cache_ = Folder::all(orderBy_);
2017-01-01 11:33:14 +01:00
if (!cache_.size()) {
qWarning() << "Invalid folder index:" << index;
return Folder();
} else {
return atIndex(index);
}
}
Folder FolderModel::atIndex(const QModelIndex &index) const {
return atIndex(index.row());
}
2016-12-31 10:48:18 +01:00
void FolderModel::showVirtualItem() {
virtualItemShown_ = true;
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
endInsertRows();
}
void FolderModel::hideVirtualItem() {
beginRemoveRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
virtualItemShown_ = false;
endRemoveRows();
}
QString FolderModel::indexToId(int index) const {
2016-12-31 10:48:18 +01:00
return data(this->index(index), IdRole).toString();
}
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 = atIndex(i);
if (folder.value("id").toString() == id) return i;
2017-01-01 11:33:14 +01:00
}
return -1;
}
QString FolderModel::lastInsertId() const {
return lastInsertId_;
2016-12-31 10:48:18 +01:00
}
bool FolderModel::virtualItemShown() const {
return virtualItemShown_;
}
2016-12-29 20:19:00 +01:00
bool FolderModel::setData(int index, const QVariant &value, int role) {
return setData(this->index(index), value, role);
}
2016-12-10 22:39:53 +00:00
QHash<int, QByteArray> FolderModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[TitleRole] = "title";
2016-12-11 16:09:39 +00:00
roles[IdRole] = "uuid";
roles[RawRole] = "raw";
2016-12-10 22:39:53 +00:00
return roles;
}
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
cache_.clear();
lastInsertId_ = folder.id().toString();
2016-12-31 10:48:18 +01:00
2016-12-29 20:19:00 +01:00
QVector<int> roles;
roles << Qt::DisplayRole;
2017-01-01 11:33:14 +01:00
int from = 0;
int to = rowCount() - 1;
// 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();
2016-12-29 20:19:00 +01:00
emit dataChanged(this->index(from), this->index(to), roles);
2017-01-01 11:33:14 +01:00
}
void FolderModel::deleteData(const int index) {
2017-01-01 23:20:06 +01:00
Folder folder = atIndex(index);
if (!folder.dispose()) return;
2017-01-01 11:33:14 +01:00
cache_.clear();
beginRemoveRows(QModelIndex(), index, index);
2016-12-31 10:48:18 +01:00
endRemoveRows();
2017-01-01 11:33:14 +01:00
QVector<int> roles;
roles << Qt::DisplayRole;
emit dataChanged(this->index(0), this->index(rowCount() - 1), roles);
2016-12-10 22:39:53 +00:00
}