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