diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-29 20:17:42 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-29 20:17:42 +0200 |
| commit | 8e910f26a28b1b1beae363e6c19f39224f74e2e8 (patch) | |
| tree | 282b768b54c0966578944a5b2cd717ba0fe58cfa /src/favouritecollection.hpp | |
| parent | dcba6f90718d3f009380ad2b2994790866b881e2 (diff) | |
| download | kristall-8e910f26a28b1b1beae363e6c19f39224f74e2e8.tar.gz | |
Paves road for new favourite system: Refactors the favourites into tree structure instead of flat list.
Diffstat (limited to 'src/favouritecollection.hpp')
| -rw-r--r-- | src/favouritecollection.hpp | 129 |
1 files changed, 111 insertions, 18 deletions
diff --git a/src/favouritecollection.hpp b/src/favouritecollection.hpp index 043fdf2..ea107ab 100644 --- a/src/favouritecollection.hpp +++ b/src/favouritecollection.hpp @@ -1,48 +1,141 @@ #ifndef FAVOURITECOLLECTION_HPP #define FAVOURITECOLLECTION_HPP -#include <QObject> -#include <QAbstractListModel> +#include <QAbstractItemModel> #include <QUrl> +#include <QString> +#include <memory> #include <QSettings> +struct Favourite +{ + QString title; + QUrl destination; + + bool isValid() const { + return destination.isValid(); + } -class FavouriteCollection : public QAbstractListModel + QString getTitle() const { + if(title.isEmpty()) + return destination.toString(QUrl::FullyDecoded); + else + return title; + } +}; + +class FavouriteCollection : public QAbstractItemModel { Q_OBJECT + struct Node { + enum Type { Root, Group, Favourite }; + Node * parent = nullptr; + int index = 0; + std::vector<std::unique_ptr<Node>> children; + Type type; + explicit Node(Type t) : type(t) { } + virtual ~Node() = default; + + template<typename T> + T & as() { return *static_cast<T*>(this); } + + template<typename T> + T const & as() const { return *static_cast<T const*>(this); } + }; + + struct FavouriteNode : Node { + ::Favourite favourite; + FavouriteNode() : Node(Favourite) { } + ~FavouriteNode() override = default; + }; + + struct GroupNode : Node { + QString title; + GroupNode() : Node(Group) { } + ~GroupNode() override = default; + }; + + struct RootNode : Node { + RootNode() : Node(Root) { } + ~RootNode() override = default; + }; + public: explicit FavouriteCollection(QObject *parent = nullptr); - void add(QUrl const & url); + FavouriteCollection(FavouriteCollection const & other); - void remove(QUrl const & url); + FavouriteCollection & operator=(FavouriteCollection const &); + FavouriteCollection & operator=(FavouriteCollection &&); - bool contains(QUrl const & url); +public: + void load(QSettings & settings); - QUrl get(QModelIndex const & index) const ; + void save(QSettings & settings) const; - bool save(QString const & fileName) const; - bool save(QSettings & settings) const; + bool addGroup(QString const & group); - bool load(QString const & fileName); - bool load(QSettings & settings); + bool addFavourite(QString const & group, Favourite const & fav); - QVector<QUrl> getAll() const { - return this->items; - } + Favourite getFavourite(QModelIndex const & index) const; + + Favourite * getMutableFavourite(QModelIndex const & index); + + QStringList groups() const; + + //! Returns the group name of the index. + QString group(QModelIndex const & index) const; + + bool destroyFavourite(QModelIndex const & index); + + bool canDeleteGroup(QString const & group_name); + bool deleteGroup(QString const & group_name); + + //! Returns a list of non-mutable references to all contained identities + QVector<Favourite const *> allFavourites() const; + + bool containsUrl(QUrl const & url) const; + + bool addUnsorted(QUrl const & url); + + bool removeUrl(QUrl const & url); public: - int rowCount(const QModelIndex &parent = QModelIndex()) const override; + // Header: + // QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + // Basic functionality: + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex &index) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; -signals: + // Drag'n'Drop + + Qt::ItemFlags flags(const QModelIndex &index) const override; + + QStringList mimeTypes() const override; + QMimeData *mimeData(const QModelIndexList &indexes) const override; + bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; + Qt::DropActions supportedDropActions() const override; + + Qt::DropActions supportedDragActions() const override; + + bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + private: - QVector<QUrl> items; + void relayout(); + + bool internalAddGroup(QString const & group_name, GroupNode * & out_group); +private: + RootNode root; }; #endif // FAVOURITECOLLECTION_HPP |
