From 86e05857e5c8ffc054aec7bf9fb5bfdbd5c320eb 2017-10-31 13:04:01 From: Thibaud Rabillard Date: 2017-10-31 13:04:01 Subject: [PATCH] Drag of product --- diff --git a/core/include/Common/MimeTypesDef.h b/core/include/Common/MimeTypesDef.h index 2212853..3766fac 100644 --- a/core/include/Common/MimeTypesDef.h +++ b/core/include/Common/MimeTypesDef.h @@ -12,6 +12,7 @@ extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_GRAPH; extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_ZONE; extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_VARIABLE_LIST; +extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_PRODUCT_LIST; extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_TIME_RANGE; diff --git a/core/include/DataSource/DataSourceController.h b/core/include/DataSource/DataSourceController.h index 568f5a9..7184dff 100644 --- a/core/include/DataSource/DataSourceController.h +++ b/core/include/DataSource/DataSourceController.h @@ -64,6 +64,9 @@ public: */ void loadProductItem(const QUuid &dataSourceUid, const DataSourceItem &productItem) noexcept; + QByteArray mimeDataForProductsData(const QVariantList &productsData) const; + QVariantList productsDataForMimeData(const QByteArray &mimeData) const; + public slots: /// Manage init/end of the controller void initialize(); diff --git a/core/meson.build b/core/meson.build index 2256757..a275a3b 100644 --- a/core/meson.build +++ b/core/meson.build @@ -3,6 +3,7 @@ core_moc_headers = [ 'include/Data/IDataProvider.h', 'include/DataSource/DataSourceController.h', 'include/DataSource/DataSourceItemAction.h', + 'include/DataSource/DataSourceWidget.h', 'include/Network/NetworkController.h', 'include/Time/TimeController.h', 'include/Variable/Variable.h', @@ -31,6 +32,7 @@ core_sources = [ 'src/DataSource/DataSourceController.cpp', 'src/DataSource/DataSourceItem.cpp', 'src/DataSource/DataSourceItemAction.cpp', + 'src/DataSource/DataSourceWidget.cpp', 'src/Network/NetworkController.cpp', 'src/Plugin/PluginManager.cpp', 'src/Settings/SqpSettingsDefs.cpp', diff --git a/core/src/Common/MimeTypesDef.cpp b/core/src/Common/MimeTypesDef.cpp index 24ef739..0393e1f 100644 --- a/core/src/Common/MimeTypesDef.cpp +++ b/core/src/Common/MimeTypesDef.cpp @@ -1,6 +1,7 @@ #include "Common/MimeTypesDef.h" -const QString MIME_TYPE_GRAPH = QStringLiteral("scqlop/graph"); -const QString MIME_TYPE_ZONE = QStringLiteral("scqlop/zone"); -const QString MIME_TYPE_VARIABLE_LIST = QStringLiteral("scqlop/var-list"); -const QString MIME_TYPE_TIME_RANGE = QStringLiteral("scqlop/time-range"); +const QString MIME_TYPE_GRAPH = QStringLiteral("sciqlop/graph"); +const QString MIME_TYPE_ZONE = QStringLiteral("sciqlop/zone"); +const QString MIME_TYPE_VARIABLE_LIST = QStringLiteral("sciqlop/var-list"); +const QString MIME_TYPE_PRODUCT_LIST = QStringLiteral("sciqlop/product-list"); +const QString MIME_TYPE_TIME_RANGE = QStringLiteral("sciqlop/time-range"); diff --git a/core/src/DataSource/DataSourceController.cpp b/core/src/DataSource/DataSourceController.cpp index 220945e..299ebfa 100644 --- a/core/src/DataSource/DataSourceController.cpp +++ b/core/src/DataSource/DataSourceController.cpp @@ -124,6 +124,26 @@ void DataSourceController::loadProductItem(const QUuid &dataSourceUid, } } +QByteArray DataSourceController::mimeDataForProductsData(const QVariantList &productsData) const +{ + QByteArray encodedData; + QDataStream stream{&encodedData, QIODevice::WriteOnly}; + + stream << productsData; + + return encodedData; +} + +QVariantList DataSourceController::productsDataForMimeData(const QByteArray &mimeData) const +{ + QDataStream stream{mimeData}; + + QVariantList productList; + stream >> productList; + + return productList; +} + void DataSourceController::initialize() { qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 5377542..0a6fbb1 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -310,7 +310,7 @@ VariableController::variablesForMimeData(const QByteArray &mimeData) const stream >> ids; for (auto id : ids) { - auto uuid = QUuid(id.toByteArray()); + auto uuid = QUuid{id.toByteArray()}; auto var = impl->findVariable(uuid); variables << var; } diff --git a/gui/include/DataSource/DataSourceTreeWidget.h b/gui/include/DataSource/DataSourceTreeWidget.h new file mode 100644 index 0000000..08043ba --- /dev/null +++ b/gui/include/DataSource/DataSourceTreeWidget.h @@ -0,0 +1,14 @@ +#ifndef SCIQLOP_DATASOURCETREEWIDGET_H +#define SCIQLOP_DATASOURCETREEWIDGET_H + +#include + +class DataSourceTreeWidget : public QTreeWidget { +public: + DataSourceTreeWidget(QWidget *parent); + +protected: + QMimeData *mimeData(const QList items) const override; +}; + +#endif // SCIQLOP_DATASOURCETREEWIDGET_H diff --git a/gui/src/DataSource/DataSourceTreeWidget.cpp b/gui/src/DataSource/DataSourceTreeWidget.cpp new file mode 100644 index 0000000..2e39d7e --- /dev/null +++ b/gui/src/DataSource/DataSourceTreeWidget.cpp @@ -0,0 +1,37 @@ +#include "DataSource/DataSourceTreeWidget.h" +#include "Common/MimeTypesDef.h" +#include "DataSource/DataSourceController.h" +#include "DataSource/DataSourceItem.h" +#include "DataSource/DataSourceTreeWidgetItem.h" + +#include "SqpApplication.h" + +#include + +DataSourceTreeWidget::DataSourceTreeWidget(QWidget *parent) : QTreeWidget(parent) {} + +QMimeData *DataSourceTreeWidget::mimeData(const QList items) const +{ + auto mimeData = new QMimeData; + + // Basic check to ensure the item are correctly typed + Q_ASSERT(items.isEmpty() || dynamic_cast(items.first()) != nullptr); + + QVariantList productData; + + for (auto item : items) { + auto dataSourceTreeItem = static_cast(item); + auto dataSource = dataSourceTreeItem->data(); + + if (dataSource->type() == DataSourceItemType::COMPONENT + || dataSource->type() == DataSourceItemType::PRODUCT) { + auto metaData = dataSource->data(); + productData << metaData; + } + } + + auto encodedData = sqpApp->dataSourceController().mimeDataForProductsData(productData); + mimeData->setData(MIME_TYPE_PRODUCT_LIST, encodedData); + + return mimeData; +} diff --git a/gui/src/DragDropHelper.cpp b/gui/src/DragDropHelper.cpp index 7e91e83..a98ab82 100644 --- a/gui/src/DragDropHelper.cpp +++ b/gui/src/DragDropHelper.cpp @@ -248,6 +248,12 @@ QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData, VisualizationDragDropContainer *dropContainer) { + if (!mimeData || !dropContainer) { + qCWarning(LOG_DragDropHelper()) << QObject::tr( + "DragDropHelper::checkMimeDataForVisualization, invalid input parameters."); + Q_ASSERT(false); + } + auto result = true; if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index 5956f76..14dfe20 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -233,7 +233,7 @@ QString VisualizationGraphWidget::name() const QMimeData *VisualizationGraphWidget::mimeData() const { auto mimeData = new QMimeData; - mimeData->setData(MIME_TYPE_GRAPH, QByteArray()); + mimeData->setData(MIME_TYPE_GRAPH, QByteArray{}); return mimeData; } diff --git a/gui/src/Visualization/VisualizationTabWidget.cpp b/gui/src/Visualization/VisualizationTabWidget.cpp index d6b63d6..a2949da 100644 --- a/gui/src/Visualization/VisualizationTabWidget.cpp +++ b/gui/src/Visualization/VisualizationTabWidget.cpp @@ -287,8 +287,13 @@ void VisualizationTabWidget::VisualizationTabWidgetPrivate::dropVariables( const QList > &variables, int index, VisualizationTabWidget *tabWidget) { - // Note: we are sure that there is a single and compatible variable here - // because the AcceptMimeDataFunction, set on the drop container, makes the check before the - // drop can occur. + // Note: the AcceptMimeDataFunction (set on the drop container) ensure there is a single and + // compatible variable here + if (variables.count() > 1) { + qCWarning(LOG_VisualizationZoneWidget()) + << tr("VisualizationTabWidget::dropVariables, dropping multiple variables, operation " + "aborted."); + return; + tabWidget->createZone(variables, index); } diff --git a/gui/src/Visualization/VisualizationZoneWidget.cpp b/gui/src/Visualization/VisualizationZoneWidget.cpp index ec39bdd..3b7c7e0 100644 --- a/gui/src/Visualization/VisualizationZoneWidget.cpp +++ b/gui/src/Visualization/VisualizationZoneWidget.cpp @@ -327,7 +327,7 @@ QString VisualizationZoneWidget::name() const QMimeData *VisualizationZoneWidget::mimeData() const { auto mimeData = new QMimeData; - mimeData->setData(MIME_TYPE_ZONE, QByteArray()); + mimeData->setData(MIME_TYPE_ZONE, QByteArray{}); return mimeData; } @@ -455,30 +455,14 @@ void VisualizationZoneWidget::VisualizationZoneWidgetPrivate::dropVariables( const QList > &variables, int index, VisualizationZoneWidget *zoneWidget) { - // Search for the top level VisualizationWidget - auto parent = zoneWidget->parentWidget(); - while (parent && qobject_cast(parent) == nullptr) { - parent = parent->parentWidget(); - } - - if (!parent) { + // Note: the AcceptMimeDataFunction (set on the drop container) ensure there is a single and + // compatible variable here + if (variables.count() > 1) { qCWarning(LOG_VisualizationZoneWidget()) - << tr("VisualizationZoneWidget::dropVariables, drop aborted, the parent " - "VisualizationWidget cannot be found."); - Q_ASSERT(false); + << tr("VisualizationZoneWidget::dropVariables, dropping multiple variables, operation " + "aborted."); return; } - auto visualizationWidget = static_cast(parent); - - // Search for the first variable which can be dropped - for (auto variable : variables) { - FindVariableOperation findVariableOperation{variable}; - visualizationWidget->accept(&findVariableOperation); - auto variableContainers = findVariableOperation.result(); - if (variableContainers.empty()) { - zoneWidget->createGraph(variable, index); - break; - } - } + zoneWidget->createGraph({variable}, index); } diff --git a/gui/ui/DataSource/DataSourceWidget.ui b/gui/ui/DataSource/DataSourceWidget.ui index e5d6f22..9b70805 100644 --- a/gui/ui/DataSource/DataSourceWidget.ui +++ b/gui/ui/DataSource/DataSourceWidget.ui @@ -18,7 +18,16 @@ - + + + true + + + QAbstractItemView::DragOnly + + + QAbstractItemView::ExtendedSelection + 1 @@ -28,6 +37,13 @@ + + + DataSourceTreeWidget + QTreeWidget +
DataSource/DataSourceTreeWidget.h
+
+