##// END OF EJS Templates
Refactoring of catalogue: use a custom item class
trabillard -
r1260:af39d3a05321
parent child
Show More
@@ -0,0 +1,34
1 #ifndef SCIQLOP_CATALOGUEABSTRACTTREEITEM_H
2 #define SCIQLOP_CATALOGUEABSTRACTTREEITEM_H
3
4 #include <Common/spimpl.h>
5 #include <QVariant>
6 #include <QVector>
7
8 class QMimeData;
9
10 class CatalogueAbstractTreeItem {
11 public:
12 constexpr static const int DEFAULT_TYPE = -1;
13
14 CatalogueAbstractTreeItem(int type = DEFAULT_TYPE);
15 virtual ~CatalogueAbstractTreeItem();
16
17 void addChild(CatalogueAbstractTreeItem *child);
18 QVector<CatalogueAbstractTreeItem *> children() const;
19 CatalogueAbstractTreeItem *parent() const;
20
21 int type() const;
22 QString text(int column = 0) const;
23
24 virtual QVariant data(int column, int role) const;
25 virtual Qt::ItemFlags flags(int column) const;
26 virtual bool setData(int column, int role, const QVariant &value);
27 virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action);
28
29 private:
30 class CatalogueAbstractTreeItemPrivate;
31 spimpl::unique_impl_ptr<CatalogueAbstractTreeItemPrivate> impl;
32 };
33
34 #endif // SCIQLOP_CATALOGUEABSTRACTTREEITEM_H
@@ -0,0 +1,23
1 #ifndef SCIQLOP_CATALOGUETEXTTREEITEM_H
2 #define SCIQLOP_CATALOGUETEXTTREEITEM_H
3
4 #include <Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h>
5 #include <Common/spimpl.h>
6
7 class CatalogueTextTreeItem : public CatalogueAbstractTreeItem {
8 public:
9 CatalogueTextTreeItem(const QIcon &icon, const QString &text, int type);
10
11 QVariant data(int column, int role) const override;
12 Qt::ItemFlags flags(int column) const override;
13
14 QString text() const;
15
16 void setEnabled(bool value);
17
18 private:
19 class CatalogueTextTreeItemPrivate;
20 spimpl::unique_impl_ptr<CatalogueTextTreeItemPrivate> impl;
21 };
22
23 #endif // SCIQLOP_CATALOGUETEXTTREEITEM_H
@@ -0,0 +1,27
1 #ifndef SCIQLOP_CATALOGUETREEITEM_H
2 #define SCIQLOP_CATALOGUETREEITEM_H
3
4 #include <Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h>
5 #include <Common/spimpl.h>
6
7 class DBCatalogue;
8
9
10 class CatalogueTreeItem : public CatalogueAbstractTreeItem {
11 public:
12 CatalogueTreeItem(std::shared_ptr<DBCatalogue> catalogue, const QIcon &icon, int type);
13
14 QVariant data(int column, int role) const override;
15 bool setData(int column, int role, const QVariant &value) override;
16 Qt::ItemFlags flags(int column) const override;
17 bool canDropMimeData(const QMimeData *data, Qt::DropAction action) override;
18
19 /// Returns the catalogue represented by the item
20 std::shared_ptr<DBCatalogue> catalogue() const;
21
22 private:
23 class CatalogueTreeItemPrivate;
24 spimpl::unique_impl_ptr<CatalogueTreeItemPrivate> impl;
25 };
26
27 #endif // SCIQLOP_CATALOGUETREEITEM_H
@@ -0,0 +1,74
1 #include "Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h"
2
3 struct CatalogueAbstractTreeItem::CatalogueAbstractTreeItemPrivate {
4 int m_Type;
5 QVector<CatalogueAbstractTreeItem *> m_Children;
6 CatalogueAbstractTreeItem *m_Parent = nullptr;
7
8 CatalogueAbstractTreeItemPrivate(int type) : m_Type(type) {}
9 };
10
11 CatalogueAbstractTreeItem::CatalogueAbstractTreeItem(int type)
12 : impl{spimpl::make_unique_impl<CatalogueAbstractTreeItemPrivate>(type)}
13 {
14 }
15
16 CatalogueAbstractTreeItem::~CatalogueAbstractTreeItem()
17 {
18 qDeleteAll(impl->m_Children);
19 }
20
21 void CatalogueAbstractTreeItem::addChild(CatalogueAbstractTreeItem *child)
22 {
23 impl->m_Children << child;
24 child->impl->m_Parent = this;
25 }
26
27 QVector<CatalogueAbstractTreeItem *> CatalogueAbstractTreeItem::children() const
28 {
29 return impl->m_Children;
30 }
31
32 CatalogueAbstractTreeItem *CatalogueAbstractTreeItem::parent() const
33 {
34 return impl->m_Parent;
35 }
36
37 int CatalogueAbstractTreeItem::type() const
38 {
39 return impl->m_Type;
40 }
41
42 QString CatalogueAbstractTreeItem::text(int column) const
43 {
44 return data(0, Qt::DisplayRole).toString();
45 }
46
47 QVariant CatalogueAbstractTreeItem::data(int column, int role) const
48 {
49 Q_UNUSED(column);
50 Q_UNUSED(role);
51 return QVariant();
52 }
53
54 Qt::ItemFlags CatalogueAbstractTreeItem::flags(int column) const
55 {
56 Q_UNUSED(column);
57 return Qt::NoItemFlags;
58 }
59
60 bool CatalogueAbstractTreeItem::setData(int column, int role, const QVariant &value)
61 {
62 Q_UNUSED(column);
63 Q_UNUSED(role);
64 Q_UNUSED(value);
65
66 return false;
67 }
68
69 bool CatalogueAbstractTreeItem::canDropMimeData(const QMimeData *data, Qt::DropAction action)
70 {
71 Q_UNUSED(data);
72 Q_UNUSED(action);
73 return false;
74 }
@@ -0,0 +1,59
1 #include "Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h"
2
3 #include <QIcon>
4
5 struct CatalogueTextTreeItem::CatalogueTextTreeItemPrivate {
6
7 QString m_Text;
8 QIcon m_Icon;
9 bool m_IsEnabled = true;
10
11 CatalogueTextTreeItemPrivate(const QIcon &icon, const QString &text)
12 : m_Text(text), m_Icon(icon)
13 {
14 }
15 };
16
17
18 CatalogueTextTreeItem::CatalogueTextTreeItem(const QIcon &icon, const QString &text, int type)
19 : CatalogueAbstractTreeItem(type),
20 impl{spimpl::make_unique_impl<CatalogueTextTreeItemPrivate>(icon, text)}
21 {
22 }
23
24 QVariant CatalogueTextTreeItem::data(int column, int role) const
25 {
26 if (column > 0) {
27 return QVariant();
28 }
29
30 switch (role) {
31 case Qt::DisplayRole:
32 return impl->m_Text;
33 case Qt::DecorationRole:
34 return impl->m_Icon;
35 }
36
37 return QVariant();
38 }
39
40 Qt::ItemFlags CatalogueTextTreeItem::flags(int column) const
41 {
42 Q_UNUSED(column);
43
44 if (!impl->m_IsEnabled) {
45 return Qt::NoItemFlags;
46 }
47
48 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
49 }
50
51 QString CatalogueTextTreeItem::text() const
52 {
53 return impl->m_Text;
54 }
55
56 void CatalogueTextTreeItem::setEnabled(bool value)
57 {
58 impl->m_IsEnabled = value;
59 }
@@ -3,11 +3,13
3
3
4 #include <Common/spimpl.h>
4 #include <Common/spimpl.h>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include <QTreeWidgetItem>
6
7 class CatalogueAbstractTreeItem;
7
8
8 /**
9 /**
9 * @brief Model to display catalogue items based on QTreeWidgetItem
10 * @brief Model to display catalogue items based on QTreeWidgetItem
10 * @warning Do not use the method QTreeWidgetItem::treeWidget for an item added to this model or the application will crash
11 * @warning Do not use the method QTreeWidgetItem::treeWidget for an item added to this model or the
12 * application will crash
11 */
13 */
12 class CatalogueTreeModel : public QAbstractItemModel {
14 class CatalogueTreeModel : public QAbstractItemModel {
13 Q_OBJECT
15 Q_OBJECT
@@ -20,14 +22,13 public:
20
22
21 enum class Column { Name, Validation, Count };
23 enum class Column { Name, Validation, Count };
22
24
23 QModelIndex addTopLevelItem(QTreeWidgetItem *item);
25 QModelIndex addTopLevelItem(CatalogueAbstractTreeItem *item);
24 int topLevelItemCount() const;
26 QVector<CatalogueAbstractTreeItem *> topLevelItems() const;
25 QTreeWidgetItem *topLevelItem(int i) const;
26
27
27 void addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex);
28 void addChildItem(CatalogueAbstractTreeItem *child, const QModelIndex &parentIndex);
28
29
29 QTreeWidgetItem *item(const QModelIndex &index) const;
30 CatalogueAbstractTreeItem *item(const QModelIndex &index) const;
30 QModelIndex indexOf(QTreeWidgetItem *item, int column = 0) const;
31 QModelIndex indexOf(CatalogueAbstractTreeItem *item, int column = 0) const;
31
32
32 // model
33 // model
33 QModelIndex index(int row, int column,
34 QModelIndex index(int row, int column,
@@ -39,6 +40,13 public:
39 QVariant data(const QModelIndex &index, int role) const override;
40 QVariant data(const QModelIndex &index, int role) const override;
40 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
41 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
41
42
43 bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
44 const QModelIndex &parent) const override;
45 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
46 const QModelIndex &parent) override;
47 Qt::DropActions supportedDropActions() const;
48 QStringList mimeTypes() const;
49
42 private:
50 private:
43 class CatalogueTreeModelPrivate;
51 class CatalogueTreeModelPrivate;
44 spimpl::unique_impl_ptr<CatalogueTreeModelPrivate> impl;
52 spimpl::unique_impl_ptr<CatalogueTreeModelPrivate> impl;
@@ -119,7 +119,9 gui_sources = [
119 'src/Catalogue/CatalogueEventsWidget.cpp',
119 'src/Catalogue/CatalogueEventsWidget.cpp',
120 'src/Catalogue/CatalogueSideBarWidget.cpp',
120 'src/Catalogue/CatalogueSideBarWidget.cpp',
121 'src/Catalogue/CatalogueInspectorWidget.cpp',
121 'src/Catalogue/CatalogueInspectorWidget.cpp',
122 'src/Catalogue/CatalogueTreeWidgetItem.cpp',
122 'src/Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.cpp',
123 'src/Catalogue/CatalogueTreeItems/CatalogueTreeItem.cpp',
124 'src/Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.cpp',
123 'src/Catalogue/CatalogueEventsModel.cpp',
125 'src/Catalogue/CatalogueEventsModel.cpp',
124 'src/Catalogue/CatalogueExplorerHelper.cpp',
126 'src/Catalogue/CatalogueExplorerHelper.cpp',
125 'src/Catalogue/CatalogueActionManager.cpp',
127 'src/Catalogue/CatalogueActionManager.cpp',
@@ -4,8 +4,9
4
4
5 #include <Catalogue/CatalogueController.h>
5 #include <Catalogue/CatalogueController.h>
6 #include <Catalogue/CatalogueExplorerHelper.h>
6 #include <Catalogue/CatalogueExplorerHelper.h>
7 #include <Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h>
8 #include <Catalogue/CatalogueTreeItems/CatalogueTreeItem.h>
7 #include <Catalogue/CatalogueTreeModel.h>
9 #include <Catalogue/CatalogueTreeModel.h>
8 #include <Catalogue/CatalogueTreeWidgetItem.h>
9 #include <CatalogueDao.h>
10 #include <CatalogueDao.h>
10 #include <ComparaisonPredicate.h>
11 #include <ComparaisonPredicate.h>
11 #include <DBCatalogue.h>
12 #include <DBCatalogue.h>
@@ -15,10 +16,10
15 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
16 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
16
17
17
18
18 constexpr auto ALL_EVENT_ITEM_TYPE = QTreeWidgetItem::UserType;
19 constexpr auto ALL_EVENT_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 1;
19 constexpr auto TRASH_ITEM_TYPE = QTreeWidgetItem::UserType + 1;
20 constexpr auto TRASH_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 2;
20 constexpr auto CATALOGUE_ITEM_TYPE = QTreeWidgetItem::UserType + 2;
21 constexpr auto CATALOGUE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 3;
21 constexpr auto DATABASE_ITEM_TYPE = QTreeWidgetItem::UserType + 3;
22 constexpr auto DATABASE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 4;
22
23
23
24
24 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
25 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
@@ -27,11 +28,11 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
27
28
28 void configureTreeWidget(QTreeView *treeView);
29 void configureTreeWidget(QTreeView *treeView);
29 QModelIndex addDatabaseItem(const QString &name);
30 QModelIndex addDatabaseItem(const QString &name);
30 QTreeWidgetItem *getDatabaseItem(const QString &name);
31 CatalogueAbstractTreeItem *getDatabaseItem(const QString &name);
31 void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
32 void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
32 const QModelIndex &databaseIndex);
33 const QModelIndex &databaseIndex);
33
34
34 CatalogueTreeWidgetItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
35 CatalogueTreeItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
35 void setHasChanges(bool value, const QModelIndex &index, QTreeView *treeView);
36 void setHasChanges(bool value, const QModelIndex &index, QTreeView *treeView);
36 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
37 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
37 };
38 };
@@ -74,12 +75,10 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
74 if (item && item->type() == selectionType) {
75 if (item && item->type() == selectionType) {
75 switch (selectionType) {
76 switch (selectionType) {
76 case CATALOGUE_ITEM_TYPE:
77 case CATALOGUE_ITEM_TYPE:
77 catalogues.append(
78 catalogues.append(static_cast<CatalogueTreeItem *>(item)->catalogue());
78 static_cast<CatalogueTreeWidgetItem *>(item)->catalogue());
79 break;
79 break;
80 case DATABASE_ITEM_TYPE:
80 case DATABASE_ITEM_TYPE:
81 selectionType = DATABASE_ITEM_TYPE;
81 databases.append(item->text());
82 databases.append(item->text(0));
83 case ALL_EVENT_ITEM_TYPE: // fallthrough
82 case ALL_EVENT_ITEM_TYPE: // fallthrough
84 case TRASH_ITEM_TYPE: // fallthrough
83 case TRASH_ITEM_TYPE: // fallthrough
85 default:
84 default:
@@ -128,7 +127,7 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
128 });
127 });
129
128
130 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
129 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
131 connect(ui->treeView, &QTreeWidget::customContextMenuRequested, this,
130 connect(ui->treeView, &QTreeView::customContextMenuRequested, this,
132 &CatalogueSideBarWidget::onContextMenuRequested);
131 &CatalogueSideBarWidget::onContextMenuRequested);
133 }
132 }
134
133
@@ -143,7 +142,7 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalog
143 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
142 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
144 auto index = impl->m_TreeModel->indexOf(catalogueItem);
143 auto index = impl->m_TreeModel->indexOf(catalogueItem);
145 impl->setHasChanges(hasChanges, index, ui->treeView);
144 impl->setHasChanges(hasChanges, index, ui->treeView);
146 catalogueItem->refresh();
145 // catalogueItem->refresh();
147 }
146 }
148 }
147 }
149
148
@@ -181,18 +180,19 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
181
180
182 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
181 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
183 {
182 {
184 auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE};
183 auto allEventsItem = new CatalogueTextTreeItem{QIcon{":/icones/allEvents.png"}, "All Events",
185 allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png"));
184 ALL_EVENT_ITEM_TYPE};
186 m_TreeModel->addTopLevelItem(allEventsItem);
185 m_TreeModel->addTopLevelItem(allEventsItem);
187
186
188 auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE};
187 auto trashItem
189 trashItem->setIcon(0, QIcon(":/icones/trash.png"));
188 = new CatalogueTextTreeItem{QIcon{":/icones/trash.png"}, "Trash", TRASH_ITEM_TYPE};
190 m_TreeModel->addTopLevelItem(trashItem);
189 m_TreeModel->addTopLevelItem(trashItem);
191
190
192 auto separator = new QFrame{treeView};
191 auto separator = new QFrame{treeView};
193 separator->setFrameShape(QFrame::HLine);
192 separator->setFrameShape(QFrame::HLine);
194 auto separatorItem = new QTreeWidgetItem{};
193 auto separatorItem
195 separatorItem->setFlags(Qt::NoItemFlags);
194 = new CatalogueTextTreeItem{QIcon{}, QString{}, CatalogueAbstractTreeItem::DEFAULT_TYPE};
195 separatorItem->setEnabled(false);
196 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
196 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
197 treeView->setIndexWidget(separatorIndex, separator);
197 treeView->setIndexWidget(separatorIndex, separator);
198
198
@@ -211,19 +211,18 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(
211 QModelIndex
211 QModelIndex
212 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
212 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
213 {
213 {
214 auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE};
214 auto databaseItem
215 databaseItem->setIcon(0, QIcon{":/icones/database.png"});
215 = new CatalogueTextTreeItem{QIcon{":/icones/database.png"}, {name}, DATABASE_ITEM_TYPE};
216 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
216 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
217
217
218 return databaseIndex;
218 return databaseIndex;
219 }
219 }
220
220
221 QTreeWidgetItem *
221 CatalogueAbstractTreeItem *
222 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
222 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
223 {
223 {
224 for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) {
224 for (auto item : m_TreeModel->topLevelItems()) {
225 auto item = m_TreeModel->topLevelItem(i);
225 if (item->type() == DATABASE_ITEM_TYPE && item->text() == name) {
226 if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) {
227 return item;
226 return item;
228 }
227 }
229 }
228 }
@@ -234,21 +233,19 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QSt
234 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
233 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
235 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
234 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
236 {
235 {
237 auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE};
236 auto catalogueItem
238 catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"});
237 = new CatalogueTreeItem{catalogue, QIcon{":/icones/catalogue.png"}, CATALOGUE_ITEM_TYPE};
239 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
238 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
240 }
239 }
241
240
242 CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
241 CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
243 const std::shared_ptr<DBCatalogue> &catalogue) const
242 const std::shared_ptr<DBCatalogue> &catalogue) const
244 {
243 {
245 for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) {
244 for (auto item : m_TreeModel->topLevelItems()) {
246 auto item = m_TreeModel->topLevelItem(i);
247 if (item->type() == DATABASE_ITEM_TYPE) {
245 if (item->type() == DATABASE_ITEM_TYPE) {
248 for (auto j = 0; j < item->childCount(); ++j) {
246 for (auto childItem : item->children()) {
249 auto childItem = item->child(j);
250 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
247 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
251 auto catalogueItem = static_cast<CatalogueTreeWidgetItem *>(childItem);
248 auto catalogueItem = static_cast<CatalogueTreeItem *>(childItem);
252 if (catalogueItem->catalogue() == catalogue) {
249 if (catalogueItem->catalogue() == catalogue) {
253 return catalogueItem;
250 return catalogueItem;
254 }
251 }
@@ -1,69 +1,85
1 #include "Catalogue/CatalogueTreeWidgetItem.h"
1 #include "Catalogue/CatalogueTreeItems/CatalogueTreeItem.h"
2 #include <Catalogue/CatalogueExplorerHelper.h>
2 #include <Catalogue/CatalogueExplorerHelper.h>
3
3
4 #include <Catalogue/CatalogueController.h>
4 #include <Catalogue/CatalogueController.h>
5 #include <Common/MimeTypesDef.h>
6 #include <QIcon>
7 #include <QMimeData>
5 #include <SqpApplication.h>
8 #include <SqpApplication.h>
6
9
7 #include <memory>
10 #include <memory>
8
11
9 #include <DBCatalogue.h>
12 #include <DBCatalogue.h>
10
13
11 /// Column in the tree widget where the apply and cancel buttons must appear
14 struct CatalogueTreeItem::CatalogueTreeItemPrivate {
12 const auto APPLY_CANCEL_BUTTONS_COLUMN = 1;
13
14 struct CatalogueTreeWidgetItem::CatalogueTreeWidgetItemPrivate {
15
15
16 std::shared_ptr<DBCatalogue> m_Catalogue;
16 std::shared_ptr<DBCatalogue> m_Catalogue;
17 QIcon m_Icon;
17
18
18 CatalogueTreeWidgetItemPrivate(std::shared_ptr<DBCatalogue> catalogue) : m_Catalogue(catalogue)
19 CatalogueTreeItemPrivate(std::shared_ptr<DBCatalogue> catalogue, const QIcon &icon)
20 : m_Catalogue(catalogue), m_Icon(icon)
19 {
21 {
20 }
22 }
21 };
23 };
22
24
23
25
24 CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(std::shared_ptr<DBCatalogue> catalogue, int type)
26 CatalogueTreeItem::CatalogueTreeItem(std::shared_ptr<DBCatalogue> catalogue, const QIcon &icon,
25 : QTreeWidgetItem(type),
27 int type)
26 impl{spimpl::make_unique_impl<CatalogueTreeWidgetItemPrivate>(catalogue)}
28 : CatalogueAbstractTreeItem(type),
29 impl{spimpl::make_unique_impl<CatalogueTreeItemPrivate>(catalogue, icon)}
27 {
30 {
28 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
29 }
31 }
30
32
31 QVariant CatalogueTreeWidgetItem::data(int column, int role) const
33 QVariant CatalogueTreeItem::data(int column, int role) const
32 {
34 {
33 if (column == 0) {
35 if (column == 0) {
34 switch (role) {
36 switch (role) {
35 case Qt::EditRole: // fallthrough
37 case Qt::EditRole: // fallthrough
36 case Qt::DisplayRole:
38 case Qt::DisplayRole:
37 return impl->m_Catalogue->getName();
39 return impl->m_Catalogue->getName();
40 case Qt::DecorationRole:
41 return impl->m_Icon;
38 default:
42 default:
39 break;
43 break;
40 }
44 }
41 }
45 }
42
46
43 return QTreeWidgetItem::data(column, role);
47 return QVariant();
44 }
48 }
45
49
46 void CatalogueTreeWidgetItem::setData(int column, int role, const QVariant &value)
50 bool CatalogueTreeItem::setData(int column, int role, const QVariant &value)
47 {
51 {
52 bool result = false;
53
48 if (role == Qt::EditRole && column == 0) {
54 if (role == Qt::EditRole && column == 0) {
49 auto newName = value.toString();
55 auto newName = value.toString();
50 if (newName != impl->m_Catalogue->getName()) {
56 if (newName != impl->m_Catalogue->getName()) {
51 setText(0, newName);
52 impl->m_Catalogue->setName(newName);
57 impl->m_Catalogue->setName(newName);
53 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
58 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
59 result = true;
54 }
60 }
55 }
61 }
62
63 return result;
64 }
65
66 Qt::ItemFlags CatalogueTreeItem::flags(int column) const
67 {
68 if (column == 0) {
69 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable
70 | Qt::ItemIsDropEnabled;
71 }
56 else {
72 else {
57 QTreeWidgetItem::setData(column, role, value);
73 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
58 }
74 }
59 }
75 }
60
76
61 std::shared_ptr<DBCatalogue> CatalogueTreeWidgetItem::catalogue() const
77 bool CatalogueTreeItem::canDropMimeData(const QMimeData *data, Qt::DropAction action)
62 {
78 {
63 return impl->m_Catalogue;
79 return data->hasFormat(MIME_TYPE_EVENT_LIST);
64 }
80 }
65
81
66 void CatalogueTreeWidgetItem::refresh()
82 std::shared_ptr<DBCatalogue> CatalogueTreeItem::catalogue() const
67 {
83 {
68 emitDataChanged();
84 return impl->m_Catalogue;
69 }
85 }
@@ -1,12 +1,15
1 #include "Catalogue/CatalogueTreeModel.h"
1 #include "Catalogue/CatalogueTreeModel.h"
2 #include <Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h>
2
3
3 #include <QTreeWidgetItem>
4 #include <QMimeData>
4 #include <memory>
5 #include <memory>
5
6
7 #include <Common/MimeTypesDef.h>
8
6 struct CatalogueTreeModel::CatalogueTreeModelPrivate {
9 struct CatalogueTreeModel::CatalogueTreeModelPrivate {
7 std::unique_ptr<QTreeWidgetItem> m_RootItem = nullptr;
10 std::unique_ptr<CatalogueAbstractTreeItem> m_RootItem = nullptr;
8
11
9 CatalogueTreeModelPrivate() : m_RootItem{std::make_unique<QTreeWidgetItem>()} {}
12 CatalogueTreeModelPrivate() : m_RootItem{std::make_unique<CatalogueAbstractTreeItem>()} {}
10 };
13 };
11
14
12 CatalogueTreeModel::CatalogueTreeModel(QObject *parent)
15 CatalogueTreeModel::CatalogueTreeModel(QObject *parent)
@@ -14,65 +17,71 CatalogueTreeModel::CatalogueTreeModel(QObject *parent)
14 {
17 {
15 }
18 }
16
19
17 QModelIndex CatalogueTreeModel::addTopLevelItem(QTreeWidgetItem *item)
20 QModelIndex CatalogueTreeModel::addTopLevelItem(CatalogueAbstractTreeItem *item)
18 {
21 {
19 beginInsertRows(QModelIndex(), impl->m_RootItem->childCount(), impl->m_RootItem->childCount());
22 auto nbTopLevelItems = impl->m_RootItem->children().count();
23 beginInsertRows(QModelIndex(), nbTopLevelItems, nbTopLevelItems);
20 impl->m_RootItem->addChild(item);
24 impl->m_RootItem->addChild(item);
21 endInsertRows();
25 endInsertRows();
22
26
23 return index(impl->m_RootItem->childCount() - 1, 0);
27 emit dataChanged(QModelIndex(), QModelIndex());
24 }
25
28
26 int CatalogueTreeModel::topLevelItemCount() const
29 return index(nbTopLevelItems, 0);
27 {
28 return impl->m_RootItem->childCount();
29 }
30 }
30
31
31 QTreeWidgetItem *CatalogueTreeModel::topLevelItem(int i) const
32 QVector<CatalogueAbstractTreeItem *> CatalogueTreeModel::topLevelItems() const
32 {
33 {
33 return impl->m_RootItem->child(i);
34 return impl->m_RootItem->children();
34 }
35 }
35
36
36 void CatalogueTreeModel::addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex)
37 void CatalogueTreeModel::addChildItem(CatalogueAbstractTreeItem *child,
38 const QModelIndex &parentIndex)
37 {
39 {
38 auto parentItem = item(parentIndex);
40 auto parentItem = item(parentIndex);
39 beginInsertRows(parentIndex, parentItem->childCount(), parentItem->childCount());
41 int c = parentItem->children().count();
42 beginInsertRows(parentIndex, c, c);
40 parentItem->addChild(child);
43 parentItem->addChild(child);
41 endInsertRows();
44 endInsertRows();
45
46 emit dataChanged(QModelIndex(), QModelIndex());
42 }
47 }
43
48
44 QTreeWidgetItem *CatalogueTreeModel::item(const QModelIndex &index) const
49 CatalogueAbstractTreeItem *CatalogueTreeModel::item(const QModelIndex &index) const
45 {
50 {
46 return static_cast<QTreeWidgetItem *>(index.internalPointer());
51 return static_cast<CatalogueAbstractTreeItem *>(index.internalPointer());
47 }
52 }
48
53
49 QModelIndex CatalogueTreeModel::indexOf(QTreeWidgetItem *item, int column) const
54 QModelIndex CatalogueTreeModel::indexOf(CatalogueAbstractTreeItem *item, int column) const
50 {
55 {
51 auto parentItem = item->parent();
56 auto parentItem = item->parent();
52 if (!parentItem) {
57 if (!parentItem) {
53 return QModelIndex();
58 return QModelIndex();
54 }
59 }
55
60
56 auto row = parentItem->indexOfChild(item);
61 auto row = parentItem->children().indexOf(item);
57 return createIndex(row, column, item);
62 return createIndex(row, column, item);
58 }
63 }
59
64
60 QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const
65 QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const
61 {
66 {
67 if (column > 0) {
68 int a = 0;
69 }
70
62 if (!hasIndex(row, column, parent)) {
71 if (!hasIndex(row, column, parent)) {
63 return QModelIndex();
72 return QModelIndex();
64 }
73 }
65
74
66 QTreeWidgetItem *parentItem = nullptr;
75 CatalogueAbstractTreeItem *parentItem = nullptr;
67
76
68 if (!parent.isValid()) {
77 if (!parent.isValid()) {
69 parentItem = impl->m_RootItem.get();
78 parentItem = impl->m_RootItem.get();
70 }
79 }
71 else {
80 else {
72 parentItem = static_cast<QTreeWidgetItem *>(parent.internalPointer());
81 parentItem = item(parent);
73 }
82 }
74
83
75 QTreeWidgetItem *childItem = parentItem->child(row);
84 auto childItem = parentItem->children().value(row);
76 if (childItem) {
85 if (childItem) {
77 return createIndex(row, column, childItem);
86 return createIndex(row, column, childItem);
78 }
87 }
@@ -87,32 +96,29 QModelIndex CatalogueTreeModel::parent(const QModelIndex &index) const
87 return QModelIndex();
96 return QModelIndex();
88 }
97 }
89
98
90 auto childItem = static_cast<QTreeWidgetItem *>(index.internalPointer());
99 auto childItem = item(index);
91 auto parentItem = childItem->parent();
100 auto parentItem = childItem->parent();
92
101
93 if (parentItem == nullptr || parentItem->parent() == nullptr) {
102 if (parentItem == nullptr || parentItem->parent() == nullptr) {
94 return QModelIndex();
103 return QModelIndex();
95 }
104 }
96
105
97 auto row = parentItem->parent()->indexOfChild(parentItem);
106 auto row = parentItem->parent()->children().indexOf(parentItem);
98 return createIndex(row, 0, parentItem);
107 return createIndex(row, 0, parentItem);
99 }
108 }
100
109
101 int CatalogueTreeModel::rowCount(const QModelIndex &parent) const
110 int CatalogueTreeModel::rowCount(const QModelIndex &parent) const
102 {
111 {
103 QTreeWidgetItem *parentItem = nullptr;
112 CatalogueAbstractTreeItem *parentItem = nullptr;
104 if (parent.column() > 0) {
105 return 0;
106 }
107
113
108 if (!parent.isValid()) {
114 if (!parent.isValid()) {
109 parentItem = impl->m_RootItem.get();
115 parentItem = impl->m_RootItem.get();
110 }
116 }
111 else {
117 else {
112 parentItem = static_cast<QTreeWidgetItem *>(parent.internalPointer());
118 parentItem = item(parent);
113 }
119 }
114
120
115 return parentItem->childCount();
121 return parentItem->children().count();
116 }
122 }
117
123
118 int CatalogueTreeModel::columnCount(const QModelIndex &parent) const
124 int CatalogueTreeModel::columnCount(const QModelIndex &parent) const
@@ -122,13 +128,9 int CatalogueTreeModel::columnCount(const QModelIndex &parent) const
122
128
123 Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const
129 Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const
124 {
130 {
125 if (index.column() == (int)Column::Validation) {
131 auto treeItem = item(index);
126 return Qt::NoItemFlags;
132 if (treeItem) {
127 }
133 return treeItem->flags(index.column());
128
129 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
130 if (item) {
131 return item->flags();
132 }
134 }
133
135
134 return Qt::NoItemFlags;
136 return Qt::NoItemFlags;
@@ -136,13 +138,9 Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const
136
138
137 QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const
139 QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const
138 {
140 {
139 if (!index.isValid()) {
141 auto treeItem = item(index);
140 return QModelIndex();
142 if (treeItem) {
141 }
143 return treeItem->data(index.column(), role);
142
143 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
144 if (item) {
145 return item->data(index.column(), role);
146 }
144 }
147
145
148 return QModelIndex();
146 return QModelIndex();
@@ -150,20 +148,43 QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const
150
148
151 bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
149 bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
152 {
150 {
153 if (!index.isValid()) {
151 auto treeItem = item(index);
154 return false;
152 if (treeItem) {
155 }
153 auto result = treeItem->setData(index.column(), role, value);
156
157 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
158 if (item) {
159 item->setData(index.column(), role, value);
160
154
161 if (index.column() == (int)Column::Name) {
155 if (result && index.column() == (int)Column::Name) {
162 emit itemRenamed(index);
156 emit itemRenamed(index);
163 }
157 }
164
158
165 return true;
159 return result;
166 }
160 }
167
161
168 return false;
162 return false;
169 }
163 }
164 bool CatalogueTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
165 int column, const QModelIndex &parent) const
166 {
167 auto draggedIndex = parent;
168 auto draggedItem = item(draggedIndex);
169 if (draggedItem) {
170 return draggedItem->canDropMimeData(data, action);
171 }
172
173 return false;
174 }
175
176 bool CatalogueTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
177 int column, const QModelIndex &parent)
178 {
179 return false;
180 }
181
182 Qt::DropActions CatalogueTreeModel::supportedDropActions() const
183 {
184 return Qt::CopyAction | Qt::MoveAction;
185 }
186
187 QStringList CatalogueTreeModel::mimeTypes() const
188 {
189 return {MIME_TYPE_EVENT_LIST};
190 }
@@ -77,19 +77,13
77 <bool>true</bool>
77 <bool>true</bool>
78 </property>
78 </property>
79 <property name="dragDropMode">
79 <property name="dragDropMode">
80 <enum>QAbstractItemView::DropOnly</enum>
80 <enum>QAbstractItemView::DragDrop</enum>
81 </property>
81 </property>
82 <property name="defaultDropAction">
82 <attribute name="headerVisible">
83 <enum>Qt::CopyAction</enum>
83 <bool>false</bool>
84 </property>
84 </attribute>
85 <property name="selectionMode">
85 <attribute name="headerStretchLastSection">
86 <enum>QAbstractItemView::ExtendedSelection</enum>
86 <bool>false</bool>
87 </property>
88 <property name="headerHidden">
89 <bool>true</bool>
90 </property>
91 <attribute name="headerDefaultSectionSize">
92 <number>0</number>
93 </attribute>
87 </attribute>
94 </widget>
88 </widget>
95 </item>
89 </item>
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now