Auto status change to "Under Review"
@@ -1,151 +1,151 | |||
|
1 | 1 | #include <Data/ArrayData.h> |
|
2 | 2 | #include <Data/OptionalAxis.h> |
|
3 | 3 | |
|
4 | 4 | #include <QObject> |
|
5 | 5 | #include <QtTest> |
|
6 | 6 | |
|
7 | 7 | Q_DECLARE_METATYPE(OptionalAxis) |
|
8 | 8 | Q_DECLARE_METATYPE(Unit) |
|
9 | 9 | |
|
10 | 10 | class TestOptionalAxis : public QObject { |
|
11 | 11 | Q_OBJECT |
|
12 | 12 | |
|
13 | 13 | private slots: |
|
14 | 14 | /// Tests the creation of a undefined axis |
|
15 | 15 | void testNotDefinedAxisCtor(); |
|
16 | 16 | |
|
17 | 17 | /// Tests the creation of a undefined axis |
|
18 | 18 | void testDefinedAxisCtor_data(); |
|
19 | 19 | void testDefinedAxisCtor(); |
|
20 | 20 | |
|
21 | 21 | /// Tests @sa OptionalAxis::at() method |
|
22 | 22 | void testAt_data(); |
|
23 | 23 | void testAt(); |
|
24 | 24 | |
|
25 | 25 | /// Tests @sa OptionalAxis::size() method |
|
26 | 26 | void testSize_data(); |
|
27 | 27 | void testSize(); |
|
28 | 28 | |
|
29 | 29 | /// Tests @sa OptionalAxis::unit() method |
|
30 | 30 | void testUnit_data(); |
|
31 | 31 | void testUnit(); |
|
32 | 32 | }; |
|
33 | 33 | |
|
34 | 34 | void TestOptionalAxis::testNotDefinedAxisCtor() |
|
35 | 35 | { |
|
36 | 36 | OptionalAxis notDefinedAxis{}; |
|
37 | 37 | QVERIFY(!notDefinedAxis.isDefined()); |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | void TestOptionalAxis::testDefinedAxisCtor_data() |
|
41 | 41 | { |
|
42 | 42 | QTest::addColumn<bool>("noData"); // If set to true, nullptr is passed as data of the axis |
|
43 | 43 | QTest::addColumn<std::vector<double> >( |
|
44 | 44 | "data"); // Values assigned to the axis when 'noData' flag is set to false |
|
45 | 45 | QTest::addColumn<Unit>("unit"); // Unit assigned to the axis |
|
46 | 46 | |
|
47 | 47 | QTest::newRow("validData") << false << std::vector<double>{1, 2, 3} << Unit{"Hz"}; |
|
48 | 48 | QTest::newRow("invalidData") << true << std::vector<double>{} << Unit{"Hz"}; |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void TestOptionalAxis::testDefinedAxisCtor() |
|
52 | 52 | { |
|
53 | 53 | QFETCH(bool, noData); |
|
54 | 54 | QFETCH(Unit, unit); |
|
55 | 55 | |
|
56 | 56 | // When there is no data, we expect that the constructor returns exception |
|
57 | 57 | if (noData) { |
|
58 | 58 | QVERIFY_EXCEPTION_THROWN(OptionalAxis(nullptr, unit), std::invalid_argument); |
|
59 | 59 | } |
|
60 | 60 | else { |
|
61 | 61 | QFETCH(std::vector<double>, data); |
|
62 | 62 | |
|
63 | 63 | OptionalAxis axis{std::make_shared<ArrayData<1> >(data), unit}; |
|
64 | 64 | QVERIFY(axis.isDefined()); |
|
65 | 65 | } |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void TestOptionalAxis::testAt_data() |
|
69 | 69 | { |
|
70 | 70 | QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not) |
|
71 | 71 | QTest::addColumn<int>("index"); // Index to test in the axis |
|
72 | 72 | QTest::addColumn<double>("expectedValue"); // Expected axis value for the index |
|
73 | 73 | |
|
74 | 74 | OptionalAxis definedAxis{std::make_shared<ArrayData<1> >(std::vector<double>{1, 2, 3}), |
|
75 | 75 | Unit{"Hz"}}; |
|
76 | 76 | |
|
77 | 77 | QTest::newRow("data1") << definedAxis << 0 << 1.; |
|
78 | 78 | QTest::newRow("data2") << definedAxis << 1 << 2.; |
|
79 | 79 | QTest::newRow("data3") << definedAxis << 2 << 3.; |
|
80 | 80 | QTest::newRow("data4 (index out of bounds)") |
|
81 | 81 | << definedAxis << 3 |
|
82 | 82 | << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for out of bounds index |
|
83 | 83 | QTest::newRow("data5 (index out of bounds)") |
|
84 | 84 | << definedAxis << -1 |
|
85 | 85 | << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for out of bounds index |
|
86 | 86 | QTest::newRow("data6 (axis not defined)") |
|
87 | 87 | << OptionalAxis{} << 0 |
|
88 | 88 | << std::numeric_limits<double>::quiet_NaN(); // Expects NaN for undefined axis |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | void TestOptionalAxis::testAt() |
|
92 | 92 | { |
|
93 | 93 | QFETCH(OptionalAxis, axis); |
|
94 | 94 | QFETCH(int, index); |
|
95 | 95 | QFETCH(double, expectedValue); |
|
96 | 96 | |
|
97 | 97 | auto value = axis.at(index); |
|
98 | 98 | QVERIFY((std::isnan(value) && std::isnan(expectedValue)) || value == expectedValue); |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | void TestOptionalAxis::testSize_data() |
|
102 | 102 | { |
|
103 | 103 | QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not) |
|
104 | 104 | QTest::addColumn<int>("expectedSize"); // Expected number of data in the axis |
|
105 | 105 | |
|
106 | 106 | // Lambda that creates default defined axis (with the values passed in parameter) |
|
107 | 107 | auto axis = [](std::vector<double> values) { |
|
108 | 108 | return OptionalAxis{std::make_shared<ArrayData<1> >(std::move(values)), Unit{"Hz"}}; |
|
109 | 109 | }; |
|
110 | 110 | |
|
111 | 111 | QTest::newRow("data1") << axis({}) << 0; |
|
112 | 112 | QTest::newRow("data2") << axis({1, 2, 3}) << 3; |
|
113 | 113 | QTest::newRow("data3") << axis({1, 2, 3, 4}) << 4; |
|
114 | QTest::newRow("data4 (axis not defined)") | |
|
115 |
|
|
|
114 | QTest::newRow("data4 (axis not defined)") << OptionalAxis{} | |
|
115 | << 0; // Expects 0 for undefined axis | |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | void TestOptionalAxis::testSize() |
|
119 | 119 | { |
|
120 | 120 | QFETCH(OptionalAxis, axis); |
|
121 | 121 | QFETCH(int, expectedSize); |
|
122 | 122 | |
|
123 | 123 | QCOMPARE(axis.size(), expectedSize); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void TestOptionalAxis::testUnit_data() |
|
127 | 127 | { |
|
128 | 128 | QTest::addColumn<OptionalAxis>("axis"); // Axis used for test case (defined or not) |
|
129 | 129 | QTest::addColumn<Unit>("expectedUnit"); // Expected unit for the axis |
|
130 | 130 | |
|
131 | 131 | // Lambda that creates default defined axis (with the unit passed in parameter) |
|
132 | 132 | auto axis = [](Unit unit) { |
|
133 | 133 | return OptionalAxis{std::make_shared<ArrayData<1> >(std::vector<double>{1, 2, 3}), unit}; |
|
134 | 134 | }; |
|
135 | 135 | |
|
136 | 136 | QTest::newRow("data1") << axis(Unit{"Hz"}) << Unit{"Hz"}; |
|
137 | 137 | QTest::newRow("data2") << axis(Unit{"t", true}) << Unit{"t", true}; |
|
138 | QTest::newRow("data3 (axis not defined)") | |
|
139 |
|
|
|
138 | QTest::newRow("data3 (axis not defined)") << OptionalAxis{} | |
|
139 | << Unit{}; // Expects default unit for undefined axis | |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | void TestOptionalAxis::testUnit() |
|
143 | 143 | { |
|
144 | 144 | QFETCH(OptionalAxis, axis); |
|
145 | 145 | QFETCH(Unit, expectedUnit); |
|
146 | 146 | |
|
147 | 147 | QCOMPARE(axis.unit(), expectedUnit); |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | QTEST_MAIN(TestOptionalAxis) |
|
151 | 151 | #include "TestOptionalAxis.moc" |
@@ -1,426 +1,426 | |||
|
1 | 1 | #include "Data/ScalarSeries.h" |
|
2 | 2 | |
|
3 | 3 | #include "DataSeriesBuilders.h" |
|
4 | 4 | #include "DataSeriesUtils.h" |
|
5 | 5 | |
|
6 | 6 | #include <QObject> |
|
7 | 7 | #include <QtTest> |
|
8 | 8 | |
|
9 | 9 | /** |
|
10 | 10 | * @brief The TestScalarSeries class defines unit tests on scalar series. |
|
11 | 11 | * |
|
12 | 12 | * Most of these unit tests use generic tests defined for DataSeries (@sa DataSeriesUtils) |
|
13 | 13 | */ |
|
14 | 14 | class TestScalarSeries : public QObject { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | private slots: |
|
17 | 17 | /// Tests construction of a scalar series |
|
18 | 18 | void testCtor_data(); |
|
19 | 19 | void testCtor(); |
|
20 | 20 | |
|
21 | 21 | /// Tests merge of two scalar series |
|
22 | 22 | void testMerge_data(); |
|
23 | 23 | void testMerge(); |
|
24 | 24 | |
|
25 | 25 | /// Tests merge of a vector series in a scalar series |
|
26 | 26 | void testMergeWithVector_data(); |
|
27 | 27 | void testMergeWithVector(); |
|
28 | 28 | |
|
29 | 29 | /// Tests get min x-axis data of a scalar series |
|
30 | 30 | void testMinXAxisData_data(); |
|
31 | 31 | void testMinXAxisData(); |
|
32 | 32 | |
|
33 | 33 | /// Tests get max x-axis data of a scalar series |
|
34 | 34 | void testMaxXAxisData_data(); |
|
35 | 35 | void testMaxXAxisData(); |
|
36 | 36 | |
|
37 | 37 | /// Tests purge of a scalar series |
|
38 | 38 | void testPurge_data(); |
|
39 | 39 | void testPurge(); |
|
40 | 40 | |
|
41 | 41 | /// Tests get x-axis range of a scalar series |
|
42 | 42 | void testXAxisRange_data(); |
|
43 | 43 | void testXAxisRange(); |
|
44 | 44 | |
|
45 | 45 | /// Tests get values bounds of a scalar series |
|
46 | 46 | void testValuesBounds_data(); |
|
47 | 47 | void testValuesBounds(); |
|
48 | 48 | }; |
|
49 | 49 | |
|
50 | 50 | void TestScalarSeries::testCtor_data() |
|
51 | 51 | { |
|
52 | 52 | // x-axis data |
|
53 | 53 | QTest::addColumn<DataContainer>("xAxisData"); |
|
54 | 54 | // values data |
|
55 | 55 | QTest::addColumn<DataContainer>("valuesData"); |
|
56 | 56 | |
|
57 | 57 | // construction expected to be valid |
|
58 | 58 | QTest::addColumn<bool>("expectOK"); |
|
59 | 59 | // expected x-axis data (when construction is valid) |
|
60 | 60 | QTest::addColumn<DataContainer>("expectedXAxisData"); |
|
61 | 61 | // expected values data (when construction is valid) |
|
62 | 62 | QTest::addColumn<DataContainer>("expectedValuesData"); |
|
63 | 63 | |
|
64 | 64 | QTest::newRow("invalidData (different sizes of vectors)") |
|
65 | 65 | << DataContainer{1., 2., 3., 4., 5.} << DataContainer{100., 200., 300.} << false |
|
66 | 66 | << DataContainer{} << DataContainer{}; |
|
67 | 67 | |
|
68 | 68 | QTest::newRow("sortedData") << DataContainer{1., 2., 3., 4., 5.} |
|
69 | 69 | << DataContainer{100., 200., 300., 400., 500.} << true |
|
70 | 70 | << DataContainer{1., 2., 3., 4., 5.} |
|
71 | 71 | << DataContainer{100., 200., 300., 400., 500.}; |
|
72 | 72 | |
|
73 | 73 | QTest::newRow("unsortedData") << DataContainer{5., 4., 3., 2., 1.} |
|
74 | 74 | << DataContainer{100., 200., 300., 400., 500.} << true |
|
75 | 75 | << DataContainer{1., 2., 3., 4., 5.} |
|
76 | 76 | << DataContainer{500., 400., 300., 200., 100.}; |
|
77 | 77 | |
|
78 | 78 | QTest::newRow("unsortedData2") |
|
79 | 79 | << DataContainer{1., 4., 3., 5., 2.} << DataContainer{100., 200., 300., 400., 500.} << true |
|
80 | 80 | << DataContainer{1., 2., 3., 4., 5.} << DataContainer{100., 500., 300., 200., 400.}; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | void TestScalarSeries::testCtor() |
|
84 | 84 | { |
|
85 | 85 | // Creates series |
|
86 | 86 | QFETCH(DataContainer, xAxisData); |
|
87 | 87 | QFETCH(DataContainer, valuesData); |
|
88 | 88 | QFETCH(bool, expectOK); |
|
89 | 89 | |
|
90 | 90 | if (expectOK) { |
|
91 | 91 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), |
|
92 | 92 | Unit{}, Unit{}); |
|
93 | 93 | |
|
94 | 94 | // Validates results : we check that the data series is sorted on its x-axis data |
|
95 | 95 | QFETCH(DataContainer, expectedXAxisData); |
|
96 | 96 | QFETCH(DataContainer, expectedValuesData); |
|
97 | 97 | |
|
98 | 98 | validateRange(series->cbegin(), series->cend(), expectedXAxisData, expectedValuesData); |
|
99 | 99 | } |
|
100 | 100 | else { |
|
101 | 101 | QVERIFY_EXCEPTION_THROWN(std::make_shared<ScalarSeries>( |
|
102 | 102 | std::move(xAxisData), std::move(valuesData), Unit{}, Unit{}), |
|
103 | 103 | std::invalid_argument); |
|
104 | 104 | } |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | void TestScalarSeries::testMerge_data() |
|
108 | 108 | { |
|
109 | 109 | testMerge_struct<ScalarSeries, DataContainer>(); |
|
110 | 110 | |
|
111 | 111 | QTest::newRow("sortedMerge") << ScalarBuilder{} |
|
112 | 112 | .setX({1., 2., 3., 4., 5.}) |
|
113 | 113 | .setValues({100., 200., 300., 400., 500.}) |
|
114 | 114 | .build() |
|
115 | 115 | << ScalarBuilder{} |
|
116 | 116 | .setX({6., 7., 8., 9., 10.}) |
|
117 | 117 | .setValues({600., 700., 800., 900., 1000.}) |
|
118 | 118 | .build() |
|
119 | 119 | << DataContainer{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
120 | 120 | << DataContainer{100., 200., 300., 400., 500., |
|
121 | 121 | 600., 700., 800., 900., 1000.}; |
|
122 | 122 | |
|
123 | 123 | QTest::newRow("unsortedMerge") |
|
124 | 124 | << ScalarBuilder{} |
|
125 | 125 | .setX({6., 7., 8., 9., 10.}) |
|
126 | 126 | .setValues({600., 700., 800., 900., 1000.}) |
|
127 | 127 | .build() |
|
128 | 128 | << ScalarBuilder{} |
|
129 | 129 | .setX({1., 2., 3., 4., 5.}) |
|
130 | 130 | .setValues({100., 200., 300., 400., 500.}) |
|
131 | 131 | .build() |
|
132 | 132 | << DataContainer{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
133 | 133 | << DataContainer{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; |
|
134 | 134 | |
|
135 | 135 | QTest::newRow("unsortedMerge2 (merge not made because source is in the bounds of dest)") |
|
136 | 136 | << ScalarBuilder{} |
|
137 | 137 | .setX({1., 2., 8., 9., 10}) |
|
138 | 138 | .setValues({100., 200., 800., 900., 1000.}) |
|
139 | 139 | .build() |
|
140 | 140 | << ScalarBuilder{} |
|
141 | 141 | .setX({3., 4., 5., 6., 7.}) |
|
142 | 142 | .setValues({300., 400., 500., 600., 700.}) |
|
143 | 143 | .build() |
|
144 | 144 | << DataContainer{1., 2., 8., 9., 10.} << DataContainer{100., 200., 800., 900., 1000.}; |
|
145 | 145 | |
|
146 | 146 | QTest::newRow("unsortedMerge3") |
|
147 | 147 | << ScalarBuilder{} |
|
148 | 148 | .setX({3., 4., 5., 7., 8}) |
|
149 | 149 | .setValues({300., 400., 500., 700., 800.}) |
|
150 | 150 | .build() |
|
151 | 151 | << ScalarBuilder{} |
|
152 | 152 | .setX({1., 2., 3., 7., 10.}) |
|
153 | 153 | .setValues({100., 200., 333., 777., 1000.}) |
|
154 | 154 | .build() |
|
155 | 155 | << DataContainer{1., 2., 3., 4., 5., 7., 8., 10.} |
|
156 | 156 | << DataContainer{100., 200., 300., 400., 500., 700., 800., 1000.}; |
|
157 | 157 | |
|
158 | 158 | QTest::newRow("emptySource") << ScalarBuilder{} |
|
159 | 159 | .setX({3., 4., 5., 7., 8}) |
|
160 | 160 | .setValues({300., 400., 500., 700., 800.}) |
|
161 | 161 | .build() |
|
162 | 162 | << ScalarBuilder{}.setX({}).setValues({}).build() |
|
163 | 163 | << DataContainer{3., 4., 5., 7., 8.} |
|
164 | 164 | << DataContainer{300., 400., 500., 700., 800.}; |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | void TestScalarSeries::testMerge() |
|
168 | 168 | { |
|
169 | 169 | testMerge_t<ScalarSeries, DataContainer>(); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void TestScalarSeries::testMergeWithVector_data() |
|
173 | 173 | { |
|
174 | 174 | testMergeDifferentTypes_struct<VectorSeries, ScalarSeries>(); |
|
175 | 175 | |
|
176 | QTest::newRow("mergeVectorInScalar") | |
|
177 | << ScalarBuilder{} | |
|
176 | QTest::newRow("mergeVectorInScalar") << ScalarBuilder{} | |
|
178 | 177 | .setX({1., 2., 3., 4., 5.}) |
|
179 | 178 | .setValues({100., 200., 300., 400., 500.}) |
|
180 | 179 | .build() |
|
181 | 180 | << VectorBuilder{} |
|
182 | 181 | .setX({6., 7., 8., 9., 10.}) |
|
183 | 182 | .setXValues({600., 700., 800., 900., 1000.}) |
|
184 | 183 | .setYValues({610., 710., 810., 910., 1010.}) |
|
185 | 184 | .setZValues({620., 720., 820., 920., 1020.}) |
|
186 | 185 | .build() |
|
187 | << DataContainer{1., 2., 3., 4., 5.} << DataContainer{100., 200., 300., 400., 500.}; | |
|
186 | << DataContainer{1., 2., 3., 4., 5.} | |
|
187 | << DataContainer{100., 200., 300., 400., 500.}; | |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | void TestScalarSeries::testMergeWithVector() |
|
191 | 191 | { |
|
192 | 192 | testMergeDifferentTypes_t<VectorSeries, ScalarSeries>(); |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | void TestScalarSeries::testMinXAxisData_data() |
|
196 | 196 | { |
|
197 | 197 | testMinXAxisData_struct<ScalarSeries>(); |
|
198 | 198 | |
|
199 | 199 | QTest::newRow("minData1") << ScalarBuilder{} |
|
200 | 200 | .setX({1., 2., 3., 4., 5.}) |
|
201 | 201 | .setValues({100., 200., 300., 400., 500.}) |
|
202 | 202 | .build() |
|
203 | 203 | << 0. << true << 1.; |
|
204 | 204 | QTest::newRow("minData2") << ScalarBuilder{} |
|
205 | 205 | .setX({1., 2., 3., 4., 5.}) |
|
206 | 206 | .setValues({100., 200., 300., 400., 500.}) |
|
207 | 207 | .build() |
|
208 | 208 | << 1. << true << 1.; |
|
209 | 209 | QTest::newRow("minData3") << ScalarBuilder{} |
|
210 | 210 | .setX({1., 2., 3., 4., 5.}) |
|
211 | 211 | .setValues({100., 200., 300., 400., 500.}) |
|
212 | 212 | .build() |
|
213 | 213 | << 1.1 << true << 2.; |
|
214 | 214 | QTest::newRow("minData4") << ScalarBuilder{} |
|
215 | 215 | .setX({1., 2., 3., 4., 5.}) |
|
216 | 216 | .setValues({100., 200., 300., 400., 500.}) |
|
217 | 217 | .build() |
|
218 | 218 | << 5. << true << 5.; |
|
219 | 219 | QTest::newRow("minData5") << ScalarBuilder{} |
|
220 | 220 | .setX({1., 2., 3., 4., 5.}) |
|
221 | 221 | .setValues({100., 200., 300., 400., 500.}) |
|
222 | 222 | .build() |
|
223 | 223 | << 5.1 << false << std::numeric_limits<double>::quiet_NaN(); |
|
224 | 224 | QTest::newRow("minData6") << ScalarBuilder{}.setX({}).setValues({}).build() << 1.1 << false |
|
225 | 225 | << std::numeric_limits<double>::quiet_NaN(); |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | void TestScalarSeries::testMinXAxisData() |
|
229 | 229 | { |
|
230 | 230 | testMinXAxisData_t<ScalarSeries>(); |
|
231 | 231 | } |
|
232 | 232 | |
|
233 | 233 | void TestScalarSeries::testMaxXAxisData_data() |
|
234 | 234 | { |
|
235 | 235 | testMaxXAxisData_struct<ScalarSeries>(); |
|
236 | 236 | |
|
237 | 237 | QTest::newRow("maxData1") << ScalarBuilder{} |
|
238 | 238 | .setX({1., 2., 3., 4., 5.}) |
|
239 | 239 | .setValues({100., 200., 300., 400., 500.}) |
|
240 | 240 | .build() |
|
241 | 241 | << 6. << true << 5.; |
|
242 | 242 | QTest::newRow("maxData2") << ScalarBuilder{} |
|
243 | 243 | .setX({1., 2., 3., 4., 5.}) |
|
244 | 244 | .setValues({100., 200., 300., 400., 500.}) |
|
245 | 245 | .build() |
|
246 | 246 | << 5. << true << 5.; |
|
247 | 247 | QTest::newRow("maxData3") << ScalarBuilder{} |
|
248 | 248 | .setX({1., 2., 3., 4., 5.}) |
|
249 | 249 | .setValues({100., 200., 300., 400., 500.}) |
|
250 | 250 | .build() |
|
251 | 251 | << 4.9 << true << 4.; |
|
252 | 252 | QTest::newRow("maxData4") << ScalarBuilder{} |
|
253 | 253 | .setX({1., 2., 3., 4., 5.}) |
|
254 | 254 | .setValues({100., 200., 300., 400., 500.}) |
|
255 | 255 | .build() |
|
256 | 256 | << 1.1 << true << 1.; |
|
257 | 257 | QTest::newRow("maxData5") << ScalarBuilder{} |
|
258 | 258 | .setX({1., 2., 3., 4., 5.}) |
|
259 | 259 | .setValues({100., 200., 300., 400., 500.}) |
|
260 | 260 | .build() |
|
261 | 261 | << 1. << true << 1.; |
|
262 | 262 | QTest::newRow("maxData6") << ScalarBuilder{}.setX({}).setValues({}).build() << 1.1 << false |
|
263 | 263 | << std::numeric_limits<double>::quiet_NaN(); |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | void TestScalarSeries::testMaxXAxisData() |
|
267 | 267 | { |
|
268 | 268 | testMaxXAxisData_t<ScalarSeries>(); |
|
269 | 269 | } |
|
270 | 270 | |
|
271 | 271 | void TestScalarSeries::testPurge_data() |
|
272 | 272 | { |
|
273 | 273 | testPurge_struct<ScalarSeries>(); |
|
274 | 274 | |
|
275 | 275 | QTest::newRow("purgeScalar") << ScalarBuilder{} |
|
276 | 276 | .setX({1., 2., 3., 4., 5.}) |
|
277 | 277 | .setValues({100., 200., 300., 400., 500.}) |
|
278 | 278 | .build() |
|
279 | 279 | << 2. << 4. << DataContainer{2., 3., 4.} |
|
280 | 280 | << std::vector<DataContainer>{{200., 300., 400.}}; |
|
281 | QTest::newRow("purgeScalar1 (min/max swap)") | |
|
282 | << ScalarBuilder{} | |
|
281 | QTest::newRow("purgeScalar1 (min/max swap)") << ScalarBuilder{} | |
|
283 | 282 | .setX({1., 2., 3., 4., 5.}) |
|
284 | 283 | .setValues({100., 200., 300., 400., 500.}) |
|
285 | 284 | .build() |
|
286 | << 4. << 2. << DataContainer{2., 3., 4.} << std::vector<DataContainer>{{200., 300., 400.}}; | |
|
285 | << 4. << 2. << DataContainer{2., 3., 4.} | |
|
286 | << std::vector<DataContainer>{{200., 300., 400.}}; | |
|
287 | 287 | QTest::newRow("purgeScalar2") << ScalarBuilder{} |
|
288 | 288 | .setX({1., 2., 3., 4., 5.}) |
|
289 | 289 | .setValues({100., 200., 300., 400., 500.}) |
|
290 | 290 | .build() |
|
291 | 291 | << 0. << 2.5 << DataContainer{1., 2.} |
|
292 | 292 | << std::vector<DataContainer>{{100., 200.}}; |
|
293 | 293 | QTest::newRow("purgeScalar3") << ScalarBuilder{} |
|
294 | 294 | .setX({1., 2., 3., 4., 5.}) |
|
295 | 295 | .setValues({100., 200., 300., 400., 500.}) |
|
296 | 296 | .build() |
|
297 | 297 | << 3.5 << 7. << DataContainer{4., 5.} |
|
298 | 298 | << std::vector<DataContainer>{{400., 500.}}; |
|
299 | 299 | QTest::newRow("purgeScalar4") << ScalarBuilder{} |
|
300 | 300 | .setX({1., 2., 3., 4., 5.}) |
|
301 | 301 | .setValues({100., 200., 300., 400., 500.}) |
|
302 | 302 | .build() |
|
303 | 303 | << 0. << 7. << DataContainer{1., 2., 3., 4., 5.} |
|
304 | 304 | << std::vector<DataContainer>{{100., 200., 300., 400., 500.}}; |
|
305 | 305 | QTest::newRow("purgeScalar5") << ScalarBuilder{} |
|
306 | 306 | .setX({1., 2., 3., 4., 5.}) |
|
307 | 307 | .setValues({100., 200., 300., 400., 500.}) |
|
308 | 308 | .build() |
|
309 | 309 | << 5.5 << 7. << DataContainer{} << std::vector<DataContainer>{{}}; |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void TestScalarSeries::testPurge() |
|
313 | 313 | { |
|
314 | 314 | testPurge_t<ScalarSeries>(); |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | void TestScalarSeries::testXAxisRange_data() |
|
318 | 318 | { |
|
319 | 319 | testXAxisRange_struct<ScalarSeries>(); |
|
320 | 320 | |
|
321 | 321 | QTest::newRow("xAxisRange") << ScalarBuilder{} |
|
322 | 322 | .setX({1., 2., 3., 4., 5.}) |
|
323 | 323 | .setValues({100., 200., 300., 400., 500.}) |
|
324 | 324 | .build() |
|
325 | 325 | << -1. << 3.2 << DataContainer{1., 2., 3.} |
|
326 | 326 | << DataContainer{100., 200., 300.}; |
|
327 | QTest::newRow("xAxisRange1 (min/max swap)") | |
|
328 | << ScalarBuilder{} | |
|
327 | QTest::newRow("xAxisRange1 (min/max swap)") << ScalarBuilder{} | |
|
329 | 328 | .setX({1., 2., 3., 4., 5.}) |
|
330 | 329 | .setValues({100., 200., 300., 400., 500.}) |
|
331 | 330 | .build() |
|
332 | << 3.2 << -1. << DataContainer{1., 2., 3.} << DataContainer{100., 200., 300.}; | |
|
331 | << 3.2 << -1. << DataContainer{1., 2., 3.} | |
|
332 | << DataContainer{100., 200., 300.}; | |
|
333 | 333 | QTest::newRow("xAxisRange2") << ScalarBuilder{} |
|
334 | 334 | .setX({1., 2., 3., 4., 5.}) |
|
335 | 335 | .setValues({100., 200., 300., 400., 500.}) |
|
336 | 336 | .build() |
|
337 | 337 | << 1. << 4. << DataContainer{1., 2., 3., 4.} |
|
338 | 338 | << DataContainer{100., 200., 300., 400.}; |
|
339 | 339 | QTest::newRow("xAxisRange3") << ScalarBuilder{} |
|
340 | 340 | .setX({1., 2., 3., 4., 5.}) |
|
341 | 341 | .setValues({100., 200., 300., 400., 500.}) |
|
342 | 342 | .build() |
|
343 | 343 | << 1. << 3.9 << DataContainer{1., 2., 3.} |
|
344 | 344 | << DataContainer{100., 200., 300.}; |
|
345 | 345 | QTest::newRow("xAxisRange4") << ScalarBuilder{} |
|
346 | 346 | .setX({1., 2., 3., 4., 5.}) |
|
347 | 347 | .setValues({100., 200., 300., 400., 500.}) |
|
348 | 348 | .build() |
|
349 | 349 | << 0. << 0.9 << DataContainer{} << DataContainer{}; |
|
350 | 350 | QTest::newRow("xAxisRange5") << ScalarBuilder{} |
|
351 | 351 | .setX({1., 2., 3., 4., 5.}) |
|
352 | 352 | .setValues({100., 200., 300., 400., 500.}) |
|
353 | 353 | .build() |
|
354 | 354 | << 0. << 1. << DataContainer{1.} << DataContainer{100.}; |
|
355 | 355 | QTest::newRow("xAxisRange6") << ScalarBuilder{} |
|
356 | 356 | .setX({1., 2., 3., 4., 5.}) |
|
357 | 357 | .setValues({100., 200., 300., 400., 500.}) |
|
358 | 358 | .build() |
|
359 | 359 | << 2.1 << 6. << DataContainer{3., 4., 5.} |
|
360 | 360 | << DataContainer{300., 400., 500.}; |
|
361 | 361 | QTest::newRow("xAxisRange7") << ScalarBuilder{} |
|
362 | 362 | .setX({1., 2., 3., 4., 5.}) |
|
363 | 363 | .setValues({100., 200., 300., 400., 500.}) |
|
364 | 364 | .build() |
|
365 | 365 | << 6. << 9. << DataContainer{} << DataContainer{}; |
|
366 | 366 | QTest::newRow("xAxisRange8") << ScalarBuilder{} |
|
367 | 367 | .setX({1., 2., 3., 4., 5.}) |
|
368 | 368 | .setValues({100., 200., 300., 400., 500.}) |
|
369 | 369 | .build() |
|
370 | 370 | << 5. << 9. << DataContainer{5.} << DataContainer{500.}; |
|
371 | 371 | } |
|
372 | 372 | |
|
373 | 373 | void TestScalarSeries::testXAxisRange() |
|
374 | 374 | { |
|
375 | 375 | testXAxisRange_t<ScalarSeries>(); |
|
376 | 376 | } |
|
377 | 377 | |
|
378 | 378 | void TestScalarSeries::testValuesBounds_data() |
|
379 | 379 | { |
|
380 | 380 | testValuesBounds_struct<ScalarSeries>(); |
|
381 | 381 | |
|
382 | 382 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
|
383 | 383 | |
|
384 | 384 | QTest::newRow("scalarBounds1") << ScalarBuilder{} |
|
385 | 385 | .setX({1., 2., 3., 4., 5.}) |
|
386 | 386 | .setValues({100., 200., 300., 400., 500.}) |
|
387 | 387 | .build() |
|
388 | 388 | << 0. << 6. << true << 100. << 500.; |
|
389 | 389 | QTest::newRow("scalarBounds2") << ScalarBuilder{} |
|
390 | 390 | .setX({1., 2., 3., 4., 5.}) |
|
391 | 391 | .setValues({100., 200., 300., 400., 500.}) |
|
392 | 392 | .build() |
|
393 | 393 | << 2. << 4. << true << 200. << 400.; |
|
394 | 394 | QTest::newRow("scalarBounds3") << ScalarBuilder{} |
|
395 | 395 | .setX({1., 2., 3., 4., 5.}) |
|
396 | 396 | .setValues({100., 200., 300., 400., 500.}) |
|
397 | 397 | .build() |
|
398 | 398 | << 0. << 0.5 << false << nan << nan; |
|
399 | 399 | QTest::newRow("scalarBounds4") << ScalarBuilder{} |
|
400 | 400 | .setX({1., 2., 3., 4., 5.}) |
|
401 | 401 | .setValues({100., 200., 300., 400., 500.}) |
|
402 | 402 | .build() |
|
403 | 403 | << 5.1 << 6. << false << nan << nan; |
|
404 | QTest::newRow("scalarBounds5") | |
|
405 | << ScalarBuilder{}.setX({1.}).setValues({100.}).build() << 0. << 2. << true << 100. << 100.; | |
|
406 | QTest::newRow("scalarBounds6") | |
|
407 | << ScalarBuilder{}.setX({}).setValues({}).build() << 0. << 2. << false << nan << nan; | |
|
404 | QTest::newRow("scalarBounds5") << ScalarBuilder{}.setX({1.}).setValues({100.}).build() << 0. | |
|
405 | << 2. << true << 100. << 100.; | |
|
406 | QTest::newRow("scalarBounds6") << ScalarBuilder{}.setX({}).setValues({}).build() << 0. << 2. | |
|
407 | << false << nan << nan; | |
|
408 | 408 | |
|
409 | 409 | // Tests with NaN values: NaN values are not included in min/max search |
|
410 | 410 | QTest::newRow("scalarBounds7") << ScalarBuilder{} |
|
411 | 411 | .setX({1., 2., 3., 4., 5.}) |
|
412 | 412 | .setValues({nan, 200., 300., 400., nan}) |
|
413 | 413 | .build() |
|
414 | 414 | << 0. << 6. << true << 200. << 400.; |
|
415 | 415 | QTest::newRow("scalarBounds8") |
|
416 | 416 | << ScalarBuilder{}.setX({1., 2., 3., 4., 5.}).setValues({nan, nan, nan, nan, nan}).build() |
|
417 | 417 | << 0. << 6. << true << nan << nan; |
|
418 | 418 | } |
|
419 | 419 | |
|
420 | 420 | void TestScalarSeries::testValuesBounds() |
|
421 | 421 | { |
|
422 | 422 | testValuesBounds_t<ScalarSeries>(); |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | QTEST_MAIN(TestScalarSeries) |
|
426 | 426 | #include "TestScalarSeries.moc" |
@@ -1,71 +1,75 | |||
|
1 | 1 | #ifndef SCIQLOP_DRAGDROPHELPER_H |
|
2 | 2 | #define SCIQLOP_DRAGDROPHELPER_H |
|
3 | 3 | |
|
4 | 4 | #include <Common/spimpl.h> |
|
5 | 5 | #include <QLoggingCategory> |
|
6 | 6 | #include <QWidget> |
|
7 | 7 | |
|
8 | 8 | class QVBoxLayout; |
|
9 | 9 | class QScrollArea; |
|
10 | 10 | class QTabBar; |
|
11 | 11 | class VisualizationDragWidget; |
|
12 | 12 | class VisualizationDragDropContainer; |
|
13 | 13 | class QMimeData; |
|
14 | 14 | |
|
15 | 15 | Q_DECLARE_LOGGING_CATEGORY(LOG_DragDropHelper) |
|
16 | 16 | |
|
17 | 17 | /** |
|
18 | 18 | * @brief Helper class for drag&drop operations. |
|
19 | 19 | * @note The helper is accessible from the sqpApp singleton and has the same life as the whole |
|
20 | 20 | * application (like a controller). But contrary to a controller, it doesn't live in a thread and |
|
21 | 21 | * can interect with the gui. |
|
22 | 22 | * @see SqpApplication |
|
23 | 23 | */ |
|
24 | 24 | class DragDropHelper { |
|
25 | 25 | public: |
|
26 | 26 | static const QString MIME_TYPE_GRAPH; |
|
27 | 27 | static const QString MIME_TYPE_ZONE; |
|
28 | 28 | |
|
29 | 29 | enum class PlaceHolderType { Default, Graph, Zone }; |
|
30 | 30 | |
|
31 | 31 | DragDropHelper(); |
|
32 | 32 | virtual ~DragDropHelper(); |
|
33 | 33 | |
|
34 | 34 | /// Resets some internal variables. Must be called before any new drag&drop operation. |
|
35 | 35 | void resetDragAndDrop(); |
|
36 | 36 | |
|
37 | 37 | /// Sets the visualization widget currently being drag on the visualization. |
|
38 | 38 | void setCurrentDragWidget(VisualizationDragWidget *dragWidget); |
|
39 | 39 | |
|
40 | 40 | /// Returns the visualization widget currently being drag on the visualization. |
|
41 | 41 | /// Can be null if a new visualization widget is intended to be created by the drag&drop |
|
42 | 42 | /// operation. |
|
43 | 43 | VisualizationDragWidget *getCurrentDragWidget() const; |
|
44 | 44 | |
|
45 | 45 | QWidget &placeHolder() const; |
|
46 | 46 | void insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type, |
|
47 | 47 | const QString &topLabelText); |
|
48 | 48 | void removePlaceHolder(); |
|
49 | 49 | bool isPlaceHolderSet() const; |
|
50 | 50 | |
|
51 | 51 | /// Checks if the specified mime data is valid for a drop in the visualization |
|
52 | 52 | bool checkMimeDataForVisualization(const QMimeData *mimeData, |
|
53 | 53 | VisualizationDragDropContainer *dropContainer); |
|
54 | 54 | |
|
55 | 55 | void addDragDropScrollArea(QScrollArea *scrollArea); |
|
56 | 56 | void removeDragDropScrollArea(QScrollArea *scrollArea); |
|
57 | 57 | |
|
58 | 58 | void addDragDropTabBar(QTabBar *tabBar); |
|
59 | 59 | void removeDragDropTabBar(QTabBar *tabBar); |
|
60 | 60 | |
|
61 | 61 | QUrl imageTemporaryUrl(const QImage &image) const; |
|
62 | 62 | |
|
63 | 63 | void setHightlightedDragWidget(VisualizationDragWidget *dragWidget); |
|
64 | 64 | VisualizationDragWidget *getHightlightedDragWidget() const; |
|
65 | 65 | |
|
66 | /// Delays the closing of a widget during a drag&drop operation | |
|
67 | void delayedCloseWidget(QWidget *widget); | |
|
68 | void doCloseWidgets(); | |
|
69 | ||
|
66 | 70 | private: |
|
67 | 71 | class DragDropHelperPrivate; |
|
68 | 72 | spimpl::unique_impl_ptr<DragDropHelperPrivate> impl; |
|
69 | 73 | }; |
|
70 | 74 | |
|
71 | 75 | #endif // SCIQLOP_DRAGDROPHELPER_H |
@@ -1,45 +1,47 | |||
|
1 | 1 | #include "DataSource/DataSourceTreeWidget.h" |
|
2 | 2 | #include "Common/MimeTypesDef.h" |
|
3 | 3 | #include "DataSource/DataSourceController.h" |
|
4 | 4 | #include "DataSource/DataSourceItem.h" |
|
5 | 5 | #include "DataSource/DataSourceTreeWidgetItem.h" |
|
6 | 6 | |
|
7 | 7 | #include "DragAndDrop/DragDropHelper.h" |
|
8 | 8 | #include "SqpApplication.h" |
|
9 | 9 | |
|
10 | 10 | #include <QMimeData> |
|
11 | 11 | |
|
12 |
DataSourceTreeWidget::DataSourceTreeWidget(QWidget *parent) : QTreeWidget(parent) |
|
|
12 | DataSourceTreeWidget::DataSourceTreeWidget(QWidget *parent) : QTreeWidget(parent) | |
|
13 | { | |
|
14 | } | |
|
13 | 15 | |
|
14 | 16 | QMimeData *DataSourceTreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const |
|
15 | 17 | { |
|
16 | 18 | auto mimeData = new QMimeData; |
|
17 | 19 | |
|
18 | 20 | // Basic check to ensure the item are correctly typed |
|
19 | 21 | Q_ASSERT(items.isEmpty() || dynamic_cast<DataSourceTreeWidgetItem *>(items.first()) != nullptr); |
|
20 | 22 | |
|
21 | 23 | QVariantList productData; |
|
22 | 24 | |
|
23 | 25 | for (auto item : items) { |
|
24 | 26 | auto dataSourceTreeItem = static_cast<DataSourceTreeWidgetItem *>(item); |
|
25 | 27 | auto dataSource = dataSourceTreeItem->data(); |
|
26 | 28 | |
|
27 | 29 | if (dataSource->type() == DataSourceItemType::COMPONENT |
|
28 | 30 | || dataSource->type() == DataSourceItemType::PRODUCT) { |
|
29 | 31 | auto metaData = dataSource->data(); |
|
30 | 32 | productData << metaData; |
|
31 | 33 | } |
|
32 | 34 | } |
|
33 | 35 | |
|
34 | 36 | auto encodedData = sqpApp->dataSourceController().mimeDataForProductsData(productData); |
|
35 | 37 | mimeData->setData(MIME_TYPE_PRODUCT_LIST, encodedData); |
|
36 | 38 | |
|
37 | 39 | return mimeData; |
|
38 | 40 | } |
|
39 | 41 | |
|
40 | 42 | void DataSourceTreeWidget::startDrag(Qt::DropActions supportedActions) |
|
41 | 43 | { |
|
42 | 44 | // Resets the drag&drop operations before it's starting |
|
43 | 45 | sqpApp->dragDropHelper().resetDragAndDrop(); |
|
44 | 46 | QTreeWidget::startDrag(supportedActions); |
|
45 | 47 | } |
@@ -1,275 +1,294 | |||
|
1 | 1 | #include "DragAndDrop/DragDropHelper.h" |
|
2 | 2 | #include "DragAndDrop/DragDropScroller.h" |
|
3 | 3 | #include "DragAndDrop/DragDropTabSwitcher.h" |
|
4 | 4 | #include "SqpApplication.h" |
|
5 | 5 | #include "Visualization/VisualizationDragDropContainer.h" |
|
6 | 6 | #include "Visualization/VisualizationDragWidget.h" |
|
7 | 7 | #include "Visualization/VisualizationWidget.h" |
|
8 | 8 | #include "Visualization/operations/FindVariableOperation.h" |
|
9 | 9 | |
|
10 | 10 | #include "Variable/Variable.h" |
|
11 | 11 | #include "Variable/VariableController.h" |
|
12 | 12 | |
|
13 | 13 | #include "Common/MimeTypesDef.h" |
|
14 | 14 | #include "Common/VisualizationDef.h" |
|
15 | 15 | |
|
16 | 16 | #include <QDir> |
|
17 | 17 | #include <QLabel> |
|
18 | 18 | #include <QUrl> |
|
19 | 19 | #include <QVBoxLayout> |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | Q_LOGGING_CATEGORY(LOG_DragDropHelper, "DragDropHelper") |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | struct DragDropHelper::DragDropHelperPrivate { |
|
26 | 26 | |
|
27 | 27 | VisualizationDragWidget *m_CurrentDragWidget = nullptr; |
|
28 | 28 | std::unique_ptr<QWidget> m_PlaceHolder = nullptr; |
|
29 | 29 | QLabel *m_PlaceHolderLabel; |
|
30 | 30 | QWidget *m_PlaceBackground; |
|
31 | 31 | std::unique_ptr<DragDropScroller> m_DragDropScroller = nullptr; |
|
32 | 32 | std::unique_ptr<DragDropTabSwitcher> m_DragDropTabSwitcher = nullptr; |
|
33 | 33 | QString m_ImageTempUrl; // Temporary file for image url generated by the drag & drop. Not using |
|
34 | 34 | // QTemporaryFile to have a name which is not generated. |
|
35 | 35 | |
|
36 | 36 | VisualizationDragWidget *m_HighlightedDragWidget = nullptr; |
|
37 | 37 | |
|
38 | 38 | QMetaObject::Connection m_DragWidgetDestroyedConnection; |
|
39 | 39 | QMetaObject::Connection m_HighlightedWidgetDestroyedConnection; |
|
40 | 40 | |
|
41 | QList<QWidget *> m_WidgetToClose; | |
|
42 | ||
|
41 | 43 | explicit DragDropHelperPrivate() |
|
42 | 44 | : m_PlaceHolder{std::make_unique<QWidget>()}, |
|
43 | 45 | m_DragDropScroller{std::make_unique<DragDropScroller>()}, |
|
44 | 46 | m_DragDropTabSwitcher{std::make_unique<DragDropTabSwitcher>()} |
|
45 | 47 | { |
|
46 | 48 | |
|
47 | 49 | auto layout = new QVBoxLayout{m_PlaceHolder.get()}; |
|
48 | 50 | layout->setSpacing(0); |
|
49 | 51 | layout->setContentsMargins(0, 0, 0, 0); |
|
50 | 52 | |
|
51 | 53 | m_PlaceHolderLabel = new QLabel{"", m_PlaceHolder.get()}; |
|
52 | 54 | m_PlaceHolderLabel->setMinimumHeight(25); |
|
53 | 55 | layout->addWidget(m_PlaceHolderLabel); |
|
54 | 56 | |
|
55 | 57 | m_PlaceBackground = new QWidget{m_PlaceHolder.get()}; |
|
56 | 58 | m_PlaceBackground->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
57 | 59 | layout->addWidget(m_PlaceBackground); |
|
58 | 60 | |
|
59 | 61 | sqpApp->installEventFilter(m_DragDropScroller.get()); |
|
60 | 62 | sqpApp->installEventFilter(m_DragDropTabSwitcher.get()); |
|
61 | 63 | |
|
62 | 64 | m_ImageTempUrl = QDir::temp().absoluteFilePath("Sciqlop_graph.png"); |
|
63 | 65 | } |
|
64 | 66 | |
|
65 | 67 | void preparePlaceHolder(DragDropHelper::PlaceHolderType type, const QString &topLabelText) const |
|
66 | 68 | { |
|
67 | 69 | if (m_CurrentDragWidget) { |
|
68 | 70 | m_PlaceHolder->setMinimumSize(m_CurrentDragWidget->size()); |
|
69 | 71 | m_PlaceHolder->setSizePolicy(m_CurrentDragWidget->sizePolicy()); |
|
70 | 72 | } |
|
71 | 73 | else { |
|
72 | 74 | // Configuration of the placeHolder when there is no dragWidget |
|
73 | 75 | // (for instance with a drag from a variable) |
|
74 | 76 | |
|
75 | 77 | m_PlaceHolder->setMinimumSize(0, GRAPH_MINIMUM_HEIGHT); |
|
76 | 78 | m_PlaceHolder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
77 | 79 | } |
|
78 | 80 | |
|
79 | 81 | switch (type) { |
|
80 | 82 | case DragDropHelper::PlaceHolderType::Graph: |
|
81 | 83 | m_PlaceBackground->setStyleSheet( |
|
82 | 84 | "background-color: #BBD5EE; border: 1px solid #2A7FD4"); |
|
83 | 85 | break; |
|
84 | 86 | case DragDropHelper::PlaceHolderType::Zone: |
|
85 | 87 | case DragDropHelper::PlaceHolderType::Default: |
|
86 | 88 | m_PlaceBackground->setStyleSheet( |
|
87 | 89 | "background-color: #BBD5EE; border: 2px solid #2A7FD4"); |
|
88 | 90 | m_PlaceHolderLabel->setStyleSheet("color: #2A7FD4"); |
|
89 | 91 | break; |
|
90 | 92 | } |
|
91 | 93 | |
|
92 | 94 | m_PlaceHolderLabel->setText(topLabelText); |
|
93 | 95 | m_PlaceHolderLabel->setVisible(!topLabelText.isEmpty()); |
|
94 | 96 | } |
|
95 | 97 | }; |
|
96 | 98 | |
|
97 | 99 | |
|
98 |
DragDropHelper::DragDropHelper() : impl{spimpl::make_unique_impl<DragDropHelperPrivate>()} |
|
|
100 | DragDropHelper::DragDropHelper() : impl{spimpl::make_unique_impl<DragDropHelperPrivate>()} | |
|
101 | { | |
|
102 | } | |
|
99 | 103 | |
|
100 | 104 | DragDropHelper::~DragDropHelper() |
|
101 | 105 | { |
|
102 | 106 | QFile::remove(impl->m_ImageTempUrl); |
|
103 | 107 | } |
|
104 | 108 | |
|
105 | 109 | void DragDropHelper::resetDragAndDrop() |
|
106 | 110 | { |
|
107 | 111 | setCurrentDragWidget(nullptr); |
|
108 | 112 | impl->m_HighlightedDragWidget = nullptr; |
|
109 | 113 | } |
|
110 | 114 | |
|
111 | 115 | void DragDropHelper::setCurrentDragWidget(VisualizationDragWidget *dragWidget) |
|
112 | 116 | { |
|
113 | 117 | if (impl->m_CurrentDragWidget) { |
|
114 | 118 | |
|
115 | 119 | QObject::disconnect(impl->m_DragWidgetDestroyedConnection); |
|
116 | 120 | } |
|
117 | 121 | |
|
118 | 122 | if (dragWidget) { |
|
119 | 123 | // ensures the impl->m_CurrentDragWidget is reset when the widget is destroyed |
|
120 | 124 | impl->m_DragWidgetDestroyedConnection |
|
121 | 125 | = QObject::connect(dragWidget, &VisualizationDragWidget::destroyed, |
|
122 | 126 | [this]() { impl->m_CurrentDragWidget = nullptr; }); |
|
123 | 127 | } |
|
124 | 128 | |
|
125 | 129 | impl->m_CurrentDragWidget = dragWidget; |
|
126 | 130 | } |
|
127 | 131 | |
|
128 | 132 | VisualizationDragWidget *DragDropHelper::getCurrentDragWidget() const |
|
129 | 133 | { |
|
130 | 134 | return impl->m_CurrentDragWidget; |
|
131 | 135 | } |
|
132 | 136 | |
|
133 | 137 | QWidget &DragDropHelper::placeHolder() const |
|
134 | 138 | { |
|
135 | 139 | return *impl->m_PlaceHolder; |
|
136 | 140 | } |
|
137 | 141 | |
|
138 | 142 | void DragDropHelper::insertPlaceHolder(QVBoxLayout *layout, int index, PlaceHolderType type, |
|
139 | 143 | const QString &topLabelText) |
|
140 | 144 | { |
|
141 | 145 | removePlaceHolder(); |
|
142 | 146 | impl->preparePlaceHolder(type, topLabelText); |
|
143 | 147 | layout->insertWidget(index, impl->m_PlaceHolder.get()); |
|
144 | 148 | impl->m_PlaceHolder->show(); |
|
145 | 149 | } |
|
146 | 150 | |
|
147 | 151 | void DragDropHelper::removePlaceHolder() |
|
148 | 152 | { |
|
149 | 153 | auto parentWidget = impl->m_PlaceHolder->parentWidget(); |
|
150 | 154 | if (parentWidget) { |
|
151 | 155 | parentWidget->layout()->removeWidget(impl->m_PlaceHolder.get()); |
|
152 | 156 | impl->m_PlaceHolder->setParent(nullptr); |
|
153 | 157 | impl->m_PlaceHolder->hide(); |
|
154 | 158 | } |
|
155 | 159 | } |
|
156 | 160 | |
|
157 | 161 | bool DragDropHelper::isPlaceHolderSet() const |
|
158 | 162 | { |
|
159 | 163 | return impl->m_PlaceHolder->parentWidget(); |
|
160 | 164 | } |
|
161 | 165 | |
|
162 | 166 | void DragDropHelper::addDragDropScrollArea(QScrollArea *scrollArea) |
|
163 | 167 | { |
|
164 | 168 | impl->m_DragDropScroller->addScrollArea(scrollArea); |
|
165 | 169 | } |
|
166 | 170 | |
|
167 | 171 | void DragDropHelper::removeDragDropScrollArea(QScrollArea *scrollArea) |
|
168 | 172 | { |
|
169 | 173 | impl->m_DragDropScroller->removeScrollArea(scrollArea); |
|
170 | 174 | } |
|
171 | 175 | |
|
172 | 176 | void DragDropHelper::addDragDropTabBar(QTabBar *tabBar) |
|
173 | 177 | { |
|
174 | 178 | impl->m_DragDropTabSwitcher->addTabBar(tabBar); |
|
175 | 179 | } |
|
176 | 180 | |
|
177 | 181 | void DragDropHelper::removeDragDropTabBar(QTabBar *tabBar) |
|
178 | 182 | { |
|
179 | 183 | impl->m_DragDropTabSwitcher->removeTabBar(tabBar); |
|
180 | 184 | } |
|
181 | 185 | |
|
182 | 186 | QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const |
|
183 | 187 | { |
|
184 | 188 | image.save(impl->m_ImageTempUrl); |
|
185 | 189 | return QUrl::fromLocalFile(impl->m_ImageTempUrl); |
|
186 | 190 | } |
|
187 | 191 | |
|
188 | 192 | void DragDropHelper::setHightlightedDragWidget(VisualizationDragWidget *dragWidget) |
|
189 | 193 | { |
|
190 | 194 | if (impl->m_HighlightedDragWidget) { |
|
191 | 195 | impl->m_HighlightedDragWidget->highlightForMerge(false); |
|
192 | 196 | QObject::disconnect(impl->m_HighlightedWidgetDestroyedConnection); |
|
193 | 197 | } |
|
194 | 198 | |
|
195 | 199 | if (dragWidget) { |
|
196 | 200 | dragWidget->highlightForMerge(true); |
|
197 | 201 | |
|
198 | 202 | // ensures the impl->m_HighlightedDragWidget is reset when the widget is destroyed |
|
199 | 203 | impl->m_DragWidgetDestroyedConnection |
|
200 | 204 | = QObject::connect(dragWidget, &VisualizationDragWidget::destroyed, |
|
201 | 205 | [this]() { impl->m_HighlightedDragWidget = nullptr; }); |
|
202 | 206 | } |
|
203 | 207 | |
|
204 | 208 | impl->m_HighlightedDragWidget = dragWidget; |
|
205 | 209 | } |
|
206 | 210 | |
|
207 | 211 | VisualizationDragWidget *DragDropHelper::getHightlightedDragWidget() const |
|
208 | 212 | { |
|
209 | 213 | return impl->m_HighlightedDragWidget; |
|
210 | 214 | } |
|
211 | 215 | |
|
216 | void DragDropHelper::delayedCloseWidget(QWidget *widget) | |
|
217 | { | |
|
218 | widget->hide(); | |
|
219 | impl->m_WidgetToClose << widget; | |
|
220 | } | |
|
221 | ||
|
222 | void DragDropHelper::doCloseWidgets() | |
|
223 | { | |
|
224 | for (auto widget : impl->m_WidgetToClose) { | |
|
225 | widget->close(); | |
|
226 | } | |
|
227 | ||
|
228 | impl->m_WidgetToClose.clear(); | |
|
229 | } | |
|
230 | ||
|
212 | 231 | bool DragDropHelper::checkMimeDataForVisualization(const QMimeData *mimeData, |
|
213 | 232 | VisualizationDragDropContainer *dropContainer) |
|
214 | 233 | { |
|
215 | 234 | if (!mimeData || !dropContainer) { |
|
216 | 235 | qCWarning(LOG_DragDropHelper()) << QObject::tr( |
|
217 | 236 | "DragDropHelper::checkMimeDataForVisualization, invalid input parameters."); |
|
218 | 237 | Q_ASSERT(false); |
|
219 | 238 | return false; |
|
220 | 239 | } |
|
221 | 240 | |
|
222 | 241 | auto result = false; |
|
223 | 242 | |
|
224 | 243 | if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { |
|
225 | 244 | auto variables = sqpApp->variableController().variablesForMimeData( |
|
226 | 245 | mimeData->data(MIME_TYPE_VARIABLE_LIST)); |
|
227 | 246 | |
|
228 | 247 | if (variables.count() == 1) { |
|
229 | 248 | |
|
230 | 249 | auto variable = variables.first(); |
|
231 | 250 | if (variable->dataSeries() != nullptr) { |
|
232 | 251 | |
|
233 | 252 | // Check that the variable is not already in a graph |
|
234 | 253 | |
|
235 | 254 | auto parent = dropContainer->parentWidget(); |
|
236 | 255 | while (parent && qobject_cast<VisualizationWidget *>(parent) == nullptr) { |
|
237 | 256 | parent = parent->parentWidget(); // Search for the top level VisualizationWidget |
|
238 | 257 | } |
|
239 | 258 | |
|
240 | 259 | if (parent) { |
|
241 | 260 | auto visualizationWidget = static_cast<VisualizationWidget *>(parent); |
|
242 | 261 | |
|
243 | 262 | FindVariableOperation findVariableOperation{variable}; |
|
244 | 263 | visualizationWidget->accept(&findVariableOperation); |
|
245 | 264 | auto variableContainers = findVariableOperation.result(); |
|
246 | 265 | if (variableContainers.empty()) { |
|
247 | 266 | result = true; |
|
248 | 267 | } |
|
249 | 268 | else { |
|
250 | 269 | // result = false: the variable already exist in the visualisation |
|
251 | 270 | } |
|
252 | 271 | } |
|
253 | 272 | else { |
|
254 | 273 | qCWarning(LOG_DragDropHelper()) << QObject::tr( |
|
255 | 274 | "DragDropHelper::checkMimeDataForVisualization, the parent " |
|
256 | 275 | "VisualizationWidget cannot be found. Cannot check if the variable is " |
|
257 | 276 | "already used or not."); |
|
258 | 277 | } |
|
259 | 278 | } |
|
260 | 279 | else { |
|
261 | 280 | // result = false: the variable is not fully loaded |
|
262 | 281 | } |
|
263 | 282 | } |
|
264 | 283 | else { |
|
265 | 284 | // result = false: cannot drop multiple variables in the visualisation |
|
266 | 285 | } |
|
267 | 286 | } |
|
268 | 287 | else { |
|
269 | 288 | // Other MIME data |
|
270 | 289 | // no special rules, accepted by default |
|
271 | 290 | result = true; |
|
272 | 291 | } |
|
273 | 292 | |
|
274 | 293 | return result; |
|
275 | 294 | } |
@@ -1,13 +1,15 | |||
|
1 | 1 | #include "Variable/VariableInspectorTableView.h" |
|
2 | 2 | |
|
3 | 3 | #include "DragAndDrop/DragDropHelper.h" |
|
4 | 4 | #include "SqpApplication.h" |
|
5 | 5 | |
|
6 |
VariableInspectorTableView::VariableInspectorTableView(QWidget *parent) : QTableView(parent) |
|
|
6 | VariableInspectorTableView::VariableInspectorTableView(QWidget *parent) : QTableView(parent) | |
|
7 | { | |
|
8 | } | |
|
7 | 9 | |
|
8 | 10 | void VariableInspectorTableView::startDrag(Qt::DropActions supportedActions) |
|
9 | 11 | { |
|
10 | 12 | // Resets the drag&drop operations before it's starting |
|
11 | 13 | sqpApp->dragDropHelper().resetDragAndDrop(); |
|
12 | 14 | QTableView::startDrag(supportedActions); |
|
13 | 15 | } |
@@ -1,452 +1,453 | |||
|
1 | 1 | #include "Visualization/VisualizationDragDropContainer.h" |
|
2 | 2 | #include "DragAndDrop/DragDropHelper.h" |
|
3 | 3 | #include "SqpApplication.h" |
|
4 | 4 | #include "Visualization/VisualizationDragWidget.h" |
|
5 | 5 | |
|
6 | 6 | #include "Common/VisualizationDef.h" |
|
7 | 7 | |
|
8 | 8 | #include <QDrag> |
|
9 | 9 | #include <QDragEnterEvent> |
|
10 | 10 | #include <QVBoxLayout> |
|
11 | 11 | |
|
12 | 12 | #include <cmath> |
|
13 | 13 | #include <memory> |
|
14 | 14 | |
|
15 | 15 | Q_LOGGING_CATEGORY(LOG_VisualizationDragDropContainer, "VisualizationDragDropContainer") |
|
16 | 16 | |
|
17 | 17 | struct VisualizationDragDropContainer::VisualizationDragDropContainerPrivate { |
|
18 | 18 | |
|
19 | 19 | QVBoxLayout *m_Layout; |
|
20 | 20 | QHash<QString, VisualizationDragDropContainer::DropBehavior> m_AcceptedMimeTypes; |
|
21 | 21 | QString m_PlaceHolderText; |
|
22 | 22 | DragDropHelper::PlaceHolderType m_PlaceHolderType = DragDropHelper::PlaceHolderType::Graph; |
|
23 | 23 | |
|
24 | 24 | VisualizationDragDropContainer::AcceptMimeDataFunction m_AcceptMimeDataFun |
|
25 | 25 | = [](auto mimeData) { return true; }; |
|
26 | 26 | |
|
27 | 27 | int m_MinContainerHeight = 0; |
|
28 | 28 | |
|
29 | 29 | explicit VisualizationDragDropContainerPrivate(QWidget *widget) |
|
30 | 30 | { |
|
31 | 31 | m_Layout = new QVBoxLayout(widget); |
|
32 | 32 | m_Layout->setContentsMargins(0, 0, 0, 0); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | bool acceptMimeData(const QMimeData *data) const |
|
36 | 36 | { |
|
37 | 37 | for (const auto &type : m_AcceptedMimeTypes.keys()) { |
|
38 | 38 | if (data->hasFormat(type) && m_AcceptMimeDataFun(data)) { |
|
39 | 39 | return true; |
|
40 | 40 | } |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | return false; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | bool allowMergeForMimeData(const QMimeData *data) const |
|
47 | 47 | { |
|
48 | 48 | bool result = false; |
|
49 | 49 | for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd(); |
|
50 | 50 | ++it) { |
|
51 | 51 | |
|
52 | 52 | if (data->hasFormat(it.key()) |
|
53 | 53 | && (it.value() == VisualizationDragDropContainer::DropBehavior::Merged |
|
54 | 54 | || it.value() |
|
55 | 55 | == VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) { |
|
56 | 56 | result = true; |
|
57 | 57 | } |
|
58 | 58 | else if (data->hasFormat(it.key()) |
|
59 | 59 | && it.value() == VisualizationDragDropContainer::DropBehavior::Inserted) { |
|
60 | 60 | // Merge is forbidden if the mime data contain an acceptable type which cannot be |
|
61 | 61 | // merged |
|
62 | 62 | result = false; |
|
63 | 63 | break; |
|
64 | 64 | } |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | return result; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | bool allowInsertForMimeData(const QMimeData *data) const |
|
71 | 71 | { |
|
72 | 72 | for (auto it = m_AcceptedMimeTypes.constBegin(); it != m_AcceptedMimeTypes.constEnd(); |
|
73 | 73 | ++it) { |
|
74 | 74 | if (data->hasFormat(it.key()) |
|
75 | 75 | && (it.value() == VisualizationDragDropContainer::DropBehavior::Inserted |
|
76 | 76 | || it.value() |
|
77 | 77 | == VisualizationDragDropContainer::DropBehavior::InsertedAndMerged)) { |
|
78 | 78 | return true; |
|
79 | 79 | } |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | return false; |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | bool hasPlaceHolder() const |
|
86 | 86 | { |
|
87 | 87 | return sqpApp->dragDropHelper().placeHolder().parentWidget() == m_Layout->parentWidget(); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | VisualizationDragWidget *getChildDragWidgetAt(const QWidget *parent, const QPoint &pos) const |
|
91 | 91 | { |
|
92 | 92 | VisualizationDragWidget *dragWidget = nullptr; |
|
93 | 93 | |
|
94 | 94 | for (auto child : parent->children()) { |
|
95 | 95 | auto widget = qobject_cast<VisualizationDragWidget *>(child); |
|
96 | 96 | if (widget && widget->isVisible()) { |
|
97 | 97 | if (widget->frameGeometry().contains(pos)) { |
|
98 | 98 | dragWidget = widget; |
|
99 | 99 | break; |
|
100 | 100 | } |
|
101 | 101 | } |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | return dragWidget; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | bool cursorIsInContainer(QWidget *container) const |
|
108 | 108 | { |
|
109 | 109 | return container->isAncestorOf(sqpApp->widgetAt(QCursor::pos())); |
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | int countDragWidget(const QWidget *parent, bool onlyVisible = false) const |
|
113 | 113 | { |
|
114 | 114 | auto nbGraph = 0; |
|
115 | 115 | for (auto child : parent->children()) { |
|
116 | 116 | if (qobject_cast<VisualizationDragWidget *>(child)) { |
|
117 | 117 | if (!onlyVisible || qobject_cast<VisualizationDragWidget *>(child)->isVisible()) { |
|
118 | 118 | nbGraph += 1; |
|
119 | 119 | } |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | return nbGraph; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void findPlaceHolderPosition(const QPoint &pos, bool canInsert, bool canMerge, |
|
127 | 127 | const VisualizationDragDropContainer *container); |
|
128 | 128 | }; |
|
129 | 129 | |
|
130 | 130 | VisualizationDragDropContainer::VisualizationDragDropContainer(QWidget *parent) |
|
131 | 131 | : QFrame{parent}, |
|
132 | 132 | impl{spimpl::make_unique_impl<VisualizationDragDropContainerPrivate>(this)} |
|
133 | 133 | { |
|
134 | 134 | setAcceptDrops(true); |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | void VisualizationDragDropContainer::addDragWidget(VisualizationDragWidget *dragWidget) |
|
138 | 138 | { |
|
139 | 139 | impl->m_Layout->addWidget(dragWidget); |
|
140 | 140 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); |
|
141 | 141 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, |
|
142 | 142 | &VisualizationDragDropContainer::startDrag); |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | void VisualizationDragDropContainer::insertDragWidget(int index, |
|
146 | 146 | VisualizationDragWidget *dragWidget) |
|
147 | 147 | { |
|
148 | 148 | impl->m_Layout->insertWidget(index, dragWidget); |
|
149 | 149 | disconnect(dragWidget, &VisualizationDragWidget::dragDetected, nullptr, nullptr); |
|
150 | 150 | connect(dragWidget, &VisualizationDragWidget::dragDetected, this, |
|
151 | 151 | &VisualizationDragDropContainer::startDrag); |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | void VisualizationDragDropContainer::addAcceptedMimeType( |
|
155 | 155 | const QString &mimeType, VisualizationDragDropContainer::DropBehavior behavior) |
|
156 | 156 | { |
|
157 | 157 | impl->m_AcceptedMimeTypes[mimeType] = behavior; |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | int VisualizationDragDropContainer::countDragWidget() const |
|
161 | 161 | { |
|
162 | 162 | return impl->countDragWidget(this); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | void VisualizationDragDropContainer::setAcceptMimeDataFunction( |
|
166 | 166 | VisualizationDragDropContainer::AcceptMimeDataFunction fun) |
|
167 | 167 | { |
|
168 | 168 | impl->m_AcceptMimeDataFun = fun; |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void VisualizationDragDropContainer::setPlaceHolderType(DragDropHelper::PlaceHolderType type, |
|
172 | 172 | const QString &placeHolderText) |
|
173 | 173 | { |
|
174 | 174 | impl->m_PlaceHolderType = type; |
|
175 | 175 | impl->m_PlaceHolderText = placeHolderText; |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | void VisualizationDragDropContainer::startDrag(VisualizationDragWidget *dragWidget, |
|
179 | 179 | const QPoint &dragPosition) |
|
180 | 180 | { |
|
181 | 181 | auto &helper = sqpApp->dragDropHelper(); |
|
182 | 182 | helper.resetDragAndDrop(); |
|
183 | 183 | |
|
184 | 184 | // Note: The management of the drag object is done by Qt |
|
185 | 185 | auto drag = new QDrag{dragWidget}; |
|
186 | 186 | drag->setHotSpot(dragPosition); |
|
187 | 187 | |
|
188 | 188 | auto mimeData = dragWidget->mimeData(); |
|
189 | 189 | drag->setMimeData(mimeData); |
|
190 | 190 | |
|
191 | 191 | auto pixmap = QPixmap(dragWidget->size()); |
|
192 | 192 | dragWidget->render(&pixmap); |
|
193 | 193 | drag->setPixmap(pixmap); |
|
194 | 194 | |
|
195 | 195 | auto image = pixmap.toImage(); |
|
196 | 196 | mimeData->setImageData(image); |
|
197 | 197 | mimeData->setUrls({helper.imageTemporaryUrl(image)}); |
|
198 | 198 | |
|
199 | 199 | if (impl->m_Layout->indexOf(dragWidget) >= 0) { |
|
200 | 200 | helper.setCurrentDragWidget(dragWidget); |
|
201 | 201 | |
|
202 | 202 | if (impl->cursorIsInContainer(this)) { |
|
203 | 203 | auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); |
|
204 | 204 | helper.insertPlaceHolder(impl->m_Layout, dragWidgetIndex, impl->m_PlaceHolderType, |
|
205 | 205 | impl->m_PlaceHolderText); |
|
206 | 206 | dragWidget->setVisible(false); |
|
207 | 207 | } |
|
208 | 208 | else { |
|
209 | 209 | // The drag starts directly outside the drop zone |
|
210 | 210 | // do not add the placeHolder |
|
211 | 211 | } |
|
212 | 212 | |
|
213 | // Note: The exec() is blocking on windows but not on linux and macOS | |
|
214 | drag->exec(Qt::MoveAction | Qt::CopyAction); | |
|
213 | drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); | |
|
214 | ||
|
215 | helper.doCloseWidgets(); | |
|
215 | 216 | } |
|
216 | 217 | else { |
|
217 | 218 | qCWarning(LOG_VisualizationDragDropContainer()) |
|
218 | 219 | << tr("VisualizationDragDropContainer::startDrag, drag aborted, the specified " |
|
219 | 220 | "VisualizationDragWidget is not found in this container."); |
|
220 | 221 | } |
|
221 | 222 | } |
|
222 | 223 | |
|
223 | 224 | void VisualizationDragDropContainer::dragEnterEvent(QDragEnterEvent *event) |
|
224 | 225 | { |
|
225 | 226 | if (impl->acceptMimeData(event->mimeData())) { |
|
226 | 227 | event->acceptProposedAction(); |
|
227 | 228 | |
|
228 | 229 | auto &helper = sqpApp->dragDropHelper(); |
|
229 | 230 | |
|
230 | 231 | if (!impl->hasPlaceHolder()) { |
|
231 | 232 | auto dragWidget = helper.getCurrentDragWidget(); |
|
232 | 233 | |
|
233 | 234 | if (dragWidget) { |
|
234 | 235 | // If the drag&drop is internal to the visualization, entering the container hide |
|
235 | 236 | // the dragWidget which was made visible by the dragLeaveEvent |
|
236 | 237 | auto parentWidget |
|
237 | 238 | = qobject_cast<VisualizationDragDropContainer *>(dragWidget->parentWidget()); |
|
238 | 239 | if (parentWidget) { |
|
239 | 240 | dragWidget->setVisible(false); |
|
240 | 241 | } |
|
241 | 242 | } |
|
242 | 243 | |
|
243 | 244 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); |
|
244 | 245 | auto canInsert = impl->allowInsertForMimeData(event->mimeData()); |
|
245 | 246 | impl->findPlaceHolderPosition(event->pos(), canInsert, canMerge, this); |
|
246 | 247 | } |
|
247 | 248 | else { |
|
248 | 249 | // do nothing |
|
249 | 250 | } |
|
250 | 251 | } |
|
251 | 252 | else { |
|
252 | 253 | event->ignore(); |
|
253 | 254 | } |
|
254 | 255 | |
|
255 | 256 | QWidget::dragEnterEvent(event); |
|
256 | 257 | } |
|
257 | 258 | |
|
258 | 259 | void VisualizationDragDropContainer::dragLeaveEvent(QDragLeaveEvent *event) |
|
259 | 260 | { |
|
260 | 261 | Q_UNUSED(event); |
|
261 | 262 | |
|
262 | 263 | auto &helper = sqpApp->dragDropHelper(); |
|
263 | 264 | |
|
264 | 265 | if (!impl->cursorIsInContainer(this)) { |
|
265 | 266 | helper.removePlaceHolder(); |
|
266 | 267 | helper.setHightlightedDragWidget(nullptr); |
|
267 | 268 | impl->m_MinContainerHeight = 0; |
|
268 | 269 | |
|
269 | 270 | auto dragWidget = helper.getCurrentDragWidget(); |
|
270 | 271 | if (dragWidget) { |
|
271 | 272 | // dragWidget has a value only if the drag is started from the visualization |
|
272 | 273 | // In that case, shows the drag widget at its original place |
|
273 | 274 | // So the drag widget doesn't stay hidden if the drop occurs outside the visualization |
|
274 | 275 | // drop zone (It is not possible to catch a drop event outside of the application) |
|
275 | 276 | |
|
276 | 277 | if (dragWidget) { |
|
277 | 278 | dragWidget->setVisible(true); |
|
278 | 279 | } |
|
279 | 280 | } |
|
280 | 281 | } |
|
281 | 282 | else { |
|
282 | 283 | // Leave event probably received for a child widget. |
|
283 | 284 | // Do nothing. |
|
284 | 285 | // Note: The DragLeave event, doesn't have any mean to determine who sent it. |
|
285 | 286 | } |
|
286 | 287 | |
|
287 | 288 | QWidget::dragLeaveEvent(event); |
|
288 | 289 | } |
|
289 | 290 | |
|
290 | 291 | void VisualizationDragDropContainer::dragMoveEvent(QDragMoveEvent *event) |
|
291 | 292 | { |
|
292 | 293 | if (impl->acceptMimeData(event->mimeData())) { |
|
293 | 294 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); |
|
294 | 295 | auto canInsert = impl->allowInsertForMimeData(event->mimeData()); |
|
295 | 296 | impl->findPlaceHolderPosition(event->pos(), canInsert, canMerge, this); |
|
296 | 297 | } |
|
297 | 298 | else { |
|
298 | 299 | event->ignore(); |
|
299 | 300 | } |
|
300 | 301 | |
|
301 | 302 | QWidget::dragMoveEvent(event); |
|
302 | 303 | } |
|
303 | 304 | |
|
304 | 305 | void VisualizationDragDropContainer::dropEvent(QDropEvent *event) |
|
305 | 306 | { |
|
306 | 307 | auto &helper = sqpApp->dragDropHelper(); |
|
307 | 308 | |
|
308 | 309 | if (impl->acceptMimeData(event->mimeData())) { |
|
309 | 310 | auto dragWidget = helper.getCurrentDragWidget(); |
|
310 | 311 | if (impl->hasPlaceHolder()) { |
|
311 | 312 | // drop where the placeHolder is located |
|
312 | 313 | |
|
313 | 314 | auto canInsert = impl->allowInsertForMimeData(event->mimeData()); |
|
314 | 315 | if (canInsert) { |
|
315 | 316 | auto droppedIndex = impl->m_Layout->indexOf(&helper.placeHolder()); |
|
316 | 317 | |
|
317 | 318 | if (dragWidget) { |
|
318 | 319 | auto dragWidgetIndex = impl->m_Layout->indexOf(dragWidget); |
|
319 | 320 | if (dragWidgetIndex >= 0 && dragWidgetIndex < droppedIndex) { |
|
320 | 321 | // Correction of the index if the drop occurs in the same container |
|
321 | 322 | // and if the drag is started from the visualization (in that case, the |
|
322 | 323 | // dragWidget is hidden) |
|
323 | 324 | droppedIndex -= 1; |
|
324 | 325 | } |
|
325 | 326 | |
|
326 | 327 | dragWidget->setVisible(true); |
|
327 | 328 | } |
|
328 | 329 | |
|
329 | 330 | event->acceptProposedAction(); |
|
330 | 331 | |
|
331 | 332 | helper.removePlaceHolder(); |
|
332 | 333 | |
|
333 | 334 | emit dropOccuredInContainer(droppedIndex, event->mimeData()); |
|
334 | 335 | } |
|
335 | 336 | else { |
|
336 | 337 | qCWarning(LOG_VisualizationDragDropContainer()) << tr( |
|
337 | 338 | "VisualizationDragDropContainer::dropEvent, dropping on the placeHolder, but " |
|
338 | 339 | "the insertion is forbidden."); |
|
339 | 340 | Q_ASSERT(false); |
|
340 | 341 | } |
|
341 | 342 | } |
|
342 | 343 | else if (helper.getHightlightedDragWidget()) { |
|
343 | 344 | // drop on the highlighted widget |
|
344 | 345 | |
|
345 | 346 | auto canMerge = impl->allowMergeForMimeData(event->mimeData()); |
|
346 | 347 | if (canMerge) { |
|
347 | 348 | event->acceptProposedAction(); |
|
348 | 349 | emit dropOccuredOnWidget(helper.getHightlightedDragWidget(), event->mimeData()); |
|
349 | 350 | } |
|
350 | 351 | else { |
|
351 | 352 | qCWarning(LOG_VisualizationDragDropContainer()) |
|
352 | 353 | << tr("VisualizationDragDropContainer::dropEvent, dropping on a widget, but " |
|
353 | 354 | "the merge is forbidden."); |
|
354 | 355 | Q_ASSERT(false); |
|
355 | 356 | } |
|
356 | 357 | } |
|
357 | 358 | } |
|
358 | 359 | else { |
|
359 | 360 | event->ignore(); |
|
360 | 361 | } |
|
361 | 362 | |
|
362 | 363 | sqpApp->dragDropHelper().setHightlightedDragWidget(nullptr); |
|
363 | 364 | impl->m_MinContainerHeight = 0; |
|
364 | 365 | |
|
365 | 366 | QWidget::dropEvent(event); |
|
366 | 367 | } |
|
367 | 368 | |
|
368 | 369 | |
|
369 | 370 | void VisualizationDragDropContainer::VisualizationDragDropContainerPrivate::findPlaceHolderPosition( |
|
370 | 371 | const QPoint &pos, bool canInsert, bool canMerge, |
|
371 | 372 | const VisualizationDragDropContainer *container) |
|
372 | 373 | { |
|
373 | 374 | auto &helper = sqpApp->dragDropHelper(); |
|
374 | 375 | |
|
375 | 376 | auto absPos = container->mapToGlobal(pos); |
|
376 | 377 | auto isOnPlaceHolder = sqpApp->widgetAt(absPos) == &(helper.placeHolder()); |
|
377 | 378 | |
|
378 | 379 | if (countDragWidget(container, true) == 0) { |
|
379 | 380 | // Drop on an empty container, just add the placeHolder at the top |
|
380 | 381 | helper.insertPlaceHolder(m_Layout, 0, m_PlaceHolderType, m_PlaceHolderText); |
|
381 | 382 | } |
|
382 | 383 | else if (!isOnPlaceHolder) { |
|
383 | 384 | auto nbDragWidget = countDragWidget(container); |
|
384 | 385 | if (nbDragWidget > 0) { |
|
385 | 386 | |
|
386 | 387 | if (m_MinContainerHeight == 0) { |
|
387 | 388 | m_MinContainerHeight = container->size().height(); |
|
388 | 389 | } |
|
389 | 390 | |
|
390 | 391 | m_MinContainerHeight = qMin(m_MinContainerHeight, container->size().height()); |
|
391 | 392 | auto graphHeight = qMax(m_MinContainerHeight / nbDragWidget, GRAPH_MINIMUM_HEIGHT); |
|
392 | 393 | |
|
393 | 394 | auto posY = pos.y(); |
|
394 | 395 | auto dropIndex = floor(posY / graphHeight); |
|
395 | 396 | auto zoneSize = qMin(graphHeight / 4.0, 75.0); |
|
396 | 397 | |
|
397 | 398 | |
|
398 | 399 | auto isOnTop = posY < dropIndex * graphHeight + zoneSize; |
|
399 | 400 | auto isOnBottom = posY > (dropIndex + 1) * graphHeight - zoneSize; |
|
400 | 401 | |
|
401 | 402 | auto placeHolderIndex = m_Layout->indexOf(&(helper.placeHolder())); |
|
402 | 403 | |
|
403 | 404 | auto dragWidgetHovered = getChildDragWidgetAt(container, pos); |
|
404 | 405 | |
|
405 | 406 | if (canInsert && (isOnTop || isOnBottom || !canMerge)) { |
|
406 | 407 | if (isOnBottom) { |
|
407 | 408 | dropIndex += 1; |
|
408 | 409 | } |
|
409 | 410 | |
|
410 | 411 | if (helper.getCurrentDragWidget()) { |
|
411 | 412 | auto dragWidgetIndex = m_Layout->indexOf(helper.getCurrentDragWidget()); |
|
412 | 413 | if (dragWidgetIndex >= 0 && dragWidgetIndex <= dropIndex) { |
|
413 | 414 | // Correction of the index if the drop occurs in the same container |
|
414 | 415 | // and if the drag is started from the visualization (in that case, the |
|
415 | 416 | // dragWidget is hidden) |
|
416 | 417 | dropIndex += 1; |
|
417 | 418 | } |
|
418 | 419 | } |
|
419 | 420 | |
|
420 | 421 | if (dropIndex != placeHolderIndex) { |
|
421 | 422 | helper.insertPlaceHolder(m_Layout, dropIndex, m_PlaceHolderType, |
|
422 | 423 | m_PlaceHolderText); |
|
423 | 424 | } |
|
424 | 425 | |
|
425 | 426 | helper.setHightlightedDragWidget(nullptr); |
|
426 | 427 | } |
|
427 | 428 | else if (canMerge && dragWidgetHovered) { |
|
428 | 429 | // drop on the middle -> merge |
|
429 | 430 | if (hasPlaceHolder()) { |
|
430 | 431 | helper.removePlaceHolder(); |
|
431 | 432 | } |
|
432 | 433 | |
|
433 | 434 | helper.setHightlightedDragWidget(dragWidgetHovered); |
|
434 | 435 | } |
|
435 | 436 | else { |
|
436 | 437 | qCWarning(LOG_VisualizationDragDropContainer()) |
|
437 | 438 | << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no valid drop " |
|
438 | 439 | "action."); |
|
439 | 440 | } |
|
440 | 441 | } |
|
441 | 442 | else { |
|
442 | 443 | qCWarning(LOG_VisualizationDragDropContainer()) |
|
443 | 444 | << tr("VisualizationDragDropContainer::findPlaceHolderPosition, no widget " |
|
444 | 445 | "found in the " |
|
445 | 446 | "container"); |
|
446 | 447 | } |
|
447 | 448 | } |
|
448 | 449 | else { |
|
449 | 450 | // the mouse is hover the placeHolder |
|
450 | 451 | // Do nothing |
|
451 | 452 | } |
|
452 | 453 | } |
@@ -1,309 +1,309 | |||
|
1 | 1 | #include "Visualization/VisualizationTabWidget.h" |
|
2 | 2 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
3 | 3 | #include "ui_VisualizationTabWidget.h" |
|
4 | 4 | |
|
5 | 5 | #include "Visualization/VisualizationGraphWidget.h" |
|
6 | 6 | #include "Visualization/VisualizationZoneWidget.h" |
|
7 | 7 | |
|
8 | 8 | #include "Variable/VariableController.h" |
|
9 | 9 | |
|
10 | 10 | #include "Common/MimeTypesDef.h" |
|
11 | 11 | |
|
12 | 12 | #include "DragAndDrop/DragDropHelper.h" |
|
13 | 13 | #include "SqpApplication.h" |
|
14 | 14 | |
|
15 | 15 | Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget") |
|
16 | 16 | |
|
17 | 17 | namespace { |
|
18 | 18 | |
|
19 | 19 | /// Generates a default name for a new zone, according to the number of zones already displayed in |
|
20 | 20 | /// the tab |
|
21 | 21 | QString defaultZoneName(const QLayout &layout) |
|
22 | 22 | { |
|
23 | 23 | auto count = 0; |
|
24 | 24 | for (auto i = 0; i < layout.count(); ++i) { |
|
25 | 25 | if (dynamic_cast<VisualizationZoneWidget *>(layout.itemAt(i)->widget())) { |
|
26 | 26 | count++; |
|
27 | 27 | } |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | return QObject::tr("Zone %1").arg(count + 1); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | /** |
|
34 | 34 | * Applies a function to all zones of the tab represented by its layout |
|
35 | 35 | * @param layout the layout that contains zones |
|
36 | 36 | * @param fun the function to apply to each zone |
|
37 | 37 | */ |
|
38 | 38 | template <typename Fun> |
|
39 | 39 | void processZones(QLayout &layout, Fun fun) |
|
40 | 40 | { |
|
41 | 41 | for (auto i = 0; i < layout.count(); ++i) { |
|
42 | 42 | if (auto item = layout.itemAt(i)) { |
|
43 | 43 | if (auto visualizationZoneWidget |
|
44 | 44 | = dynamic_cast<VisualizationZoneWidget *>(item->widget())) { |
|
45 | 45 | fun(*visualizationZoneWidget); |
|
46 | 46 | } |
|
47 | 47 | } |
|
48 | 48 | } |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | } // namespace |
|
52 | 52 | |
|
53 | 53 | struct VisualizationTabWidget::VisualizationTabWidgetPrivate { |
|
54 | 54 | explicit VisualizationTabWidgetPrivate(const QString &name) : m_Name{name} {} |
|
55 | 55 | |
|
56 | 56 | QString m_Name; |
|
57 | 57 | |
|
58 | 58 | void dropGraph(int index, VisualizationTabWidget *tabWidget); |
|
59 | 59 | void dropZone(int index, VisualizationTabWidget *tabWidget); |
|
60 | 60 | void dropVariables(const QList<std::shared_ptr<Variable> > &variables, int index, |
|
61 | 61 | VisualizationTabWidget *tabWidget); |
|
62 | 62 | }; |
|
63 | 63 | |
|
64 | 64 | VisualizationTabWidget::VisualizationTabWidget(const QString &name, QWidget *parent) |
|
65 | 65 | : QWidget{parent}, |
|
66 | 66 | ui{new Ui::VisualizationTabWidget}, |
|
67 | 67 | impl{spimpl::make_unique_impl<VisualizationTabWidgetPrivate>(name)} |
|
68 | 68 | { |
|
69 | 69 | ui->setupUi(this); |
|
70 | 70 | |
|
71 | 71 | ui->dragDropContainer->setPlaceHolderType(DragDropHelper::PlaceHolderType::Zone, "Zone"); |
|
72 | 72 | ui->dragDropContainer->layout()->setContentsMargins(0, 0, 0, 5); |
|
73 | 73 | ui->dragDropContainer->addAcceptedMimeType( |
|
74 | 74 | MIME_TYPE_GRAPH, VisualizationDragDropContainer::DropBehavior::Inserted); |
|
75 | 75 | ui->dragDropContainer->addAcceptedMimeType( |
|
76 | 76 | MIME_TYPE_ZONE, VisualizationDragDropContainer::DropBehavior::Inserted); |
|
77 | 77 | ui->dragDropContainer->addAcceptedMimeType( |
|
78 | 78 | MIME_TYPE_VARIABLE_LIST, VisualizationDragDropContainer::DropBehavior::Inserted); |
|
79 | 79 | |
|
80 | 80 | ui->dragDropContainer->setAcceptMimeDataFunction([this](auto mimeData) { |
|
81 | 81 | return sqpApp->dragDropHelper().checkMimeDataForVisualization(mimeData, |
|
82 | 82 | ui->dragDropContainer); |
|
83 | 83 | }); |
|
84 | 84 | |
|
85 | 85 | connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredInContainer, this, |
|
86 | 86 | &VisualizationTabWidget::dropMimeData); |
|
87 | 87 | |
|
88 | 88 | sqpApp->dragDropHelper().addDragDropScrollArea(ui->scrollArea); |
|
89 | 89 | |
|
90 | 90 | // Widget is deleted when closed |
|
91 | 91 | setAttribute(Qt::WA_DeleteOnClose); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | VisualizationTabWidget::~VisualizationTabWidget() |
|
95 | 95 | { |
|
96 | 96 | sqpApp->dragDropHelper().removeDragDropScrollArea(ui->scrollArea); |
|
97 | 97 | delete ui; |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | void VisualizationTabWidget::addZone(VisualizationZoneWidget *zoneWidget) |
|
101 | 101 | { |
|
102 | 102 | ui->dragDropContainer->addDragWidget(zoneWidget); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | void VisualizationTabWidget::insertZone(int index, VisualizationZoneWidget *zoneWidget) |
|
106 | 106 | { |
|
107 | 107 | ui->dragDropContainer->insertDragWidget(index, zoneWidget); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | VisualizationZoneWidget *VisualizationTabWidget::createZone(std::shared_ptr<Variable> variable) |
|
111 | 111 | { |
|
112 | 112 | return createZone({variable}, -1); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | VisualizationZoneWidget * |
|
116 | 116 | VisualizationTabWidget::createZone(const QList<std::shared_ptr<Variable> > &variables, int index) |
|
117 | 117 | { |
|
118 | 118 | auto zoneWidget = createEmptyZone(index); |
|
119 | 119 | |
|
120 | 120 | // Creates a new graph into the zone |
|
121 | 121 | zoneWidget->createGraph(variables, index); |
|
122 | 122 | |
|
123 | 123 | return zoneWidget; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | VisualizationZoneWidget *VisualizationTabWidget::createEmptyZone(int index) |
|
127 | 127 | { |
|
128 | 128 | auto zoneWidget |
|
129 | 129 | = new VisualizationZoneWidget{defaultZoneName(*ui->dragDropContainer->layout()), this}; |
|
130 | 130 | this->insertZone(index, zoneWidget); |
|
131 | 131 | |
|
132 | 132 | return zoneWidget; |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
136 | 136 | { |
|
137 | 137 | if (visitor) { |
|
138 | 138 | visitor->visitEnter(this); |
|
139 | 139 | |
|
140 | 140 | // Apply visitor to zone children: widgets different from zones are not visited (no action) |
|
141 | 141 | processZones(tabLayout(), [visitor](VisualizationZoneWidget &zoneWidget) { |
|
142 | 142 | zoneWidget.accept(visitor); |
|
143 | 143 | }); |
|
144 | 144 | |
|
145 | 145 | visitor->visitLeave(this); |
|
146 | 146 | } |
|
147 | 147 | else { |
|
148 | 148 | qCCritical(LOG_VisualizationTabWidget()) << tr("Can't visit widget : the visitor is null"); |
|
149 | 149 | } |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | bool VisualizationTabWidget::canDrop(const Variable &variable) const |
|
153 | 153 | { |
|
154 | 154 | // A tab can always accomodate a variable |
|
155 | 155 | Q_UNUSED(variable); |
|
156 | 156 | return true; |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | bool VisualizationTabWidget::contains(const Variable &variable) const |
|
160 | 160 | { |
|
161 | 161 | Q_UNUSED(variable); |
|
162 | 162 | return false; |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | QString VisualizationTabWidget::name() const |
|
166 | 166 | { |
|
167 | 167 | return impl->m_Name; |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | void VisualizationTabWidget::closeEvent(QCloseEvent *event) |
|
171 | 171 | { |
|
172 | 172 | // Closes zones in the tab |
|
173 | 173 | processZones(tabLayout(), [](VisualizationZoneWidget &zoneWidget) { zoneWidget.close(); }); |
|
174 | 174 | |
|
175 | 175 | QWidget::closeEvent(event); |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | QLayout &VisualizationTabWidget::tabLayout() const noexcept |
|
179 | 179 | { |
|
180 | 180 | return *ui->dragDropContainer->layout(); |
|
181 | 181 | } |
|
182 | 182 | |
|
183 | 183 | void VisualizationTabWidget::dropMimeData(int index, const QMimeData *mimeData) |
|
184 | 184 | { |
|
185 | 185 | if (mimeData->hasFormat(MIME_TYPE_GRAPH)) { |
|
186 | 186 | impl->dropGraph(index, this); |
|
187 | 187 | } |
|
188 | 188 | else if (mimeData->hasFormat(MIME_TYPE_ZONE)) { |
|
189 | 189 | impl->dropZone(index, this); |
|
190 | 190 | } |
|
191 | 191 | else if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { |
|
192 | 192 | auto variables = sqpApp->variableController().variablesForMimeData( |
|
193 | 193 | mimeData->data(MIME_TYPE_VARIABLE_LIST)); |
|
194 | 194 | impl->dropVariables(variables, index, this); |
|
195 | 195 | } |
|
196 | 196 | else { |
|
197 | 197 | qCWarning(LOG_VisualizationZoneWidget()) |
|
198 | 198 | << tr("VisualizationTabWidget::dropMimeData, unknown MIME data received."); |
|
199 | 199 | } |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | void VisualizationTabWidget::VisualizationTabWidgetPrivate::dropGraph( |
|
203 | 203 | int index, VisualizationTabWidget *tabWidget) |
|
204 | 204 | { |
|
205 | 205 | auto &helper = sqpApp->dragDropHelper(); |
|
206 | 206 | |
|
207 | 207 | auto graphWidget = qobject_cast<VisualizationGraphWidget *>(helper.getCurrentDragWidget()); |
|
208 | 208 | if (!graphWidget) { |
|
209 | 209 | qCWarning(LOG_VisualizationZoneWidget()) |
|
210 | 210 | << tr("VisualizationTabWidget::dropGraph, drop aborted, the dropped graph is not " |
|
211 | 211 | "found or invalid."); |
|
212 | 212 | Q_ASSERT(false); |
|
213 | 213 | return; |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | auto parentDragDropContainer |
|
217 | 217 | = qobject_cast<VisualizationDragDropContainer *>(graphWidget->parentWidget()); |
|
218 | 218 | if (!parentDragDropContainer) { |
|
219 | 219 | qCWarning(LOG_VisualizationZoneWidget()) |
|
220 | 220 | << tr("VisualizationTabWidget::dropGraph, drop aborted, the parent container of " |
|
221 | 221 | "the dropped graph is not found."); |
|
222 | 222 | Q_ASSERT(false); |
|
223 | 223 | return; |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | auto nbGraph = parentDragDropContainer->countDragWidget(); |
|
227 | 227 | |
|
228 | 228 | const auto &variables = graphWidget->variables(); |
|
229 | 229 | |
|
230 | 230 | if (!variables.isEmpty()) { |
|
231 | 231 | // Abort the requests for the variables (if any) |
|
232 | 232 | // Commented, because it's not sure if it's needed or not |
|
233 | 233 | // for (const auto& var : variables) |
|
234 | 234 | //{ |
|
235 | 235 | // sqpApp->variableController().onAbortProgressRequested(var); |
|
236 | 236 | //} |
|
237 | 237 | |
|
238 | 238 | if (nbGraph == 1) { |
|
239 | 239 | // This is the only graph in the previous zone, close the zone |
|
240 |
graphWidget->parentZoneWidget() |
|
|
240 | helper.delayedCloseWidget(graphWidget->parentZoneWidget()); | |
|
241 | 241 | } |
|
242 | 242 | else { |
|
243 | 243 | // Close the graph |
|
244 | graphWidget->close(); | |
|
244 | helper.delayedCloseWidget(graphWidget); | |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | tabWidget->createZone(variables, index); |
|
248 | 248 | } |
|
249 | 249 | else { |
|
250 | 250 | // The graph is empty, create an empty zone and move the graph inside |
|
251 | 251 | |
|
252 | 252 | auto parentZoneWidget = graphWidget->parentZoneWidget(); |
|
253 | 253 | |
|
254 | 254 | parentDragDropContainer->layout()->removeWidget(graphWidget); |
|
255 | 255 | |
|
256 | 256 | auto zoneWidget = tabWidget->createEmptyZone(index); |
|
257 | 257 | zoneWidget->addGraph(graphWidget); |
|
258 | 258 | |
|
259 | 259 | // Close the old zone if it was the only graph inside |
|
260 | 260 | if (nbGraph == 1) { |
|
261 |
parentZoneWidget |
|
|
261 | helper.delayedCloseWidget(parentZoneWidget); | |
|
262 | 262 | } |
|
263 | 263 | } |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | void VisualizationTabWidget::VisualizationTabWidgetPrivate::dropZone( |
|
267 | 267 | int index, VisualizationTabWidget *tabWidget) |
|
268 | 268 | { |
|
269 | 269 | auto &helper = sqpApp->dragDropHelper(); |
|
270 | 270 | |
|
271 | 271 | auto zoneWidget = qobject_cast<VisualizationZoneWidget *>(helper.getCurrentDragWidget()); |
|
272 | 272 | if (!zoneWidget) { |
|
273 | 273 | qCWarning(LOG_VisualizationZoneWidget()) |
|
274 | 274 | << tr("VisualizationTabWidget::dropZone, drop aborted, the dropped zone is not " |
|
275 | 275 | "found or invalid."); |
|
276 | 276 | Q_ASSERT(false); |
|
277 | 277 | return; |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | auto parentDragDropContainer |
|
281 | 281 | = qobject_cast<VisualizationDragDropContainer *>(zoneWidget->parentWidget()); |
|
282 | 282 | if (!parentDragDropContainer) { |
|
283 | 283 | qCWarning(LOG_VisualizationZoneWidget()) |
|
284 | 284 | << tr("VisualizationTabWidget::dropZone, drop aborted, the parent container of " |
|
285 | 285 | "the dropped zone is not found."); |
|
286 | 286 | Q_ASSERT(false); |
|
287 | 287 | return; |
|
288 | 288 | } |
|
289 | 289 | |
|
290 | 290 | // Simple move of the zone, no variable operation associated |
|
291 | 291 | parentDragDropContainer->layout()->removeWidget(zoneWidget); |
|
292 | 292 | tabWidget->ui->dragDropContainer->insertDragWidget(index, zoneWidget); |
|
293 | 293 | } |
|
294 | 294 | |
|
295 | 295 | void VisualizationTabWidget::VisualizationTabWidgetPrivate::dropVariables( |
|
296 | 296 | const QList<std::shared_ptr<Variable> > &variables, int index, |
|
297 | 297 | VisualizationTabWidget *tabWidget) |
|
298 | 298 | { |
|
299 | 299 | // Note: the AcceptMimeDataFunction (set on the drop container) ensure there is a single and |
|
300 | 300 | // compatible variable here |
|
301 | 301 | if (variables.count() > 1) { |
|
302 | 302 | qCWarning(LOG_VisualizationZoneWidget()) |
|
303 | 303 | << tr("VisualizationTabWidget::dropVariables, dropping multiple variables, operation " |
|
304 | 304 | "aborted."); |
|
305 | 305 | return; |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | tabWidget->createZone(variables, index); |
|
309 | 309 | } |
@@ -1,500 +1,500 | |||
|
1 | 1 | #include "Visualization/VisualizationZoneWidget.h" |
|
2 | 2 | |
|
3 | 3 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
4 | 4 | #include "Visualization/QCustomPlotSynchronizer.h" |
|
5 | 5 | #include "Visualization/VisualizationGraphWidget.h" |
|
6 | 6 | #include "Visualization/VisualizationWidget.h" |
|
7 | 7 | #include "ui_VisualizationZoneWidget.h" |
|
8 | 8 | |
|
9 | 9 | #include "Common/MimeTypesDef.h" |
|
10 | 10 | #include "Common/VisualizationDef.h" |
|
11 | 11 | |
|
12 | 12 | #include <Data/SqpRange.h> |
|
13 | 13 | #include <Time/TimeController.h> |
|
14 | 14 | #include <Variable/Variable.h> |
|
15 | 15 | #include <Variable/VariableController.h> |
|
16 | 16 | |
|
17 | 17 | #include <Visualization/operations/FindVariableOperation.h> |
|
18 | 18 | |
|
19 | 19 | #include <DragAndDrop/DragDropHelper.h> |
|
20 | 20 | #include <QUuid> |
|
21 | 21 | #include <SqpApplication.h> |
|
22 | 22 | #include <cmath> |
|
23 | 23 | |
|
24 | 24 | #include <QLayout> |
|
25 | 25 | |
|
26 | 26 | Q_LOGGING_CATEGORY(LOG_VisualizationZoneWidget, "VisualizationZoneWidget") |
|
27 | 27 | |
|
28 | 28 | namespace { |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | /// Generates a default name for a new graph, according to the number of graphs already displayed in |
|
32 | 32 | /// the zone |
|
33 | 33 | QString defaultGraphName(const QLayout &layout) |
|
34 | 34 | { |
|
35 | 35 | auto count = 0; |
|
36 | 36 | for (auto i = 0; i < layout.count(); ++i) { |
|
37 | 37 | if (dynamic_cast<VisualizationGraphWidget *>(layout.itemAt(i)->widget())) { |
|
38 | 38 | count++; |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | return QObject::tr("Graph %1").arg(count + 1); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | /** |
|
46 | 46 | * Applies a function to all graphs of the zone represented by its layout |
|
47 | 47 | * @param layout the layout that contains graphs |
|
48 | 48 | * @param fun the function to apply to each graph |
|
49 | 49 | */ |
|
50 | 50 | template <typename Fun> |
|
51 | 51 | void processGraphs(QLayout &layout, Fun fun) |
|
52 | 52 | { |
|
53 | 53 | for (auto i = 0; i < layout.count(); ++i) { |
|
54 | 54 | if (auto item = layout.itemAt(i)) { |
|
55 | 55 | if (auto visualizationGraphWidget |
|
56 | 56 | = dynamic_cast<VisualizationGraphWidget *>(item->widget())) { |
|
57 | 57 | fun(*visualizationGraphWidget); |
|
58 | 58 | } |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | } // namespace |
|
64 | 64 | |
|
65 | 65 | struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate { |
|
66 | 66 | |
|
67 | 67 | explicit VisualizationZoneWidgetPrivate() |
|
68 | 68 | : m_SynchronisationGroupId{QUuid::createUuid()}, |
|
69 | 69 | m_Synchronizer{std::make_unique<QCustomPlotSynchronizer>()} |
|
70 | 70 | { |
|
71 | 71 | } |
|
72 | 72 | QUuid m_SynchronisationGroupId; |
|
73 | 73 | std::unique_ptr<IGraphSynchronizer> m_Synchronizer; |
|
74 | 74 | |
|
75 | 75 | // Returns the first graph in the zone or nullptr if there is no graph inside |
|
76 | 76 | VisualizationGraphWidget *firstGraph(const VisualizationZoneWidget *zoneWidget) const |
|
77 | 77 | { |
|
78 | 78 | VisualizationGraphWidget *firstGraph = nullptr; |
|
79 | 79 | auto layout = zoneWidget->ui->dragDropContainer->layout(); |
|
80 | 80 | if (layout->count() > 0) { |
|
81 | 81 | if (auto visualizationGraphWidget |
|
82 | 82 | = qobject_cast<VisualizationGraphWidget *>(layout->itemAt(0)->widget())) { |
|
83 | 83 | firstGraph = visualizationGraphWidget; |
|
84 | 84 | } |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | return firstGraph; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void dropGraph(int index, VisualizationZoneWidget *zoneWidget); |
|
91 | 91 | void dropVariables(const QList<std::shared_ptr<Variable> > &variables, int index, |
|
92 | 92 | VisualizationZoneWidget *zoneWidget); |
|
93 | 93 | }; |
|
94 | 94 | |
|
95 | 95 | VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *parent) |
|
96 | 96 | : VisualizationDragWidget{parent}, |
|
97 | 97 | ui{new Ui::VisualizationZoneWidget}, |
|
98 | 98 | impl{spimpl::make_unique_impl<VisualizationZoneWidgetPrivate>()} |
|
99 | 99 | { |
|
100 | 100 | ui->setupUi(this); |
|
101 | 101 | |
|
102 | 102 | ui->zoneNameLabel->setText(name); |
|
103 | 103 | |
|
104 | 104 | ui->dragDropContainer->setPlaceHolderType(DragDropHelper::PlaceHolderType::Graph); |
|
105 | 105 | ui->dragDropContainer->addAcceptedMimeType( |
|
106 | 106 | MIME_TYPE_GRAPH, VisualizationDragDropContainer::DropBehavior::Inserted); |
|
107 | 107 | ui->dragDropContainer->addAcceptedMimeType( |
|
108 | 108 | MIME_TYPE_VARIABLE_LIST, VisualizationDragDropContainer::DropBehavior::InsertedAndMerged); |
|
109 | 109 | ui->dragDropContainer->addAcceptedMimeType( |
|
110 | 110 | MIME_TYPE_TIME_RANGE, VisualizationDragDropContainer::DropBehavior::Merged); |
|
111 | 111 | ui->dragDropContainer->setAcceptMimeDataFunction([this](auto mimeData) { |
|
112 | 112 | return sqpApp->dragDropHelper().checkMimeDataForVisualization(mimeData, |
|
113 | 113 | ui->dragDropContainer); |
|
114 | 114 | }); |
|
115 | 115 | |
|
116 | 116 | connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredInContainer, this, |
|
117 | 117 | &VisualizationZoneWidget::dropMimeData); |
|
118 | 118 | connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccuredOnWidget, this, |
|
119 | 119 | &VisualizationZoneWidget::dropMimeDataOnGraph); |
|
120 | 120 | |
|
121 | 121 | // 'Close' options : widget is deleted when closed |
|
122 | 122 | setAttribute(Qt::WA_DeleteOnClose); |
|
123 | 123 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationZoneWidget::close); |
|
124 | 124 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); |
|
125 | 125 | |
|
126 | 126 | // Synchronisation id |
|
127 | 127 | QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronizationGroupId", |
|
128 | 128 | Qt::QueuedConnection, Q_ARG(QUuid, impl->m_SynchronisationGroupId)); |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | VisualizationZoneWidget::~VisualizationZoneWidget() |
|
132 | 132 | { |
|
133 | 133 | delete ui; |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void VisualizationZoneWidget::addGraph(VisualizationGraphWidget *graphWidget) |
|
137 | 137 | { |
|
138 | 138 | // Synchronize new graph with others in the zone |
|
139 | 139 | impl->m_Synchronizer->addGraph(*graphWidget); |
|
140 | 140 | |
|
141 | 141 | ui->dragDropContainer->addDragWidget(graphWidget); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | void VisualizationZoneWidget::insertGraph(int index, VisualizationGraphWidget *graphWidget) |
|
145 | 145 | { |
|
146 | 146 | // Synchronize new graph with others in the zone |
|
147 | 147 | impl->m_Synchronizer->addGraph(*graphWidget); |
|
148 | 148 | |
|
149 | 149 | ui->dragDropContainer->insertDragWidget(index, graphWidget); |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptr<Variable> variable) |
|
153 | 153 | { |
|
154 | 154 | return createGraph(variable, -1); |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptr<Variable> variable, |
|
158 | 158 | int index) |
|
159 | 159 | { |
|
160 | 160 | auto graphWidget |
|
161 | 161 | = new VisualizationGraphWidget{defaultGraphName(*ui->dragDropContainer->layout()), this}; |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | // Set graph properties |
|
165 | 165 | graphWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); |
|
166 | 166 | graphWidget->setMinimumHeight(GRAPH_MINIMUM_HEIGHT); |
|
167 | 167 | |
|
168 | 168 | |
|
169 | 169 | // Lambda to synchronize zone widget |
|
170 | 170 | auto synchronizeZoneWidget = [this, graphWidget](const SqpRange &graphRange, |
|
171 | 171 | const SqpRange &oldGraphRange) { |
|
172 | 172 | |
|
173 | 173 | auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange); |
|
174 | 174 | auto frameLayout = ui->dragDropContainer->layout(); |
|
175 | 175 | for (auto i = 0; i < frameLayout->count(); ++i) { |
|
176 | 176 | auto graphChild |
|
177 | 177 | = dynamic_cast<VisualizationGraphWidget *>(frameLayout->itemAt(i)->widget()); |
|
178 | 178 | if (graphChild && (graphChild != graphWidget)) { |
|
179 | 179 | |
|
180 | 180 | auto graphChildRange = graphChild->graphRange(); |
|
181 | 181 | switch (zoomType) { |
|
182 | 182 | case AcquisitionZoomType::ZoomIn: { |
|
183 | 183 | auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart; |
|
184 | 184 | auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd; |
|
185 | 185 | graphChildRange.m_TStart += deltaLeft; |
|
186 | 186 | graphChildRange.m_TEnd -= deltaRight; |
|
187 | 187 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: ZoomIn"); |
|
188 | 188 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaLeft") |
|
189 | 189 | << deltaLeft; |
|
190 | 190 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaRight") |
|
191 | 191 | << deltaRight; |
|
192 | 192 | qCDebug(LOG_VisualizationZoneWidget()) |
|
193 | 193 | << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart; |
|
194 | 194 | |
|
195 | 195 | break; |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | case AcquisitionZoomType::ZoomOut: { |
|
199 | 199 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: ZoomOut"); |
|
200 | 200 | auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart; |
|
201 | 201 | auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd; |
|
202 | 202 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaLeft") |
|
203 | 203 | << deltaLeft; |
|
204 | 204 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaRight") |
|
205 | 205 | << deltaRight; |
|
206 | 206 | qCDebug(LOG_VisualizationZoneWidget()) |
|
207 | 207 | << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart; |
|
208 | 208 | graphChildRange.m_TStart -= deltaLeft; |
|
209 | 209 | graphChildRange.m_TEnd += deltaRight; |
|
210 | 210 | break; |
|
211 | 211 | } |
|
212 | 212 | case AcquisitionZoomType::PanRight: { |
|
213 | 213 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: PanRight"); |
|
214 | 214 | auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart; |
|
215 | 215 | auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd; |
|
216 | 216 | graphChildRange.m_TStart += deltaLeft; |
|
217 | 217 | graphChildRange.m_TEnd += deltaRight; |
|
218 | 218 | qCDebug(LOG_VisualizationZoneWidget()) |
|
219 | 219 | << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart; |
|
220 | 220 | break; |
|
221 | 221 | } |
|
222 | 222 | case AcquisitionZoomType::PanLeft: { |
|
223 | 223 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: PanLeft"); |
|
224 | 224 | auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart; |
|
225 | 225 | auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd; |
|
226 | 226 | graphChildRange.m_TStart -= deltaLeft; |
|
227 | 227 | graphChildRange.m_TEnd -= deltaRight; |
|
228 | 228 | break; |
|
229 | 229 | } |
|
230 | 230 | case AcquisitionZoomType::Unknown: { |
|
231 | 231 | qCDebug(LOG_VisualizationZoneWidget()) |
|
232 | 232 | << tr("Impossible to synchronize: zoom type unknown"); |
|
233 | 233 | break; |
|
234 | 234 | } |
|
235 | 235 | default: |
|
236 | 236 | qCCritical(LOG_VisualizationZoneWidget()) |
|
237 | 237 | << tr("Impossible to synchronize: zoom type not take into account"); |
|
238 | 238 | // No action |
|
239 | 239 | break; |
|
240 | 240 | } |
|
241 | 241 | graphChild->enableAcquisition(false); |
|
242 | 242 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: Range before: ") |
|
243 | 243 | << graphChild->graphRange(); |
|
244 | 244 | qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: Range after : ") |
|
245 | 245 | << graphChildRange; |
|
246 | 246 | qCDebug(LOG_VisualizationZoneWidget()) |
|
247 | 247 | << tr("TORM: child dt") << graphChildRange.m_TEnd - graphChildRange.m_TStart; |
|
248 | 248 | graphChild->setGraphRange(graphChildRange); |
|
249 | 249 | graphChild->enableAcquisition(true); |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | }; |
|
253 | 253 | |
|
254 | 254 | // connection for synchronization |
|
255 | 255 | connect(graphWidget, &VisualizationGraphWidget::synchronize, synchronizeZoneWidget); |
|
256 | 256 | connect(graphWidget, &VisualizationGraphWidget::variableAdded, this, |
|
257 | 257 | &VisualizationZoneWidget::onVariableAdded); |
|
258 | 258 | connect(graphWidget, &VisualizationGraphWidget::variableAboutToBeRemoved, this, |
|
259 | 259 | &VisualizationZoneWidget::onVariableAboutToBeRemoved); |
|
260 | 260 | |
|
261 | 261 | auto range = SqpRange{}; |
|
262 | 262 | if (auto firstGraph = impl->firstGraph(this)) { |
|
263 | 263 | // Case of a new graph in a existant zone |
|
264 | 264 | range = firstGraph->graphRange(); |
|
265 | 265 | } |
|
266 | 266 | else { |
|
267 | 267 | // Case of a new graph as the first of the zone |
|
268 | 268 | range = variable->range(); |
|
269 | 269 | } |
|
270 | 270 | |
|
271 | 271 | this->insertGraph(index, graphWidget); |
|
272 | 272 | |
|
273 | 273 | graphWidget->addVariable(variable, range); |
|
274 | 274 | graphWidget->setYRange(variable); |
|
275 | 275 | |
|
276 | 276 | return graphWidget; |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | VisualizationGraphWidget * |
|
280 | 280 | VisualizationZoneWidget::createGraph(const QList<std::shared_ptr<Variable> > variables, int index) |
|
281 | 281 | { |
|
282 | 282 | if (variables.isEmpty()) { |
|
283 | 283 | return nullptr; |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 | auto graphWidget = createGraph(variables.first(), index); |
|
287 | 287 | for (auto variableIt = variables.cbegin() + 1; variableIt != variables.cend(); ++variableIt) { |
|
288 | 288 | graphWidget->addVariable(*variableIt, graphWidget->graphRange()); |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | return graphWidget; |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | void VisualizationZoneWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
295 | 295 | { |
|
296 | 296 | if (visitor) { |
|
297 | 297 | visitor->visitEnter(this); |
|
298 | 298 | |
|
299 | 299 | // Apply visitor to graph children: widgets different from graphs are not visited (no |
|
300 | 300 | // action) |
|
301 | 301 | processGraphs( |
|
302 | 302 | *ui->dragDropContainer->layout(), |
|
303 | 303 | [visitor](VisualizationGraphWidget &graphWidget) { graphWidget.accept(visitor); }); |
|
304 | 304 | |
|
305 | 305 | visitor->visitLeave(this); |
|
306 | 306 | } |
|
307 | 307 | else { |
|
308 | 308 | qCCritical(LOG_VisualizationZoneWidget()) << tr("Can't visit widget : the visitor is null"); |
|
309 | 309 | } |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | bool VisualizationZoneWidget::canDrop(const Variable &variable) const |
|
313 | 313 | { |
|
314 | 314 | // A tab can always accomodate a variable |
|
315 | 315 | Q_UNUSED(variable); |
|
316 | 316 | return true; |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | bool VisualizationZoneWidget::contains(const Variable &variable) const |
|
320 | 320 | { |
|
321 | 321 | Q_UNUSED(variable); |
|
322 | 322 | return false; |
|
323 | 323 | } |
|
324 | 324 | |
|
325 | 325 | QString VisualizationZoneWidget::name() const |
|
326 | 326 | { |
|
327 | 327 | return ui->zoneNameLabel->text(); |
|
328 | 328 | } |
|
329 | 329 | |
|
330 | 330 | QMimeData *VisualizationZoneWidget::mimeData() const |
|
331 | 331 | { |
|
332 | 332 | auto mimeData = new QMimeData; |
|
333 | 333 | mimeData->setData(MIME_TYPE_ZONE, QByteArray{}); |
|
334 | 334 | |
|
335 | 335 | return mimeData; |
|
336 | 336 | } |
|
337 | 337 | |
|
338 | 338 | bool VisualizationZoneWidget::isDragAllowed() const |
|
339 | 339 | { |
|
340 | 340 | return true; |
|
341 | 341 | } |
|
342 | 342 | |
|
343 | 343 | void VisualizationZoneWidget::closeEvent(QCloseEvent *event) |
|
344 | 344 | { |
|
345 | 345 | // Closes graphs in the zone |
|
346 | 346 | processGraphs(*ui->dragDropContainer->layout(), |
|
347 | 347 | [](VisualizationGraphWidget &graphWidget) { graphWidget.close(); }); |
|
348 | 348 | |
|
349 | 349 | // Delete synchronization group from variable controller |
|
350 | 350 | QMetaObject::invokeMethod(&sqpApp->variableController(), "onRemoveSynchronizationGroupId", |
|
351 | 351 | Qt::QueuedConnection, Q_ARG(QUuid, impl->m_SynchronisationGroupId)); |
|
352 | 352 | |
|
353 | 353 | QWidget::closeEvent(event); |
|
354 | 354 | } |
|
355 | 355 | |
|
356 | 356 | void VisualizationZoneWidget::onVariableAdded(std::shared_ptr<Variable> variable) |
|
357 | 357 | { |
|
358 | 358 | QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronized", |
|
359 | 359 | Qt::QueuedConnection, Q_ARG(std::shared_ptr<Variable>, variable), |
|
360 | 360 | Q_ARG(QUuid, impl->m_SynchronisationGroupId)); |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | void VisualizationZoneWidget::onVariableAboutToBeRemoved(std::shared_ptr<Variable> variable) |
|
364 | 364 | { |
|
365 | 365 | QMetaObject::invokeMethod(&sqpApp->variableController(), "desynchronize", Qt::QueuedConnection, |
|
366 | 366 | Q_ARG(std::shared_ptr<Variable>, variable), |
|
367 | 367 | Q_ARG(QUuid, impl->m_SynchronisationGroupId)); |
|
368 | 368 | } |
|
369 | 369 | |
|
370 | 370 | void VisualizationZoneWidget::dropMimeData(int index, const QMimeData *mimeData) |
|
371 | 371 | { |
|
372 | 372 | if (mimeData->hasFormat(MIME_TYPE_GRAPH)) { |
|
373 | 373 | impl->dropGraph(index, this); |
|
374 | 374 | } |
|
375 | 375 | else if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { |
|
376 | 376 | auto variables = sqpApp->variableController().variablesForMimeData( |
|
377 | 377 | mimeData->data(MIME_TYPE_VARIABLE_LIST)); |
|
378 | 378 | impl->dropVariables(variables, index, this); |
|
379 | 379 | } |
|
380 | 380 | else { |
|
381 | 381 | qCWarning(LOG_VisualizationZoneWidget()) |
|
382 | 382 | << tr("VisualizationZoneWidget::dropMimeData, unknown MIME data received."); |
|
383 | 383 | } |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | void VisualizationZoneWidget::dropMimeDataOnGraph(VisualizationDragWidget *dragWidget, |
|
387 | 387 | const QMimeData *mimeData) |
|
388 | 388 | { |
|
389 | 389 | auto graphWidget = qobject_cast<VisualizationGraphWidget *>(dragWidget); |
|
390 | 390 | if (!graphWidget) { |
|
391 | 391 | qCWarning(LOG_VisualizationZoneWidget()) |
|
392 | 392 | << tr("VisualizationZoneWidget::dropMimeDataOnGraph, dropping in an unknown widget, " |
|
393 | 393 | "drop aborted"); |
|
394 | 394 | Q_ASSERT(false); |
|
395 | 395 | return; |
|
396 | 396 | } |
|
397 | 397 | |
|
398 | 398 | if (mimeData->hasFormat(MIME_TYPE_VARIABLE_LIST)) { |
|
399 | 399 | auto variables = sqpApp->variableController().variablesForMimeData( |
|
400 | 400 | mimeData->data(MIME_TYPE_VARIABLE_LIST)); |
|
401 | 401 | for (const auto &var : variables) { |
|
402 | 402 | graphWidget->addVariable(var, graphWidget->graphRange()); |
|
403 | 403 | } |
|
404 | 404 | } |
|
405 | 405 | else if (mimeData->hasFormat(MIME_TYPE_TIME_RANGE)) { |
|
406 | 406 | auto range = TimeController::timeRangeForMimeData(mimeData->data(MIME_TYPE_TIME_RANGE)); |
|
407 | 407 | graphWidget->setGraphRange(range); |
|
408 | 408 | } |
|
409 | 409 | else { |
|
410 | 410 | qCWarning(LOG_VisualizationZoneWidget()) |
|
411 | 411 | << tr("VisualizationZoneWidget::dropMimeDataOnGraph, unknown MIME data received."); |
|
412 | 412 | } |
|
413 | 413 | } |
|
414 | 414 | |
|
415 | 415 | void VisualizationZoneWidget::VisualizationZoneWidgetPrivate::dropGraph( |
|
416 | 416 | int index, VisualizationZoneWidget *zoneWidget) |
|
417 | 417 | { |
|
418 | 418 | auto &helper = sqpApp->dragDropHelper(); |
|
419 | 419 | |
|
420 | 420 | auto graphWidget = qobject_cast<VisualizationGraphWidget *>(helper.getCurrentDragWidget()); |
|
421 | 421 | if (!graphWidget) { |
|
422 | 422 | qCWarning(LOG_VisualizationZoneWidget()) |
|
423 | 423 | << tr("VisualizationZoneWidget::dropGraph, drop aborted, the dropped graph is not " |
|
424 | 424 | "found or invalid."); |
|
425 | 425 | Q_ASSERT(false); |
|
426 | 426 | return; |
|
427 | 427 | } |
|
428 | 428 | |
|
429 | 429 | auto parentDragDropContainer |
|
430 | 430 | = qobject_cast<VisualizationDragDropContainer *>(graphWidget->parentWidget()); |
|
431 | 431 | if (!parentDragDropContainer) { |
|
432 | 432 | qCWarning(LOG_VisualizationZoneWidget()) |
|
433 | 433 | << tr("VisualizationZoneWidget::dropGraph, drop aborted, the parent container of " |
|
434 | 434 | "the dropped graph is not found."); |
|
435 | 435 | Q_ASSERT(false); |
|
436 | 436 | return; |
|
437 | 437 | } |
|
438 | 438 | |
|
439 | 439 | const auto &variables = graphWidget->variables(); |
|
440 | 440 | |
|
441 | 441 | if (parentDragDropContainer != zoneWidget->ui->dragDropContainer && !variables.isEmpty()) { |
|
442 | 442 | // The drop didn't occur in the same zone |
|
443 | 443 | |
|
444 | 444 | // Abort the requests for the variables (if any) |
|
445 | 445 | // Commented, because it's not sure if it's needed or not |
|
446 | 446 | // for (const auto& var : variables) |
|
447 | 447 | //{ |
|
448 | 448 | // sqpApp->variableController().onAbortProgressRequested(var); |
|
449 | 449 | //} |
|
450 | 450 | |
|
451 | 451 | auto previousParentZoneWidget = graphWidget->parentZoneWidget(); |
|
452 | 452 | auto nbGraph = parentDragDropContainer->countDragWidget(); |
|
453 | 453 | if (nbGraph == 1) { |
|
454 | 454 | // This is the only graph in the previous zone, close the zone |
|
455 |
previousParentZoneWidget |
|
|
455 | helper.delayedCloseWidget(previousParentZoneWidget); | |
|
456 | 456 | } |
|
457 | 457 | else { |
|
458 | 458 | // Close the graph |
|
459 | graphWidget->close(); | |
|
459 | helper.delayedCloseWidget(graphWidget); | |
|
460 | 460 | } |
|
461 | 461 | |
|
462 | 462 | // Creates the new graph in the zone |
|
463 | 463 | zoneWidget->createGraph(variables, index); |
|
464 | 464 | } |
|
465 | 465 | else { |
|
466 | 466 | // The drop occurred in the same zone or the graph is empty |
|
467 | 467 | // Simple move of the graph, no variable operation associated |
|
468 | 468 | parentDragDropContainer->layout()->removeWidget(graphWidget); |
|
469 | 469 | |
|
470 | 470 | if (variables.isEmpty() && parentDragDropContainer != zoneWidget->ui->dragDropContainer) { |
|
471 | 471 | // The graph is empty and dropped in a different zone. |
|
472 | 472 | // Take the range of the first graph in the zone (if existing). |
|
473 | 473 | auto layout = zoneWidget->ui->dragDropContainer->layout(); |
|
474 | 474 | if (layout->count() > 0) { |
|
475 | 475 | if (auto visualizationGraphWidget |
|
476 | 476 | = qobject_cast<VisualizationGraphWidget *>(layout->itemAt(0)->widget())) { |
|
477 | 477 | graphWidget->setGraphRange(visualizationGraphWidget->graphRange()); |
|
478 | 478 | } |
|
479 | 479 | } |
|
480 | 480 | } |
|
481 | 481 | |
|
482 | 482 | zoneWidget->ui->dragDropContainer->insertDragWidget(index, graphWidget); |
|
483 | 483 | } |
|
484 | 484 | } |
|
485 | 485 | |
|
486 | 486 | void VisualizationZoneWidget::VisualizationZoneWidgetPrivate::dropVariables( |
|
487 | 487 | const QList<std::shared_ptr<Variable> > &variables, int index, |
|
488 | 488 | VisualizationZoneWidget *zoneWidget) |
|
489 | 489 | { |
|
490 | 490 | // Note: the AcceptMimeDataFunction (set on the drop container) ensure there is a single and |
|
491 | 491 | // compatible variable here |
|
492 | 492 | if (variables.count() > 1) { |
|
493 | 493 | qCWarning(LOG_VisualizationZoneWidget()) |
|
494 | 494 | << tr("VisualizationZoneWidget::dropVariables, dropping multiple variables, operation " |
|
495 | 495 | "aborted."); |
|
496 | 496 | return; |
|
497 | 497 | } |
|
498 | 498 | |
|
499 | 499 | zoneWidget->createGraph(variables, index); |
|
500 | 500 | } |
General Comments 1
You need to be logged in to leave comments.
Login now