From af39d3a05321d7616fd68abccefec4a132e4bd8b 2017-12-20 09:30:56 From: Thibaud Rabillard Date: 2017-12-20 09:30:56 Subject: [PATCH] Refactoring of catalogue: use a custom item class --- diff --git a/gui/include/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h b/gui/include/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h new file mode 100644 index 0000000..f520332 --- /dev/null +++ b/gui/include/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h @@ -0,0 +1,34 @@ +#ifndef SCIQLOP_CATALOGUEABSTRACTTREEITEM_H +#define SCIQLOP_CATALOGUEABSTRACTTREEITEM_H + +#include +#include +#include + +class QMimeData; + +class CatalogueAbstractTreeItem { +public: + constexpr static const int DEFAULT_TYPE = -1; + + CatalogueAbstractTreeItem(int type = DEFAULT_TYPE); + virtual ~CatalogueAbstractTreeItem(); + + void addChild(CatalogueAbstractTreeItem *child); + QVector children() const; + CatalogueAbstractTreeItem *parent() const; + + int type() const; + QString text(int column = 0) const; + + virtual QVariant data(int column, int role) const; + virtual Qt::ItemFlags flags(int column) const; + virtual bool setData(int column, int role, const QVariant &value); + virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action); + +private: + class CatalogueAbstractTreeItemPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_CATALOGUEABSTRACTTREEITEM_H diff --git a/gui/include/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h b/gui/include/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h new file mode 100644 index 0000000..fc345c8 --- /dev/null +++ b/gui/include/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h @@ -0,0 +1,23 @@ +#ifndef SCIQLOP_CATALOGUETEXTTREEITEM_H +#define SCIQLOP_CATALOGUETEXTTREEITEM_H + +#include +#include + +class CatalogueTextTreeItem : public CatalogueAbstractTreeItem { +public: + CatalogueTextTreeItem(const QIcon &icon, const QString &text, int type); + + QVariant data(int column, int role) const override; + Qt::ItemFlags flags(int column) const override; + + QString text() const; + + void setEnabled(bool value); + +private: + class CatalogueTextTreeItemPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_CATALOGUETEXTTREEITEM_H diff --git a/gui/include/Catalogue/CatalogueTreeItems/CatalogueTreeItem.h b/gui/include/Catalogue/CatalogueTreeItems/CatalogueTreeItem.h new file mode 100644 index 0000000..7165cdc --- /dev/null +++ b/gui/include/Catalogue/CatalogueTreeItems/CatalogueTreeItem.h @@ -0,0 +1,27 @@ +#ifndef SCIQLOP_CATALOGUETREEITEM_H +#define SCIQLOP_CATALOGUETREEITEM_H + +#include +#include + +class DBCatalogue; + + +class CatalogueTreeItem : public CatalogueAbstractTreeItem { +public: + CatalogueTreeItem(std::shared_ptr catalogue, const QIcon &icon, int type); + + QVariant data(int column, int role) const override; + bool setData(int column, int role, const QVariant &value) override; + Qt::ItemFlags flags(int column) const override; + bool canDropMimeData(const QMimeData *data, Qt::DropAction action) override; + + /// Returns the catalogue represented by the item + std::shared_ptr catalogue() const; + +private: + class CatalogueTreeItemPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_CATALOGUETREEITEM_H diff --git a/gui/include/Catalogue/CatalogueTreeModel.h b/gui/include/Catalogue/CatalogueTreeModel.h index 3d2447e..592a276 100644 --- a/gui/include/Catalogue/CatalogueTreeModel.h +++ b/gui/include/Catalogue/CatalogueTreeModel.h @@ -3,11 +3,13 @@ #include #include -#include + +class CatalogueAbstractTreeItem; /** * @brief Model to display catalogue items based on QTreeWidgetItem - * @warning Do not use the method QTreeWidgetItem::treeWidget for an item added to this model or the application will crash + * @warning Do not use the method QTreeWidgetItem::treeWidget for an item added to this model or the + * application will crash */ class CatalogueTreeModel : public QAbstractItemModel { Q_OBJECT @@ -20,14 +22,13 @@ public: enum class Column { Name, Validation, Count }; - QModelIndex addTopLevelItem(QTreeWidgetItem *item); - int topLevelItemCount() const; - QTreeWidgetItem *topLevelItem(int i) const; + QModelIndex addTopLevelItem(CatalogueAbstractTreeItem *item); + QVector topLevelItems() const; - void addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex); + void addChildItem(CatalogueAbstractTreeItem *child, const QModelIndex &parentIndex); - QTreeWidgetItem *item(const QModelIndex &index) const; - QModelIndex indexOf(QTreeWidgetItem *item, int column = 0) const; + CatalogueAbstractTreeItem *item(const QModelIndex &index) const; + QModelIndex indexOf(CatalogueAbstractTreeItem *item, int column = 0) const; // model QModelIndex index(int row, int column, @@ -39,6 +40,13 @@ public: QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) 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; + QStringList mimeTypes() const; + private: class CatalogueTreeModelPrivate; spimpl::unique_impl_ptr impl; diff --git a/gui/include/Catalogue/CatalogueTreeWidgetItem.h b/gui/include/Catalogue/CatalogueTreeWidgetItem.h deleted file mode 100644 index 8480c7c..0000000 --- a/gui/include/Catalogue/CatalogueTreeWidgetItem.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef SCIQLOP_CATALOGUETREEWIDGETITEM_H -#define SCIQLOP_CATALOGUETREEWIDGETITEM_H - -#include -#include - -class DBCatalogue; - - -class CatalogueTreeWidgetItem : public QTreeWidgetItem { -public: - CatalogueTreeWidgetItem(std::shared_ptr catalogue, - int type = QTreeWidgetItem::Type); - - QVariant data(int column, int role) const override; - void setData(int column, int role, const QVariant &value) override; - - /// Returns the catalogue represented by the item - std::shared_ptr catalogue() const; - - void setHasChanges(bool value); - - /// Returns true if the widget indicating the event has unsaved changes is displayed - bool hasChanges(); - - /// Refreshes the data displayed by the item from the catalogue - void refresh(); - -private: - class CatalogueTreeWidgetItemPrivate; - spimpl::unique_impl_ptr impl; -}; - -#endif // SCIQLOP_CATALOGUETREEWIDGETITEM_H diff --git a/gui/meson.build b/gui/meson.build index 135206a..add3dca 100644 --- a/gui/meson.build +++ b/gui/meson.build @@ -119,7 +119,9 @@ gui_sources = [ 'src/Catalogue/CatalogueEventsWidget.cpp', 'src/Catalogue/CatalogueSideBarWidget.cpp', 'src/Catalogue/CatalogueInspectorWidget.cpp', - 'src/Catalogue/CatalogueTreeWidgetItem.cpp', + 'src/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.cpp', + 'src/Catalogue/CatalogueTreeItems/CatalogueTreeItem.cpp', + 'src/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.cpp', 'src/Catalogue/CatalogueEventsModel.cpp', 'src/Catalogue/CatalogueExplorerHelper.cpp', 'src/Catalogue/CatalogueActionManager.cpp', diff --git a/gui/src/Catalogue/CatalogueSideBarWidget.cpp b/gui/src/Catalogue/CatalogueSideBarWidget.cpp index 6cf2bcb..c6088a4 100644 --- a/gui/src/Catalogue/CatalogueSideBarWidget.cpp +++ b/gui/src/Catalogue/CatalogueSideBarWidget.cpp @@ -4,8 +4,9 @@ #include #include +#include +#include #include -#include #include #include #include @@ -15,10 +16,10 @@ Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget") -constexpr auto ALL_EVENT_ITEM_TYPE = QTreeWidgetItem::UserType; -constexpr auto TRASH_ITEM_TYPE = QTreeWidgetItem::UserType + 1; -constexpr auto CATALOGUE_ITEM_TYPE = QTreeWidgetItem::UserType + 2; -constexpr auto DATABASE_ITEM_TYPE = QTreeWidgetItem::UserType + 3; +constexpr auto ALL_EVENT_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 1; +constexpr auto TRASH_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 2; +constexpr auto CATALOGUE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 3; +constexpr auto DATABASE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 4; struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate { @@ -27,11 +28,11 @@ struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate { void configureTreeWidget(QTreeView *treeView); QModelIndex addDatabaseItem(const QString &name); - QTreeWidgetItem *getDatabaseItem(const QString &name); + CatalogueAbstractTreeItem *getDatabaseItem(const QString &name); void addCatalogueItem(const std::shared_ptr &catalogue, const QModelIndex &databaseIndex); - CatalogueTreeWidgetItem *getCatalogueItem(const std::shared_ptr &catalogue) const; + CatalogueTreeItem *getCatalogueItem(const std::shared_ptr &catalogue) const; void setHasChanges(bool value, const QModelIndex &index, QTreeView *treeView); bool hasChanges(const QModelIndex &index, QTreeView *treeView); }; @@ -74,12 +75,10 @@ CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent) if (item && item->type() == selectionType) { switch (selectionType) { case CATALOGUE_ITEM_TYPE: - catalogues.append( - static_cast(item)->catalogue()); + catalogues.append(static_cast(item)->catalogue()); break; case DATABASE_ITEM_TYPE: - selectionType = DATABASE_ITEM_TYPE; - databases.append(item->text(0)); + databases.append(item->text()); case ALL_EVENT_ITEM_TYPE: // fallthrough case TRASH_ITEM_TYPE: // fallthrough default: @@ -128,7 +127,7 @@ CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent) }); ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->treeView, &QTreeWidget::customContextMenuRequested, this, + connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &CatalogueSideBarWidget::onContextMenuRequested); } @@ -143,7 +142,7 @@ void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptrgetCatalogueItem(catalogue)) { auto index = impl->m_TreeModel->indexOf(catalogueItem); impl->setHasChanges(hasChanges, index, ui->treeView); - catalogueItem->refresh(); + // catalogueItem->refresh(); } } @@ -181,18 +180,19 @@ void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos) void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView) { - auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE}; - allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png")); + auto allEventsItem = new CatalogueTextTreeItem{QIcon{":/icones/allEvents.png"}, "All Events", + ALL_EVENT_ITEM_TYPE}; m_TreeModel->addTopLevelItem(allEventsItem); - auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE}; - trashItem->setIcon(0, QIcon(":/icones/trash.png")); + auto trashItem + = new CatalogueTextTreeItem{QIcon{":/icones/trash.png"}, "Trash", TRASH_ITEM_TYPE}; m_TreeModel->addTopLevelItem(trashItem); auto separator = new QFrame{treeView}; separator->setFrameShape(QFrame::HLine); - auto separatorItem = new QTreeWidgetItem{}; - separatorItem->setFlags(Qt::NoItemFlags); + auto separatorItem + = new CatalogueTextTreeItem{QIcon{}, QString{}, CatalogueAbstractTreeItem::DEFAULT_TYPE}; + separatorItem->setEnabled(false); auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem); treeView->setIndexWidget(separatorIndex, separator); @@ -211,19 +211,18 @@ void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget( QModelIndex CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name) { - auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE}; - databaseItem->setIcon(0, QIcon{":/icones/database.png"}); + auto databaseItem + = new CatalogueTextTreeItem{QIcon{":/icones/database.png"}, {name}, DATABASE_ITEM_TYPE}; auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem); return databaseIndex; } -QTreeWidgetItem * +CatalogueAbstractTreeItem * CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name) { - for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) { - auto item = m_TreeModel->topLevelItem(i); - if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) { + for (auto item : m_TreeModel->topLevelItems()) { + if (item->type() == DATABASE_ITEM_TYPE && item->text() == name) { return item; } } @@ -234,21 +233,19 @@ CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QSt void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem( const std::shared_ptr &catalogue, const QModelIndex &databaseIndex) { - auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE}; - catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"}); + auto catalogueItem + = new CatalogueTreeItem{catalogue, QIcon{":/icones/catalogue.png"}, CATALOGUE_ITEM_TYPE}; m_TreeModel->addChildItem(catalogueItem, databaseIndex); } -CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem( +CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem( const std::shared_ptr &catalogue) const { - for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) { - auto item = m_TreeModel->topLevelItem(i); + for (auto item : m_TreeModel->topLevelItems()) { if (item->type() == DATABASE_ITEM_TYPE) { - for (auto j = 0; j < item->childCount(); ++j) { - auto childItem = item->child(j); + for (auto childItem : item->children()) { if (childItem->type() == CATALOGUE_ITEM_TYPE) { - auto catalogueItem = static_cast(childItem); + auto catalogueItem = static_cast(childItem); if (catalogueItem->catalogue() == catalogue) { return catalogueItem; } diff --git a/gui/src/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.cpp b/gui/src/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.cpp new file mode 100644 index 0000000..dde38ba --- /dev/null +++ b/gui/src/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.cpp @@ -0,0 +1,74 @@ +#include "Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h" + +struct CatalogueAbstractTreeItem::CatalogueAbstractTreeItemPrivate { + int m_Type; + QVector m_Children; + CatalogueAbstractTreeItem *m_Parent = nullptr; + + CatalogueAbstractTreeItemPrivate(int type) : m_Type(type) {} +}; + +CatalogueAbstractTreeItem::CatalogueAbstractTreeItem(int type) + : impl{spimpl::make_unique_impl(type)} +{ +} + +CatalogueAbstractTreeItem::~CatalogueAbstractTreeItem() +{ + qDeleteAll(impl->m_Children); +} + +void CatalogueAbstractTreeItem::addChild(CatalogueAbstractTreeItem *child) +{ + impl->m_Children << child; + child->impl->m_Parent = this; +} + +QVector CatalogueAbstractTreeItem::children() const +{ + return impl->m_Children; +} + +CatalogueAbstractTreeItem *CatalogueAbstractTreeItem::parent() const +{ + return impl->m_Parent; +} + +int CatalogueAbstractTreeItem::type() const +{ + return impl->m_Type; +} + +QString CatalogueAbstractTreeItem::text(int column) const +{ + return data(0, Qt::DisplayRole).toString(); +} + +QVariant CatalogueAbstractTreeItem::data(int column, int role) const +{ + Q_UNUSED(column); + Q_UNUSED(role); + return QVariant(); +} + +Qt::ItemFlags CatalogueAbstractTreeItem::flags(int column) const +{ + Q_UNUSED(column); + return Qt::NoItemFlags; +} + +bool CatalogueAbstractTreeItem::setData(int column, int role, const QVariant &value) +{ + Q_UNUSED(column); + Q_UNUSED(role); + Q_UNUSED(value); + + return false; +} + +bool CatalogueAbstractTreeItem::canDropMimeData(const QMimeData *data, Qt::DropAction action) +{ + Q_UNUSED(data); + Q_UNUSED(action); + return false; +} diff --git a/gui/src/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.cpp b/gui/src/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.cpp new file mode 100644 index 0000000..9360c8c --- /dev/null +++ b/gui/src/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.cpp @@ -0,0 +1,59 @@ +#include "Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h" + +#include + +struct CatalogueTextTreeItem::CatalogueTextTreeItemPrivate { + + QString m_Text; + QIcon m_Icon; + bool m_IsEnabled = true; + + CatalogueTextTreeItemPrivate(const QIcon &icon, const QString &text) + : m_Text(text), m_Icon(icon) + { + } +}; + + +CatalogueTextTreeItem::CatalogueTextTreeItem(const QIcon &icon, const QString &text, int type) + : CatalogueAbstractTreeItem(type), + impl{spimpl::make_unique_impl(icon, text)} +{ +} + +QVariant CatalogueTextTreeItem::data(int column, int role) const +{ + if (column > 0) { + return QVariant(); + } + + switch (role) { + case Qt::DisplayRole: + return impl->m_Text; + case Qt::DecorationRole: + return impl->m_Icon; + } + + return QVariant(); +} + +Qt::ItemFlags CatalogueTextTreeItem::flags(int column) const +{ + Q_UNUSED(column); + + if (!impl->m_IsEnabled) { + return Qt::NoItemFlags; + } + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +QString CatalogueTextTreeItem::text() const +{ + return impl->m_Text; +} + +void CatalogueTextTreeItem::setEnabled(bool value) +{ + impl->m_IsEnabled = value; +} diff --git a/gui/src/Catalogue/CatalogueTreeWidgetItem.cpp b/gui/src/Catalogue/CatalogueTreeItems/CatalogueTreeItem.cpp similarity index 58% rename from gui/src/Catalogue/CatalogueTreeWidgetItem.cpp rename to gui/src/Catalogue/CatalogueTreeItems/CatalogueTreeItem.cpp index 6726cbc..55c1994 100644 --- a/gui/src/Catalogue/CatalogueTreeWidgetItem.cpp +++ b/gui/src/Catalogue/CatalogueTreeItems/CatalogueTreeItem.cpp @@ -1,69 +1,85 @@ -#include "Catalogue/CatalogueTreeWidgetItem.h" +#include "Catalogue/CatalogueTreeItems/CatalogueTreeItem.h" #include #include +#include +#include +#include #include #include #include -/// Column in the tree widget where the apply and cancel buttons must appear -const auto APPLY_CANCEL_BUTTONS_COLUMN = 1; - -struct CatalogueTreeWidgetItem::CatalogueTreeWidgetItemPrivate { +struct CatalogueTreeItem::CatalogueTreeItemPrivate { std::shared_ptr m_Catalogue; + QIcon m_Icon; - CatalogueTreeWidgetItemPrivate(std::shared_ptr catalogue) : m_Catalogue(catalogue) + CatalogueTreeItemPrivate(std::shared_ptr catalogue, const QIcon &icon) + : m_Catalogue(catalogue), m_Icon(icon) { } }; -CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(std::shared_ptr catalogue, int type) - : QTreeWidgetItem(type), - impl{spimpl::make_unique_impl(catalogue)} +CatalogueTreeItem::CatalogueTreeItem(std::shared_ptr catalogue, const QIcon &icon, + int type) + : CatalogueAbstractTreeItem(type), + impl{spimpl::make_unique_impl(catalogue, icon)} { - setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled); } -QVariant CatalogueTreeWidgetItem::data(int column, int role) const +QVariant CatalogueTreeItem::data(int column, int role) const { if (column == 0) { switch (role) { case Qt::EditRole: // fallthrough case Qt::DisplayRole: return impl->m_Catalogue->getName(); + case Qt::DecorationRole: + return impl->m_Icon; default: break; } } - return QTreeWidgetItem::data(column, role); + return QVariant(); } -void CatalogueTreeWidgetItem::setData(int column, int role, const QVariant &value) +bool CatalogueTreeItem::setData(int column, int role, const QVariant &value) { + bool result = false; + if (role == Qt::EditRole && column == 0) { auto newName = value.toString(); if (newName != impl->m_Catalogue->getName()) { - setText(0, newName); impl->m_Catalogue->setName(newName); sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue); + result = true; } } + + return result; +} + +Qt::ItemFlags CatalogueTreeItem::flags(int column) const +{ + if (column == 0) { + return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable + | Qt::ItemIsDropEnabled; + } else { - QTreeWidgetItem::setData(column, role, value); + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } } -std::shared_ptr CatalogueTreeWidgetItem::catalogue() const +bool CatalogueTreeItem::canDropMimeData(const QMimeData *data, Qt::DropAction action) { - return impl->m_Catalogue; + return data->hasFormat(MIME_TYPE_EVENT_LIST); } -void CatalogueTreeWidgetItem::refresh() +std::shared_ptr CatalogueTreeItem::catalogue() const { - emitDataChanged(); + return impl->m_Catalogue; } diff --git a/gui/src/Catalogue/CatalogueTreeModel.cpp b/gui/src/Catalogue/CatalogueTreeModel.cpp index da7a33d..43cb96f 100644 --- a/gui/src/Catalogue/CatalogueTreeModel.cpp +++ b/gui/src/Catalogue/CatalogueTreeModel.cpp @@ -1,12 +1,15 @@ #include "Catalogue/CatalogueTreeModel.h" +#include -#include +#include #include +#include + struct CatalogueTreeModel::CatalogueTreeModelPrivate { - std::unique_ptr m_RootItem = nullptr; + std::unique_ptr m_RootItem = nullptr; - CatalogueTreeModelPrivate() : m_RootItem{std::make_unique()} {} + CatalogueTreeModelPrivate() : m_RootItem{std::make_unique()} {} }; CatalogueTreeModel::CatalogueTreeModel(QObject *parent) @@ -14,65 +17,71 @@ CatalogueTreeModel::CatalogueTreeModel(QObject *parent) { } -QModelIndex CatalogueTreeModel::addTopLevelItem(QTreeWidgetItem *item) +QModelIndex CatalogueTreeModel::addTopLevelItem(CatalogueAbstractTreeItem *item) { - beginInsertRows(QModelIndex(), impl->m_RootItem->childCount(), impl->m_RootItem->childCount()); + auto nbTopLevelItems = impl->m_RootItem->children().count(); + beginInsertRows(QModelIndex(), nbTopLevelItems, nbTopLevelItems); impl->m_RootItem->addChild(item); endInsertRows(); - return index(impl->m_RootItem->childCount() - 1, 0); -} + emit dataChanged(QModelIndex(), QModelIndex()); -int CatalogueTreeModel::topLevelItemCount() const -{ - return impl->m_RootItem->childCount(); + return index(nbTopLevelItems, 0); } -QTreeWidgetItem *CatalogueTreeModel::topLevelItem(int i) const +QVector CatalogueTreeModel::topLevelItems() const { - return impl->m_RootItem->child(i); + return impl->m_RootItem->children(); } -void CatalogueTreeModel::addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex) +void CatalogueTreeModel::addChildItem(CatalogueAbstractTreeItem *child, + const QModelIndex &parentIndex) { auto parentItem = item(parentIndex); - beginInsertRows(parentIndex, parentItem->childCount(), parentItem->childCount()); + int c = parentItem->children().count(); + beginInsertRows(parentIndex, c, c); parentItem->addChild(child); endInsertRows(); + + emit dataChanged(QModelIndex(), QModelIndex()); } -QTreeWidgetItem *CatalogueTreeModel::item(const QModelIndex &index) const +CatalogueAbstractTreeItem *CatalogueTreeModel::item(const QModelIndex &index) const { - return static_cast(index.internalPointer()); + return static_cast(index.internalPointer()); } -QModelIndex CatalogueTreeModel::indexOf(QTreeWidgetItem *item, int column) const +QModelIndex CatalogueTreeModel::indexOf(CatalogueAbstractTreeItem *item, int column) const { auto parentItem = item->parent(); if (!parentItem) { return QModelIndex(); } - auto row = parentItem->indexOfChild(item); + auto row = parentItem->children().indexOf(item); return createIndex(row, column, item); } QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const { + if (column > 0) { + int a = 0; + } + if (!hasIndex(row, column, parent)) { return QModelIndex(); } - QTreeWidgetItem *parentItem = nullptr; + CatalogueAbstractTreeItem *parentItem = nullptr; if (!parent.isValid()) { parentItem = impl->m_RootItem.get(); } else { - parentItem = static_cast(parent.internalPointer()); + parentItem = item(parent); } - QTreeWidgetItem *childItem = parentItem->child(row); + auto childItem = parentItem->children().value(row); if (childItem) { return createIndex(row, column, childItem); } @@ -87,32 +96,29 @@ QModelIndex CatalogueTreeModel::parent(const QModelIndex &index) const return QModelIndex(); } - auto childItem = static_cast(index.internalPointer()); + auto childItem = item(index); auto parentItem = childItem->parent(); if (parentItem == nullptr || parentItem->parent() == nullptr) { return QModelIndex(); } - auto row = parentItem->parent()->indexOfChild(parentItem); + auto row = parentItem->parent()->children().indexOf(parentItem); return createIndex(row, 0, parentItem); } int CatalogueTreeModel::rowCount(const QModelIndex &parent) const { - QTreeWidgetItem *parentItem = nullptr; - if (parent.column() > 0) { - return 0; - } + CatalogueAbstractTreeItem *parentItem = nullptr; if (!parent.isValid()) { parentItem = impl->m_RootItem.get(); } else { - parentItem = static_cast(parent.internalPointer()); + parentItem = item(parent); } - return parentItem->childCount(); + return parentItem->children().count(); } int CatalogueTreeModel::columnCount(const QModelIndex &parent) const @@ -122,13 +128,9 @@ int CatalogueTreeModel::columnCount(const QModelIndex &parent) const Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const { - if (index.column() == (int)Column::Validation) { - return Qt::NoItemFlags; - } - - auto item = static_cast(index.internalPointer()); - if (item) { - return item->flags(); + auto treeItem = item(index); + if (treeItem) { + return treeItem->flags(index.column()); } return Qt::NoItemFlags; @@ -136,13 +138,9 @@ Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) { - return QModelIndex(); - } - - auto item = static_cast(index.internalPointer()); - if (item) { - return item->data(index.column(), role); + auto treeItem = item(index); + if (treeItem) { + return treeItem->data(index.column(), role); } return QModelIndex(); @@ -150,20 +148,43 @@ QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (!index.isValid()) { - return false; - } - - auto item = static_cast(index.internalPointer()); - if (item) { - item->setData(index.column(), role, value); + auto treeItem = item(index); + if (treeItem) { + auto result = treeItem->setData(index.column(), role, value); - if (index.column() == (int)Column::Name) { + if (result && index.column() == (int)Column::Name) { emit itemRenamed(index); } - return true; + return result; } return false; } +bool CatalogueTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, + int column, const QModelIndex &parent) const +{ + auto draggedIndex = parent; + auto draggedItem = item(draggedIndex); + if (draggedItem) { + return draggedItem->canDropMimeData(data, action); + } + + return false; +} + +bool CatalogueTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, + int column, const QModelIndex &parent) +{ + return false; +} + +Qt::DropActions CatalogueTreeModel::supportedDropActions() const +{ + return Qt::CopyAction | Qt::MoveAction; +} + +QStringList CatalogueTreeModel::mimeTypes() const +{ + return {MIME_TYPE_EVENT_LIST}; +} diff --git a/gui/ui/Catalogue/CatalogueSideBarWidget.ui b/gui/ui/Catalogue/CatalogueSideBarWidget.ui index 06a2e35..c8cadef 100644 --- a/gui/ui/Catalogue/CatalogueSideBarWidget.ui +++ b/gui/ui/Catalogue/CatalogueSideBarWidget.ui @@ -77,19 +77,13 @@ true - QAbstractItemView::DropOnly + QAbstractItemView::DragDrop - - Qt::CopyAction - - - QAbstractItemView::ExtendedSelection - - - true - - - 0 + + false + + + false