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
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FolderModel::data(const QModelIndex & index, int role) const {
|
2017-01-12 23:09:24 +01:00
|
|
|
|
|
|
|
//QVector<std::unique_ptr<Folder>> folders;
|
|
|
|
// std::unique_ptr<Folder> f(new Folder());
|
|
|
|
// f->setValue("title", QString("ancd"));
|
|
|
|
// BaseModel* b = static_cast<BaseModel*>(f.get());
|
|
|
|
// qDebug() << b->value("title").toString();
|
|
|
|
// Folder* f2 = static_cast<Folder*>(b);
|
|
|
|
// qDebug() << "*" << f2->value("title").toString();
|
|
|
|
|
|
|
|
|
|
|
|
// std::unique_ptr<BaseModel> baseModel(f.release());
|
|
|
|
// qDebug() << "££££££££££££££££££££££££££££££££" << baseModel->value("title").toString();
|
|
|
|
// std::unique_ptr<Folder> f2(baseModel.release());
|
|
|
|
// qDebug() << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << f2->value("title").toString();
|
|
|
|
|
|
|
|
|
|
|
|
//f->setValue("title", "abcd");
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-31 10:48:18 +01:00
|
|
|
Folder folder;
|
|
|
|
|
2017-01-12 21:33:56 +01:00
|
|
|
if (virtualItemShown() && index.row() == rowCount() - 1) {
|
2017-01-02 13:17:15 +01:00
|
|
|
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) {
|
2017-01-02 13:17:15 +01:00
|
|
|
return folder.value("title").toQVariant();
|
2016-12-10 22:39:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 16:09:39 +00:00
|
|
|
if (role == IdRole) {
|
2017-01-02 13:17:15 +01:00
|
|
|
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) {
|
2017-01-02 13:17:15 +01:00
|
|
|
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();
|
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());
|
|
|
|
}
|
|
|
|
|
2017-01-12 23:09:24 +01:00
|
|
|
BaseModel FolderModel::newBaseModel() const {
|
2017-01-12 21:33:56 +01:00
|
|
|
return Folder();
|
2016-12-31 10:48:18 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 21:33:56 +01:00
|
|
|
int FolderModel::baseModelCount() const {
|
|
|
|
return Folder::count();
|
2016-12-31 10:48:18 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 23:09:24 +01:00
|
|
|
BaseModel FolderModel::cacheGet(int index) const {
|
|
|
|
return cache_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderModel::cacheSet(int index, const BaseModel &baseModel) {
|
|
|
|
// Folder f(baseModel);
|
|
|
|
// cache_[index] = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FolderModel::cacheIsset(int index) const {
|
|
|
|
return index > 0 && cache_.size() > index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderModel::cacheClear() {
|
|
|
|
cache_.clear();
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
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);
|
2017-01-02 13:17:15 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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-31 10:48:18 +01:00
|
|
|
void FolderModel::addData(const QString &title) {
|
2017-01-01 23:20:06 +01:00
|
|
|
Folder folder;
|
2017-01-02 13:17:15 +01:00
|
|
|
folder.setValue("title", title);
|
2017-01-01 23:20:06 +01:00
|
|
|
if (!folder.save()) return;
|
2017-01-01 11:33:14 +01:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
lastInsertId_ = folder.id().toString();
|
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;
|
2016-12-10 22:39:53 +00:00
|
|
|
}
|
2017-01-07 10:40:13 +01:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
cache_.clear();
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
cache_.clear();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
cache_.clear();
|
|
|
|
|
|
|
|
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";
|
|
|
|
cache_.clear();
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|