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