diff --git a/include/DataSource/DataSourceController.h b/include/DataSource/DataSourceController.h index 209c0e2..b58c10f 100644 --- a/include/DataSource/DataSourceController.h +++ b/include/DataSource/DataSourceController.h @@ -92,7 +92,7 @@ signals: * @param variableProvider the provider that will be used to retrieve the data of the variable * (can be null) */ - void variableCreationRequested(const QString &variableName, + void createVariable(const QString &variableName, const QVariantHash &variableMetadata, std::shared_ptr variableProvider); diff --git a/include/Variable/Variable.h b/include/Variable/Variable.h index 4a72a0d..293d1fc 100644 --- a/include/Variable/Variable.h +++ b/include/Variable/Variable.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "CoreGlobal.h" #include @@ -69,6 +70,30 @@ public: const DateTimeRange& newRange, const DateTimeRange& newCacheRange, bool notify=true); + static QByteArray mimeData(const std::vector > &variables) + { + auto encodedData = QByteArray{}; + QDataStream stream{&encodedData, QIODevice::WriteOnly}; + for (auto &var : variables) { + stream << var->ID().toByteArray(); + } + return encodedData; + } + + static std::vector variablesIDs(QByteArray mimeData) + { + std::vector variables; + QDataStream stream{mimeData}; + + QVariantList ids; + stream >> ids; + + for (const auto& id : ids) { + variables.emplace_back (id.toByteArray()); + } + return variables; + } + DEPRECATE( bool contains(const DateTimeRange &range) const noexcept; diff --git a/include/Variable/VariableController2.h b/include/Variable/VariableController2.h index b0a2152..6ea9a1b 100644 --- a/include/Variable/VariableController2.h +++ b/include/Variable/VariableController2.h @@ -37,9 +37,7 @@ public: void synchronize(const std::shared_ptr& var, const std::shared_ptr& with); - //This should be somewhere else VC has nothing to do with MIMEData - QByteArray mimeData(const std::vector> &variables) const; - const std::vector> variables(QByteArray mimeData); + const std::vector> variables(const std::vector& ids); const std::shared_ptr& operator[] (int index) const; std::shared_ptr operator[] (int index); diff --git a/include/Variable/VariableModel2.h b/include/Variable/VariableModel2.h index 960aea6..b176a55 100644 --- a/include/Variable/VariableModel2.h +++ b/include/Variable/VariableModel2.h @@ -24,9 +24,10 @@ enum VariableRoles { ProgressRole = Qt::UserRole }; */ class SCIQLOP_CORE_EXPORT VariableModel2 : public QAbstractTableModel { Q_OBJECT - std::shared_ptr _variableController; + // read only mirror of VariableController2 content + std::vector> _variables; public: - explicit VariableModel2(const std::shared_ptr& variableController, QObject *parent = nullptr); + explicit VariableModel2(QObject *parent = nullptr); // /////////////////////////// // // QAbstractTableModel methods // @@ -52,12 +53,14 @@ public: virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; - signals: - -private slots: + void createVariable(const QVariantHash &productData); + void asyncChangeRange(const std::shared_ptr& variable, const DateTimeRange& r); +public slots: /// Slot called when data of a variable has been updated void variableUpdated() noexcept; + void variableAdded(const std::shared_ptr&); + void variableDeleted(const std::shared_ptr&); }; #endif // SCIQLOP_VARIABLEMODEL2_H diff --git a/src/DataSource/DataSourceController.cpp b/src/DataSource/DataSourceController.cpp index 4b67a2b..d8af2a8 100644 --- a/src/DataSource/DataSourceController.cpp +++ b/src/DataSource/DataSourceController.cpp @@ -123,7 +123,7 @@ void DataSourceController::loadProductItem(const QUuid &dataSourceUid, auto it = impl->m_DataProviders.find(dataSourceUid); auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; - emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider); + emit createVariable(productItem.name(), productItem.data(), dataProvider); } else { qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); diff --git a/src/Variable/VariableController2.cpp b/src/Variable/VariableController2.cpp index fbc0967..53b6c76 100644 --- a/src/Variable/VariableController2.cpp +++ b/src/Variable/VariableController2.cpp @@ -32,7 +32,7 @@ class VariableController2::VariableController2Private inline void removeVariable(const std::shared_ptr& variable) { QWriteLocker lock{&_lock}; - _variables.remove(*variable); + _variables.erase(*variable); _providers.remove(*variable); _synchronizationGroups.remove(*variable); } @@ -55,10 +55,11 @@ class VariableController2::VariableController2Private inline std::shared_ptr variable(QUuid variable) { QReadLocker lock{&_lock}; + auto it = _variables.find(variable); [[unlikely]] - if(!_variables.contains(variable)) + if(it==_variables.end()) SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Unknown Variable"); - return _variables[variable]; + return (*it).second; } inline std::shared_ptr variable(int index) @@ -67,14 +68,19 @@ class VariableController2::VariableController2Private [[unlikely]] if(!_variables.size() > index) SCIQLOP_ERROR(threadSafeVaraiblesMaps,"Index is out of bounds"); - return _variables.values()[index]; + auto it = _variables.cbegin(); + while (index!=0) { + index-=1; + it++; + } + return (*it).second; } inline const std::vector> variables() { std::vector> vars; QReadLocker lock{&_lock}; - for(const auto &var:_variables) + for(const auto&[id, var]:_variables) { vars.push_back(var); } @@ -102,11 +108,11 @@ class VariableController2::VariableController2Private inline bool has(const std::shared_ptr& variable) { QReadLocker lock{&_lock}; - return _variables.contains(*variable); + return _variables.find(*variable)==_variables.end(); } private: - QMap> _variables; + std::map> _variables; QMap> _providers; QMap> _synchronizationGroups; QReadWriteLock _lock{QReadWriteLock::Recursive}; @@ -366,29 +372,12 @@ void VariableController2::synchronize(const std::shared_ptr &var, cons impl->synchronize(var, with); } -QByteArray VariableController2::mimeData(const std::vector > &variables) const -{ - auto encodedData = QByteArray{}; - QDataStream stream{&encodedData, QIODevice::WriteOnly}; - for (auto &var : variables) { - stream << var->ID().toByteArray(); - } - return encodedData; -} - -const std::vector> VariableController2::variables(QByteArray mimeData) +const std::vector> VariableController2::variables(const std::vector &ids) { std::vector> variables; - QDataStream stream{mimeData}; - - QVariantList ids; - stream >> ids; - for (const auto& id : ids) { - auto uuid = QUuid{id.toByteArray()}; - variables.push_back (impl->variable(uuid)); + variables.push_back(impl->variable(id)); } - return variables; } diff --git a/src/Variable/VariableModel2.cpp b/src/Variable/VariableModel2.cpp index 61f81ad..2d89231 100644 --- a/src/Variable/VariableModel2.cpp +++ b/src/Variable/VariableModel2.cpp @@ -1,3 +1,8 @@ +#include +#include +#include +#include + #include #include #include @@ -5,17 +10,13 @@ #include #include #include +#include #include #include #include