@@ -1,96 +1,102 | |||||
1 | #ifndef SCIQLOP_DATASOURCEITEM_H |
|
1 | #ifndef SCIQLOP_DATASOURCEITEM_H | |
2 | #define SCIQLOP_DATASOURCEITEM_H |
|
2 | #define SCIQLOP_DATASOURCEITEM_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 |
|
5 | |||
6 | #include <Common/spimpl.h> |
|
6 | #include <Common/spimpl.h> | |
7 |
|
7 | |||
8 | #include <QVariant> |
|
8 | #include <QVariant> | |
9 | #include <QVector> |
|
9 | #include <QVector> | |
10 |
|
10 | |||
11 | class DataSourceItemAction; |
|
11 | class DataSourceItemAction; | |
12 |
|
12 | |||
13 | /** |
|
13 | /** | |
14 | * Possible types of an item |
|
14 | * Possible types of an item | |
15 | */ |
|
15 | */ | |
16 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; |
|
16 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; | |
17 |
|
17 | |||
18 | /** |
|
18 | /** | |
19 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
19 | * @brief The DataSourceItem class aims to represent a structure element of a data source. | |
20 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
20 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) | |
21 | * containing other DataSourceItem objects (children). |
|
21 | * containing other DataSourceItem objects (children). | |
22 | * For each DataSourceItem can be associated a set of data representing it. |
|
22 | * For each DataSourceItem can be associated a set of data representing it. | |
23 | */ |
|
23 | */ | |
24 | class SCIQLOP_CORE_EXPORT DataSourceItem { |
|
24 | class SCIQLOP_CORE_EXPORT DataSourceItem { | |
25 | public: |
|
25 | public: | |
26 | /// Key associated with the name of the item |
|
26 | /// Key associated with the name of the item | |
27 | static const QString NAME_DATA_KEY; |
|
27 | static const QString NAME_DATA_KEY; | |
28 |
|
28 | |||
29 | explicit DataSourceItem(DataSourceItemType type, const QString &name); |
|
29 | explicit DataSourceItem(DataSourceItemType type, const QString &name); | |
30 | explicit DataSourceItem(DataSourceItemType type, QVariantHash data = {}); |
|
30 | explicit DataSourceItem(DataSourceItemType type, QVariantHash data = {}); | |
31 |
|
31 | |||
32 | /// @return the actions of the item as a vector |
|
32 | /// @return the actions of the item as a vector | |
33 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
33 | QVector<DataSourceItemAction *> actions() const noexcept; | |
34 |
|
34 | |||
35 | /** |
|
35 | /** | |
36 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
36 | * Adds an action to the item. The item takes ownership of the action, and the action is | |
37 | * automatically associated to the item |
|
37 | * automatically associated to the item | |
38 | * @param action the action to add |
|
38 | * @param action the action to add | |
39 | */ |
|
39 | */ | |
40 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
40 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; | |
41 |
|
41 | |||
42 | /** |
|
42 | /** | |
43 | * Adds a child to the item. The item takes ownership of the child. |
|
43 | * Adds a child to the item. The item takes ownership of the child. | |
44 | * @param child the child to add |
|
44 | * @param child the child to add | |
45 | */ |
|
45 | */ | |
46 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
46 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; | |
47 |
|
47 | |||
48 | /** |
|
48 | /** | |
49 | * Returns the item's child associated to an index |
|
49 | * Returns the item's child associated to an index | |
50 | * @param childIndex the index to search |
|
50 | * @param childIndex the index to search | |
51 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
51 | * @return a pointer to the child if index is valid, nullptr otherwise | |
52 | */ |
|
52 | */ | |
53 | DataSourceItem *child(int childIndex) const noexcept; |
|
53 | DataSourceItem *child(int childIndex) const noexcept; | |
54 |
|
54 | |||
55 | int childCount() const noexcept; |
|
55 | int childCount() const noexcept; | |
56 |
|
56 | |||
57 | /** |
|
57 | /** | |
58 | * Get the data associated to a key |
|
58 | * Get the data associated to a key | |
59 | * @param key the key to search |
|
59 | * @param key the key to search | |
60 | * @return the data found if key is valid, default QVariant otherwise |
|
60 | * @return the data found if key is valid, default QVariant otherwise | |
61 | */ |
|
61 | */ | |
62 | QVariant data(const QString &key) const noexcept; |
|
62 | QVariant data(const QString &key) const noexcept; | |
63 |
|
63 | |||
64 | /// Gets all data |
|
64 | /// Gets all data | |
65 | QVariantHash data() const noexcept; |
|
65 | QVariantHash data() const noexcept; | |
66 |
|
66 | |||
67 | bool isRoot() const noexcept; |
|
67 | bool isRoot() const noexcept; | |
68 |
|
68 | |||
69 | QString name() const noexcept; |
|
69 | QString name() const noexcept; | |
70 |
|
70 | |||
71 | /** |
|
71 | /** | |
72 | * Get the item's parent |
|
72 | * Get the item's parent | |
73 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
73 | * @return a pointer to the parent if it exists, nullptr if the item is a root | |
74 | */ |
|
74 | */ | |
75 | DataSourceItem *parentItem() const noexcept; |
|
75 | DataSourceItem *parentItem() const noexcept; | |
76 |
|
76 | |||
77 | /** |
|
77 | /** | |
|
78 | * Gets the item's root | |||
|
79 | * @return the top parent, the item itself if it's the root item | |||
|
80 | */ | |||
|
81 | const DataSourceItem &rootItem() const noexcept; | |||
|
82 | ||||
|
83 | /** | |||
78 | * Sets or appends a value to a key |
|
84 | * Sets or appends a value to a key | |
79 | * @param key the key |
|
85 | * @param key the key | |
80 | * @param value the value |
|
86 | * @param value the value | |
81 | * @param append if true, the value is added to the values already existing for the key, |
|
87 | * @param append if true, the value is added to the values already existing for the key, | |
82 | * otherwise it replaces the existing values |
|
88 | * otherwise it replaces the existing values | |
83 | */ |
|
89 | */ | |
84 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; |
|
90 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; | |
85 |
|
91 | |||
86 | DataSourceItemType type() const noexcept; |
|
92 | DataSourceItemType type() const noexcept; | |
87 |
|
93 | |||
88 | bool operator==(const DataSourceItem &other); |
|
94 | bool operator==(const DataSourceItem &other); | |
89 | bool operator!=(const DataSourceItem &other); |
|
95 | bool operator!=(const DataSourceItem &other); | |
90 |
|
96 | |||
91 | private: |
|
97 | private: | |
92 | class DataSourceItemPrivate; |
|
98 | class DataSourceItemPrivate; | |
93 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
99 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; | |
94 | }; |
|
100 | }; | |
95 |
|
101 | |||
96 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
|
102 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,120 +1,143 | |||||
1 | #include "DataSource/DataSourceController.h" |
|
1 | #include "DataSource/DataSourceController.h" | |
2 | #include "DataSource/DataSourceItem.h" |
|
2 | #include "DataSource/DataSourceItem.h" | |
3 |
|
3 | |||
4 | #include <Data/IDataProvider.h> |
|
4 | #include <Data/IDataProvider.h> | |
5 |
|
5 | |||
6 | #include <QMutex> |
|
6 | #include <QMutex> | |
7 | #include <QThread> |
|
7 | #include <QThread> | |
8 |
|
8 | |||
9 | #include <QDir> |
|
9 | #include <QDir> | |
10 | #include <QStandardPaths> |
|
10 | #include <QStandardPaths> | |
11 |
|
11 | |||
12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") |
|
12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") | |
13 |
|
13 | |||
|
14 | namespace { | |||
|
15 | ||||
|
16 | /** | |||
|
17 | * Builds the metadata of the variable that will be generated from the loading of an item | |||
|
18 | * @param dataSourceItem the data source item from which to generate the metadata | |||
|
19 | * @return the metadata of the variable | |||
|
20 | */ | |||
|
21 | QVariantHash variableMetadata(const DataSourceItem &dataSourceItem) | |||
|
22 | { | |||
|
23 | // Variable metadata contains... | |||
|
24 | ||||
|
25 | // ... all metadata of the item | |||
|
26 | auto result = dataSourceItem.data(); | |||
|
27 | ||||
|
28 | // ... and the name of the plugin, recovered from root item | |||
|
29 | result.insert(QStringLiteral("plugin"), dataSourceItem.rootItem().name()); | |||
|
30 | ||||
|
31 | return result; | |||
|
32 | } | |||
|
33 | ||||
|
34 | } // namespace | |||
|
35 | ||||
14 | class DataSourceController::DataSourceControllerPrivate { |
|
36 | class DataSourceController::DataSourceControllerPrivate { | |
15 | public: |
|
37 | public: | |
16 | QMutex m_WorkingMutex; |
|
38 | QMutex m_WorkingMutex; | |
17 | /// Data sources registered |
|
39 | /// Data sources registered | |
18 | QHash<QUuid, QString> m_DataSources; |
|
40 | QHash<QUuid, QString> m_DataSources; | |
19 | /// Data sources structures |
|
41 | /// Data sources structures | |
20 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; |
|
42 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; | |
21 | /// Data providers registered |
|
43 | /// Data providers registered | |
22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and |
|
44 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and | |
23 | /// continue to live without necessarily the data source controller |
|
45 | /// continue to live without necessarily the data source controller | |
24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; |
|
46 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; | |
25 | }; |
|
47 | }; | |
26 |
|
48 | |||
27 | DataSourceController::DataSourceController(QObject *parent) |
|
49 | DataSourceController::DataSourceController(QObject *parent) | |
28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
50 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} | |
29 | { |
|
51 | { | |
30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
52 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") | |
31 | << QThread::currentThread(); |
|
53 | << QThread::currentThread(); | |
32 | } |
|
54 | } | |
33 |
|
55 | |||
34 | DataSourceController::~DataSourceController() |
|
56 | DataSourceController::~DataSourceController() | |
35 | { |
|
57 | { | |
36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
58 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") | |
37 | << QThread::currentThread(); |
|
59 | << QThread::currentThread(); | |
38 | this->waitForFinish(); |
|
60 | this->waitForFinish(); | |
39 | } |
|
61 | } | |
40 |
|
62 | |||
41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
63 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept | |
42 | { |
|
64 | { | |
43 | auto dataSourceUid = QUuid::createUuid(); |
|
65 | auto dataSourceUid = QUuid::createUuid(); | |
44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
66 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); | |
45 |
|
67 | |||
46 | return dataSourceUid; |
|
68 | return dataSourceUid; | |
47 | } |
|
69 | } | |
48 |
|
70 | |||
49 | void DataSourceController::setDataSourceItem( |
|
71 | void DataSourceController::setDataSourceItem( | |
50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
72 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept | |
51 | { |
|
73 | { | |
52 | if (!dataSourceItem) { |
|
74 | if (!dataSourceItem) { | |
53 | qCWarning(LOG_DataSourceController()) |
|
75 | qCWarning(LOG_DataSourceController()) | |
54 | << tr("Data source item can't be registered (null item)"); |
|
76 | << tr("Data source item can't be registered (null item)"); | |
55 | return; |
|
77 | return; | |
56 | } |
|
78 | } | |
57 |
|
79 | |||
58 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
80 | if (impl->m_DataSources.contains(dataSourceUid)) { | |
59 | // The data provider is implicitly converted to a shared_ptr |
|
81 | // The data provider is implicitly converted to a shared_ptr | |
60 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
82 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); | |
61 |
|
83 | |||
62 | // Retrieves the data source item to emit the signal with it |
|
84 | // Retrieves the data source item to emit the signal with it | |
63 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
85 | auto it = impl->m_DataSourceItems.find(dataSourceUid); | |
64 | if (it != impl->m_DataSourceItems.end()) { |
|
86 | if (it != impl->m_DataSourceItems.end()) { | |
65 | emit dataSourceItemSet(it->second.get()); |
|
87 | emit dataSourceItemSet(it->second.get()); | |
66 | } |
|
88 | } | |
67 | } |
|
89 | } | |
68 | else { |
|
90 | else { | |
69 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
91 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " | |
70 | "data source has been registered with the uid") |
|
92 | "data source has been registered with the uid") | |
71 | .arg(dataSourceUid.toString()); |
|
93 | .arg(dataSourceUid.toString()); | |
72 | } |
|
94 | } | |
73 | } |
|
95 | } | |
74 |
|
96 | |||
75 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
97 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, | |
76 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
98 | std::unique_ptr<IDataProvider> dataProvider) noexcept | |
77 | { |
|
99 | { | |
78 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
100 | if (impl->m_DataSources.contains(dataSourceUid)) { | |
79 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
101 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); | |
80 | } |
|
102 | } | |
81 | else { |
|
103 | else { | |
82 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
104 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " | |
83 | "source has been registered with the uid") |
|
105 | "source has been registered with the uid") | |
84 | .arg(dataSourceUid.toString()); |
|
106 | .arg(dataSourceUid.toString()); | |
85 | } |
|
107 | } | |
86 | } |
|
108 | } | |
87 |
|
109 | |||
88 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, |
|
110 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, | |
89 | const DataSourceItem &productItem) noexcept |
|
111 | const DataSourceItem &productItem) noexcept | |
90 | { |
|
112 | { | |
91 | if (productItem.type() == DataSourceItemType::PRODUCT |
|
113 | if (productItem.type() == DataSourceItemType::PRODUCT | |
92 | || productItem.type() == DataSourceItemType::COMPONENT) { |
|
114 | || productItem.type() == DataSourceItemType::COMPONENT) { | |
93 | /// Retrieves the data provider of the data source (if any) |
|
115 | /// Retrieves the data provider of the data source (if any) | |
94 | auto it = impl->m_DataProviders.find(dataSourceUid); |
|
116 | auto it = impl->m_DataProviders.find(dataSourceUid); | |
95 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; |
|
117 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; | |
96 |
|
118 | |||
97 |
emit variableCreationRequested(productItem.name(), productItem |
|
119 | emit variableCreationRequested(productItem.name(), variableMetadata(productItem), | |
|
120 | dataProvider); | |||
98 | } |
|
121 | } | |
99 | else { |
|
122 | else { | |
100 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); |
|
123 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); | |
101 | } |
|
124 | } | |
102 | } |
|
125 | } | |
103 |
|
126 | |||
104 | void DataSourceController::initialize() |
|
127 | void DataSourceController::initialize() | |
105 | { |
|
128 | { | |
106 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
129 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") | |
107 | << QThread::currentThread(); |
|
130 | << QThread::currentThread(); | |
108 | impl->m_WorkingMutex.lock(); |
|
131 | impl->m_WorkingMutex.lock(); | |
109 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
132 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); | |
110 | } |
|
133 | } | |
111 |
|
134 | |||
112 | void DataSourceController::finalize() |
|
135 | void DataSourceController::finalize() | |
113 | { |
|
136 | { | |
114 | impl->m_WorkingMutex.unlock(); |
|
137 | impl->m_WorkingMutex.unlock(); | |
115 | } |
|
138 | } | |
116 |
|
139 | |||
117 | void DataSourceController::waitForFinish() |
|
140 | void DataSourceController::waitForFinish() | |
118 | { |
|
141 | { | |
119 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
142 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
120 | } |
|
143 | } |
@@ -1,140 +1,145 | |||||
1 | #include <DataSource/DataSourceItem.h> |
|
1 | #include <DataSource/DataSourceItem.h> | |
2 | #include <DataSource/DataSourceItemAction.h> |
|
2 | #include <DataSource/DataSourceItemAction.h> | |
3 |
|
3 | |||
4 | #include <QVector> |
|
4 | #include <QVector> | |
5 |
|
5 | |||
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); |
|
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); | |
7 |
|
7 | |||
8 | struct DataSourceItem::DataSourceItemPrivate { |
|
8 | struct DataSourceItem::DataSourceItemPrivate { | |
9 | explicit DataSourceItemPrivate(DataSourceItemType type, QVariantHash data) |
|
9 | explicit DataSourceItemPrivate(DataSourceItemType type, QVariantHash data) | |
10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} |
|
10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | DataSourceItem *m_Parent; |
|
14 | DataSourceItem *m_Parent; | |
15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; | |
16 | DataSourceItemType m_Type; |
|
16 | DataSourceItemType m_Type; | |
17 | QVariantHash m_Data; |
|
17 | QVariantHash m_Data; | |
18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; |
|
18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; | |
19 | }; |
|
19 | }; | |
20 |
|
20 | |||
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) |
|
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) | |
22 | : DataSourceItem{type, QVariantHash{{NAME_DATA_KEY, name}}} |
|
22 | : DataSourceItem{type, QVariantHash{{NAME_DATA_KEY, name}}} | |
23 | { |
|
23 | { | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVariantHash data) |
|
26 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVariantHash data) | |
27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} | |
28 | { |
|
28 | { | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept |
|
31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept | |
32 | { |
|
32 | { | |
33 | auto result = QVector<DataSourceItemAction *>{}; |
|
33 | auto result = QVector<DataSourceItemAction *>{}; | |
34 |
|
34 | |||
35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), |
|
35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), | |
36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); |
|
36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); | |
37 |
|
37 | |||
38 | return result; |
|
38 | return result; | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept |
|
41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept | |
42 | { |
|
42 | { | |
43 | action->setDataSourceItem(this); |
|
43 | action->setDataSourceItem(this); | |
44 | impl->m_Actions.push_back(std::move(action)); |
|
44 | impl->m_Actions.push_back(std::move(action)); | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept | |
48 | { |
|
48 | { | |
49 | child->impl->m_Parent = this; |
|
49 | child->impl->m_Parent = this; | |
50 | impl->m_Children.push_back(std::move(child)); |
|
50 | impl->m_Children.push_back(std::move(child)); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept | |
54 | { |
|
54 | { | |
55 | if (childIndex < 0 || childIndex >= childCount()) { |
|
55 | if (childIndex < 0 || childIndex >= childCount()) { | |
56 | return nullptr; |
|
56 | return nullptr; | |
57 | } |
|
57 | } | |
58 | else { |
|
58 | else { | |
59 | return impl->m_Children.at(childIndex).get(); |
|
59 | return impl->m_Children.at(childIndex).get(); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | int DataSourceItem::childCount() const noexcept |
|
63 | int DataSourceItem::childCount() const noexcept | |
64 | { |
|
64 | { | |
65 | return impl->m_Children.size(); |
|
65 | return impl->m_Children.size(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | QVariant DataSourceItem::data(const QString &key) const noexcept |
|
68 | QVariant DataSourceItem::data(const QString &key) const noexcept | |
69 | { |
|
69 | { | |
70 | return impl->m_Data.value(key); |
|
70 | return impl->m_Data.value(key); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | QVariantHash DataSourceItem::data() const noexcept |
|
73 | QVariantHash DataSourceItem::data() const noexcept | |
74 | { |
|
74 | { | |
75 | return impl->m_Data; |
|
75 | return impl->m_Data; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | bool DataSourceItem::isRoot() const noexcept |
|
78 | bool DataSourceItem::isRoot() const noexcept | |
79 | { |
|
79 | { | |
80 | return impl->m_Parent == nullptr; |
|
80 | return impl->m_Parent == nullptr; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | QString DataSourceItem::name() const noexcept |
|
83 | QString DataSourceItem::name() const noexcept | |
84 | { |
|
84 | { | |
85 | return data(NAME_DATA_KEY).toString(); |
|
85 | return data(NAME_DATA_KEY).toString(); | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
88 | DataSourceItem *DataSourceItem::parentItem() const noexcept | |
89 | { |
|
89 | { | |
90 | return impl->m_Parent; |
|
90 | return impl->m_Parent; | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
|
93 | const DataSourceItem &DataSourceItem::rootItem() const noexcept | |||
|
94 | { | |||
|
95 | return isRoot() ? *this : parentItem()->rootItem(); | |||
|
96 | } | |||
|
97 | ||||
93 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept |
|
98 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept | |
94 | { |
|
99 | { | |
95 | auto it = impl->m_Data.constFind(key); |
|
100 | auto it = impl->m_Data.constFind(key); | |
96 | if (append && it != impl->m_Data.constEnd()) { |
|
101 | if (append && it != impl->m_Data.constEnd()) { | |
97 | // Case of an existing value to which we want to add to the new value |
|
102 | // Case of an existing value to which we want to add to the new value | |
98 | if (it->canConvert<QVariantList>()) { |
|
103 | if (it->canConvert<QVariantList>()) { | |
99 | auto variantList = it->value<QVariantList>(); |
|
104 | auto variantList = it->value<QVariantList>(); | |
100 | variantList.append(value); |
|
105 | variantList.append(value); | |
101 |
|
106 | |||
102 | impl->m_Data.insert(key, variantList); |
|
107 | impl->m_Data.insert(key, variantList); | |
103 | } |
|
108 | } | |
104 | else { |
|
109 | else { | |
105 | impl->m_Data.insert(key, QVariantList{*it, value}); |
|
110 | impl->m_Data.insert(key, QVariantList{*it, value}); | |
106 | } |
|
111 | } | |
107 | } |
|
112 | } | |
108 | else { |
|
113 | else { | |
109 | // Other cases : |
|
114 | // Other cases : | |
110 | // - new value in map OR |
|
115 | // - new value in map OR | |
111 | // - replacement of an existing value (not appending) |
|
116 | // - replacement of an existing value (not appending) | |
112 | impl->m_Data.insert(key, value); |
|
117 | impl->m_Data.insert(key, value); | |
113 | } |
|
118 | } | |
114 | } |
|
119 | } | |
115 |
|
120 | |||
116 | DataSourceItemType DataSourceItem::type() const noexcept |
|
121 | DataSourceItemType DataSourceItem::type() const noexcept | |
117 | { |
|
122 | { | |
118 | return impl->m_Type; |
|
123 | return impl->m_Type; | |
119 | } |
|
124 | } | |
120 |
|
125 | |||
121 | bool DataSourceItem::operator==(const DataSourceItem &other) |
|
126 | bool DataSourceItem::operator==(const DataSourceItem &other) | |
122 | { |
|
127 | { | |
123 | // Compares items' attributes |
|
128 | // Compares items' attributes | |
124 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { |
|
129 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { | |
125 | // Compares contents of items' children |
|
130 | // Compares contents of items' children | |
126 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), |
|
131 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), | |
127 | std::cbegin(other.impl->m_Children), |
|
132 | std::cbegin(other.impl->m_Children), | |
128 | [](const auto &itemChild, const auto &otherChild) { |
|
133 | [](const auto &itemChild, const auto &otherChild) { | |
129 | return *itemChild == *otherChild; |
|
134 | return *itemChild == *otherChild; | |
130 | }); |
|
135 | }); | |
131 | } |
|
136 | } | |
132 | else { |
|
137 | else { | |
133 | return false; |
|
138 | return false; | |
134 | } |
|
139 | } | |
135 | } |
|
140 | } | |
136 |
|
141 | |||
137 | bool DataSourceItem::operator!=(const DataSourceItem &other) |
|
142 | bool DataSourceItem::operator!=(const DataSourceItem &other) | |
138 | { |
|
143 | { | |
139 | return !(*this == other); |
|
144 | return !(*this == other); | |
140 | } |
|
145 | } |
@@ -1,255 +1,258 | |||||
1 | #include <Variable/Variable.h> |
|
1 | #include <Variable/Variable.h> | |
2 | #include <Variable/VariableModel.h> |
|
2 | #include <Variable/VariableModel.h> | |
3 |
|
3 | |||
4 | #include <Common/DateUtils.h> |
|
4 | #include <Common/DateUtils.h> | |
5 |
|
5 | |||
6 | #include <Data/IDataSeries.h> |
|
6 | #include <Data/IDataSeries.h> | |
7 |
|
7 | |||
8 | #include <QSize> |
|
8 | #include <QSize> | |
9 | #include <unordered_map> |
|
9 | #include <unordered_map> | |
10 |
|
10 | |||
11 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
11 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | |
12 |
|
12 | |||
13 | namespace { |
|
13 | namespace { | |
14 |
|
14 | |||
15 | // Column indexes |
|
15 | // Column indexes | |
16 | const auto NAME_COLUMN = 0; |
|
16 | const auto NAME_COLUMN = 0; | |
17 | const auto TSTART_COLUMN = 1; |
|
17 | const auto TSTART_COLUMN = 1; | |
18 | const auto TEND_COLUMN = 2; |
|
18 | const auto TEND_COLUMN = 2; | |
19 | const auto UNIT_COLUMN = 3; |
|
19 | const auto UNIT_COLUMN = 3; | |
20 | const auto MISSION_COLUMN = 4; |
|
20 | const auto MISSION_COLUMN = 4; | |
21 |
const auto N |
|
21 | const auto PLUGIN_COLUMN = 5; | |
|
22 | const auto NB_COLUMNS = 6; | |||
22 |
|
23 | |||
23 | // Column properties |
|
24 | // Column properties | |
24 | const auto DEFAULT_HEIGHT = 25; |
|
25 | const auto DEFAULT_HEIGHT = 25; | |
25 | const auto DEFAULT_WIDTH = 100; |
|
26 | const auto DEFAULT_WIDTH = 100; | |
26 |
|
27 | |||
27 | struct ColumnProperties { |
|
28 | struct ColumnProperties { | |
28 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, |
|
29 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, | |
29 | int height = DEFAULT_HEIGHT) |
|
30 | int height = DEFAULT_HEIGHT) | |
30 | : m_Name{name}, m_Width{width}, m_Height{height} |
|
31 | : m_Name{name}, m_Width{width}, m_Height{height} | |
31 | { |
|
32 | { | |
32 | } |
|
33 | } | |
33 |
|
34 | |||
34 | QString m_Name; |
|
35 | QString m_Name; | |
35 | int m_Width; |
|
36 | int m_Width; | |
36 | int m_Height; |
|
37 | int m_Height; | |
37 | }; |
|
38 | }; | |
38 |
|
39 | |||
39 | const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{ |
|
40 | const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{ | |
40 | {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, |
|
41 | {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, | |
41 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}}, |
|
42 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}}, | |
42 | {MISSION_COLUMN, {QObject::tr("Mission")}}}; |
|
43 | {MISSION_COLUMN, {QObject::tr("Mission")}}, {PLUGIN_COLUMN, {QObject::tr("Plugin")}}}; | |
43 |
|
44 | |||
44 | /// Format for datetimes |
|
45 | /// Format for datetimes | |
45 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); |
|
46 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | |
46 |
|
47 | |||
47 |
|
48 | |||
48 | } // namespace |
|
49 | } // namespace | |
49 |
|
50 | |||
50 | struct VariableModel::VariableModelPrivate { |
|
51 | struct VariableModel::VariableModelPrivate { | |
51 | /// Variables created in SciQlop |
|
52 | /// Variables created in SciQlop | |
52 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
53 | std::vector<std::shared_ptr<Variable> > m_Variables; | |
53 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; |
|
54 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; | |
54 |
|
55 | |||
55 | /// Return the row index of the variable. -1 if it's not found |
|
56 | /// Return the row index of the variable. -1 if it's not found | |
56 | int indexOfVariable(Variable *variable) const noexcept; |
|
57 | int indexOfVariable(Variable *variable) const noexcept; | |
57 | }; |
|
58 | }; | |
58 |
|
59 | |||
59 | VariableModel::VariableModel(QObject *parent) |
|
60 | VariableModel::VariableModel(QObject *parent) | |
60 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
61 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | |
61 | { |
|
62 | { | |
62 | } |
|
63 | } | |
63 |
|
64 | |||
64 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, |
|
65 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | |
65 | const SqpRange &dateTime, |
|
66 | const SqpRange &dateTime, | |
66 | const QVariantHash &metadata) noexcept |
|
67 | const QVariantHash &metadata) noexcept | |
67 | { |
|
68 | { | |
68 | auto insertIndex = rowCount(); |
|
69 | auto insertIndex = rowCount(); | |
69 | beginInsertRows({}, insertIndex, insertIndex); |
|
70 | beginInsertRows({}, insertIndex, insertIndex); | |
70 |
|
71 | |||
71 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); |
|
72 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); | |
72 |
|
73 | |||
73 | impl->m_Variables.push_back(variable); |
|
74 | impl->m_Variables.push_back(variable); | |
74 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); |
|
75 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); | |
75 |
|
76 | |||
76 | endInsertRows(); |
|
77 | endInsertRows(); | |
77 |
|
78 | |||
78 | return variable; |
|
79 | return variable; | |
79 | } |
|
80 | } | |
80 |
|
81 | |||
81 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
82 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept | |
82 | { |
|
83 | { | |
83 | if (!variable) { |
|
84 | if (!variable) { | |
84 | qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; |
|
85 | qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; | |
85 | return; |
|
86 | return; | |
86 | } |
|
87 | } | |
87 |
|
88 | |||
88 | // Finds variable in the model |
|
89 | // Finds variable in the model | |
89 | auto begin = impl->m_Variables.cbegin(); |
|
90 | auto begin = impl->m_Variables.cbegin(); | |
90 | auto end = impl->m_Variables.cend(); |
|
91 | auto end = impl->m_Variables.cend(); | |
91 | auto it = std::find(begin, end, variable); |
|
92 | auto it = std::find(begin, end, variable); | |
92 | if (it != end) { |
|
93 | if (it != end) { | |
93 | auto removeIndex = std::distance(begin, it); |
|
94 | auto removeIndex = std::distance(begin, it); | |
94 |
|
95 | |||
95 | // Deletes variable |
|
96 | // Deletes variable | |
96 | beginRemoveRows({}, removeIndex, removeIndex); |
|
97 | beginRemoveRows({}, removeIndex, removeIndex); | |
97 | impl->m_Variables.erase(it); |
|
98 | impl->m_Variables.erase(it); | |
98 | endRemoveRows(); |
|
99 | endRemoveRows(); | |
99 | } |
|
100 | } | |
100 | else { |
|
101 | else { | |
101 | qCritical(LOG_VariableModel()) |
|
102 | qCritical(LOG_VariableModel()) | |
102 | << tr("Can't delete variable %1 from the model: the variable is not in the model") |
|
103 | << tr("Can't delete variable %1 from the model: the variable is not in the model") | |
103 | .arg(variable->name()); |
|
104 | .arg(variable->name()); | |
104 | } |
|
105 | } | |
105 |
|
106 | |||
106 | // Removes variable from progress map |
|
107 | // Removes variable from progress map | |
107 | impl->m_VariableToProgress.erase(variable); |
|
108 | impl->m_VariableToProgress.erase(variable); | |
108 | } |
|
109 | } | |
109 |
|
110 | |||
110 |
|
111 | |||
111 | std::shared_ptr<Variable> VariableModel::variable(int index) const |
|
112 | std::shared_ptr<Variable> VariableModel::variable(int index) const | |
112 | { |
|
113 | { | |
113 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; |
|
114 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | |
114 | } |
|
115 | } | |
115 |
|
116 | |||
116 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) |
|
117 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) | |
117 | { |
|
118 | { | |
118 | if (progress > 0.0) { |
|
119 | if (progress > 0.0) { | |
119 | impl->m_VariableToProgress[variable] = progress; |
|
120 | impl->m_VariableToProgress[variable] = progress; | |
120 | } |
|
121 | } | |
121 | else { |
|
122 | else { | |
122 | impl->m_VariableToProgress.erase(variable); |
|
123 | impl->m_VariableToProgress.erase(variable); | |
123 | } |
|
124 | } | |
124 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); |
|
125 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); | |
125 |
|
126 | |||
126 | emit dataChanged(modelIndex, modelIndex); |
|
127 | emit dataChanged(modelIndex, modelIndex); | |
127 | } |
|
128 | } | |
128 |
|
129 | |||
129 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
130 | int VariableModel::columnCount(const QModelIndex &parent) const | |
130 | { |
|
131 | { | |
131 | Q_UNUSED(parent); |
|
132 | Q_UNUSED(parent); | |
132 |
|
133 | |||
133 | return NB_COLUMNS; |
|
134 | return NB_COLUMNS; | |
134 | } |
|
135 | } | |
135 |
|
136 | |||
136 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
137 | int VariableModel::rowCount(const QModelIndex &parent) const | |
137 | { |
|
138 | { | |
138 | Q_UNUSED(parent); |
|
139 | Q_UNUSED(parent); | |
139 |
|
140 | |||
140 | return impl->m_Variables.size(); |
|
141 | return impl->m_Variables.size(); | |
141 | } |
|
142 | } | |
142 |
|
143 | |||
143 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
144 | QVariant VariableModel::data(const QModelIndex &index, int role) const | |
144 | { |
|
145 | { | |
145 | if (!index.isValid()) { |
|
146 | if (!index.isValid()) { | |
146 | return QVariant{}; |
|
147 | return QVariant{}; | |
147 | } |
|
148 | } | |
148 |
|
149 | |||
149 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
150 | if (index.row() < 0 || index.row() >= rowCount()) { | |
150 | return QVariant{}; |
|
151 | return QVariant{}; | |
151 | } |
|
152 | } | |
152 |
|
153 | |||
153 | if (role == Qt::DisplayRole) { |
|
154 | if (role == Qt::DisplayRole) { | |
154 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
155 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | |
155 | /// Lambda function that builds the variant to return for a time value |
|
156 | /// Lambda function that builds the variant to return for a time value | |
156 | auto dateTimeVariant = [](double secs) { |
|
157 | auto dateTimeVariant = [](double secs) { | |
157 | auto dateTime = DateUtils::dateTime(secs); |
|
158 | auto dateTime = DateUtils::dateTime(secs); | |
158 | return dateTime.toString(DATETIME_FORMAT); |
|
159 | return dateTime.toString(DATETIME_FORMAT); | |
159 | }; |
|
160 | }; | |
160 |
|
161 | |||
161 | switch (index.column()) { |
|
162 | switch (index.column()) { | |
162 | case NAME_COLUMN: |
|
163 | case NAME_COLUMN: | |
163 | return variable->name(); |
|
164 | return variable->name(); | |
164 | case TSTART_COLUMN: |
|
165 | case TSTART_COLUMN: | |
165 | return dateTimeVariant(variable->range().m_TStart); |
|
166 | return dateTimeVariant(variable->range().m_TStart); | |
166 | case TEND_COLUMN: |
|
167 | case TEND_COLUMN: | |
167 | return dateTimeVariant(variable->range().m_TEnd); |
|
168 | return dateTimeVariant(variable->range().m_TEnd); | |
168 | case UNIT_COLUMN: |
|
169 | case UNIT_COLUMN: | |
169 | return variable->metadata().value(QStringLiteral("units")); |
|
170 | return variable->metadata().value(QStringLiteral("units")); | |
170 | case MISSION_COLUMN: |
|
171 | case MISSION_COLUMN: | |
171 | return variable->metadata().value(QStringLiteral("mission")); |
|
172 | return variable->metadata().value(QStringLiteral("mission")); | |
|
173 | case PLUGIN_COLUMN: | |||
|
174 | return variable->metadata().value(QStringLiteral("plugin")); | |||
172 | default: |
|
175 | default: | |
173 | // No action |
|
176 | // No action | |
174 | break; |
|
177 | break; | |
175 | } |
|
178 | } | |
176 |
|
179 | |||
177 | qWarning(LOG_VariableModel()) |
|
180 | qWarning(LOG_VariableModel()) | |
178 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
181 | << tr("Can't get data (unknown column %1)").arg(index.column()); | |
179 | } |
|
182 | } | |
180 | else { |
|
183 | else { | |
181 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
184 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); | |
182 | } |
|
185 | } | |
183 | } |
|
186 | } | |
184 | else if (role == VariableRoles::ProgressRole) { |
|
187 | else if (role == VariableRoles::ProgressRole) { | |
185 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
188 | if (auto variable = impl->m_Variables.at(index.row())) { | |
186 |
|
189 | |||
187 | auto it = impl->m_VariableToProgress.find(variable); |
|
190 | auto it = impl->m_VariableToProgress.find(variable); | |
188 | if (it != impl->m_VariableToProgress.cend()) { |
|
191 | if (it != impl->m_VariableToProgress.cend()) { | |
189 | return it->second; |
|
192 | return it->second; | |
190 | } |
|
193 | } | |
191 | } |
|
194 | } | |
192 | } |
|
195 | } | |
193 |
|
196 | |||
194 | return QVariant{}; |
|
197 | return QVariant{}; | |
195 | } |
|
198 | } | |
196 |
|
199 | |||
197 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
200 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | |
198 | { |
|
201 | { | |
199 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { |
|
202 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { | |
200 | return QVariant{}; |
|
203 | return QVariant{}; | |
201 | } |
|
204 | } | |
202 |
|
205 | |||
203 | if (orientation == Qt::Horizontal) { |
|
206 | if (orientation == Qt::Horizontal) { | |
204 | auto propertiesIt = COLUMN_PROPERTIES.find(section); |
|
207 | auto propertiesIt = COLUMN_PROPERTIES.find(section); | |
205 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { |
|
208 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { | |
206 | // Role is either DisplayRole or SizeHintRole |
|
209 | // Role is either DisplayRole or SizeHintRole | |
207 | return (role == Qt::DisplayRole) |
|
210 | return (role == Qt::DisplayRole) | |
208 | ? QVariant{propertiesIt->m_Name} |
|
211 | ? QVariant{propertiesIt->m_Name} | |
209 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; |
|
212 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; | |
210 | } |
|
213 | } | |
211 | else { |
|
214 | else { | |
212 | qWarning(LOG_VariableModel()) |
|
215 | qWarning(LOG_VariableModel()) | |
213 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
216 | << tr("Can't get header data (unknown column %1)").arg(section); | |
214 | } |
|
217 | } | |
215 | } |
|
218 | } | |
216 |
|
219 | |||
217 | return QVariant{}; |
|
220 | return QVariant{}; | |
218 | } |
|
221 | } | |
219 |
|
222 | |||
220 | void VariableModel::abortProgress(const QModelIndex &index) |
|
223 | void VariableModel::abortProgress(const QModelIndex &index) | |
221 | { |
|
224 | { | |
222 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
225 | if (auto variable = impl->m_Variables.at(index.row())) { | |
223 | emit abortProgessRequested(variable); |
|
226 | emit abortProgessRequested(variable); | |
224 | } |
|
227 | } | |
225 | } |
|
228 | } | |
226 |
|
229 | |||
227 | void VariableModel::onVariableUpdated() noexcept |
|
230 | void VariableModel::onVariableUpdated() noexcept | |
228 | { |
|
231 | { | |
229 | // Finds variable that has been updated in the model |
|
232 | // Finds variable that has been updated in the model | |
230 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { |
|
233 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { | |
231 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); |
|
234 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); | |
232 |
|
235 | |||
233 | if (updatedVariableIndex > -1) { |
|
236 | if (updatedVariableIndex > -1) { | |
234 | emit dataChanged(createIndex(updatedVariableIndex, 0), |
|
237 | emit dataChanged(createIndex(updatedVariableIndex, 0), | |
235 | createIndex(updatedVariableIndex, columnCount() - 1)); |
|
238 | createIndex(updatedVariableIndex, columnCount() - 1)); | |
236 | } |
|
239 | } | |
237 | } |
|
240 | } | |
238 | } |
|
241 | } | |
239 |
|
242 | |||
240 | int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept |
|
243 | int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept | |
241 | { |
|
244 | { | |
242 | auto begin = std::cbegin(m_Variables); |
|
245 | auto begin = std::cbegin(m_Variables); | |
243 | auto end = std::cend(m_Variables); |
|
246 | auto end = std::cend(m_Variables); | |
244 | auto it |
|
247 | auto it | |
245 | = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); |
|
248 | = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); | |
246 |
|
249 | |||
247 | if (it != end) { |
|
250 | if (it != end) { | |
248 | // Gets the index of the variable in the model: we assume here that views have the same |
|
251 | // Gets the index of the variable in the model: we assume here that views have the same | |
249 | // order as the model |
|
252 | // order as the model | |
250 | return std::distance(begin, it); |
|
253 | return std::distance(begin, it); | |
251 | } |
|
254 | } | |
252 | else { |
|
255 | else { | |
253 | return -1; |
|
256 | return -1; | |
254 | } |
|
257 | } | |
255 | } |
|
258 | } |
General Comments 0
You need to be logged in to leave comments.
Login now