diff --git a/QtClient/JoplinQtClient/JoplinQtClient.pro b/QtClient/JoplinQtClient/JoplinQtClient.pro index 3bc69a24ac..327a7be6e9 100755 --- a/QtClient/JoplinQtClient/JoplinQtClient.pro +++ b/QtClient/JoplinQtClient/JoplinQtClient.pro @@ -22,7 +22,8 @@ SOURCES += \ models/setting.cpp \ paths.cpp \ window.cpp \ - filters.cpp + filters.cpp \ + models/abstractlistmodel.cpp RESOURCES += qml.qrc \ database.qrc @@ -59,7 +60,8 @@ HEADERS += \ paths.h \ constants.h \ window.h \ - filters.h + filters.h \ + models/abstractlistmodel.h DISTFILES += \ AndroidManifest.xml diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.cpp b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp new file mode 100755 index 0000000000..5a4b3b4db1 --- /dev/null +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.cpp @@ -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 AbstractListModel::roleNames() const { + QHash roles = QAbstractItemModel::roleNames(); + roles[TitleRole] = "title"; + roles[IdRole] = "id"; + //roles[RawRole] = "raw"; + return roles; +} diff --git a/QtClient/JoplinQtClient/models/abstractlistmodel.h b/QtClient/JoplinQtClient/models/abstractlistmodel.h new file mode 100755 index 0000000000..cc25c045f3 --- /dev/null +++ b/QtClient/JoplinQtClient/models/abstractlistmodel.h @@ -0,0 +1,47 @@ +#ifndef ABSTRACTLISTMODEL_H +#define ABSTRACTLISTMODEL_H + +#include +#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 roleNames() const; + +}; + +} + +#endif // ABSTRACTLISTMODEL_H diff --git a/QtClient/JoplinQtClient/models/foldermodel.cpp b/QtClient/JoplinQtClient/models/foldermodel.cpp index 4c7def9d45..6b31d06982 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.cpp +++ b/QtClient/JoplinQtClient/models/foldermodel.cpp @@ -4,23 +4,17 @@ using namespace jop; -FolderModel::FolderModel() : QAbstractListModel(), orderBy_("title") { - virtualItemShown_ = false; - +FolderModel::FolderModel() : AbstractListModel(), orderBy_("title") { 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))); 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 { Folder folder; - if (virtualItemShown_ && index.row() == rowCount() - 1) { + if (virtualItemShown() && index.row() == rowCount() - 1) { folder.setValue("title", BaseModel::Value(QString("Untitled"))); } else { folder = atIndex(index.row()); @@ -77,16 +71,12 @@ Folder FolderModel::atIndex(const QModelIndex &index) const { return atIndex(index.row()); } -void FolderModel::showVirtualItem() { - virtualItemShown_ = true; - beginInsertRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1); - endInsertRows(); +BaseModel FolderModel::baseModel() const { + return Folder(); } -void FolderModel::hideVirtualItem() { - beginRemoveRows(QModelIndex(), this->rowCount() - 1, this->rowCount() - 1); - virtualItemShown_ = false; - endRemoveRows(); +int FolderModel::baseModelCount() const { + return Folder::count(); } QString FolderModel::indexToId(int index) const { @@ -106,22 +96,10 @@ QString FolderModel::lastInsertId() const { return lastInsertId_; } -bool FolderModel::virtualItemShown() const { - return virtualItemShown_; -} - bool FolderModel::setData(int index, const QVariant &value, int role) { return setData(this->index(index), value, role); } -QHash FolderModel::roleNames() const { - QHash roles = QAbstractItemModel::roleNames(); - roles[TitleRole] = "title"; - roles[IdRole] = "uuid"; - roles[RawRole] = "raw"; - return roles; -} - void FolderModel::addData(const QString &title) { Folder folder; folder.setValue("title", title); diff --git a/QtClient/JoplinQtClient/models/foldermodel.h b/QtClient/JoplinQtClient/models/foldermodel.h index c29aeb3f9c..c57f41568c 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.h +++ b/QtClient/JoplinQtClient/models/foldermodel.h @@ -4,26 +4,20 @@ #include #include "models/folder.h" +#include "models/abstractlistmodel.h" #include "database.h" namespace jop { -class FolderModel : public QAbstractListModel { +class FolderModel : public AbstractListModel { Q_OBJECT public: - enum FolderRoles { - IdRole = Qt::UserRole + 1, - TitleRole, - RawRole - }; - FolderModel(); void addFolder(Folder* folder); - int rowCount(const QModelIndex & parent = QModelIndex()) const; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); Folder atIndex(int index) const; @@ -31,12 +25,12 @@ public: protected: - QHash roleNames() const; + BaseModel baseModel() const; + int baseModelCount() const; private: QList folders_; - bool virtualItemShown_; QString orderBy_; mutable QVector cache_; QString lastInsertId_; @@ -45,10 +39,7 @@ public slots: void addData(const QString& title); void deleteData(const int index); - bool setData(int index, const QVariant &value, int role = Qt::EditRole); - void showVirtualItem(); - bool virtualItemShown() const; - void hideVirtualItem(); + bool setData(int index, const QVariant &value, int role = Qt::EditRole); QString indexToId(int index) const; int idToIndex(const QString& id) const; QString lastInsertId() const;