##// END OF EJS Templates
Move event in catalogue with Drag&Drop
trabillard -
r1362:c5e93e891fc6
parent child
Show More
@@ -74,6 +74,13 public:
74 74 /// Returns the list of variables contained in a MIME data
75 75 QVector<std::shared_ptr<DBEvent> > eventsForMimeData(const QByteArray &mimeData) const;
76 76
77 /// Returns the MIME data associated to a list of variables
78 QByteArray
79 mimeDataForCatalogues(const QVector<std::shared_ptr<DBCatalogue> > &catalogues) const;
80
81 /// Returns the list of variables contained in a MIME data
82 QVector<std::shared_ptr<DBCatalogue> > cataloguesForMimeData(const QByteArray &mimeData) const;
83
77 84 public slots:
78 85 /// Manage init/end of the controller
79 86 void initialize();
@@ -16,6 +16,7 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_PRODUCT_LIST;
16 16 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_TIME_RANGE;
17 17 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_SELECTION_ZONE;
18 18 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_EVENT_LIST;
19 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_SOURCE_CATALOGUE_LIST;
19 20
20 21
21 22 #endif // SCIQLOP_MIMETYPESDEF_H
@@ -382,6 +382,46 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
382 382 return events;
383 383 }
384 384
385 QByteArray CatalogueController::mimeDataForCatalogues(
386 const QVector<std::shared_ptr<DBCatalogue> > &catalogues) const
387 {
388 auto encodedData = QByteArray{};
389
390 QMap<QString, QVariantList> idsPerRepository;
391 for (auto catalogue : catalogues) {
392 idsPerRepository[catalogue->getRepository()] << catalogue->getUniqId();
393 }
394
395 QDataStream stream{&encodedData, QIODevice::WriteOnly};
396 stream << idsPerRepository;
397
398 return encodedData;
399 }
400
401 QVector<std::shared_ptr<DBCatalogue> >
402 CatalogueController::cataloguesForMimeData(const QByteArray &mimeData) const
403 {
404 auto catalogues = QVector<std::shared_ptr<DBCatalogue> >{};
405 QDataStream stream{mimeData};
406
407 QMap<QString, QVariantList> idsPerRepository;
408 stream >> idsPerRepository;
409
410 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
411 auto repository = it.key();
412 auto allRepositoryCatalogues = retrieveCatalogues(repository);
413 for (auto uuid : it.value()) {
414 for (auto repositoryCatalogues : allRepositoryCatalogues) {
415 if (uuid.toUuid() == repositoryCatalogues->getUniqId()) {
416 catalogues << repositoryCatalogues;
417 }
418 }
419 }
420 }
421
422 return catalogues;
423 }
424
385 425 void CatalogueController::initialize()
386 426 {
387 427 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
@@ -7,3 +7,4 const QString MIME_TYPE_PRODUCT_LIST = QStringLiteral("sciqlop/product-list");
7 7 const QString MIME_TYPE_TIME_RANGE = QStringLiteral("sciqlop/time-range");
8 8 const QString MIME_TYPE_SELECTION_ZONE = QStringLiteral("sciqlop/selection-zone");
9 9 const QString MIME_TYPE_EVENT_LIST = QStringLiteral("sciqlop/event-list");
10 const QString MIME_TYPE_SOURCE_CATALOGUE_LIST = QStringLiteral("sciqlop/source-catalogue-list");
@@ -6,6 +6,7
6 6 #include <QLoggingCategory>
7 7 #include <unordered_set>
8 8
9 class DBCatalogue;
9 10 class DBEvent;
10 11 class DBEventProduct;
11 12
@@ -22,6 +23,7 public:
22 23
23 24 enum class Column { Name, TStart, TEnd, Tags, Product, Validation, NbColumn };
24 25
26 void setSourceCatalogues(const QVector<std::shared_ptr<DBCatalogue> > &catalogues);
25 27 void setEvents(const QVector<std::shared_ptr<DBEvent> > &events);
26 28 void addEvent(const std::shared_ptr<DBEvent> &event);
27 29 void removeEvent(const std::shared_ptr<DBEvent> &event);
@@ -16,7 +16,7 class CatalogueTreeModel : public QAbstractItemModel {
16 16
17 17 signals:
18 18 void itemRenamed(const QModelIndex &index);
19 void itemDropped(const QModelIndex &parentIndex);
19 void itemDropped(const QModelIndex &parentIndex, const QMimeData *data, Qt::DropAction action);
20 20
21 21 public:
22 22 CatalogueTreeModel(QObject *parent = nullptr);
@@ -24,6 +24,7 const auto EVENT_PRODUCT_ITEM_TYPE = 2;
24 24 struct CatalogueEventsModel::CatalogueEventsModelPrivate {
25 25 QVector<std::shared_ptr<DBEvent> > m_Events;
26 26 std::unordered_map<DBEvent *, QVector<std::shared_ptr<DBEventProduct> > > m_EventProducts;
27 QVector<std::shared_ptr<DBCatalogue> > m_SourceCatalogue;
27 28
28 29 QStringList columnNames()
29 30 {
@@ -129,6 +130,12 CatalogueEventsModel::CatalogueEventsModel(QObject *parent)
129 130 {
130 131 }
131 132
133 void CatalogueEventsModel::setSourceCatalogues(
134 const QVector<std::shared_ptr<DBCatalogue> > &catalogues)
135 {
136 impl->m_SourceCatalogue = catalogues;
137 }
138
132 139 void CatalogueEventsModel::setEvents(const QVector<std::shared_ptr<DBEvent> > &events)
133 140 {
134 141 beginResetModel();
@@ -388,12 +395,12 void CatalogueEventsModel::sort(int column, Qt::SortOrder order)
388 395
389 396 Qt::DropActions CatalogueEventsModel::supportedDragActions() const
390 397 {
391 return Qt::CopyAction;
398 return Qt::CopyAction | Qt::MoveAction;
392 399 }
393 400
394 401 QStringList CatalogueEventsModel::mimeTypes() const
395 402 {
396 return {MIME_TYPE_EVENT_LIST, MIME_TYPE_TIME_RANGE};
403 return {MIME_TYPE_EVENT_LIST, MIME_TYPE_SOURCE_CATALOGUE_LIST, MIME_TYPE_TIME_RANGE};
397 404 }
398 405
399 406 QMimeData *CatalogueEventsModel::mimeData(const QModelIndexList &indexes) const
@@ -436,6 +443,10 QMimeData *CatalogueEventsModel::mimeData(const QModelIndexList &indexes) const
436 443 if (!eventList.isEmpty() && eventProductList.isEmpty()) {
437 444 auto eventsEncodedData = sqpApp->catalogueController().mimeDataForEvents(eventList);
438 445 mimeData->setData(MIME_TYPE_EVENT_LIST, eventsEncodedData);
446
447 auto sourceCataloguesEncodedData
448 = sqpApp->catalogueController().mimeDataForCatalogues(impl->m_SourceCatalogue);
449 mimeData->setData(MIME_TYPE_SOURCE_CATALOGUE_LIST, sourceCataloguesEncodedData);
439 450 }
440 451
441 452 if (eventList.count() + eventProductList.count() == 1) {
@@ -44,6 +44,7 struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate {
44 44 void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, CatalogueEventsWidget *widget)
45 45 {
46 46 widget->ui->treeView->setSortingEnabled(false);
47 m_Model->setSourceCatalogues(m_DisplayedCatalogues);
47 48 m_Model->setEvents(events);
48 49 widget->ui->treeView->setSortingEnabled(true);
49 50
@@ -8,11 +8,13
8 8 #include <Catalogue/CatalogueTreeItems/CatalogueTreeItem.h>
9 9 #include <Catalogue/CatalogueTreeModel.h>
10 10 #include <CatalogueDao.h>
11 #include <Common/MimeTypesDef.h>
11 12 #include <ComparaisonPredicate.h>
12 13 #include <DBCatalogue.h>
13 14
14 15 #include <QMenu>
15 16 #include <QMessageBox>
17 #include <QMimeData>
16 18
17 19 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
18 20
@@ -126,13 +128,26 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
126 128 });
127 129
128 130
129 connect(impl->m_TreeModel, &CatalogueTreeModel::itemDropped, [this](auto index) {
130 auto item = impl->m_TreeModel->item(index);
131 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
132 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
133 this->setCatalogueChanges(catalogue, true);
134 }
135 });
131 connect(impl->m_TreeModel, &CatalogueTreeModel::itemDropped,
132 [this](auto index, auto mimeData, auto action) {
133 auto item = impl->m_TreeModel->item(index);
134 if (item && item->type() == CATALOGUE_ITEM_TYPE) {
135 auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
136 this->setCatalogueChanges(catalogue, true);
137 }
138
139 if (action == Qt::MoveAction) {
140 /// Display a save button on source catalogues
141 auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
142 mimeData->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
143 for (auto catalogue : sourceCatalogues) {
144 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
145 this->setCatalogueChanges(catalogue, true);
146 }
147 }
148 }
149 });
150
136 151 connect(ui->btnRemove, &QToolButton::clicked, [this]() {
137 152 QVector<QPair<std::shared_ptr<DBCatalogue>, CatalogueAbstractTreeItem *> >
138 153 cataloguesToItems;
@@ -344,7 +359,7 CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCat
344 359 for (auto childItem : item->children()) {
345 360 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
346 361 auto catalogueItem = static_cast<CatalogueTreeItem *>(childItem);
347 if (catalogueItem->catalogue() == catalogue) {
362 if (catalogueItem->catalogue()->getUniqId() == catalogue->getUniqId()) {
348 363 return catalogueItem;
349 364 }
350 365 }
@@ -76,12 +76,22 Qt::ItemFlags CatalogueTreeItem::flags(int column) const
76 76
77 77 bool CatalogueTreeItem::canDropMimeData(const QMimeData *data, Qt::DropAction action)
78 78 {
79 // Check that the event is not dropped on the same catalogue
80 auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
81 data->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
82 for (auto catalogue : sourceCatalogues) {
83 if (catalogue->getUniqId() == impl->m_Catalogue->getUniqId()) {
84 return false;
85 }
86 }
87
79 88 auto events = sqpApp->catalogueController().eventsForMimeData(data->data(MIME_TYPE_EVENT_LIST));
80 89 auto canDrop = data->hasFormat(MIME_TYPE_EVENT_LIST);
81 90
82 91 for (auto event : events) {
83 92 canDrop &= (event->getRepository() == impl->m_Catalogue->getRepository());
84 93 }
94
85 95 return canDrop;
86 96 }
87 97
@@ -89,14 +99,28 bool CatalogueTreeItem::dropMimeData(const QMimeData *data, Qt::DropAction actio
89 99 {
90 100 Q_ASSERT(canDropMimeData(data, action));
91 101 // Warning: Check that the events aren't already in the catalogue
92 // Also check for the repository !!!
102 // No need to check check for the repository: inter-repository drop is forbidden in
103 // canDropMimeData
93 104
94 105 auto events = sqpApp->catalogueController().eventsForMimeData(data->data(MIME_TYPE_EVENT_LIST));
106 auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
107 data->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
95 108
96 109 for (auto event : events) {
110
111 if (action == Qt::MoveAction) {
112 for (auto catalogue : sourceCatalogues) {
113 catalogue->removeEvent(event->getUniqId());
114 }
115 }
116
97 117 impl->m_Catalogue->addEvent(event->getUniqId());
98 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
99 118 }
119
120 for (auto catalogue : sourceCatalogues) {
121 sqpApp->catalogueController().updateCatalogue(catalogue);
122 }
123 sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue);
100 124 }
101 125
102 126 std::shared_ptr<DBCatalogue> CatalogueTreeItem::catalogue() const
@@ -178,6 +178,7 bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value
178 178
179 179 return false;
180 180 }
181
181 182 bool CatalogueTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
182 183 int column, const QModelIndex &parent) const
183 184 {
@@ -200,7 +201,7 bool CatalogueTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
200 201 if (draggedItem) {
201 202 result = draggedItem->dropMimeData(data, action);
202 203 if (result) {
203 emit itemDropped(draggedIndex);
204 emit itemDropped(draggedIndex, data, action);
204 205 }
205 206 }
206 207
@@ -214,5 +215,5 Qt::DropActions CatalogueTreeModel::supportedDropActions() const
214 215
215 216 QStringList CatalogueTreeModel::mimeTypes() const
216 217 {
217 return {MIME_TYPE_EVENT_LIST};
218 return {MIME_TYPE_EVENT_LIST, MIME_TYPE_SOURCE_CATALOGUE_LIST};
218 219 }
General Comments 0
You need to be logged in to leave comments. Login now