##// END OF EJS Templates
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
trabillard -
r1259:e88efff11587
parent child
Show More
@@ -0,0 +1,47
1 #ifndef SCIQLOP_CATALOGUETREEMODEL_H
2 #define SCIQLOP_CATALOGUETREEMODEL_H
3
4 #include <Common/spimpl.h>
5 #include <QAbstractItemModel>
6 #include <QTreeWidgetItem>
7
8 /**
9 * @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 */
12 class CatalogueTreeModel : public QAbstractItemModel {
13 Q_OBJECT
14
15 signals:
16 void itemRenamed(const QModelIndex &index);
17
18 public:
19 CatalogueTreeModel(QObject *parent = nullptr);
20
21 enum class Column { Name, Validation, Count };
22
23 QModelIndex addTopLevelItem(QTreeWidgetItem *item);
24 int topLevelItemCount() const;
25 QTreeWidgetItem *topLevelItem(int i) const;
26
27 void addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex);
28
29 QTreeWidgetItem *item(const QModelIndex &index) const;
30 QModelIndex indexOf(QTreeWidgetItem *item, int column = 0) const;
31
32 // model
33 QModelIndex index(int row, int column,
34 const QModelIndex &parent = QModelIndex()) const override;
35 QModelIndex parent(const QModelIndex &index) const override;
36 int rowCount(const QModelIndex &parent) const override;
37 int columnCount(const QModelIndex &parent) const override;
38 Qt::ItemFlags flags(const QModelIndex &index) const override;
39 QVariant data(const QModelIndex &index, int role) const override;
40 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
41
42 private:
43 class CatalogueTreeModelPrivate;
44 spimpl::unique_impl_ptr<CatalogueTreeModelPrivate> impl;
45 };
46
47 #endif // CATALOGUETREEMODEL_H
@@ -0,0 +1,169
1 #include "Catalogue/CatalogueTreeModel.h"
2
3 #include <QTreeWidgetItem>
4 #include <memory>
5
6 struct CatalogueTreeModel::CatalogueTreeModelPrivate {
7 std::unique_ptr<QTreeWidgetItem> m_RootItem = nullptr;
8
9 CatalogueTreeModelPrivate() : m_RootItem{std::make_unique<QTreeWidgetItem>()} {}
10 };
11
12 CatalogueTreeModel::CatalogueTreeModel(QObject *parent)
13 : QAbstractItemModel(parent), impl{spimpl::make_unique_impl<CatalogueTreeModelPrivate>()}
14 {
15 }
16
17 QModelIndex CatalogueTreeModel::addTopLevelItem(QTreeWidgetItem *item)
18 {
19 beginInsertRows(QModelIndex(), impl->m_RootItem->childCount(), impl->m_RootItem->childCount());
20 impl->m_RootItem->addChild(item);
21 endInsertRows();
22
23 return index(impl->m_RootItem->childCount() - 1, 0);
24 }
25
26 int CatalogueTreeModel::topLevelItemCount() const
27 {
28 return impl->m_RootItem->childCount();
29 }
30
31 QTreeWidgetItem *CatalogueTreeModel::topLevelItem(int i) const
32 {
33 return impl->m_RootItem->child(i);
34 }
35
36 void CatalogueTreeModel::addChildItem(QTreeWidgetItem *child, const QModelIndex &parentIndex)
37 {
38 auto parentItem = item(parentIndex);
39 beginInsertRows(parentIndex, parentItem->childCount(), parentItem->childCount());
40 parentItem->addChild(child);
41 endInsertRows();
42 }
43
44 QTreeWidgetItem *CatalogueTreeModel::item(const QModelIndex &index) const
45 {
46 return static_cast<QTreeWidgetItem *>(index.internalPointer());
47 }
48
49 QModelIndex CatalogueTreeModel::indexOf(QTreeWidgetItem *item, int column) const
50 {
51 auto parentItem = item->parent();
52 if (!parentItem) {
53 return QModelIndex();
54 }
55
56 auto row = parentItem->indexOfChild(item);
57 return createIndex(row, column, item);
58 }
59
60 QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const
61 {
62 if (!hasIndex(row, column, parent)) {
63 return QModelIndex();
64 }
65
66 QTreeWidgetItem *parentItem = nullptr;
67
68 if (!parent.isValid()) {
69 parentItem = impl->m_RootItem.get();
70 }
71 else {
72 parentItem = static_cast<QTreeWidgetItem *>(parent.internalPointer());
73 }
74
75 QTreeWidgetItem *childItem = parentItem->child(row);
76 if (childItem) {
77 return createIndex(row, column, childItem);
78 }
79
80 return QModelIndex();
81 }
82
83
84 QModelIndex CatalogueTreeModel::parent(const QModelIndex &index) const
85 {
86 if (!index.isValid()) {
87 return QModelIndex();
88 }
89
90 auto childItem = static_cast<QTreeWidgetItem *>(index.internalPointer());
91 auto parentItem = childItem->parent();
92
93 if (parentItem == nullptr || parentItem->parent() == nullptr) {
94 return QModelIndex();
95 }
96
97 auto row = parentItem->parent()->indexOfChild(parentItem);
98 return createIndex(row, 0, parentItem);
99 }
100
101 int CatalogueTreeModel::rowCount(const QModelIndex &parent) const
102 {
103 QTreeWidgetItem *parentItem = nullptr;
104 if (parent.column() > 0) {
105 return 0;
106 }
107
108 if (!parent.isValid()) {
109 parentItem = impl->m_RootItem.get();
110 }
111 else {
112 parentItem = static_cast<QTreeWidgetItem *>(parent.internalPointer());
113 }
114
115 return parentItem->childCount();
116 }
117
118 int CatalogueTreeModel::columnCount(const QModelIndex &parent) const
119 {
120 return (int)Column::Count;
121 }
122
123 Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const
124 {
125 if (index.column() == (int)Column::Validation) {
126 return Qt::NoItemFlags;
127 }
128
129 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
130 if (item) {
131 return item->flags();
132 }
133
134 return Qt::NoItemFlags;
135 }
136
137 QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const
138 {
139 if (!index.isValid()) {
140 return QModelIndex();
141 }
142
143 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
144 if (item) {
145 return item->data(index.column(), role);
146 }
147
148 return QModelIndex();
149 }
150
151 bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
152 {
153 if (!index.isValid()) {
154 return false;
155 }
156
157 auto item = static_cast<QTreeWidgetItem *>(index.internalPointer());
158 if (item) {
159 item->setData(index.column(), role, value);
160
161 if (index.column() == (int)Column::Name) {
162 emit itemRenamed(index);
163 }
164
165 return true;
166 }
167
168 return false;
169 }
@@ -18,8 +18,6 public:
18 /// Returns the catalogue represented by the item
18 /// Returns the catalogue represented by the item
19 std::shared_ptr<DBCatalogue> catalogue() const;
19 std::shared_ptr<DBCatalogue> catalogue() const;
20
20
21 /// Displays or hides the save and cancel buttons indicating that the catalogue has unsaved
22 /// changes
23 void setHasChanges(bool value);
21 void setHasChanges(bool value);
24
22
25 /// Returns true if the widget indicating the event has unsaved changes is displayed
23 /// Returns true if the widget indicating the event has unsaved changes is displayed
@@ -27,7 +27,8 gui_moc_headers = [
27 'include/Catalogue/CatalogueSideBarWidget.h',
27 'include/Catalogue/CatalogueSideBarWidget.h',
28 'include/Catalogue/CatalogueInspectorWidget.h',
28 'include/Catalogue/CatalogueInspectorWidget.h',
29 'include/Catalogue/CatalogueEventsModel.h',
29 'include/Catalogue/CatalogueEventsModel.h',
30 'include/Catalogue/CreateEventDialog.h'
30 'include/Catalogue/CreateEventDialog.h',
31 'include/Catalogue/CatalogueTreeModel.h'
31 ]
32 ]
32
33
33 gui_ui_files = [
34 gui_ui_files = [
@@ -122,7 +123,8 gui_sources = [
122 'src/Catalogue/CatalogueEventsModel.cpp',
123 'src/Catalogue/CatalogueEventsModel.cpp',
123 'src/Catalogue/CatalogueExplorerHelper.cpp',
124 'src/Catalogue/CatalogueExplorerHelper.cpp',
124 'src/Catalogue/CatalogueActionManager.cpp',
125 'src/Catalogue/CatalogueActionManager.cpp',
125 'src/Catalogue/CreateEventDialog.cpp'
126 'src/Catalogue/CreateEventDialog.cpp',
127 'src/Catalogue/CatalogueTreeModel.cpp'
126 ]
128 ]
127
129
128 gui_inc = include_directories(['include'])
130 gui_inc = include_directories(['include'])
@@ -3,6 +3,8
3 #include <SqpApplication.h>
3 #include <SqpApplication.h>
4
4
5 #include <Catalogue/CatalogueController.h>
5 #include <Catalogue/CatalogueController.h>
6 #include <Catalogue/CatalogueExplorerHelper.h>
7 #include <Catalogue/CatalogueTreeModel.h>
6 #include <Catalogue/CatalogueTreeWidgetItem.h>
8 #include <Catalogue/CatalogueTreeWidgetItem.h>
7 #include <CatalogueDao.h>
9 #include <CatalogueDao.h>
8 #include <ComparaisonPredicate.h>
10 #include <ComparaisonPredicate.h>
@@ -21,14 +23,17 constexpr auto DATABASE_ITEM_TYPE = QTreeWidgetItem::UserType + 3;
21
23
22 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
24 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
23
25
24 void configureTreeWidget(QTreeWidget *treeWidget);
26 CatalogueTreeModel *m_TreeModel = nullptr;
25 QTreeWidgetItem *addDatabaseItem(const QString &name, QTreeWidget *treeWidget);
27
26 QTreeWidgetItem *getDatabaseItem(const QString &name, QTreeWidget *treeWidget);
28 void configureTreeWidget(QTreeView *treeView);
29 QModelIndex addDatabaseItem(const QString &name);
30 QTreeWidgetItem *getDatabaseItem(const QString &name);
27 void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
31 void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
28 QTreeWidgetItem *parentDatabaseItem);
32 const QModelIndex &databaseIndex);
29
33
30 CatalogueTreeWidgetItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
34 CatalogueTreeWidgetItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
31 QTreeWidget *treeWidget) const;
35 void setHasChanges(bool value, const QModelIndex &index, QTreeView *treeView);
36 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
32 };
37 };
33
38
34 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
39 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
@@ -37,26 +42,36 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
37 impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
42 impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
38 {
43 {
39 ui->setupUi(this);
44 ui->setupUi(this);
40 impl->configureTreeWidget(ui->treeWidget);
41
45
42 ui->treeWidget->setColumnCount(2);
46 impl->m_TreeModel = new CatalogueTreeModel(this);
43 ui->treeWidget->header()->setStretchLastSection(false);
47 ui->treeView->setModel(impl->m_TreeModel);
44 ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
48
45 ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
49 impl->configureTreeWidget(ui->treeView);
50
51 ui->treeView->header()->setStretchLastSection(false);
52 ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
53 ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
46
54
47 auto emitSelection = [this]() {
55 auto emitSelection = [this]() {
48
56
49 auto selectedItems = ui->treeWidget->selectedItems();
57 auto selectedItems = ui->treeView->selectionModel()->selectedRows();
50 if (selectedItems.isEmpty()) {
58 if (selectedItems.isEmpty()) {
51 emit this->selectionCleared();
59 emit this->selectionCleared();
52 }
60 }
53 else {
61 else {
54 QVector<std::shared_ptr<DBCatalogue> > catalogues;
62 QVector<std::shared_ptr<DBCatalogue> > catalogues;
55 QStringList databases;
63 QStringList databases;
56 int selectionType = selectedItems.first()->type();
64 auto firstIndex = selectedItems.first();
65 auto firstItem = impl->m_TreeModel->item(firstIndex);
66 if (!firstItem) {
67 Q_ASSERT(false);
68 return;
69 }
70 auto selectionType = firstItem->type();
57
71
58 for (auto item : ui->treeWidget->selectedItems()) {
72 for (auto itemIndex : selectedItems) {
59 if (item->type() == selectionType) {
73 auto item = impl->m_TreeModel->item(itemIndex);
74 if (item && item->type() == selectionType) {
60 switch (selectionType) {
75 switch (selectionType) {
61 case CATALOGUE_ITEM_TYPE:
76 case CATALOGUE_ITEM_TYPE:
62 catalogues.append(
77 catalogues.append(
@@ -100,19 +115,20 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
100
115
101 };
116 };
102
117
103 connect(ui->treeWidget, &QTreeWidget::itemClicked, emitSelection);
118 connect(ui->treeView, &QTreeView::clicked, emitSelection);
104 connect(ui->treeWidget, &QTreeWidget::currentItemChanged, emitSelection);
119 connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, emitSelection);
105 connect(ui->treeWidget, &QTreeWidget::itemChanged,
120 connect(impl->m_TreeModel, &CatalogueTreeModel::itemRenamed, [emitSelection, this](auto index) {
106 [emitSelection, this](auto item, auto column) {
121 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
107 auto selectedItems = ui->treeWidget->selectedItems();
122 if (selectedIndexes.contains(index)) {
108 qDebug() << "ITEM CHANGED" << column;
123 emitSelection();
109 if (selectedItems.contains(item) && column == 0) {
124 }
110 emitSelection();
125
111 }
126 auto item = impl->m_TreeModel->item(index);
112 });
127 impl->setHasChanges(true, index, ui->treeView);
128 });
113
129
114 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
130 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
115 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
131 connect(ui->treeView, &QTreeWidget::customContextMenuRequested, this,
116 &CatalogueSideBarWidget::onContextMenuRequested);
132 &CatalogueSideBarWidget::onContextMenuRequested);
117 }
133 }
118
134
@@ -124,8 +140,9 CatalogueSideBarWidget::~CatalogueSideBarWidget()
124 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue,
140 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue,
125 bool hasChanges)
141 bool hasChanges)
126 {
142 {
127 if (auto catalogueItem = impl->getCatalogueItem(catalogue, ui->treeWidget)) {
143 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
128 catalogueItem->setHasChanges(hasChanges);
144 auto index = impl->m_TreeModel->indexOf(catalogueItem);
145 impl->setHasChanges(hasChanges, index, ui->treeView);
129 catalogueItem->refresh();
146 catalogueItem->refresh();
130 }
147 }
131 }
148 }
@@ -134,11 +151,15 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
134 {
151 {
135 QMenu menu{this};
152 QMenu menu{this};
136
153
137 auto currentItem = ui->treeWidget->currentItem();
154 auto currentIndex = ui->treeView->currentIndex();
155 auto currentItem = impl->m_TreeModel->item(currentIndex);
156 if (!currentItem) {
157 return;
158 }
159
138 switch (currentItem->type()) {
160 switch (currentItem->type()) {
139 case CATALOGUE_ITEM_TYPE:
161 case CATALOGUE_ITEM_TYPE:
140 menu.addAction("Rename",
162 menu.addAction("Rename", [this, currentIndex]() { ui->treeView->edit(currentIndex); });
141 [this, currentItem]() { ui->treeWidget->editItem(currentItem); });
142 break;
163 break;
143 case DATABASE_ITEM_TYPE:
164 case DATABASE_ITEM_TYPE:
144 break;
165 break;
@@ -154,58 +175,54 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
154 }
175 }
155
176
156 if (!menu.isEmpty()) {
177 if (!menu.isEmpty()) {
157 menu.exec(ui->treeWidget->mapToGlobal(pos));
178 menu.exec(ui->treeView->mapToGlobal(pos));
158 }
179 }
159 }
180 }
160
181
161 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(
182 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
162 QTreeWidget *treeWidget)
163 {
183 {
164 auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE};
184 auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE};
165 allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png"));
185 allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png"));
166 treeWidget->addTopLevelItem(allEventsItem);
186 m_TreeModel->addTopLevelItem(allEventsItem);
167
187
168 auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE};
188 auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE};
169 trashItem->setIcon(0, QIcon(":/icones/trash.png"));
189 trashItem->setIcon(0, QIcon(":/icones/trash.png"));
170 treeWidget->addTopLevelItem(trashItem);
190 m_TreeModel->addTopLevelItem(trashItem);
171
191
172 auto separator = new QFrame{treeWidget};
192 auto separator = new QFrame{treeView};
173 separator->setFrameShape(QFrame::HLine);
193 separator->setFrameShape(QFrame::HLine);
174 auto separatorItem = new QTreeWidgetItem{};
194 auto separatorItem = new QTreeWidgetItem{};
175 separatorItem->setFlags(Qt::NoItemFlags);
195 separatorItem->setFlags(Qt::NoItemFlags);
176 treeWidget->addTopLevelItem(separatorItem);
196 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
177 treeWidget->setItemWidget(separatorItem, 0, separator);
197 treeView->setIndexWidget(separatorIndex, separator);
178
198
179 auto repositories = sqpApp->catalogueController().getRepositories();
199 auto repositories = sqpApp->catalogueController().getRepositories();
180 for (auto dbname : repositories) {
200 for (auto dbname : repositories) {
181 auto db = addDatabaseItem(dbname, treeWidget);
201 auto dbIndex = addDatabaseItem(dbname);
182
183 auto catalogues = sqpApp->catalogueController().retrieveCatalogues(dbname);
202 auto catalogues = sqpApp->catalogueController().retrieveCatalogues(dbname);
184 for (auto catalogue : catalogues) {
203 for (auto catalogue : catalogues) {
185 addCatalogueItem(catalogue, db);
204 addCatalogueItem(catalogue, dbIndex);
186 }
205 }
187 }
206 }
188
207
189 treeWidget->expandAll();
208 treeView->expandAll();
190 }
209 }
191
210
192 QTreeWidgetItem *
211 QModelIndex
193 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name,
212 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
194 QTreeWidget *treeWidget)
195 {
213 {
196 auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE};
214 auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE};
197 databaseItem->setIcon(0, QIcon{":/icones/database.png"});
215 databaseItem->setIcon(0, QIcon{":/icones/database.png"});
198 treeWidget->addTopLevelItem(databaseItem);
216 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
199
217
200 return databaseItem;
218 return databaseIndex;
201 }
219 }
202
220
203 QTreeWidgetItem *
221 QTreeWidgetItem *
204 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name,
222 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
205 QTreeWidget *treeWidget)
206 {
223 {
207 for (auto i = 0; i < treeWidget->topLevelItemCount(); ++i) {
224 for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) {
208 auto item = treeWidget->topLevelItem(i);
225 auto item = m_TreeModel->topLevelItem(i);
209 if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) {
226 if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) {
210 return item;
227 return item;
211 }
228 }
@@ -215,18 +232,18 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QSt
215 }
232 }
216
233
217 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
234 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
218 const std::shared_ptr<DBCatalogue> &catalogue, QTreeWidgetItem *parentDatabaseItem)
235 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
219 {
236 {
220 auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE};
237 auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE};
221 catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"});
238 catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"});
222 parentDatabaseItem->addChild(catalogueItem);
239 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
223 }
240 }
224
241
225 CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
242 CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
226 const std::shared_ptr<DBCatalogue> &catalogue, QTreeWidget *treeWidget) const
243 const std::shared_ptr<DBCatalogue> &catalogue) const
227 {
244 {
228 for (auto i = 0; i < treeWidget->topLevelItemCount(); ++i) {
245 for (auto i = 0; i < m_TreeModel->topLevelItemCount(); ++i) {
229 auto item = treeWidget->topLevelItem(i);
246 auto item = m_TreeModel->topLevelItem(i);
230 if (item->type() == DATABASE_ITEM_TYPE) {
247 if (item->type() == DATABASE_ITEM_TYPE) {
231 for (auto j = 0; j < item->childCount(); ++j) {
248 for (auto j = 0; j < item->childCount(); ++j) {
232 auto childItem = item->child(j);
249 auto childItem = item->child(j);
@@ -248,3 +265,34 CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::
248
265
249 return nullptr;
266 return nullptr;
250 }
267 }
268
269 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::setHasChanges(bool value,
270 const QModelIndex &index,
271 QTreeView *treeView)
272 {
273 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
274 if (value) {
275 if (!hasChanges(validationIndex, treeView)) {
276 auto widget = CatalogueExplorerHelper::buildValidationWidget(
277 treeView,
278 [this, validationIndex, treeView]() {
279 setHasChanges(false, validationIndex, treeView);
280 },
281 [this, validationIndex, treeView]() {
282 setHasChanges(false, validationIndex, treeView);
283 });
284 treeView->setIndexWidget(validationIndex, widget);
285 }
286 }
287 else {
288 // Note: the widget is destroyed
289 treeView->setIndexWidget(validationIndex, nullptr);
290 }
291 }
292
293 bool CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::hasChanges(const QModelIndex &index,
294 QTreeView *treeView)
295 {
296 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
297 return treeView->indexWidget(validationIndex) != nullptr;
298 }
@@ -25,7 +25,7 CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(std::shared_ptr<DBCatalogue> ca
25 : QTreeWidgetItem(type),
25 : QTreeWidgetItem(type),
26 impl{spimpl::make_unique_impl<CatalogueTreeWidgetItemPrivate>(catalogue)}
26 impl{spimpl::make_unique_impl<CatalogueTreeWidgetItemPrivate>(catalogue)}
27 {
27 {
28 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
28 setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
29 }
29 }
30
30
31 QVariant CatalogueTreeWidgetItem::data(int column, int role) const
31 QVariant CatalogueTreeWidgetItem::data(int column, int role) const
@@ -51,7 +51,6 void CatalogueTreeWidgetItem::setData(int column, int role, const QVariant &valu
51 setText(0, newName);
51 setText(0, newName);
52 impl->m_Catalogue->setName(newName);
52 impl->m_Catalogue->setName(newName);
53 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
53 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
54 setHasChanges(true);
55 }
54 }
56 }
55 }
57 else {
56 else {
@@ -64,27 +63,6 std::shared_ptr<DBCatalogue> CatalogueTreeWidgetItem::catalogue() const
64 return impl->m_Catalogue;
63 return impl->m_Catalogue;
65 }
64 }
66
65
67 void CatalogueTreeWidgetItem::setHasChanges(bool value)
68 {
69 if (value) {
70 if (!hasChanges()) {
71 auto widget = CatalogueExplorerHelper::buildValidationWidget(
72 treeWidget(), [this]() { setHasChanges(false); },
73 [this]() { setHasChanges(false); });
74 treeWidget()->setItemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN, widget);
75 }
76 }
77 else {
78 // Note: the widget is destroyed
79 treeWidget()->setItemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN, nullptr);
80 }
81 }
82
83 bool CatalogueTreeWidgetItem::hasChanges()
84 {
85 return treeWidget()->itemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN) != nullptr;
86 }
87
88 void CatalogueTreeWidgetItem::refresh()
66 void CatalogueTreeWidgetItem::refresh()
89 {
67 {
90 emitDataChanged();
68 emitDataChanged();
@@ -72,18 +72,25
72 </layout>
72 </layout>
73 </item>
73 </item>
74 <item>
74 <item>
75 <widget class="QTreeWidget" name="treeWidget">
75 <widget class="QTreeView" name="treeView">
76 <property name="acceptDrops">
77 <bool>true</bool>
78 </property>
79 <property name="dragDropMode">
80 <enum>QAbstractItemView::DropOnly</enum>
81 </property>
82 <property name="defaultDropAction">
83 <enum>Qt::CopyAction</enum>
84 </property>
76 <property name="selectionMode">
85 <property name="selectionMode">
77 <enum>QAbstractItemView::ExtendedSelection</enum>
86 <enum>QAbstractItemView::ExtendedSelection</enum>
78 </property>
87 </property>
79 <attribute name="headerVisible">
88 <property name="headerHidden">
80 <bool>false</bool>
89 <bool>true</bool>
90 </property>
91 <attribute name="headerDefaultSectionSize">
92 <number>0</number>
81 </attribute>
93 </attribute>
82 <column>
83 <property name="text">
84 <string notr="true">1</string>
85 </property>
86 </column>
87 </widget>
94 </widget>
88 </item>
95 </item>
89 </layout>
96 </layout>
General Comments 0
You need to be logged in to leave comments. Login now