##// END OF EJS Templates
Remove the catalogue from discard or remove button have now the same gui...
perrinel -
r1371:232b551e25db
parent child
Show More
@@ -1,443 +1,444
1 #include "Catalogue/CatalogueSideBarWidget.h"
1 #include "Catalogue/CatalogueSideBarWidget.h"
2 #include "ui_CatalogueSideBarWidget.h"
2 #include "ui_CatalogueSideBarWidget.h"
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>
6 #include <Catalogue/CatalogueExplorerHelper.h>
7 #include <Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h>
7 #include <Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h>
8 #include <Catalogue/CatalogueTreeItems/CatalogueTreeItem.h>
8 #include <Catalogue/CatalogueTreeItems/CatalogueTreeItem.h>
9 #include <Catalogue/CatalogueTreeModel.h>
9 #include <Catalogue/CatalogueTreeModel.h>
10 #include <CatalogueDao.h>
10 #include <CatalogueDao.h>
11 #include <Common/MimeTypesDef.h>
11 #include <Common/MimeTypesDef.h>
12 #include <ComparaisonPredicate.h>
12 #include <ComparaisonPredicate.h>
13 #include <DBCatalogue.h>
13 #include <DBCatalogue.h>
14
14
15 #include <QKeyEvent>
15 #include <QKeyEvent>
16 #include <QMenu>
16 #include <QMenu>
17 #include <QMessageBox>
17 #include <QMessageBox>
18 #include <QMimeData>
18 #include <QMimeData>
19
19
20 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
20 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
21
21
22
22
23 constexpr auto ALL_EVENT_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 1;
23 constexpr auto ALL_EVENT_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 1;
24 constexpr auto TRASH_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 2;
24 constexpr auto TRASH_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 2;
25 constexpr auto CATALOGUE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 3;
25 constexpr auto CATALOGUE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 3;
26 constexpr auto DATABASE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 4;
26 constexpr auto DATABASE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 4;
27
27
28
28
29 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
29 struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
30
30
31 CatalogueTreeModel *m_TreeModel = nullptr;
31 CatalogueTreeModel *m_TreeModel = nullptr;
32
32
33 void configureTreeWidget(QTreeView *treeView);
33 void configureTreeWidget(QTreeView *treeView);
34 QModelIndex addDatabaseItem(const QString &name);
34 QModelIndex addDatabaseItem(const QString &name);
35 CatalogueAbstractTreeItem *getDatabaseItem(const QString &name);
35 CatalogueAbstractTreeItem *getDatabaseItem(const QString &name);
36 CatalogueAbstractTreeItem *addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
36 CatalogueAbstractTreeItem *addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
37 const QModelIndex &databaseIndex);
37 const QModelIndex &databaseIndex);
38
38
39 CatalogueTreeItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
39 CatalogueTreeItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
40 void setHasChanges(bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget);
40 void setHasChanges(bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget);
41 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
41 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
42
42
43 int selectionType(QTreeView *treeView) const
43 int selectionType(QTreeView *treeView) const
44 {
44 {
45 auto selectedItems = treeView->selectionModel()->selectedRows();
45 auto selectedItems = treeView->selectionModel()->selectedRows();
46 if (selectedItems.isEmpty()) {
46 if (selectedItems.isEmpty()) {
47 return CatalogueAbstractTreeItem::DEFAULT_TYPE;
47 return CatalogueAbstractTreeItem::DEFAULT_TYPE;
48 }
48 }
49 else {
49 else {
50 auto firstIndex = selectedItems.first();
50 auto firstIndex = selectedItems.first();
51 auto firstItem = m_TreeModel->item(firstIndex);
51 auto firstItem = m_TreeModel->item(firstIndex);
52 if (!firstItem) {
52 if (!firstItem) {
53 Q_ASSERT(false);
53 Q_ASSERT(false);
54 return CatalogueAbstractTreeItem::DEFAULT_TYPE;
54 return CatalogueAbstractTreeItem::DEFAULT_TYPE;
55 }
55 }
56 auto selectionType = firstItem->type();
56 auto selectionType = firstItem->type();
57
57
58 for (auto itemIndex : selectedItems) {
58 for (auto itemIndex : selectedItems) {
59 auto item = m_TreeModel->item(itemIndex);
59 auto item = m_TreeModel->item(itemIndex);
60 if (!item || item->type() != selectionType) {
60 if (!item || item->type() != selectionType) {
61 // Incoherent multi selection
61 // Incoherent multi selection
62 selectionType = CatalogueAbstractTreeItem::DEFAULT_TYPE;
62 selectionType = CatalogueAbstractTreeItem::DEFAULT_TYPE;
63 break;
63 break;
64 }
64 }
65 }
65 }
66
66
67 return selectionType;
67 return selectionType;
68 }
68 }
69 }
69 }
70
70
71 QVector<std::shared_ptr<DBCatalogue> > selectedCatalogues(QTreeView *treeView) const
71 QVector<std::shared_ptr<DBCatalogue> > selectedCatalogues(QTreeView *treeView) const
72 {
72 {
73 QVector<std::shared_ptr<DBCatalogue> > catalogues;
73 QVector<std::shared_ptr<DBCatalogue> > catalogues;
74 auto selectedItems = treeView->selectionModel()->selectedRows();
74 auto selectedItems = treeView->selectionModel()->selectedRows();
75 for (auto itemIndex : selectedItems) {
75 for (auto itemIndex : selectedItems) {
76 auto item = m_TreeModel->item(itemIndex);
76 auto item = m_TreeModel->item(itemIndex);
77 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
77 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
78 catalogues.append(static_cast<CatalogueTreeItem *>(item)->catalogue());
78 catalogues.append(static_cast<CatalogueTreeItem *>(item)->catalogue());
79 }
79 }
80 }
80 }
81
81
82 return catalogues;
82 return catalogues;
83 }
83 }
84
84
85 QStringList selectedRepositories(QTreeView *treeView) const
85 QStringList selectedRepositories(QTreeView *treeView) const
86 {
86 {
87 QStringList repositories;
87 QStringList repositories;
88 auto selectedItems = treeView->selectionModel()->selectedRows();
88 auto selectedItems = treeView->selectionModel()->selectedRows();
89 for (auto itemIndex : selectedItems) {
89 for (auto itemIndex : selectedItems) {
90 auto item = m_TreeModel->item(itemIndex);
90 auto item = m_TreeModel->item(itemIndex);
91 if (item && item->type() == DATABASE_ITEM_TYPE) {
91 if (item && item->type() == DATABASE_ITEM_TYPE) {
92 repositories.append(item->text());
92 repositories.append(item->text());
93 }
93 }
94 }
94 }
95
95
96 return repositories;
96 return repositories;
97 }
97 }
98 };
98 };
99
99
100 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
100 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
101 : QWidget(parent),
101 : QWidget(parent),
102 ui(new Ui::CatalogueSideBarWidget),
102 ui(new Ui::CatalogueSideBarWidget),
103 impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
103 impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
104 {
104 {
105 ui->setupUi(this);
105 ui->setupUi(this);
106
106
107 impl->m_TreeModel = new CatalogueTreeModel(this);
107 impl->m_TreeModel = new CatalogueTreeModel(this);
108 ui->treeView->setModel(impl->m_TreeModel);
108 ui->treeView->setModel(impl->m_TreeModel);
109
109
110 impl->configureTreeWidget(ui->treeView);
110 impl->configureTreeWidget(ui->treeView);
111
111
112 ui->treeView->header()->setStretchLastSection(false);
112 ui->treeView->header()->setStretchLastSection(false);
113 ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
113 ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
114 ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
114 ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
115
115
116 connect(ui->treeView, &QTreeView::clicked, this, &CatalogueSideBarWidget::emitSelection);
116 connect(ui->treeView, &QTreeView::clicked, this, &CatalogueSideBarWidget::emitSelection);
117 connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this,
117 connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this,
118 &CatalogueSideBarWidget::emitSelection);
118 &CatalogueSideBarWidget::emitSelection);
119
119
120
120
121 connect(ui->btnAdd, &QToolButton::clicked, [this]() {
121 connect(ui->btnAdd, &QToolButton::clicked, [this]() {
122 auto catalogue = std::make_shared<DBCatalogue>();
122 auto catalogue = std::make_shared<DBCatalogue>();
123 catalogue->setName(QString("Cat"));
123 catalogue->setName(QString("Cat"));
124 sqpApp->catalogueController().addCatalogue(catalogue);
124 sqpApp->catalogueController().addCatalogue(catalogue);
125 auto item = this->addCatalogue(catalogue, REPOSITORY_DEFAULT);
125 auto item = this->addCatalogue(catalogue, REPOSITORY_DEFAULT);
126 this->setCatalogueChanges(catalogue, true);
126 this->setCatalogueChanges(catalogue, true);
127 ui->treeView->edit(impl->m_TreeModel->indexOf(item));
127 ui->treeView->edit(impl->m_TreeModel->indexOf(item));
128
128
129 });
129 });
130
130
131
131
132 connect(impl->m_TreeModel, &CatalogueTreeModel::itemDropped,
132 connect(impl->m_TreeModel, &CatalogueTreeModel::itemDropped,
133 [this](auto index, auto mimeData, auto action) {
133 [this](auto index, auto mimeData, auto action) {
134 auto item = impl->m_TreeModel->item(index);
134 auto item = impl->m_TreeModel->item(index);
135 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
135 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
136 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
136 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
137 this->setCatalogueChanges(catalogue, true);
137 this->setCatalogueChanges(catalogue, true);
138 }
138 }
139
139
140 if (action == Qt::MoveAction) {
140 if (action == Qt::MoveAction) {
141 /// Display a save button on source catalogues
141 /// Display a save button on source catalogues
142 auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
142 auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
143 mimeData->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
143 mimeData->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
144 for (auto catalogue : sourceCatalogues) {
144 for (auto catalogue : sourceCatalogues) {
145 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
145 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
146 this->setCatalogueChanges(catalogue, true);
146 this->setCatalogueChanges(catalogue, true);
147 }
147 }
148 }
148 }
149 }
149 }
150 });
150 });
151
151
152 connect(ui->btnRemove, &QToolButton::clicked, [this]() {
152 connect(ui->btnRemove, &QToolButton::clicked, [this]() {
153 QVector<QPair<std::shared_ptr<DBCatalogue>, CatalogueAbstractTreeItem *> >
153 QVector<QPair<std::shared_ptr<DBCatalogue>, CatalogueAbstractTreeItem *> >
154 cataloguesToItems;
154 cataloguesToItems;
155 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
155 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
156
156
157 for (auto index : selectedIndexes) {
157 for (auto index : selectedIndexes) {
158 auto item = impl->m_TreeModel->item(index);
158 auto item = impl->m_TreeModel->item(index);
159 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
159 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
160 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
160 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
161 cataloguesToItems << qMakePair(catalogue, item);
161 cataloguesToItems << qMakePair(catalogue, item);
162 }
162 }
163 }
163 }
164
164
165 if (!cataloguesToItems.isEmpty()) {
165 if (!cataloguesToItems.isEmpty()) {
166
166
167 if (QMessageBox::warning(this, tr("Remove Catalogue(s)"),
167 if (QMessageBox::warning(this, tr("Remove Catalogue(s)"),
168 tr("The selected catalogues(s) will be completly removed "
168 tr("The selected catalogues(s) will be completly removed "
169 "from the repository!\nAre you sure you want to continue?"),
169 "from the repository!\nAre you sure you want to continue?"),
170 QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
170 QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
171 == QMessageBox::Yes) {
171 == QMessageBox::Yes) {
172
172
173 for (auto catalogueToItem : cataloguesToItems) {
173 for (auto catalogueToItem : cataloguesToItems) {
174 sqpApp->catalogueController().removeCatalogue(catalogueToItem.first);
174 sqpApp->catalogueController().removeCatalogue(catalogueToItem.first);
175 impl->m_TreeModel->removeChildItem(
175 impl->m_TreeModel->removeChildItem(
176 catalogueToItem.second,
176 catalogueToItem.second,
177 impl->m_TreeModel->indexOf(catalogueToItem.second->parent()));
177 impl->m_TreeModel->indexOf(catalogueToItem.second->parent()));
178 }
178 }
179 emitSelection();
179 }
180 }
180 }
181 }
181 });
182 });
182
183
183 connect(impl->m_TreeModel, &CatalogueTreeModel::itemRenamed, [this](auto index) {
184 connect(impl->m_TreeModel, &CatalogueTreeModel::itemRenamed, [this](auto index) {
184 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
185 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
185 if (selectedIndexes.contains(index)) {
186 if (selectedIndexes.contains(index)) {
186 this->emitSelection();
187 this->emitSelection();
187 }
188 }
188 impl->setHasChanges(true, index, this);
189 impl->setHasChanges(true, index, this);
189 });
190 });
190
191
191 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
192 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
192 connect(ui->treeView, &QTreeView::customContextMenuRequested, this,
193 connect(ui->treeView, &QTreeView::customContextMenuRequested, this,
193 &CatalogueSideBarWidget::onContextMenuRequested);
194 &CatalogueSideBarWidget::onContextMenuRequested);
194 }
195 }
195
196
196 CatalogueSideBarWidget::~CatalogueSideBarWidget()
197 CatalogueSideBarWidget::~CatalogueSideBarWidget()
197 {
198 {
198 delete ui;
199 delete ui;
199 }
200 }
200
201
201 CatalogueAbstractTreeItem *
202 CatalogueAbstractTreeItem *
202 CatalogueSideBarWidget::addCatalogue(const std::shared_ptr<DBCatalogue> &catalogue,
203 CatalogueSideBarWidget::addCatalogue(const std::shared_ptr<DBCatalogue> &catalogue,
203 const QString &repository)
204 const QString &repository)
204 {
205 {
205 auto repositoryItem = impl->getDatabaseItem(repository);
206 auto repositoryItem = impl->getDatabaseItem(repository);
206 return impl->addCatalogueItem(catalogue, impl->m_TreeModel->indexOf(repositoryItem));
207 return impl->addCatalogueItem(catalogue, impl->m_TreeModel->indexOf(repositoryItem));
207 }
208 }
208
209
209 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue,
210 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue,
210 bool hasChanges)
211 bool hasChanges)
211 {
212 {
212 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
213 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
213 auto index = impl->m_TreeModel->indexOf(catalogueItem);
214 auto index = impl->m_TreeModel->indexOf(catalogueItem);
214 impl->setHasChanges(hasChanges, index, this);
215 impl->setHasChanges(hasChanges, index, this);
215 // catalogueItem->refresh();
216 // catalogueItem->refresh();
216 }
217 }
217 }
218 }
218
219
219 QVector<std::shared_ptr<DBCatalogue> >
220 QVector<std::shared_ptr<DBCatalogue> >
220 CatalogueSideBarWidget::getCatalogues(const QString &repository) const
221 CatalogueSideBarWidget::getCatalogues(const QString &repository) const
221 {
222 {
222 QVector<std::shared_ptr<DBCatalogue> > result;
223 QVector<std::shared_ptr<DBCatalogue> > result;
223 auto repositoryItem = impl->getDatabaseItem(repository);
224 auto repositoryItem = impl->getDatabaseItem(repository);
224 for (auto child : repositoryItem->children()) {
225 for (auto child : repositoryItem->children()) {
225 if (child->type() == CATALOGUE_ITEM_TYPE) {
226 if (child->type() == CATALOGUE_ITEM_TYPE) {
226 auto catalogueItem = static_cast<CatalogueTreeItem *>(child);
227 auto catalogueItem = static_cast<CatalogueTreeItem *>(child);
227 result << catalogueItem->catalogue();
228 result << catalogueItem->catalogue();
228 }
229 }
229 else {
230 else {
230 qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogues: invalid structure";
231 qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogues: invalid structure";
231 }
232 }
232 }
233 }
233
234
234 return result;
235 return result;
235 }
236 }
236
237
237 void CatalogueSideBarWidget::emitSelection()
238 void CatalogueSideBarWidget::emitSelection()
238 {
239 {
239 auto selectionType = impl->selectionType(ui->treeView);
240 auto selectionType = impl->selectionType(ui->treeView);
240
241
241 switch (selectionType) {
242 switch (selectionType) {
242 case CATALOGUE_ITEM_TYPE:
243 case CATALOGUE_ITEM_TYPE:
243 emit this->catalogueSelected(impl->selectedCatalogues(ui->treeView));
244 emit this->catalogueSelected(impl->selectedCatalogues(ui->treeView));
244 break;
245 break;
245 case DATABASE_ITEM_TYPE:
246 case DATABASE_ITEM_TYPE:
246 emit this->databaseSelected(impl->selectedRepositories(ui->treeView));
247 emit this->databaseSelected(impl->selectedRepositories(ui->treeView));
247 break;
248 break;
248 case ALL_EVENT_ITEM_TYPE:
249 case ALL_EVENT_ITEM_TYPE:
249 emit this->allEventsSelected();
250 emit this->allEventsSelected();
250 break;
251 break;
251 case TRASH_ITEM_TYPE:
252 case TRASH_ITEM_TYPE:
252 emit this->trashSelected();
253 emit this->trashSelected();
253 break;
254 break;
254 default:
255 default:
255 emit this->selectionCleared();
256 emit this->selectionCleared();
256 break;
257 break;
257 }
258 }
258 }
259 }
259
260
260 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
261 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
261 {
262 {
262 QMenu menu{this};
263 QMenu menu{this};
263
264
264 auto currentIndex = ui->treeView->currentIndex();
265 auto currentIndex = ui->treeView->currentIndex();
265 auto currentItem = impl->m_TreeModel->item(currentIndex);
266 auto currentItem = impl->m_TreeModel->item(currentIndex);
266 if (!currentItem) {
267 if (!currentItem) {
267 return;
268 return;
268 }
269 }
269
270
270 switch (currentItem->type()) {
271 switch (currentItem->type()) {
271 case CATALOGUE_ITEM_TYPE:
272 case CATALOGUE_ITEM_TYPE:
272 menu.addAction("Rename", [this, currentIndex]() { ui->treeView->edit(currentIndex); });
273 menu.addAction("Rename", [this, currentIndex]() { ui->treeView->edit(currentIndex); });
273 break;
274 break;
274 case DATABASE_ITEM_TYPE:
275 case DATABASE_ITEM_TYPE:
275 break;
276 break;
276 case ALL_EVENT_ITEM_TYPE:
277 case ALL_EVENT_ITEM_TYPE:
277 break;
278 break;
278 case TRASH_ITEM_TYPE:
279 case TRASH_ITEM_TYPE:
279 menu.addAction("Empty Trash", []() {
280 menu.addAction("Empty Trash", []() {
280 // TODO
281 // TODO
281 });
282 });
282 break;
283 break;
283 default:
284 default:
284 break;
285 break;
285 }
286 }
286
287
287 if (!menu.isEmpty()) {
288 if (!menu.isEmpty()) {
288 menu.exec(ui->treeView->mapToGlobal(pos));
289 menu.exec(ui->treeView->mapToGlobal(pos));
289 }
290 }
290 }
291 }
291
292
292 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
293 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
293 {
294 {
294 auto allEventsItem = new CatalogueTextTreeItem{QIcon{":/icones/allEvents.png"}, "All Events",
295 auto allEventsItem = new CatalogueTextTreeItem{QIcon{":/icones/allEvents.png"}, "All Events",
295 ALL_EVENT_ITEM_TYPE};
296 ALL_EVENT_ITEM_TYPE};
296 auto allEventIndex = m_TreeModel->addTopLevelItem(allEventsItem);
297 auto allEventIndex = m_TreeModel->addTopLevelItem(allEventsItem);
297 treeView->setCurrentIndex(allEventIndex);
298 treeView->setCurrentIndex(allEventIndex);
298
299
299 auto trashItem
300 auto trashItem
300 = new CatalogueTextTreeItem{QIcon{":/icones/trash.png"}, "Trash", TRASH_ITEM_TYPE};
301 = new CatalogueTextTreeItem{QIcon{":/icones/trash.png"}, "Trash", TRASH_ITEM_TYPE};
301 m_TreeModel->addTopLevelItem(trashItem);
302 m_TreeModel->addTopLevelItem(trashItem);
302
303
303 auto separator = new QFrame{treeView};
304 auto separator = new QFrame{treeView};
304 separator->setFrameShape(QFrame::HLine);
305 separator->setFrameShape(QFrame::HLine);
305 auto separatorItem
306 auto separatorItem
306 = new CatalogueTextTreeItem{QIcon{}, QString{}, CatalogueAbstractTreeItem::DEFAULT_TYPE};
307 = new CatalogueTextTreeItem{QIcon{}, QString{}, CatalogueAbstractTreeItem::DEFAULT_TYPE};
307 separatorItem->setEnabled(false);
308 separatorItem->setEnabled(false);
308 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
309 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
309 treeView->setIndexWidget(separatorIndex, separator);
310 treeView->setIndexWidget(separatorIndex, separator);
310
311
311 auto repositories = sqpApp->catalogueController().getRepositories();
312 auto repositories = sqpApp->catalogueController().getRepositories();
312 for (auto dbname : repositories) {
313 for (auto dbname : repositories) {
313 auto dbIndex = addDatabaseItem(dbname);
314 auto dbIndex = addDatabaseItem(dbname);
314 auto catalogues = sqpApp->catalogueController().retrieveCatalogues(dbname);
315 auto catalogues = sqpApp->catalogueController().retrieveCatalogues(dbname);
315 for (auto catalogue : catalogues) {
316 for (auto catalogue : catalogues) {
316 addCatalogueItem(catalogue, dbIndex);
317 addCatalogueItem(catalogue, dbIndex);
317 }
318 }
318 }
319 }
319
320
320 treeView->expandAll();
321 treeView->expandAll();
321 }
322 }
322
323
323 QModelIndex
324 QModelIndex
324 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
325 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
325 {
326 {
326 auto databaseItem
327 auto databaseItem
327 = new CatalogueTextTreeItem{QIcon{":/icones/database.png"}, {name}, DATABASE_ITEM_TYPE};
328 = new CatalogueTextTreeItem{QIcon{":/icones/database.png"}, {name}, DATABASE_ITEM_TYPE};
328 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
329 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
329
330
330 return databaseIndex;
331 return databaseIndex;
331 }
332 }
332
333
333 CatalogueAbstractTreeItem *
334 CatalogueAbstractTreeItem *
334 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
335 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
335 {
336 {
336 for (auto item : m_TreeModel->topLevelItems()) {
337 for (auto item : m_TreeModel->topLevelItems()) {
337 if (item->type() == DATABASE_ITEM_TYPE && item->text() == name) {
338 if (item->type() == DATABASE_ITEM_TYPE && item->text() == name) {
338 return item;
339 return item;
339 }
340 }
340 }
341 }
341
342
342 return nullptr;
343 return nullptr;
343 }
344 }
344
345
345 CatalogueAbstractTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
346 CatalogueAbstractTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
346 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
347 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
347 {
348 {
348 auto catalogueItem
349 auto catalogueItem
349 = new CatalogueTreeItem{catalogue, QIcon{":/icones/catalogue.png"}, CATALOGUE_ITEM_TYPE};
350 = new CatalogueTreeItem{catalogue, QIcon{":/icones/catalogue.png"}, CATALOGUE_ITEM_TYPE};
350 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
351 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
351
352
352 return catalogueItem;
353 return catalogueItem;
353 }
354 }
354
355
355 CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
356 CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
356 const std::shared_ptr<DBCatalogue> &catalogue) const
357 const std::shared_ptr<DBCatalogue> &catalogue) const
357 {
358 {
358 for (auto item : m_TreeModel->topLevelItems()) {
359 for (auto item : m_TreeModel->topLevelItems()) {
359 if (item->type() == DATABASE_ITEM_TYPE) {
360 if (item->type() == DATABASE_ITEM_TYPE) {
360 for (auto childItem : item->children()) {
361 for (auto childItem : item->children()) {
361 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
362 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
362 auto catalogueItem = static_cast<CatalogueTreeItem *>(childItem);
363 auto catalogueItem = static_cast<CatalogueTreeItem *>(childItem);
363 if (catalogueItem->catalogue()->getUniqId() == catalogue->getUniqId()) {
364 if (catalogueItem->catalogue()->getUniqId() == catalogue->getUniqId()) {
364 return catalogueItem;
365 return catalogueItem;
365 }
366 }
366 }
367 }
367 else {
368 else {
368 qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogueItem: Invalid tree "
369 qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogueItem: Invalid tree "
369 "structure. A database item should "
370 "structure. A database item should "
370 "only contain catalogues.";
371 "only contain catalogues.";
371 Q_ASSERT(false);
372 Q_ASSERT(false);
372 }
373 }
373 }
374 }
374 }
375 }
375 }
376 }
376
377
377 return nullptr;
378 return nullptr;
378 }
379 }
379
380
380 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::setHasChanges(
381 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::setHasChanges(
381 bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget)
382 bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget)
382 {
383 {
383 std::shared_ptr<DBCatalogue> catalogue = nullptr;
384 std::shared_ptr<DBCatalogue> catalogue = nullptr;
384 auto item = m_TreeModel->item(index);
385 auto item = m_TreeModel->item(index);
385 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
386 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
386 catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
387 catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
387 }
388 }
388
389
389 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
390 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
390 if (value) {
391 if (value) {
391 if (!hasChanges(validationIndex, sideBarWidget->ui->treeView)) {
392 if (!hasChanges(validationIndex, sideBarWidget->ui->treeView)) {
392 auto widget = CatalogueExplorerHelper::buildValidationWidget(
393 auto widget = CatalogueExplorerHelper::buildValidationWidget(
393 sideBarWidget->ui->treeView,
394 sideBarWidget->ui->treeView,
394 [this, validationIndex, sideBarWidget, catalogue]() {
395 [this, validationIndex, sideBarWidget, catalogue]() {
395 if (catalogue) {
396 if (catalogue) {
396 sqpApp->catalogueController().saveCatalogue(catalogue);
397 sqpApp->catalogueController().saveCatalogue(catalogue);
397 emit sideBarWidget->catalogueSaved(catalogue);
398 emit sideBarWidget->catalogueSaved(catalogue);
398 }
399 }
399 setHasChanges(false, validationIndex, sideBarWidget);
400 setHasChanges(false, validationIndex, sideBarWidget);
400 },
401 },
401 [this, validationIndex, sideBarWidget, catalogue, item]() {
402 [this, validationIndex, sideBarWidget, catalogue, item]() {
402 if (catalogue) {
403 if (catalogue) {
403 bool removed;
404 bool removed;
404 sqpApp->catalogueController().discardCatalogue(catalogue, removed);
405 sqpApp->catalogueController().discardCatalogue(catalogue, removed);
405
406
406 if (removed) {
407 if (removed) {
407 m_TreeModel->removeChildItem(item,
408 m_TreeModel->removeChildItem(item,
408 m_TreeModel->indexOf(item->parent()));
409 m_TreeModel->indexOf(item->parent()));
409 }
410 }
410 else {
411 else {
411 m_TreeModel->refresh(m_TreeModel->indexOf(item));
412 m_TreeModel->refresh(m_TreeModel->indexOf(item));
412 setHasChanges(false, validationIndex, sideBarWidget);
413 setHasChanges(false, validationIndex, sideBarWidget);
413 }
414 }
414 sideBarWidget->emitSelection();
415 sideBarWidget->emitSelection();
415 }
416 }
416 });
417 });
417 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, widget);
418 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, widget);
418 }
419 }
419 }
420 }
420 else {
421 else {
421 // Note: the widget is destroyed
422 // Note: the widget is destroyed
422 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, nullptr);
423 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, nullptr);
423 }
424 }
424 }
425 }
425
426
426 bool CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::hasChanges(const QModelIndex &index,
427 bool CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::hasChanges(const QModelIndex &index,
427 QTreeView *treeView)
428 QTreeView *treeView)
428 {
429 {
429 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
430 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
430 return treeView->indexWidget(validationIndex) != nullptr;
431 return treeView->indexWidget(validationIndex) != nullptr;
431 }
432 }
432
433
433
434
434 void CatalogueSideBarWidget::keyPressEvent(QKeyEvent *event)
435 void CatalogueSideBarWidget::keyPressEvent(QKeyEvent *event)
435 {
436 {
436 switch (event->key()) {
437 switch (event->key()) {
437 case Qt::Key_Delete: {
438 case Qt::Key_Delete: {
438 ui->btnRemove->click();
439 ui->btnRemove->click();
439 }
440 }
440 default:
441 default:
441 break;
442 break;
442 }
443 }
443 }
444 }
General Comments 0
You need to be logged in to leave comments. Login now