##// END OF EJS Templates
Drag of product
trabillard -
r862:e45b43f0766d
parent child
Show More
@@ -0,0 +1,14
1 #ifndef SCIQLOP_DATASOURCETREEWIDGET_H
2 #define SCIQLOP_DATASOURCETREEWIDGET_H
3
4 #include <QTreeWidget>
5
6 class DataSourceTreeWidget : public QTreeWidget {
7 public:
8 DataSourceTreeWidget(QWidget *parent);
9
10 protected:
11 QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const override;
12 };
13
14 #endif // SCIQLOP_DATASOURCETREEWIDGET_H
@@ -0,0 +1,38
1 #include "DataSource/DataSourceTreeWidget.h"
2 #include "Common/MimeTypesDef.h"
3 #include "DataSource/DataSourceItem.h"
4 #include "DataSource/DataSourceTreeWidgetItem.h"
5
6 #include <QMimeData>
7
8 DataSourceTreeWidget::DataSourceTreeWidget(QWidget *parent) : QTreeWidget(parent) {}
9
10 QMimeData *DataSourceTreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const
11 {
12 auto mimeData = new QMimeData;
13
14 // Basic check to ensure the item are correctly typed
15 Q_ASSERT(items.isEmpty() || dynamic_cast<DataSourceTreeWidgetItem *>(items.first()) != nullptr);
16
17 QVariantList productData;
18
19 for (auto item : items) {
20 auto dataSourceTreeItem = static_cast<DataSourceTreeWidgetItem *>(item);
21 auto dataSource = dataSourceTreeItem->data();
22
23 if (dataSource->type() == DataSourceItemType::COMPONENT
24 || dataSource->type() == DataSourceItemType::PRODUCT) {
25 auto metaData = dataSource->data();
26 productData << metaData;
27 }
28 }
29
30 QByteArray encodedData;
31 QDataStream stream(&encodedData, QIODevice::WriteOnly);
32
33 stream << productData;
34
35 mimeData->setData(MIME_TYPE_PRODUCT_LIST, encodedData);
36
37 return mimeData;
38 }
@@ -1,18 +1,19
1 1 #ifndef SCIQLOP_MIMETYPESDEF_H
2 2 #define SCIQLOP_MIMETYPESDEF_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <QString>
7 7
8 8 // ////////////////// //
9 9 // SciQlop Mime Types //
10 10 // ////////////////// //
11 11
12 12 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_GRAPH;
13 13 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_ZONE;
14 14 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_VARIABLE_LIST;
15 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_PRODUCT_LIST;
15 16 extern SCIQLOP_CORE_EXPORT const QString MIME_TYPE_TIME_RANGE;
16 17
17 18
18 19 #endif // SCIQLOP_MIMETYPESDEF_H
@@ -1,63 +1,65
1 1
2 2 core_moc_headers = [
3 3 'include/Data/IDataProvider.h',
4 4 'include/DataSource/DataSourceController.h',
5 5 'include/DataSource/DataSourceItemAction.h',
6 'include/DataSource/DataSourceWidget.h',
6 7 'include/Network/NetworkController.h',
7 8 'include/Time/TimeController.h',
8 9 'include/Variable/Variable.h',
9 10 'include/Variable/VariableCacheController.h',
10 11 'include/Variable/VariableController.h',
11 12 'include/Variable/VariableAcquisitionWorker.h',
12 13 'include/Variable/VariableCacheStrategy.h',
13 14 'include/Variable/VariableSynchronizationGroup.h',
14 15 'include/Variable/VariableModel.h',
15 16 'include/Visualization/VisualizationController.h'
16 17 ]
17 18
18 19
19 20 core_moc_files = qt5.preprocess(moc_headers : core_moc_headers)
20 21
21 22 core_sources = [
22 23 'src/Common/DateUtils.cpp',
23 24 'src/Common/StringUtils.cpp',
24 25 'src/Common/MimeTypesDef.cpp',
25 26 'src/Data/ScalarSeries.cpp',
26 27 'src/Data/DataSeriesIterator.cpp',
27 28 'src/Data/ArrayDataIterator.cpp',
28 29 'src/Data/VectorSeries.cpp',
29 30 'src/DataSource/DataSourceController.cpp',
30 31 'src/DataSource/DataSourceItem.cpp',
31 32 'src/DataSource/DataSourceItemAction.cpp',
33 'src/DataSource/DataSourceWidget.cpp',
32 34 'src/Network/NetworkController.cpp',
33 35 'src/Plugin/PluginManager.cpp',
34 36 'src/Settings/SqpSettingsDefs.cpp',
35 37 'src/Time/TimeController.cpp',
36 38 'src/Variable/Variable.cpp',
37 39 'src/Variable/VariableCacheController.cpp',
38 40 'src/Variable/VariableController.cpp',
39 41 'src/Variable/VariableAcquisitionWorker.cpp',
40 42 'src/Variable/VariableSynchronizationGroup.cpp',
41 43 'src/Variable/VariableModel.cpp',
42 44 'src/Visualization/VisualizationController.cpp'
43 45 ]
44 46
45 47 core_inc = include_directories(['include', '../plugin/include'])
46 48
47 49 sciqlop_core_lib = library('sciqlopcore',
48 50 core_sources,
49 51 core_moc_files,
50 52 cpp_args : '-DCORE_LIB',
51 53 include_directories : core_inc,
52 54 dependencies : [qt5core, qt5network],
53 55 install : true
54 56 )
55 57
56 58
57 59 sciqlop_core = declare_dependency(link_with : sciqlop_core_lib,
58 60 include_directories : core_inc,
59 61 dependencies : [qt5core, qt5network])
60 62
61 63
62 64 subdir('tests')
63 65
@@ -1,6 +1,7
1 1 #include "Common/MimeTypesDef.h"
2 2
3 3 const QString MIME_TYPE_GRAPH = QStringLiteral("scqlop/graph");
4 4 const QString MIME_TYPE_ZONE = QStringLiteral("scqlop/zone");
5 5 const QString MIME_TYPE_VARIABLE_LIST = QStringLiteral("scqlop/var-list");
6 const QString MIME_TYPE_PRODUCT_LIST = QStringLiteral("scqlop/product-list");
6 7 const QString MIME_TYPE_TIME_RANGE = QStringLiteral("scqlop/time-range");
@@ -1,176 +1,185
1 1 #include <DataSource/DataSourceItem.h>
2 2 #include <DataSource/DataSourceItemAction.h>
3 3 #include <DataSource/DataSourceTreeWidgetItem.h>
4 4
5 5 #include <QAction>
6 6
7 7 Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
8 8
9 9 namespace {
10 10
11 11 // Column indexes
12 12 const auto NAME_COLUMN = 0;
13 13
14 14 QIcon itemIcon(const DataSourceItem *dataSource)
15 15 {
16 16 if (dataSource) {
17 17 auto dataSourceType = dataSource->type();
18 18 switch (dataSourceType) {
19 19 case DataSourceItemType::NODE: {
20 20 return dataSource->isRoot() ? QIcon{":/icones/dataSourceRoot.png"}
21 21 : QIcon{":/icones/dataSourceNode.png"};
22 22 }
23 23 case DataSourceItemType::PRODUCT:
24 24 return QIcon{":/icones/dataSourceProduct.png"};
25 25 case DataSourceItemType::COMPONENT:
26 26 return QIcon{":/icones/dataSourceComponent.png"};
27 27 default:
28 28 // No action
29 29 break;
30 30 }
31 31
32 32 qCWarning(LOG_DataSourceTreeWidgetItem())
33 33 << QObject::tr("Can't set data source icon : unknown data source type");
34 34 }
35 35 else {
36 36 qCCritical(LOG_DataSourceTreeWidgetItem())
37 37 << QObject::tr("Can't set data source icon : the data source is null");
38 38 }
39 39
40 40 // Default cases
41 41 return QIcon{};
42 42 }
43 43
44 44 /// @return the tooltip text for a variant. The text depends on whether the data is a simple variant
45 45 /// or a list of variants
46 46 QString tooltipValue(const QVariant &variant) noexcept
47 47 {
48 48 // If the variant is a list of variants, the text of the tooltip is of the form: {val1, val2,
49 49 // ...}
50 50 if (variant.canConvert<QVariantList>()) {
51 51 auto valueString = QStringLiteral("{");
52 52
53 53 auto variantList = variant.value<QVariantList>();
54 54 for (auto it = variantList.cbegin(), end = variantList.cend(); it != end; ++it) {
55 55 valueString.append(it->toString());
56 56
57 57 if (std::distance(it, end) != 1) {
58 58 valueString.append(", ");
59 59 }
60 60 }
61 61
62 62 valueString.append(QStringLiteral("}"));
63 63
64 64 return valueString;
65 65 }
66 66 else {
67 67 return variant.toString();
68 68 }
69 69 }
70 70
71 71 QString itemTooltip(const DataSourceItem *dataSource) noexcept
72 72 {
73 73 // The tooltip displays all item's data
74 74 if (dataSource) {
75 75 auto result = QString{};
76 76
77 77 const auto &data = dataSource->data();
78 78 for (auto it = data.cbegin(), end = data.cend(); it != end; ++it) {
79 79 result.append(QString{"<b>%1:</b> %2<br/>"}.arg(it.key(), tooltipValue(it.value())));
80 80 }
81 81
82 82 return result;
83 83 }
84 84 else {
85 85 qCCritical(LOG_DataSourceTreeWidgetItem())
86 86 << QObject::tr("Can't set data source tooltip : the data source is null");
87 87
88 88 return QString{};
89 89 }
90 90 }
91 91
92 92 } // namespace
93 93
94 94 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
95 95 explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
96 96
97 97 /// Model used to retrieve data source information
98 98 const DataSourceItem *m_Data;
99 99 /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
100 100 /// the actions
101 101 QList<QAction *> m_Actions;
102 102 };
103 103
104 104 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type)
105 105 : DataSourceTreeWidgetItem{nullptr, data, type}
106 106 {
107 107 }
108 108
109 109 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data,
110 110 int type)
111 111 : QTreeWidgetItem{parent, type},
112 112 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
113 113 {
114 114 // Sets the icon and the tooltip depending on the data source
115 115 setIcon(0, itemIcon(impl->m_Data));
116 116 setToolTip(0, itemTooltip(impl->m_Data));
117 117
118 118 // Generates tree actions based on the item actions
119 119 auto createTreeAction = [this, &parent](const auto &itemAction) {
120 120 auto treeAction = new QAction{itemAction->name(), parent};
121 121
122 122 // Executes item action when tree action is triggered
123 123 QObject::connect(treeAction, &QAction::triggered, itemAction,
124 124 &DataSourceItemAction::execute);
125 125
126 126 return treeAction;
127 127 };
128 128
129 129 auto itemActions = impl->m_Data->actions();
130 130 std::transform(std::cbegin(itemActions), std::cend(itemActions),
131 131 std::back_inserter(impl->m_Actions), createTreeAction);
132
133 // Sets the flags of the items
134 auto flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
135 if (data->type() == DataSourceItemType::COMPONENT
136 || data->type() == DataSourceItemType::PRODUCT) {
137 flags |= Qt::ItemIsDragEnabled;
138 }
139
140 setFlags(flags);
132 141 }
133 142
134 143 const DataSourceItem *DataSourceTreeWidgetItem::data() const
135 144 {
136 145 return impl->m_Data;
137 146 }
138 147
139 148 QVariant DataSourceTreeWidgetItem::data(int column, int role) const
140 149 {
141 150 if (role == Qt::DisplayRole) {
142 151 if (impl->m_Data) {
143 152 switch (column) {
144 153 case NAME_COLUMN:
145 154 return impl->m_Data->name();
146 155 default:
147 156 // No action
148 157 break;
149 158 }
150 159
151 160 qCWarning(LOG_DataSourceTreeWidgetItem())
152 161 << QObject::tr("Can't get data (unknown column %1)").arg(column);
153 162 }
154 163 else {
155 164 qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)");
156 165 }
157 166
158 167 return QVariant{};
159 168 }
160 169 else {
161 170 return QTreeWidgetItem::data(column, role);
162 171 }
163 172 }
164 173
165 174 void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value)
166 175 {
167 176 // Data can't be changed by edition
168 177 if (role != Qt::EditRole) {
169 178 QTreeWidgetItem::setData(column, role, value);
170 179 }
171 180 }
172 181
173 182 QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
174 183 {
175 184 return impl->m_Actions;
176 185 }
@@ -1,33 +1,46
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>DataSourceWidget</class>
4 4 <widget class="QWidget" name="DataSourceWidget">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>400</width>
10 10 <height>300</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Data sources</string>
15 15 </property>
16 16 <layout class="QGridLayout" name="gridLayout">
17 17 <item row="0" column="0">
18 18 <widget class="QLineEdit" name="filterLineEdit"/>
19 19 </item>
20 20 <item row="1" column="0">
21 <widget class="QTreeWidget" name="treeWidget">
21 <widget class="DataSourceTreeWidget" name="treeWidget">
22 <property name="dragEnabled">
23 <bool>true</bool>
24 </property>
25 <property name="dragDropMode">
26 <enum>QAbstractItemView::DragOnly</enum>
27 </property>
22 28 <column>
23 29 <property name="text">
24 30 <string notr="true">1</string>
25 31 </property>
26 32 </column>
27 33 </widget>
28 34 </item>
29 35 </layout>
30 36 </widget>
37 <customwidgets>
38 <customwidget>
39 <class>DataSourceTreeWidget</class>
40 <extends>QTreeWidget</extends>
41 <header>DataSource/DataSourceTreeWidget.h</header>
42 </customwidget>
43 </customwidgets>
31 44 <resources/>
32 45 <connections/>
33 46 </ui>
@@ -1,37 +1,40
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>VariableInspectorWidget</class>
4 4 <widget class="QWidget" name="VariableInspectorWidget">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>400</width>
10 10 <height>300</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Variables</string>
15 15 </property>
16 16 <layout class="QGridLayout" name="gridLayout">
17 17 <item row="0" column="0">
18 18 <widget class="QTableView" name="tableView">
19 <property name="acceptDrops">
20 <bool>true</bool>
21 </property>
19 22 <property name="dragEnabled">
20 23 <bool>true</bool>
21 24 </property>
22 25 <property name="dragDropMode">
23 26 <enum>QAbstractItemView::DragDrop</enum>
24 27 </property>
25 28 <property name="sortingEnabled">
26 29 <bool>true</bool>
27 30 </property>
28 31 <attribute name="horizontalHeaderStretchLastSection">
29 32 <bool>true</bool>
30 33 </attribute>
31 34 </widget>
32 35 </item>
33 36 </layout>
34 37 </widget>
35 38 <resources/>
36 39 <connections/>
37 40 </ui>
General Comments 0
You need to be logged in to leave comments. Login now