##// END OF EJS Templates
Removed bad dependency between VC and VariableModel, moved mime stuff...
jeandet -
r27:c08d1b8ad297
parent child
Show More
@@ -92,7 +92,7 signals:
92 * @param variableProvider the provider that will be used to retrieve the data of the variable
92 * @param variableProvider the provider that will be used to retrieve the data of the variable
93 * (can be null)
93 * (can be null)
94 */
94 */
95 void variableCreationRequested(const QString &variableName,
95 void createVariable(const QString &variableName,
96 const QVariantHash &variableMetadata,
96 const QVariantHash &variableMetadata,
97 std::shared_ptr<IDataProvider> variableProvider);
97 std::shared_ptr<IDataProvider> variableProvider);
98
98
@@ -7,6 +7,7
7 #include <QObject>
7 #include <QObject>
8 #include <QUuid>
8 #include <QUuid>
9 #include <QReadWriteLock>
9 #include <QReadWriteLock>
10 #include <QDataStream>
10
11
11 #include "CoreGlobal.h"
12 #include "CoreGlobal.h"
12 #include <Data/DataSeriesIterator.h>
13 #include <Data/DataSeriesIterator.h>
@@ -69,6 +70,30 public:
69 const DateTimeRange& newRange, const DateTimeRange& newCacheRange,
70 const DateTimeRange& newRange, const DateTimeRange& newCacheRange,
70 bool notify=true);
71 bool notify=true);
71
72
73 static QByteArray mimeData(const std::vector<std::shared_ptr<Variable> > &variables)
74 {
75 auto encodedData = QByteArray{};
76 QDataStream stream{&encodedData, QIODevice::WriteOnly};
77 for (auto &var : variables) {
78 stream << var->ID().toByteArray();
79 }
80 return encodedData;
81 }
82
83 static std::vector<QUuid> variablesIDs(QByteArray mimeData)
84 {
85 std::vector<QUuid> variables;
86 QDataStream stream{mimeData};
87
88 QVariantList ids;
89 stream >> ids;
90
91 for (const auto& id : ids) {
92 variables.emplace_back (id.toByteArray());
93 }
94 return variables;
95 }
96
72 DEPRECATE(
97 DEPRECATE(
73
98
74 bool contains(const DateTimeRange &range) const noexcept;
99 bool contains(const DateTimeRange &range) const noexcept;
@@ -37,9 +37,7 public:
37
37
38 void synchronize(const std::shared_ptr<Variable>& var, const std::shared_ptr<Variable>& with);
38 void synchronize(const std::shared_ptr<Variable>& var, const std::shared_ptr<Variable>& with);
39
39
40 //This should be somewhere else VC has nothing to do with MIMEData
40 const std::vector<std::shared_ptr<Variable>> variables(const std::vector<QUuid>& ids);
41 QByteArray mimeData(const std::vector<std::shared_ptr<Variable>> &variables) const;
42 const std::vector<std::shared_ptr<Variable>> variables(QByteArray mimeData);
43
41
44 const std::shared_ptr<Variable>& operator[] (int index) const;
42 const std::shared_ptr<Variable>& operator[] (int index) const;
45 std::shared_ptr<Variable> operator[] (int index);
43 std::shared_ptr<Variable> operator[] (int index);
@@ -24,9 +24,10 enum VariableRoles { ProgressRole = Qt::UserRole };
24 */
24 */
25 class SCIQLOP_CORE_EXPORT VariableModel2 : public QAbstractTableModel {
25 class SCIQLOP_CORE_EXPORT VariableModel2 : public QAbstractTableModel {
26 Q_OBJECT
26 Q_OBJECT
27 std::shared_ptr<VariableController2> _variableController;
27 // read only mirror of VariableController2 content
28 std::vector<std::shared_ptr<Variable>> _variables;
28 public:
29 public:
29 explicit VariableModel2(const std::shared_ptr<VariableController2>& variableController, QObject *parent = nullptr);
30 explicit VariableModel2(QObject *parent = nullptr);
30
31
31 // /////////////////////////// //
32 // /////////////////////////// //
32 // QAbstractTableModel methods //
33 // QAbstractTableModel methods //
@@ -52,12 +53,14 public:
52 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
53 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
53 const QModelIndex &parent) override;
54 const QModelIndex &parent) override;
54
55
55
56 signals:
56 signals:
57
57 void createVariable(const QVariantHash &productData);
58 private slots:
58 void asyncChangeRange(const std::shared_ptr<Variable>& variable, const DateTimeRange& r);
59 public slots:
59 /// Slot called when data of a variable has been updated
60 /// Slot called when data of a variable has been updated
60 void variableUpdated() noexcept;
61 void variableUpdated() noexcept;
62 void variableAdded(const std::shared_ptr<Variable>&);
63 void variableDeleted(const std::shared_ptr<Variable>&);
61 };
64 };
62
65
63 #endif // SCIQLOP_VARIABLEMODEL2_H
66 #endif // SCIQLOP_VARIABLEMODEL2_H
@@ -123,7 +123,7 void DataSourceController::loadProductItem(const QUuid &dataSourceUid,
123 auto it = impl->m_DataProviders.find(dataSourceUid);
123 auto it = impl->m_DataProviders.find(dataSourceUid);
124 auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr;
124 auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr;
125
125
126 emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider);
126 emit createVariable(productItem.name(), productItem.data(), dataProvider);
127 }
127 }
128 else {
128 else {
129 qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product");
129 qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product");
@@ -32,7 +32,7 class VariableController2::VariableController2Private
32 inline void removeVariable(const std::shared_ptr<Variable>& variable)
32 inline void removeVariable(const std::shared_ptr<Variable>& variable)
33 {
33 {
34 QWriteLocker lock{&_lock};
34 QWriteLocker lock{&_lock};
35 _variables.remove(*variable);
35 _variables.erase(*variable);
36 _providers.remove(*variable);
36 _providers.remove(*variable);
37 _synchronizationGroups.remove(*variable);
37 _synchronizationGroups.remove(*variable);
38 }
38 }
@@ -55,10 +55,11 class VariableController2::VariableController2Private
55 inline std::shared_ptr<Variable> variable(QUuid variable)
55 inline std::shared_ptr<Variable> variable(QUuid variable)
56 {
56 {
57 QReadLocker lock{&_lock};
57 QReadLocker lock{&_lock};
58 auto it = _variables.find(variable);
58 [[unlikely]]
59 [[unlikely]]
59 if(!_variables.contains(variable))
60 if(it==_variables.end())
60 SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Unknown Variable");
61 SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Unknown Variable");
61 return _variables[variable];
62 return (*it).second;
62 }
63 }
63
64
64 inline std::shared_ptr<Variable> variable(int index)
65 inline std::shared_ptr<Variable> variable(int index)
@@ -67,14 +68,19 class VariableController2::VariableController2Private
67 [[unlikely]]
68 [[unlikely]]
68 if(!_variables.size() > index)
69 if(!_variables.size() > index)
69 SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Index is out of bounds");
70 SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Index is out of bounds");
70 return _variables.values()[index];
71 auto it = _variables.cbegin();
72 while (index!=0) {
73 index-=1;
74 it++;
75 }
76 return (*it).second;
71 }
77 }
72
78
73 inline const std::vector<std::shared_ptr<Variable>> variables()
79 inline const std::vector<std::shared_ptr<Variable>> variables()
74 {
80 {
75 std::vector<std::shared_ptr<Variable>> vars;
81 std::vector<std::shared_ptr<Variable>> vars;
76 QReadLocker lock{&_lock};
82 QReadLocker lock{&_lock};
77 for(const auto &var:_variables)
83 for(const auto&[id, var]:_variables)
78 {
84 {
79 vars.push_back(var);
85 vars.push_back(var);
80 }
86 }
@@ -102,11 +108,11 class VariableController2::VariableController2Private
102 inline bool has(const std::shared_ptr<Variable>& variable)
108 inline bool has(const std::shared_ptr<Variable>& variable)
103 {
109 {
104 QReadLocker lock{&_lock};
110 QReadLocker lock{&_lock};
105 return _variables.contains(*variable);
111 return _variables.find(*variable)==_variables.end();
106 }
112 }
107
113
108 private:
114 private:
109 QMap<QUuid,std::shared_ptr<Variable>> _variables;
115 std::map<QUuid,std::shared_ptr<Variable>> _variables;
110 QMap<QUuid,std::shared_ptr<IDataProvider>> _providers;
116 QMap<QUuid,std::shared_ptr<IDataProvider>> _providers;
111 QMap<QUuid,std::shared_ptr<VariableSynchronizationGroup2>> _synchronizationGroups;
117 QMap<QUuid,std::shared_ptr<VariableSynchronizationGroup2>> _synchronizationGroups;
112 QReadWriteLock _lock{QReadWriteLock::Recursive};
118 QReadWriteLock _lock{QReadWriteLock::Recursive};
@@ -366,29 +372,12 void VariableController2::synchronize(const std::shared_ptr<Variable> &var, cons
366 impl->synchronize(var, with);
372 impl->synchronize(var, with);
367 }
373 }
368
374
369 QByteArray VariableController2::mimeData(const std::vector<std::shared_ptr<Variable> > &variables) const
375 const std::vector<std::shared_ptr<Variable>> VariableController2::variables(const std::vector<QUuid> &ids)
370 {
371 auto encodedData = QByteArray{};
372 QDataStream stream{&encodedData, QIODevice::WriteOnly};
373 for (auto &var : variables) {
374 stream << var->ID().toByteArray();
375 }
376 return encodedData;
377 }
378
379 const std::vector<std::shared_ptr<Variable>> VariableController2::variables(QByteArray mimeData)
380 {
376 {
381 std::vector<std::shared_ptr<Variable>> variables;
377 std::vector<std::shared_ptr<Variable>> variables;
382 QDataStream stream{mimeData};
383
384 QVariantList ids;
385 stream >> ids;
386
387 for (const auto& id : ids) {
378 for (const auto& id : ids) {
388 auto uuid = QUuid{id.toByteArray()};
379 variables.push_back(impl->variable(id));
389 variables.push_back (impl->variable(uuid));
390 }
380 }
391
392 return variables;
381 return variables;
393 }
382 }
394
383
@@ -1,3 +1,8
1 #include <QMimeData>
2 #include <QSize>
3 #include <QTimer>
4 #include <unordered_map>
5
1 #include <Variable/Variable.h>
6 #include <Variable/Variable.h>
2 #include <Variable/VariableController2.h>
7 #include <Variable/VariableController2.h>
3 #include <Variable/VariableModel2.h>
8 #include <Variable/VariableModel2.h>
@@ -5,17 +10,13
5 #include <Common/DateUtils.h>
10 #include <Common/DateUtils.h>
6 #include <Common/MimeTypesDef.h>
11 #include <Common/MimeTypesDef.h>
7 #include <Common/StringUtils.h>
12 #include <Common/StringUtils.h>
13 #include <Common/containers.h>
8
14
9 #include <Data/IDataSeries.h>
15 #include <Data/IDataSeries.h>
10
16
11 #include <DataSource/DataSourceController.h>
17 #include <DataSource/DataSourceController.h>
12 #include <Time/TimeController.h>
18 #include <Time/TimeController.h>
13
19
14 #include <QMimeData>
15 #include <QSize>
16 #include <QTimer>
17 #include <unordered_map>
18
19 namespace {
20 namespace {
20
21
21 // Column indexes
22 // Column indexes
@@ -66,8 +67,8 QString uniqueName(const QString &defaultName,
66
67
67
68
68
69
69 VariableModel2::VariableModel2(const std::shared_ptr<VariableController2> &variableController, QObject *parent)
70 VariableModel2::VariableModel2(QObject *parent)
70 : QAbstractTableModel{parent}, _variableController{variableController}
71 : QAbstractTableModel{parent}
71 {
72 {
72 }
73 }
73
74
@@ -82,7 +83,7 int VariableModel2::columnCount(const QModelIndex &parent) const
82 int VariableModel2::rowCount(const QModelIndex &parent) const
83 int VariableModel2::rowCount(const QModelIndex &parent) const
83 {
84 {
84 Q_UNUSED(parent);
85 Q_UNUSED(parent);
85 return static_cast<int>(_variableController->variables().size());
86 return _variables.size();
86 }
87 }
87
88
88 QVariant VariableModel2::data(const QModelIndex &index, int role) const
89 QVariant VariableModel2::data(const QModelIndex &index, int role) const
@@ -96,7 +97,7 QVariant VariableModel2::data(const QModelIndex &index, int role) const
96 }
97 }
97
98
98 if (role == Qt::DisplayRole) {
99 if (role == Qt::DisplayRole) {
99 if (auto variable = _variableController->variables()[index.row()]) {
100 if (auto variable = _variables[index.row()]) {
100 switch (index.column()) {
101 switch (index.column()) {
101 case NAME_COLUMN:
102 case NAME_COLUMN:
102 return variable->name();
103 return variable->name();
@@ -178,19 +179,19 QMimeData *VariableModel2::mimeData(const QModelIndexList &indexes) const
178 DateTimeRange firstTimeRange;
179 DateTimeRange firstTimeRange;
179 for (const auto &index : indexes) {
180 for (const auto &index : indexes) {
180 if (index.column() == 0) { // only the first column
181 if (index.column() == 0) { // only the first column
181 auto variable = _variableController->variables()[index.row()];
182 auto variable = _variables[index.row()];
182 if (variable.get() && index.isValid()) {
183 if (variable.get() && index.isValid()) {
183
184
184 if (variables.size()==0) {
185 if (variables.size()==0) {
185 // Gets the range of the first variable
186 // Gets the range of the first variable
186 firstTimeRange = std::move(variable->range());
187 firstTimeRange = variable->range();
187 }
188 }
188 variables.push_back(variable);
189 variables.push_back(variable);
189 }
190 }
190 }
191 }
191 }
192 }
192
193
193 auto variablesEncodedData = _variableController->mimeData(variables);
194 auto variablesEncodedData = Variable::mimeData(variables);
194 mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
195 mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
195
196
196 if (variables.size() == 1) {
197 if (variables.size() == 1) {
@@ -205,6 +206,7 QMimeData *VariableModel2::mimeData(const QModelIndexList &indexes) const
205 bool VariableModel2::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
206 bool VariableModel2::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
206 int column, const QModelIndex &parent) const
207 int column, const QModelIndex &parent) const
207 {
208 {
209 Q_UNUSED(column);
208 // drop of a product
210 // drop of a product
209 return data->hasFormat(MIME_TYPE_PRODUCT_LIST)
211 return data->hasFormat(MIME_TYPE_PRODUCT_LIST)
210 || (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()
212 || (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()
@@ -221,17 +223,16 bool VariableModel2::dropMimeData(const QMimeData *data, Qt::DropAction action,
221 = DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST));
223 = DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST));
222
224
223 for (auto metaData : productList) {
225 for (auto metaData : productList) {
224 //emit requestVariable(metaData.toHash());
226 emit createVariable(metaData.toHash());
225 //@TODO No idea what this does
226 }
227 }
227
228
228 dropDone = true;
229 dropDone = true;
229 }
230 }
230 else if (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()) {
231 else if (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()) {
231 auto variable = _variableController->variables()[parent.row()];
232 auto variable = _variables[parent.row()];
232 auto range = TimeController::timeRangeForMimeData(data->data(MIME_TYPE_TIME_RANGE));
233 auto range = TimeController::timeRangeForMimeData(data->data(MIME_TYPE_TIME_RANGE));
233
234
234 _variableController->asyncChangeRange(variable, range);
235 emit asyncChangeRange(variable, range);
235
236
236 dropDone = true;
237 dropDone = true;
237 }
238 }
@@ -241,6 +242,29 bool VariableModel2::dropMimeData(const QMimeData *data, Qt::DropAction action,
241
242
242 void VariableModel2::variableUpdated() noexcept
243 void VariableModel2::variableUpdated() noexcept
243 {
244 {
245 emit dataChanged(QModelIndex(),QModelIndex());
246 }
244
247
248 void VariableModel2::variableAdded(const std::shared_ptr<Variable> & variable)
249 {
250 if(!SciQLop::containers::contains(_variables,variable))
251 {
252 beginInsertRows(QModelIndex(), this->_variables.size(), this->_variables.size());
253 this->_variables.push_back(variable);
254 endInsertRows();
255 connect(variable.get(),&Variable::updated,this,&VariableModel2::variableUpdated);
256 }
257 }
258
259 void VariableModel2::variableDeleted(const std::shared_ptr<Variable> &variable)
260 {
261 auto it = std::find(_variables.begin(), _variables.end(), variable);
262 if (it != _variables.end())
263 {
264 auto index = std::distance(_variables.begin(), it);
265 beginRemoveRows(QModelIndex(), index, index);
266 _variables.erase(it);
267 endRemoveRows();
268 }
245 }
269 }
246
270
General Comments 0
You need to be logged in to leave comments. Login now