You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-13 22:12:50 +02:00
refactoring list model
This commit is contained in:
@@ -22,7 +22,8 @@ SOURCES += \
|
|||||||
models/setting.cpp \
|
models/setting.cpp \
|
||||||
paths.cpp \
|
paths.cpp \
|
||||||
window.cpp \
|
window.cpp \
|
||||||
filters.cpp
|
filters.cpp \
|
||||||
|
models/abstractlistmodel.cpp
|
||||||
|
|
||||||
RESOURCES += qml.qrc \
|
RESOURCES += qml.qrc \
|
||||||
database.qrc
|
database.qrc
|
||||||
@@ -59,7 +60,8 @@ HEADERS += \
|
|||||||
paths.h \
|
paths.h \
|
||||||
constants.h \
|
constants.h \
|
||||||
window.h \
|
window.h \
|
||||||
filters.h
|
filters.h \
|
||||||
|
models/abstractlistmodel.h
|
||||||
|
|
||||||
DISTFILES += \
|
DISTFILES += \
|
||||||
AndroidManifest.xml
|
AndroidManifest.xml
|
||||||
|
91
QtClient/JoplinQtClient/models/abstractlistmodel.cpp
Executable file
91
QtClient/JoplinQtClient/models/abstractlistmodel.cpp
Executable file
@@ -0,0 +1,91 @@
|
|||||||
|
#include "abstractlistmodel.h"
|
||||||
|
|
||||||
|
using namespace jop;
|
||||||
|
|
||||||
|
AbstractListModel::AbstractListModel() : QAbstractListModel() {
|
||||||
|
virtualItemShown_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int AbstractListModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent);
|
||||||
|
return baseModelCount() + (virtualItemShown() ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//QVariant AbstractListModel::data(const QModelIndex & index, int role) const {
|
||||||
|
// BaseModel model = baseModel();
|
||||||
|
|
||||||
|
// if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||||
|
// model.setValue("title", BaseModel::Value(QString("Untitled")));
|
||||||
|
// } else {
|
||||||
|
// model = atIndex(index.row());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (role == Qt::DisplayRole) {
|
||||||
|
// return model.value("title").toQVariant();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (role == IdRole) {
|
||||||
|
// return model.id().toQVariant();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return QVariant();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//BaseModel AbstractListModel::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();
|
||||||
|
|
||||||
|
// cache_ = Folder::all(orderBy_);
|
||||||
|
|
||||||
|
// if (!cache_.size()) {
|
||||||
|
// qWarning() << "Invalid folder index:" << index;
|
||||||
|
// return Folder();
|
||||||
|
// } else {
|
||||||
|
// return atIndex(index);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//BaseModel AbstractListModel::atIndex(const QModelIndex &index) const {
|
||||||
|
// return atIndex(index.row());
|
||||||
|
//}
|
||||||
|
|
||||||
|
BaseModel AbstractListModel::baseModel() const {
|
||||||
|
qFatal("AbstractListModel::baseModel() not implemented");
|
||||||
|
return BaseModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
int AbstractListModel::baseModelCount() const {
|
||||||
|
qFatal("AbstractListModel::baseModelCount() not implemented");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractListModel::showVirtualItem() {
|
||||||
|
virtualItemShown_ = true;
|
||||||
|
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
|
||||||
|
endInsertRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractListModel::hideVirtualItem() {
|
||||||
|
beginRemoveRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
|
||||||
|
virtualItemShown_ = false;
|
||||||
|
endRemoveRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AbstractListModel::virtualItemShown() const {
|
||||||
|
return virtualItemShown_;
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<int, QByteArray> AbstractListModel::roleNames() const {
|
||||||
|
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
|
||||||
|
roles[TitleRole] = "title";
|
||||||
|
roles[IdRole] = "id";
|
||||||
|
//roles[RawRole] = "raw";
|
||||||
|
return roles;
|
||||||
|
}
|
47
QtClient/JoplinQtClient/models/abstractlistmodel.h
Executable file
47
QtClient/JoplinQtClient/models/abstractlistmodel.h
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef ABSTRACTLISTMODEL_H
|
||||||
|
#define ABSTRACTLISTMODEL_H
|
||||||
|
|
||||||
|
#include <stable.h>
|
||||||
|
#include "models/basemodel.h"
|
||||||
|
|
||||||
|
namespace jop {
|
||||||
|
|
||||||
|
class AbstractListModel : public QAbstractListModel {
|
||||||
|
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
enum ModelRoles {
|
||||||
|
IdRole = Qt::UserRole + 1,
|
||||||
|
TitleRole,
|
||||||
|
RawRole
|
||||||
|
};
|
||||||
|
|
||||||
|
AbstractListModel();
|
||||||
|
int rowCount(const QModelIndex & parent = QModelIndex()) const;
|
||||||
|
//QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||||
|
// BaseModel atIndex(int index) const;
|
||||||
|
// BaseModel atIndex(const QModelIndex &index) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual BaseModel baseModel() const;
|
||||||
|
virtual int baseModelCount() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
bool virtualItemShown_;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
void showVirtualItem();
|
||||||
|
bool virtualItemShown() const;
|
||||||
|
void hideVirtualItem();
|
||||||
|
QHash<int, QByteArray> roleNames() const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ABSTRACTLISTMODEL_H
|
@@ -4,23 +4,17 @@
|
|||||||
|
|
||||||
using namespace jop;
|
using namespace jop;
|
||||||
|
|
||||||
FolderModel::FolderModel() : QAbstractListModel(), orderBy_("title") {
|
FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") {
|
||||||
virtualItemShown_ = false;
|
|
||||||
|
|
||||||
connect(&dispatcher(), SIGNAL(folderCreated(QString)), this, SLOT(dispatcher_folderCreated(QString)));
|
connect(&dispatcher(), SIGNAL(folderCreated(QString)), this, SLOT(dispatcher_folderCreated(QString)));
|
||||||
connect(&dispatcher(), SIGNAL(folderUpdated(QString)), this, SLOT(dispatcher_folderUpdated(QString)));
|
connect(&dispatcher(), SIGNAL(folderUpdated(QString)), this, SLOT(dispatcher_folderUpdated(QString)));
|
||||||
connect(&dispatcher(), SIGNAL(folderDeleted(QString)), this, SLOT(dispatcher_folderDeleted(QString)));
|
connect(&dispatcher(), SIGNAL(folderDeleted(QString)), this, SLOT(dispatcher_folderDeleted(QString)));
|
||||||
connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted()));
|
connect(&dispatcher(), SIGNAL(allFoldersDeleted()), this, SLOT(dispatcher_allFoldersDeleted()));
|
||||||
}
|
}
|
||||||
|
|
||||||
int FolderModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent);
|
|
||||||
return Folder::count() + (virtualItemShown_ ? 1 : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant FolderModel::data(const QModelIndex & index, int role) const {
|
QVariant FolderModel::data(const QModelIndex & index, int role) const {
|
||||||
Folder folder;
|
Folder folder;
|
||||||
|
|
||||||
if (virtualItemShown_ && index.row() == rowCount() - 1) {
|
if (virtualItemShown() && index.row() == rowCount() - 1) {
|
||||||
folder.setValue("title", BaseModel::Value(QString("Untitled")));
|
folder.setValue("title", BaseModel::Value(QString("Untitled")));
|
||||||
} else {
|
} else {
|
||||||
folder = atIndex(index.row());
|
folder = atIndex(index.row());
|
||||||
@@ -77,16 +71,12 @@ Folder FolderModel::atIndex(const QModelIndex &index) const {
|
|||||||
return atIndex(index.row());
|
return atIndex(index.row());
|
||||||
}
|
}
|
||||||
|
|
||||||
void FolderModel::showVirtualItem() {
|
BaseModel FolderModel::baseModel() const {
|
||||||
virtualItemShown_ = true;
|
return Folder();
|
||||||
beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
|
|
||||||
endInsertRows();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FolderModel::hideVirtualItem() {
|
int FolderModel::baseModelCount() const {
|
||||||
beginRemoveRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1);
|
return Folder::count();
|
||||||
virtualItemShown_ = false;
|
|
||||||
endRemoveRows();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString FolderModel::indexToId(int index) const {
|
QString FolderModel::indexToId(int index) const {
|
||||||
@@ -106,22 +96,10 @@ QString FolderModel::lastInsertId() const {
|
|||||||
return lastInsertId_;
|
return lastInsertId_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FolderModel::virtualItemShown() const {
|
|
||||||
return virtualItemShown_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FolderModel::setData(int index, const QVariant &value, int role) {
|
bool FolderModel::setData(int index, const QVariant &value, int role) {
|
||||||
return setData(this->index(index), value, role);
|
return setData(this->index(index), value, role);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QByteArray> FolderModel::roleNames() const {
|
|
||||||
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
|
|
||||||
roles[TitleRole] = "title";
|
|
||||||
roles[IdRole] = "uuid";
|
|
||||||
roles[RawRole] = "raw";
|
|
||||||
return roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FolderModel::addData(const QString &title) {
|
void FolderModel::addData(const QString &title) {
|
||||||
Folder folder;
|
Folder folder;
|
||||||
folder.setValue("title", title);
|
folder.setValue("title", title);
|
||||||
|
@@ -4,26 +4,20 @@
|
|||||||
#include <stable.h>
|
#include <stable.h>
|
||||||
|
|
||||||
#include "models/folder.h"
|
#include "models/folder.h"
|
||||||
|
#include "models/abstractlistmodel.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
|
||||||
namespace jop {
|
namespace jop {
|
||||||
|
|
||||||
class FolderModel : public QAbstractListModel {
|
class FolderModel : public AbstractListModel {
|
||||||
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum FolderRoles {
|
|
||||||
IdRole = Qt::UserRole + 1,
|
|
||||||
TitleRole,
|
|
||||||
RawRole
|
|
||||||
};
|
|
||||||
|
|
||||||
FolderModel();
|
FolderModel();
|
||||||
|
|
||||||
void addFolder(Folder* folder);
|
void addFolder(Folder* folder);
|
||||||
int rowCount(const QModelIndex & parent = QModelIndex()) const;
|
|
||||||
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
||||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
||||||
Folder atIndex(int index) const;
|
Folder atIndex(int index) const;
|
||||||
@@ -31,12 +25,12 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
QHash<int, QByteArray> roleNames() const;
|
BaseModel baseModel() const;
|
||||||
|
int baseModelCount() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QList<Folder> folders_;
|
QList<Folder> folders_;
|
||||||
bool virtualItemShown_;
|
|
||||||
QString orderBy_;
|
QString orderBy_;
|
||||||
mutable QVector<Folder> cache_;
|
mutable QVector<Folder> cache_;
|
||||||
QString lastInsertId_;
|
QString lastInsertId_;
|
||||||
@@ -45,10 +39,7 @@ public slots:
|
|||||||
|
|
||||||
void addData(const QString& title);
|
void addData(const QString& title);
|
||||||
void deleteData(const int index);
|
void deleteData(const int index);
|
||||||
bool setData(int index, const QVariant &value, int role = Qt::EditRole);
|
bool setData(int index, const QVariant &value, int role = Qt::EditRole);
|
||||||
void showVirtualItem();
|
|
||||||
bool virtualItemShown() const;
|
|
||||||
void hideVirtualItem();
|
|
||||||
QString indexToId(int index) const;
|
QString indexToId(int index) const;
|
||||||
int idToIndex(const QString& id) const;
|
int idToIndex(const QString& id) const;
|
||||||
QString lastInsertId() const;
|
QString lastInsertId() const;
|
||||||
|
Reference in New Issue
Block a user