#ifndef REPOSITORIESMODEL_H #define REPOSITORIESMODEL_H #include #include #include #include class RepositoriesModel : public QAbstractItemModel { Q_OBJECT enum class ItemType { None, Catalogue, Repository }; struct RepoModelItem { ItemType type; std::variant item; RepoModelItem() : type { ItemType::None } {} RepoModelItem(const QString& repo); RepoModelItem(const CatalogueController::Catalogue_ptr& catalogue, RepoModelItem* parent) : type { ItemType::Catalogue } , item { catalogue } , parent { parent } , icon { ":/icones/catalogue.png" } { } QString repository() const { return std::get(item); } CatalogueController::Catalogue_ptr catalogue() const { return std::get(item); } QVariant data(int role) const; QString text() const { if (type == ItemType::Catalogue) return QString::fromStdString(catalogue()->name); if (type == ItemType::Repository) return repository(); return QString(); } std::vector> children; RepoModelItem* parent = nullptr; QIcon icon; }; std::vector> _items; inline RepoModelItem* to_item(const QModelIndex& index) const { return static_cast(index.internalPointer()); } public: RepositoriesModel(QObject* parent = nullptr); ItemType type(const QModelIndex& index) const; Qt::ItemFlags flags(const QModelIndex& index) const override { return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled; } QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex& index) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override; int columnCount(const QModelIndex& parent = QModelIndex()) const override { return 1; } public slots: void refresh(); }; #endif // REPOSITORIESMODEL_H