From 8e910f26a28b1b1beae363e6c19f39224f74e2e8 Mon Sep 17 00:00:00 2001 From: "Felix (xq) Queißner" Date: Mon, 29 Jun 2020 20:17:42 +0200 Subject: Paves road for new favourite system: Refactors the favourites into tree structure instead of flat list. --- src/favouritecollection.hpp | 129 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 111 insertions(+), 18 deletions(-) (limited to 'src/favouritecollection.hpp') 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 -#include +#include #include +#include +#include #include +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> children; + Type type; + explicit Node(Type t) : type(t) { } + virtual ~Node() = default; + + template + T & as() { return *static_cast(this); } + + template + T const & as() const { return *static_cast(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 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 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 items; + void relayout(); + + bool internalAddGroup(QString const & group_name, GroupNode * & out_group); +private: + RootNode root; }; #endif // FAVOURITECOLLECTION_HPP -- cgit v1.2.3