##// END OF EJS Templates
Add implementation of progress bar on variable inspector connected to...
perrinel -
r401:5601a2d8f3ef
parent child
Show More
@@ -1,43 +1,46
1 #ifndef SCIQLOP_NETWORKCONTROLLER_H
1 #ifndef SCIQLOP_NETWORKCONTROLLER_H
2 #define SCIQLOP_NETWORKCONTROLLER_H
2 #define SCIQLOP_NETWORKCONTROLLER_H
3
3
4 #include <QLoggingCategory>
4 #include <QLoggingCategory>
5 #include <QObject>
5 #include <QObject>
6 #include <QUuid>
6 #include <QUuid>
7
7
8 #include <Common/spimpl.h>
8 #include <Common/spimpl.h>
9 #include <functional>
9 #include <functional>
10
10
11 Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController)
11 Q_DECLARE_LOGGING_CATEGORY(LOG_NetworkController)
12
12
13 class QNetworkReply;
13 class QNetworkReply;
14 class QNetworkRequest;
14 class QNetworkRequest;
15
15
16 /**
16 /**
17 * @brief The NetworkController class aims to handle all network connection of SciQlop.
17 * @brief The NetworkController class aims to handle all network connection of SciQlop.
18 */
18 */
19 class NetworkController : public QObject {
19 class NetworkController : public QObject {
20 Q_OBJECT
20 Q_OBJECT
21 public:
21 public:
22 explicit NetworkController(QObject *parent = 0);
22 explicit NetworkController(QObject *parent = 0);
23
23
24 void initialize();
24 void initialize();
25 void finalize();
25 void finalize();
26
26
27 public slots:
27 public slots:
28 /// Execute request and call callback when the reply is finished. Identifier is attached to the
29 /// callback
28 void onProcessRequested(const QNetworkRequest &request, QUuid identifier,
30 void onProcessRequested(const QNetworkRequest &request, QUuid identifier,
29 std::function<void(QNetworkReply *, QUuid)> callback);
31 std::function<void(QNetworkReply *, QUuid)> callback);
32 /// Cancel the request of identifier
30 void onReplyCanceled(QUuid identifier);
33 void onReplyCanceled(QUuid identifier);
31
34
32 signals:
35 signals:
33 void replyFinished(QNetworkReply *reply, QUuid identifier);
36 void replyFinished(QNetworkReply *reply, QUuid identifier);
34 void replyDownloadProgress(QUuid identifier);
37 void replyDownloadProgress(QUuid identifier, double progress);
35
38
36 private:
39 private:
37 void waitForFinish();
40 void waitForFinish();
38
41
39 class NetworkControllerPrivate;
42 class NetworkControllerPrivate;
40 spimpl::unique_impl_ptr<NetworkControllerPrivate> impl;
43 spimpl::unique_impl_ptr<NetworkControllerPrivate> impl;
41 };
44 };
42
45
43 #endif // SCIQLOP_NETWORKCONTROLLER_H
46 #endif // SCIQLOP_NETWORKCONTROLLER_H
@@ -1,80 +1,83
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
3
3
4 #include <Data/SqpDateTime.h>
4 #include <Data/SqpDateTime.h>
5
5
6 #include <QLoggingCategory>
6 #include <QLoggingCategory>
7 #include <QObject>
7 #include <QObject>
8
8
9 #include <Common/spimpl.h>
9 #include <Common/spimpl.h>
10
10
11 class IDataProvider;
11 class IDataProvider;
12 class QItemSelectionModel;
12 class QItemSelectionModel;
13 class TimeController;
13 class TimeController;
14 class Variable;
14 class Variable;
15 class VariableModel;
15 class VariableModel;
16
16
17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
18
18
19 /**
19 /**
20 * @brief The VariableController class aims to handle the variables in SciQlop.
20 * @brief The VariableController class aims to handle the variables in SciQlop.
21 */
21 */
22 class VariableController : public QObject {
22 class VariableController : public QObject {
23 Q_OBJECT
23 Q_OBJECT
24 public:
24 public:
25 explicit VariableController(QObject *parent = 0);
25 explicit VariableController(QObject *parent = 0);
26 virtual ~VariableController();
26 virtual ~VariableController();
27
27
28 VariableModel *variableModel() noexcept;
28 VariableModel *variableModel() noexcept;
29 QItemSelectionModel *variableSelectionModel() noexcept;
29 QItemSelectionModel *variableSelectionModel() noexcept;
30
30
31 void setTimeController(TimeController *timeController) noexcept;
31 void setTimeController(TimeController *timeController) noexcept;
32
32
33 /**
33 /**
34 * Deletes from the controller the variable passed in parameter.
34 * Deletes from the controller the variable passed in parameter.
35 *
35 *
36 * Delete a variable includes:
36 * Delete a variable includes:
37 * - the deletion of the various references to the variable in SciQlop
37 * - the deletion of the various references to the variable in SciQlop
38 * - the deletion of the model variable
38 * - the deletion of the model variable
39 * - the deletion of the provider associated with the variable
39 * - the deletion of the provider associated with the variable
40 * - removing the cache associated with the variable
40 * - removing the cache associated with the variable
41 *
41 *
42 * @param variable the variable to delete from the controller.
42 * @param variable the variable to delete from the controller.
43 */
43 */
44 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
44 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
45
45
46 /**
46 /**
47 * Deletes from the controller the variables passed in parameter.
47 * Deletes from the controller the variables passed in parameter.
48 * @param variables the variables to delete from the controller.
48 * @param variables the variables to delete from the controller.
49 * @sa deleteVariable()
49 * @sa deleteVariable()
50 */
50 */
51 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
51 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
52
52
53 signals:
53 signals:
54 /// Signal emitted when a variable is about to be deleted from the controller
54 /// Signal emitted when a variable is about to be deleted from the controller
55 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
55 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
56
56
57 public slots:
57 public slots:
58 /// Request the data loading of the variable whithin dateTime
58 /// Request the data loading of the variable whithin dateTime
59 void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
59 void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
60 /**
60 /**
61 * Creates a new variable and adds it to the model
61 * Creates a new variable and adds it to the model
62 * @param name the name of the new variable
62 * @param name the name of the new variable
63 * @param provider the data provider for the new variable
63 * @param provider the data provider for the new variable
64 */
64 */
65 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
65 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
66
66
67 /// Update the temporal parameters of every selected variable to dateTime
67 /// Update the temporal parameters of every selected variable to dateTime
68 void onDateTimeOnSelection(const SqpDateTime &dateTime);
68 void onDateTimeOnSelection(const SqpDateTime &dateTime);
69
69
70
71 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
72
70 void initialize();
73 void initialize();
71 void finalize();
74 void finalize();
72
75
73 private:
76 private:
74 void waitForFinish();
77 void waitForFinish();
75
78
76 class VariableControllerPrivate;
79 class VariableControllerPrivate;
77 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
80 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
78 };
81 };
79
82
80 #endif // SCIQLOP_VARIABLECONTROLLER_H
83 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,63 +1,70
1 #ifndef SCIQLOP_VARIABLEMODEL_H
1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
3
3
4
4
5 #include <Data/SqpDateTime.h>
5 #include <Data/SqpDateTime.h>
6
6
7 #include <QAbstractTableModel>
7 #include <QAbstractTableModel>
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9
9
10 #include <Common/MetaTypes.h>
10 #include <Common/MetaTypes.h>
11 #include <Common/spimpl.h>
11 #include <Common/spimpl.h>
12
12
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
14
14
15 enum VariableRoles { progressRole = Qt::UserRole };
16
17
15 class IDataSeries;
18 class IDataSeries;
16 class Variable;
19 class Variable;
17
20
18 /**
21 /**
19 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
22 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
20 */
23 */
21 class VariableModel : public QAbstractTableModel {
24 class VariableModel : public QAbstractTableModel {
22 public:
25 public:
23 explicit VariableModel(QObject *parent = nullptr);
26 explicit VariableModel(QObject *parent = nullptr);
24
27
25 /**
28 /**
26 * Creates a new variable in the model
29 * Creates a new variable in the model
27 * @param name the name of the new variable
30 * @param name the name of the new variable
28 * @param dateTime the dateTime of the new variable
31 * @param dateTime the dateTime of the new variable
29 * @return the pointer to the new variable
32 * @return the pointer to the new variable
30 */
33 */
31 std::shared_ptr<Variable> createVariable(const QString &name,
34 std::shared_ptr<Variable> createVariable(const QString &name,
32 const SqpDateTime &dateTime) noexcept;
35 const SqpDateTime &dateTime) noexcept;
33
36
34 /**
37 /**
35 * Deletes a variable from the model, if it exists
38 * Deletes a variable from the model, if it exists
36 * @param variable the variable to delete
39 * @param variable the variable to delete
37 */
40 */
38 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
41 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
39
42
43
40 std::shared_ptr<Variable> variable(int index) const;
44 std::shared_ptr<Variable> variable(int index) const;
41
45
46 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
47
42 // /////////////////////////// //
48 // /////////////////////////// //
43 // QAbstractTableModel methods //
49 // QAbstractTableModel methods //
44 // /////////////////////////// //
50 // /////////////////////////// //
51
45 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
52 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
46 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
53 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
47 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
54 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
48 virtual QVariant headerData(int section, Qt::Orientation orientation,
55 virtual QVariant headerData(int section, Qt::Orientation orientation,
49 int role = Qt::DisplayRole) const override;
56 int role = Qt::DisplayRole) const override;
50
57
51 private:
58 private:
52 class VariableModelPrivate;
59 class VariableModelPrivate;
53 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
60 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
54
61
55 private slots:
62 private slots:
56 /// Slot called when data of a variable has been updated
63 /// Slot called when data of a variable has been updated
57 void onVariableUpdated() noexcept;
64 void onVariableUpdated() noexcept;
58 };
65 };
59
66
60 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
67 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
61 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
68 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
62
69
63 #endif // SCIQLOP_VARIABLEMODEL_H
70 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,88 +1,87
1 #include "Network/NetworkController.h"
1 #include "Network/NetworkController.h"
2
2
3 #include <QMutex>
3 #include <QMutex>
4 #include <QNetworkAccessManager>
4 #include <QNetworkAccessManager>
5 #include <QNetworkReply>
5 #include <QNetworkReply>
6 #include <QNetworkRequest>
6 #include <QNetworkRequest>
7 #include <QThread>
7 #include <QThread>
8
8
9 #include <unordered_map>
9 #include <unordered_map>
10
10
11 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
11 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
12
12
13 struct NetworkController::NetworkControllerPrivate {
13 struct NetworkController::NetworkControllerPrivate {
14 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
14 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 QMutex m_WorkingMutex;
15 QMutex m_WorkingMutex;
16
16
17 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
17 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
18 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
18 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
19 };
19 };
20
20
21 NetworkController::NetworkController(QObject *parent)
21 NetworkController::NetworkController(QObject *parent)
22 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
22 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
23 {
23 {
24 }
24 }
25
25
26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
27 std::function<void(QNetworkReply *, QUuid)> callback)
27 std::function<void(QNetworkReply *, QUuid)> callback)
28 {
28 {
29 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
29 qCInfo(LOG_NetworkController()) << tr("NetworkController registered")
30 << QThread::currentThread();
30 << QThread::currentThread();
31 auto reply = impl->m_AccessManager->get(request);
31 auto reply = impl->m_AccessManager->get(request);
32
32
33 // Store the couple reply id
33 // Store the couple reply id
34 impl->m_NetworkReplyToVariableId[reply] = identifier;
34 impl->m_NetworkReplyToVariableId[reply] = identifier;
35
35
36 auto onReplyFinished = [reply, this, identifier, callback]() {
36 auto onReplyFinished = [reply, this, identifier, callback]() {
37
37
38 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
38 qCInfo(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
39 << QThread::currentThread();
39 << QThread::currentThread();
40 auto it = impl->m_NetworkReplyToVariableId.find(reply);
40 auto it = impl->m_NetworkReplyToVariableId.find(reply);
41 if (it != impl->m_NetworkReplyToVariableId.cend()) {
41 if (it != impl->m_NetworkReplyToVariableId.cend()) {
42 callback(reply, identifier);
42 callback(reply, identifier);
43 }
43 }
44 };
44 };
45
45
46 auto onReplyDownloadProgress = [reply, this]() {
46 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
47
47
48 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyDownloadProgress")
48 double progress = (bytesRead * 100.0) / totalBytes;
49 << QThread::currentThread();
50 auto it = impl->m_NetworkReplyToVariableId.find(reply);
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
50 if (it != impl->m_NetworkReplyToVariableId.cend()) {
52 emit this->replyDownloadProgress(it->second);
51 emit this->replyDownloadProgress(it->second, progress);
53 }
52 }
54 };
53 };
55
54
56
55
57 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
56 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
58 connect(reply, &QNetworkReply::downloadProgress, this, onReplyDownloadProgress);
57 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
59 }
58 }
60
59
61 void NetworkController::initialize()
60 void NetworkController::initialize()
62 {
61 {
63 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
62 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
64 impl->m_WorkingMutex.lock();
63 impl->m_WorkingMutex.lock();
65 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
64 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
66 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
65 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
67 }
66 }
68
67
69 void NetworkController::finalize()
68 void NetworkController::finalize()
70 {
69 {
71 impl->m_WorkingMutex.unlock();
70 impl->m_WorkingMutex.unlock();
72 }
71 }
73
72
74 void NetworkController::onReplyCanceled(QUuid identifier)
73 void NetworkController::onReplyCanceled(QUuid identifier)
75 {
74 {
76 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
75 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
77
76
78 auto end = impl->m_NetworkReplyToVariableId.cend();
77 auto end = impl->m_NetworkReplyToVariableId.cend();
79 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
78 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
80 if (it != end) {
79 if (it != end) {
81 it->first->abort();
80 it->first->abort();
82 }
81 }
83 }
82 }
84
83
85 void NetworkController::waitForFinish()
84 void NetworkController::waitForFinish()
86 {
85 {
87 QMutexLocker locker{&impl->m_WorkingMutex};
86 QMutexLocker locker{&impl->m_WorkingMutex};
88 }
87 }
@@ -1,205 +1,216
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableCacheController.h>
2 #include <Variable/VariableCacheController.h>
3 #include <Variable/VariableController.h>
3 #include <Variable/VariableController.h>
4 #include <Variable/VariableModel.h>
4 #include <Variable/VariableModel.h>
5
5
6 #include <Data/DataProviderParameters.h>
6 #include <Data/DataProviderParameters.h>
7 #include <Data/IDataProvider.h>
7 #include <Data/IDataProvider.h>
8 #include <Data/IDataSeries.h>
8 #include <Data/IDataSeries.h>
9 #include <Time/TimeController.h>
9 #include <Time/TimeController.h>
10
10
11 #include <QDateTime>
11 #include <QDateTime>
12 #include <QMutex>
12 #include <QMutex>
13 #include <QThread>
13 #include <QThread>
14 #include <QUuid>
14 #include <QUuid>
15 #include <QtCore/QItemSelectionModel>
15 #include <QtCore/QItemSelectionModel>
16
16
17 #include <unordered_map>
17 #include <unordered_map>
18
18
19 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
20
20
21 struct VariableController::VariableControllerPrivate {
21 struct VariableController::VariableControllerPrivate {
22 explicit VariableControllerPrivate(VariableController *parent)
22 explicit VariableControllerPrivate(VariableController *parent)
23 : m_WorkingMutex{},
23 : m_WorkingMutex{},
24 m_VariableModel{new VariableModel{parent}},
24 m_VariableModel{new VariableModel{parent}},
25 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
25 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
26 m_VariableCacheController{std::make_unique<VariableCacheController>()}
26 m_VariableCacheController{std::make_unique<VariableCacheController>()}
27 {
27 {
28 }
28 }
29
29
30 QMutex m_WorkingMutex;
30 QMutex m_WorkingMutex;
31 /// Variable model. The VariableController has the ownership
31 /// Variable model. The VariableController has the ownership
32 VariableModel *m_VariableModel;
32 VariableModel *m_VariableModel;
33 QItemSelectionModel *m_VariableSelectionModel;
33 QItemSelectionModel *m_VariableSelectionModel;
34
34
35
35
36 TimeController *m_TimeController{nullptr};
36 TimeController *m_TimeController{nullptr};
37 std::unique_ptr<VariableCacheController> m_VariableCacheController;
37 std::unique_ptr<VariableCacheController> m_VariableCacheController;
38
38
39 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
39 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
40 m_VariableToProviderMap;
40 m_VariableToProviderMap;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier;
42 };
42 };
43
43
44 VariableController::VariableController(QObject *parent)
44 VariableController::VariableController(QObject *parent)
45 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
45 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
46 {
46 {
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
48 << QThread::currentThread();
48 << QThread::currentThread();
49 }
49 }
50
50
51 VariableController::~VariableController()
51 VariableController::~VariableController()
52 {
52 {
53 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
53 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
54 << QThread::currentThread();
54 << QThread::currentThread();
55 this->waitForFinish();
55 this->waitForFinish();
56 }
56 }
57
57
58 VariableModel *VariableController::variableModel() noexcept
58 VariableModel *VariableController::variableModel() noexcept
59 {
59 {
60 return impl->m_VariableModel;
60 return impl->m_VariableModel;
61 }
61 }
62
62
63 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
63 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
64 {
64 {
65 return impl->m_VariableSelectionModel;
65 return impl->m_VariableSelectionModel;
66 }
66 }
67
67
68 void VariableController::setTimeController(TimeController *timeController) noexcept
68 void VariableController::setTimeController(TimeController *timeController) noexcept
69 {
69 {
70 impl->m_TimeController = timeController;
70 impl->m_TimeController = timeController;
71 }
71 }
72
72
73 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
73 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
74 {
74 {
75 if (!variable) {
75 if (!variable) {
76 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
76 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
77 return;
77 return;
78 }
78 }
79
79
80 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
80 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
81 // make some treatments before the deletion
81 // make some treatments before the deletion
82 emit variableAboutToBeDeleted(variable);
82 emit variableAboutToBeDeleted(variable);
83
83
84 // Deletes provider
84 // Deletes provider
85 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
85 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
86 qCDebug(LOG_VariableController())
86 qCDebug(LOG_VariableController())
87 << tr("Number of providers deleted for variable %1: %2")
87 << tr("Number of providers deleted for variable %1: %2")
88 .arg(variable->name(), QString::number(nbProvidersDeleted));
88 .arg(variable->name(), QString::number(nbProvidersDeleted));
89
89
90 // Clears cache
90 // Clears cache
91 impl->m_VariableCacheController->clear(variable);
91 impl->m_VariableCacheController->clear(variable);
92
92
93 // Deletes from model
93 // Deletes from model
94 impl->m_VariableModel->deleteVariable(variable);
94 impl->m_VariableModel->deleteVariable(variable);
95 }
95 }
96
96
97 void VariableController::deleteVariables(
97 void VariableController::deleteVariables(
98 const QVector<std::shared_ptr<Variable> > &variables) noexcept
98 const QVector<std::shared_ptr<Variable> > &variables) noexcept
99 {
99 {
100 for (auto variable : qAsConst(variables)) {
100 for (auto variable : qAsConst(variables)) {
101 deleteVariable(variable);
101 deleteVariable(variable);
102 }
102 }
103 }
103 }
104
104
105 void VariableController::createVariable(const QString &name,
105 void VariableController::createVariable(const QString &name,
106 std::shared_ptr<IDataProvider> provider) noexcept
106 std::shared_ptr<IDataProvider> provider) noexcept
107 {
107 {
108
108
109 if (!impl->m_TimeController) {
109 if (!impl->m_TimeController) {
110 qCCritical(LOG_VariableController())
110 qCCritical(LOG_VariableController())
111 << tr("Impossible to create variable: The time controller is null");
111 << tr("Impossible to create variable: The time controller is null");
112 return;
112 return;
113 }
113 }
114
114
115
115
116 /// @todo : for the moment :
116 /// @todo : for the moment :
117 /// - the provider is only used to retrieve data from the variable for its initialization, but
117 /// - the provider is only used to retrieve data from the variable for its initialization, but
118 /// it will be retained later
118 /// it will be retained later
119 /// - default data are generated for the variable, without taking into account the timerange set
119 /// - default data are generated for the variable, without taking into account the timerange set
120 /// in sciqlop
120 /// in sciqlop
121 auto dateTime = impl->m_TimeController->dateTime();
121 auto dateTime = impl->m_TimeController->dateTime();
122 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
122 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
123 auto identifier = QUuid::createUuid();
123 auto identifier = QUuid::createUuid();
124
124
125 // store the provider
125 // store the provider
126 impl->m_VariableToProviderMap[newVariable] = provider;
126 impl->m_VariableToProviderMap[newVariable] = provider;
127 impl->m_VariableToIdentifier[newVariable] = identifier;
127 impl->m_VariableToIdentifier[newVariable] = identifier;
128
128
129 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
129 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
130 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
130 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
131 {
131 {
132 if (auto variable = varW.lock()) {
132 if (auto variable = varW.lock()) {
133 auto varIdentifier = impl->m_VariableToIdentifier.at(variable);
133 auto varIdentifier = impl->m_VariableToIdentifier.at(variable);
134 if (varIdentifier == identifier) {
134 if (varIdentifier == identifier) {
135 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
135 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
136 variable->setDataSeries(dataSeriesAcquired);
136 variable->setDataSeries(dataSeriesAcquired);
137 }
137 }
138 }
138 }
139 };
139 };
140
140
141 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
141 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
142 this->onRequestDataLoading(newVariable, dateTime);
142 this->onRequestDataLoading(newVariable, dateTime);
143 }
143 }
144 }
144 }
145
145
146 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
146 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
147 {
147 {
148 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
148 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
149 << QThread::currentThread()->objectName();
149 << QThread::currentThread()->objectName();
150 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
150 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
151
151
152 for (const auto &selectedRow : qAsConst(selectedRows)) {
152 for (const auto &selectedRow : qAsConst(selectedRows)) {
153 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
153 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
154 selectedVariable->setDateTime(dateTime);
154 selectedVariable->setDateTime(dateTime);
155 this->onRequestDataLoading(selectedVariable, dateTime);
155 this->onRequestDataLoading(selectedVariable, dateTime);
156 }
156 }
157 }
157 }
158 }
158 }
159
159
160 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
161 {
162 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
163
164 auto end = impl->m_VariableToIdentifier.cend();
165 auto it = std::find_if(impl->m_VariableToIdentifier.cbegin(), end, findReply);
166 if (it != end) {
167 impl->m_VariableModel->setDataProgress(it->first, progress);
168 }
169 }
170
160
171
161 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
172 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
162 const SqpDateTime &dateTime)
173 const SqpDateTime &dateTime)
163 {
174 {
164 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
175 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
165 << QThread::currentThread()->objectName();
176 << QThread::currentThread()->objectName();
166 // we want to load data of the variable for the dateTime.
177 // we want to load data of the variable for the dateTime.
167 // First we check if the cache contains some of them.
178 // First we check if the cache contains some of them.
168 // For the other, we ask the provider to give them.
179 // For the other, we ask the provider to give them.
169 if (variable) {
180 if (variable) {
170
181
171 auto dateTimeListNotInCache
182 auto dateTimeListNotInCache
172 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
183 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
173
184
174 if (!dateTimeListNotInCache.empty()) {
185 if (!dateTimeListNotInCache.empty()) {
175 // Ask the provider for each data on the dateTimeListNotInCache
186 // Ask the provider for each data on the dateTimeListNotInCache
176 auto identifier = impl->m_VariableToIdentifier.at(variable);
187 auto identifier = impl->m_VariableToIdentifier.at(variable);
177 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
188 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
178 identifier, std::move(dateTimeListNotInCache));
189 identifier, std::move(dateTimeListNotInCache));
179 }
190 }
180 else {
191 else {
181 emit variable->updated();
192 emit variable->updated();
182 }
193 }
183 }
194 }
184 else {
195 else {
185 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
196 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
186 }
197 }
187 }
198 }
188
199
189
200
190 void VariableController::initialize()
201 void VariableController::initialize()
191 {
202 {
192 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
203 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
193 impl->m_WorkingMutex.lock();
204 impl->m_WorkingMutex.lock();
194 qCDebug(LOG_VariableController()) << tr("VariableController init END");
205 qCDebug(LOG_VariableController()) << tr("VariableController init END");
195 }
206 }
196
207
197 void VariableController::finalize()
208 void VariableController::finalize()
198 {
209 {
199 impl->m_WorkingMutex.unlock();
210 impl->m_WorkingMutex.unlock();
200 }
211 }
201
212
202 void VariableController::waitForFinish()
213 void VariableController::waitForFinish()
203 {
214 {
204 QMutexLocker locker{&impl->m_WorkingMutex};
215 QMutexLocker locker{&impl->m_WorkingMutex};
205 }
216 }
@@ -1,200 +1,236
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableModel.h>
2 #include <Variable/VariableModel.h>
3
3
4 #include <Data/IDataSeries.h>
4 #include <Data/IDataSeries.h>
5
5
6 #include <QDateTime>
6 #include <QDateTime>
7 #include <QSize>
7 #include <QSize>
8 #include <unordered_map>
8
9
9 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
10 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
10
11
11 namespace {
12 namespace {
12
13
13 // Column indexes
14 // Column indexes
14 const auto NAME_COLUMN = 0;
15 const auto NAME_COLUMN = 0;
15 const auto TSTART_COLUMN = 1;
16 const auto TSTART_COLUMN = 1;
16 const auto TEND_COLUMN = 2;
17 const auto TEND_COLUMN = 2;
17 const auto NB_COLUMNS = 3;
18 const auto NB_COLUMNS = 3;
18
19
19 // Column properties
20 // Column properties
20 const auto DEFAULT_HEIGHT = 25;
21 const auto DEFAULT_HEIGHT = 25;
21 const auto DEFAULT_WIDTH = 100;
22 const auto DEFAULT_WIDTH = 100;
22
23
23 struct ColumnProperties {
24 struct ColumnProperties {
24 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
25 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
25 int height = DEFAULT_HEIGHT)
26 int height = DEFAULT_HEIGHT)
26 : m_Name{name}, m_Width{width}, m_Height{height}
27 : m_Name{name}, m_Width{width}, m_Height{height}
27 {
28 {
28 }
29 }
29
30
30 QString m_Name;
31 QString m_Name;
31 int m_Width;
32 int m_Width;
32 int m_Height;
33 int m_Height;
33 };
34 };
34
35
35 const auto COLUMN_PROPERTIES
36 const auto COLUMN_PROPERTIES
36 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
37 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
37 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
38 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
38 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
39 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
39
40
40 /// Format for datetimes
41 /// Format for datetimes
41 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42
43
44
43 } // namespace
45 } // namespace
44
46
45 struct VariableModel::VariableModelPrivate {
47 struct VariableModel::VariableModelPrivate {
46 /// Variables created in SciQlop
48 /// Variables created in SciQlop
47 std::vector<std::shared_ptr<Variable> > m_Variables;
49 std::vector<std::shared_ptr<Variable> > m_Variables;
50 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
51
52
53 /// Return te row index of the variable
54 int indexOfVariable(Variable *variable) const noexcept;
48 };
55 };
49
56
50 VariableModel::VariableModel(QObject *parent)
57 VariableModel::VariableModel(QObject *parent)
51 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
58 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
52 {
59 {
53 }
60 }
54
61
55 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
62 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
56 const SqpDateTime &dateTime) noexcept
63 const SqpDateTime &dateTime) noexcept
57 {
64 {
58 auto insertIndex = rowCount();
65 auto insertIndex = rowCount();
59 beginInsertRows({}, insertIndex, insertIndex);
66 beginInsertRows({}, insertIndex, insertIndex);
60
67
61 /// @todo For the moment, the other data of the variable is initialized with default values
68 /// @todo For the moment, the other data of the variable is initialized with default values
62 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
69 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
63 QStringLiteral("mission"), dateTime);
70 QStringLiteral("mission"), dateTime);
64
71
65 impl->m_Variables.push_back(variable);
72 impl->m_Variables.push_back(variable);
66 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
73 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
67
74
68 endInsertRows();
75 endInsertRows();
69
76
70 return variable;
77 return variable;
71 }
78 }
72
79
73 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
80 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
74 {
81 {
75 if (!variable) {
82 if (!variable) {
76 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
83 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
77 return;
84 return;
78 }
85 }
79
86
80 // Finds variable in the model
87 // Finds variable in the model
81 auto begin = impl->m_Variables.cbegin();
88 auto begin = impl->m_Variables.cbegin();
82 auto end = impl->m_Variables.cend();
89 auto end = impl->m_Variables.cend();
83 auto it = std::find(begin, end, variable);
90 auto it = std::find(begin, end, variable);
84 if (it != end) {
91 if (it != end) {
85 auto removeIndex = std::distance(begin, it);
92 auto removeIndex = std::distance(begin, it);
86
93
87 // Deletes variable
94 // Deletes variable
88 beginRemoveRows({}, removeIndex, removeIndex);
95 beginRemoveRows({}, removeIndex, removeIndex);
89 impl->m_Variables.erase(it);
96 impl->m_Variables.erase(it);
90 endRemoveRows();
97 endRemoveRows();
91 }
98 }
92 else {
99 else {
93 qCritical(LOG_VariableModel())
100 qCritical(LOG_VariableModel())
94 << tr("Can't delete variable %1 from the model: the variable is not in the model")
101 << tr("Can't delete variable %1 from the model: the variable is not in the model")
95 .arg(variable->name());
102 .arg(variable->name());
96 }
103 }
97 }
104 }
98
105
106
99 std::shared_ptr<Variable> VariableModel::variable(int index) const
107 std::shared_ptr<Variable> VariableModel::variable(int index) const
100 {
108 {
101 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
109 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
102 }
110 }
103
111
112 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
113 {
114
115 impl->m_VariableToProgress[variable] = progress;
116 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
117
118 emit dataChanged(modelIndex, modelIndex);
119 }
120
104 int VariableModel::columnCount(const QModelIndex &parent) const
121 int VariableModel::columnCount(const QModelIndex &parent) const
105 {
122 {
106 Q_UNUSED(parent);
123 Q_UNUSED(parent);
107
124
108 return NB_COLUMNS;
125 return NB_COLUMNS;
109 }
126 }
110
127
111 int VariableModel::rowCount(const QModelIndex &parent) const
128 int VariableModel::rowCount(const QModelIndex &parent) const
112 {
129 {
113 Q_UNUSED(parent);
130 Q_UNUSED(parent);
114
131
115 return impl->m_Variables.size();
132 return impl->m_Variables.size();
116 }
133 }
117
134
118 QVariant VariableModel::data(const QModelIndex &index, int role) const
135 QVariant VariableModel::data(const QModelIndex &index, int role) const
119 {
136 {
120 if (!index.isValid()) {
137 if (!index.isValid()) {
121 return QVariant{};
138 return QVariant{};
122 }
139 }
123
140
124 if (index.row() < 0 || index.row() >= rowCount()) {
141 if (index.row() < 0 || index.row() >= rowCount()) {
125 return QVariant{};
142 return QVariant{};
126 }
143 }
127
144
128 if (role == Qt::DisplayRole) {
145 if (role == Qt::DisplayRole) {
129 if (auto variable = impl->m_Variables.at(index.row()).get()) {
146 if (auto variable = impl->m_Variables.at(index.row()).get()) {
130 /// Lambda function that builds the variant to return for a time value
147 /// Lambda function that builds the variant to return for a time value
131 auto dateTimeVariant = [](double time) {
148 auto dateTimeVariant = [](double time) {
132 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
149 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
133 return dateTime.toString(DATETIME_FORMAT);
150 return dateTime.toString(DATETIME_FORMAT);
134 };
151 };
135
152
136 switch (index.column()) {
153 switch (index.column()) {
137 case NAME_COLUMN:
154 case NAME_COLUMN:
138 return variable->name();
155 return variable->name();
139 case TSTART_COLUMN:
156 case TSTART_COLUMN:
140 return dateTimeVariant(variable->dateTime().m_TStart);
157 return dateTimeVariant(variable->dateTime().m_TStart);
141 case TEND_COLUMN:
158 case TEND_COLUMN:
142 return dateTimeVariant(variable->dateTime().m_TEnd);
159 return dateTimeVariant(variable->dateTime().m_TEnd);
143 default:
160 default:
144 // No action
161 // No action
145 break;
162 break;
146 }
163 }
147
164
148 qWarning(LOG_VariableModel())
165 qWarning(LOG_VariableModel())
149 << tr("Can't get data (unknown column %1)").arg(index.column());
166 << tr("Can't get data (unknown column %1)").arg(index.column());
150 }
167 }
151 else {
168 else {
152 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
169 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
153 }
170 }
154 }
171 }
172 else if (role == VariableRoles::progressRole) {
173 if (auto variable = impl->m_Variables.at(index.row())) {
174
175 auto it = impl->m_VariableToProgress.find(variable);
176 if (it != impl->m_VariableToProgress.cend()) {
177 return it->second;
178 }
179 }
180 }
155
181
156 return QVariant{};
182 return QVariant{};
157 }
183 }
158
184
159 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
185 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
160 {
186 {
161 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
187 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
162 return QVariant{};
188 return QVariant{};
163 }
189 }
164
190
165 if (orientation == Qt::Horizontal) {
191 if (orientation == Qt::Horizontal) {
166 auto propertiesIt = COLUMN_PROPERTIES.find(section);
192 auto propertiesIt = COLUMN_PROPERTIES.find(section);
167 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
193 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
168 // Role is either DisplayRole or SizeHintRole
194 // Role is either DisplayRole or SizeHintRole
169 return (role == Qt::DisplayRole)
195 return (role == Qt::DisplayRole)
170 ? QVariant{propertiesIt->m_Name}
196 ? QVariant{propertiesIt->m_Name}
171 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
197 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
172 }
198 }
173 else {
199 else {
174 qWarning(LOG_VariableModel())
200 qWarning(LOG_VariableModel())
175 << tr("Can't get header data (unknown column %1)").arg(section);
201 << tr("Can't get header data (unknown column %1)").arg(section);
176 }
202 }
177 }
203 }
178
204
179 return QVariant{};
205 return QVariant{};
180 }
206 }
181
207
182 void VariableModel::onVariableUpdated() noexcept
208 void VariableModel::onVariableUpdated() noexcept
183 {
209 {
184 // Finds variable that has been updated in the model
210 // Finds variable that has been updated in the model
185 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
211 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
186 auto begin = std::cbegin(impl->m_Variables);
212 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
187 auto end = std::cend(impl->m_Variables);
213
188 auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) {
214 if (updatedVariableIndex > -1) {
189 return variable.get() == updatedVariable;
215 emit dataChanged(createIndex(updatedVariableIndex, 0),
190 });
216 createIndex(updatedVariableIndex, columnCount() - 1));
191
192 if (it != end) {
193 // Gets the index of the variable in the model: we assume here that views have the same
194 // order as the model
195 auto updateVariableIndex = std::distance(begin, it);
196 emit dataChanged(createIndex(updateVariableIndex, 0),
197 createIndex(updateVariableIndex, columnCount() - 1));
198 }
217 }
199 }
218 }
200 }
219 }
220
221 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
222 {
223 auto begin = std::cbegin(m_Variables);
224 auto end = std::cend(m_Variables);
225 auto it
226 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
227
228 if (it != end) {
229 // Gets the index of the variable in the model: we assume here that views have the same
230 // order as the model
231 return std::distance(begin, it);
232 }
233 else {
234 return -1;
235 }
236 }
@@ -1,52 +1,56
1 #ifndef SCIQLOP_VARIABLEINSPECTORWIDGET_H
1 #ifndef SCIQLOP_VARIABLEINSPECTORWIDGET_H
2 #define SCIQLOP_VARIABLEINSPECTORWIDGET_H
2 #define SCIQLOP_VARIABLEINSPECTORWIDGET_H
3
3
4 #include <QLoggingCategory>
4 #include <QLoggingCategory>
5 #include <QMenu>
5 #include <QMenu>
6 #include <QWidget>
6 #include <QWidget>
7
7
8 #include <memory>
8 #include <memory>
9
9
10 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableInspectorWidget)
10 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableInspectorWidget)
11
11
12 class Variable;
12 class Variable;
13
13
14 class QProgressBarItemDelegate;
15
14 namespace Ui {
16 namespace Ui {
15 class VariableInspectorWidget;
17 class VariableInspectorWidget;
16 } // Ui
18 } // Ui
17
19
18 /**
20 /**
19 * @brief The VariableInspectorWidget class representes represents the variable inspector, from
21 * @brief The VariableInspectorWidget class representes represents the variable inspector, from
20 * which it is possible to view the loaded variables, handle them or trigger their display in
22 * which it is possible to view the loaded variables, handle them or trigger their display in
21 * visualization
23 * visualization
22 */
24 */
23 class VariableInspectorWidget : public QWidget {
25 class VariableInspectorWidget : public QWidget {
24 Q_OBJECT
26 Q_OBJECT
25
27
26 public:
28 public:
27 explicit VariableInspectorWidget(QWidget *parent = 0);
29 explicit VariableInspectorWidget(QWidget *parent = 0);
28 virtual ~VariableInspectorWidget();
30 virtual ~VariableInspectorWidget();
29
31
30 signals:
32 signals:
31 /**
33 /**
32 * Signal emitted before a menu concerning variables is displayed. It is used for other widgets
34 * Signal emitted before a menu concerning variables is displayed. It is used for other widgets
33 * to complete the menu.
35 * to complete the menu.
34 * @param tableMenu the menu to be completed
36 * @param tableMenu the menu to be completed
35 * @param variables the variables concerned by the menu
37 * @param variables the variables concerned by the menu
36 * @remarks To make the dynamic addition of menus work, the connections to this signal must be
38 * @remarks To make the dynamic addition of menus work, the connections to this signal must be
37 * in Qt :: DirectConnection
39 * in Qt :: DirectConnection
38 */
40 */
39 void tableMenuAboutToBeDisplayed(QMenu *tableMenu,
41 void tableMenuAboutToBeDisplayed(QMenu *tableMenu,
40 const QVector<std::shared_ptr<Variable> > &variables);
42 const QVector<std::shared_ptr<Variable> > &variables);
41
43
42 private:
44 private:
43 Ui::VariableInspectorWidget *ui;
45 Ui::VariableInspectorWidget *ui;
44
46
47 QProgressBarItemDelegate *m_ProgressBarItemDelegate;
48
45 private slots:
49 private slots:
46 /// Slot called when right clicking on an variable in the table (displays a menu)
50 /// Slot called when right clicking on an variable in the table (displays a menu)
47 void onTableMenuRequested(const QPoint &pos) noexcept;
51 void onTableMenuRequested(const QPoint &pos) noexcept;
48 /// Refreshes instantly the variable view
52 /// Refreshes instantly the variable view
49 void refresh() noexcept;
53 void refresh() noexcept;
50 };
54 };
51
55
52 #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H
56 #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H
@@ -1,140 +1,145
1 #include "SqpApplication.h"
1 #include "SqpApplication.h"
2
2
3 #include <Data/IDataProvider.h>
3 #include <Data/IDataProvider.h>
4 #include <DataSource/DataSourceController.h>
4 #include <DataSource/DataSourceController.h>
5 #include <Network/NetworkController.h>
5 #include <Network/NetworkController.h>
6 #include <QThread>
6 #include <QThread>
7 #include <Time/TimeController.h>
7 #include <Time/TimeController.h>
8 #include <Variable/Variable.h>
8 #include <Variable/Variable.h>
9 #include <Variable/VariableController.h>
9 #include <Variable/VariableController.h>
10 #include <Visualization/VisualizationController.h>
10 #include <Visualization/VisualizationController.h>
11
11
12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
13
13
14 class SqpApplication::SqpApplicationPrivate {
14 class SqpApplication::SqpApplicationPrivate {
15 public:
15 public:
16 SqpApplicationPrivate()
16 SqpApplicationPrivate()
17 : m_DataSourceController{std::make_unique<DataSourceController>()},
17 : m_DataSourceController{std::make_unique<DataSourceController>()},
18 m_NetworkController{std::make_unique<NetworkController>()},
18 m_NetworkController{std::make_unique<NetworkController>()},
19 m_TimeController{std::make_unique<TimeController>()},
19 m_TimeController{std::make_unique<TimeController>()},
20 m_VariableController{std::make_unique<VariableController>()},
20 m_VariableController{std::make_unique<VariableController>()},
21 m_VisualizationController{std::make_unique<VisualizationController>()}
21 m_VisualizationController{std::make_unique<VisualizationController>()}
22 {
22 {
23 // /////////////////////////////// //
23 // /////////////////////////////// //
24 // Connections between controllers //
24 // Connections between controllers //
25 // /////////////////////////////// //
25 // /////////////////////////////// //
26
26
27 // VariableController <-> DataSourceController
27 // VariableController <-> DataSourceController
28 connect(m_DataSourceController.get(),
28 connect(m_DataSourceController.get(),
29 SIGNAL(variableCreationRequested(const QString &, std::shared_ptr<IDataProvider>)),
29 SIGNAL(variableCreationRequested(const QString &, std::shared_ptr<IDataProvider>)),
30 m_VariableController.get(),
30 m_VariableController.get(),
31 SLOT(createVariable(const QString &, std::shared_ptr<IDataProvider>)));
31 SLOT(createVariable(const QString &, std::shared_ptr<IDataProvider>)));
32
32
33 // VariableController <-> VisualizationController
33 // VariableController <-> VisualizationController
34 connect(m_VariableController.get(),
34 connect(m_VariableController.get(),
35 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
35 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
36 m_VisualizationController.get(),
36 m_VisualizationController.get(),
37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
38
38
39
39
40 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
40 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
41 m_NetworkController->moveToThread(&m_NetworkControllerThread);
41 m_NetworkController->moveToThread(&m_NetworkControllerThread);
42 m_VariableController->moveToThread(&m_VariableControllerThread);
42 m_VariableController->moveToThread(&m_VariableControllerThread);
43 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
43 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
44
44
45
45
46 // Additionnal init
46 // Additionnal init
47 m_VariableController->setTimeController(m_TimeController.get());
47 m_VariableController->setTimeController(m_TimeController.get());
48 }
48 }
49
49
50 virtual ~SqpApplicationPrivate()
50 virtual ~SqpApplicationPrivate()
51 {
51 {
52 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
52 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
53 m_DataSourceControllerThread.quit();
53 m_DataSourceControllerThread.quit();
54 m_DataSourceControllerThread.wait();
54 m_DataSourceControllerThread.wait();
55
55
56 m_NetworkControllerThread.quit();
56 m_NetworkControllerThread.quit();
57 m_NetworkControllerThread.wait();
57 m_NetworkControllerThread.wait();
58
58
59 m_VariableControllerThread.quit();
59 m_VariableControllerThread.quit();
60 m_VariableControllerThread.wait();
60 m_VariableControllerThread.wait();
61
61
62 m_VisualizationControllerThread.quit();
62 m_VisualizationControllerThread.quit();
63 m_VisualizationControllerThread.wait();
63 m_VisualizationControllerThread.wait();
64 }
64 }
65
65
66 std::unique_ptr<DataSourceController> m_DataSourceController;
66 std::unique_ptr<DataSourceController> m_DataSourceController;
67 std::unique_ptr<VariableController> m_VariableController;
67 std::unique_ptr<VariableController> m_VariableController;
68 std::unique_ptr<TimeController> m_TimeController;
68 std::unique_ptr<TimeController> m_TimeController;
69 std::unique_ptr<NetworkController> m_NetworkController;
69 std::unique_ptr<NetworkController> m_NetworkController;
70 std::unique_ptr<VisualizationController> m_VisualizationController;
70 std::unique_ptr<VisualizationController> m_VisualizationController;
71 QThread m_DataSourceControllerThread;
71 QThread m_DataSourceControllerThread;
72 QThread m_NetworkControllerThread;
72 QThread m_NetworkControllerThread;
73 QThread m_VariableControllerThread;
73 QThread m_VariableControllerThread;
74 QThread m_VisualizationControllerThread;
74 QThread m_VisualizationControllerThread;
75 };
75 };
76
76
77
77
78 SqpApplication::SqpApplication(int &argc, char **argv)
78 SqpApplication::SqpApplication(int &argc, char **argv)
79 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
79 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
80 {
80 {
81 qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread();
81 qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread();
82
82
83 connect(&impl->m_DataSourceControllerThread, &QThread::started,
83 connect(&impl->m_DataSourceControllerThread, &QThread::started,
84 impl->m_DataSourceController.get(), &DataSourceController::initialize);
84 impl->m_DataSourceController.get(), &DataSourceController::initialize);
85 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
85 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
86 impl->m_DataSourceController.get(), &DataSourceController::finalize);
86 impl->m_DataSourceController.get(), &DataSourceController::finalize);
87
87
88 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
88 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
89 &NetworkController::initialize);
89 &NetworkController::initialize);
90 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
90 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
91 &NetworkController::finalize);
91 &NetworkController::finalize);
92
92
93 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
93 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
94 &VariableController::initialize);
94 &VariableController::initialize);
95 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
95 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
96 &VariableController::finalize);
96 &VariableController::finalize);
97
97
98 connect(&impl->m_VisualizationControllerThread, &QThread::started,
98 connect(&impl->m_VisualizationControllerThread, &QThread::started,
99 impl->m_VisualizationController.get(), &VisualizationController::initialize);
99 impl->m_VisualizationController.get(), &VisualizationController::initialize);
100 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
100 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
101 impl->m_VisualizationController.get(), &VisualizationController::finalize);
101 impl->m_VisualizationController.get(), &VisualizationController::finalize);
102
102
103 impl->m_DataSourceControllerThread.start();
103 impl->m_DataSourceControllerThread.start();
104 impl->m_NetworkControllerThread.start();
104 impl->m_NetworkControllerThread.start();
105 impl->m_VariableControllerThread.start();
105 impl->m_VariableControllerThread.start();
106 impl->m_VisualizationControllerThread.start();
106 impl->m_VisualizationControllerThread.start();
107
108 // Core connections:
109 // NetworkController <-> VariableController
110 connect(&sqpApp->networkController(), &NetworkController::replyDownloadProgress,
111 &sqpApp->variableController(), &VariableController::onVariableRetrieveDataInProgress);
107 }
112 }
108
113
109 SqpApplication::~SqpApplication()
114 SqpApplication::~SqpApplication()
110 {
115 {
111 }
116 }
112
117
113 void SqpApplication::initialize()
118 void SqpApplication::initialize()
114 {
119 {
115 }
120 }
116
121
117 DataSourceController &SqpApplication::dataSourceController() noexcept
122 DataSourceController &SqpApplication::dataSourceController() noexcept
118 {
123 {
119 return *impl->m_DataSourceController;
124 return *impl->m_DataSourceController;
120 }
125 }
121
126
122 NetworkController &SqpApplication::networkController() noexcept
127 NetworkController &SqpApplication::networkController() noexcept
123 {
128 {
124 return *impl->m_NetworkController;
129 return *impl->m_NetworkController;
125 }
130 }
126
131
127 TimeController &SqpApplication::timeController() noexcept
132 TimeController &SqpApplication::timeController() noexcept
128 {
133 {
129 return *impl->m_TimeController;
134 return *impl->m_TimeController;
130 }
135 }
131
136
132 VariableController &SqpApplication::variableController() noexcept
137 VariableController &SqpApplication::variableController() noexcept
133 {
138 {
134 return *impl->m_VariableController;
139 return *impl->m_VariableController;
135 }
140 }
136
141
137 VisualizationController &SqpApplication::visualizationController() noexcept
142 VisualizationController &SqpApplication::visualizationController() noexcept
138 {
143 {
139 return *impl->m_VisualizationController;
144 return *impl->m_VisualizationController;
140 }
145 }
@@ -1,101 +1,140
1 #include <Variable/VariableController.h>
1 #include <Variable/VariableController.h>
2 #include <Variable/VariableInspectorWidget.h>
2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableMenuHeaderWidget.h>
3 #include <Variable/VariableMenuHeaderWidget.h>
4 #include <Variable/VariableModel.h>
4 #include <Variable/VariableModel.h>
5
5
6 #include <ui_VariableInspectorWidget.h>
6 #include <ui_VariableInspectorWidget.h>
7
7
8 #include <QSortFilterProxyModel>
8 #include <QSortFilterProxyModel>
9 #include <QStyledItemDelegate>
9 #include <QWidgetAction>
10 #include <QWidgetAction>
10
11
11 #include <SqpApplication.h>
12 #include <SqpApplication.h>
12
13
13 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
14 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
14
15
16
17 class QProgressBarItemDelegate : public QStyledItemDelegate {
18
19 public:
20 QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
21
22 void paint(QPainter *painter, const QStyleOptionViewItem &option,
23 const QModelIndex &index) const
24 {
25 auto data = index.data(Qt::DisplayRole);
26 auto progressData = index.data(VariableRoles::progressRole);
27 if (data.isValid() && progressData.isValid()) {
28 auto name = data.value<QString>();
29 auto progress = progressData.value<double>();
30 if (progress >= 0) {
31 auto progressBarOption = QStyleOptionProgressBar{};
32 progressBarOption.rect = option.rect;
33 progressBarOption.minimum = 0;
34 progressBarOption.maximum = 100;
35 progressBarOption.progress = progress;
36 progressBarOption.text
37 = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%");
38 progressBarOption.textVisible = true;
39 progressBarOption.textAlignment = Qt::AlignCenter;
40
41 QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption,
42 painter);
43 }
44 }
45 else {
46 QStyledItemDelegate::paint(painter, option, index);
47 }
48 }
49 };
50
15 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
51 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
16 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
52 : QWidget{parent},
53 ui{new Ui::VariableInspectorWidget},
54 m_ProgressBarItemDelegate{new QProgressBarItemDelegate(this)}
17 {
55 {
18 ui->setupUi(this);
56 ui->setupUi(this);
19
57
20 // Sets model for table
58 // Sets model for table
21 // auto sortFilterModel = new QSortFilterProxyModel{this};
59 // auto sortFilterModel = new QSortFilterProxyModel{this};
22 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
60 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
23
61
24 auto variableModel = sqpApp->variableController().variableModel();
62 auto variableModel = sqpApp->variableController().variableModel();
25 ui->tableView->setModel(variableModel);
63 ui->tableView->setModel(variableModel);
26
64
27 // Adds extra signal/slot between view and model, so the view can be updated instantly when
65 // Adds extra signal/slot between view and model, so the view can be updated instantly when
28 // there is a change of data in the model
66 // there is a change of data in the model
29 connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
67 connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
30 SLOT(refresh()));
68 SLOT(refresh()));
31
69
32 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
70 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
71 ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate);
33
72
34 // Fixes column sizes
73 // Fixes column sizes
35 auto model = ui->tableView->model();
74 auto model = ui->tableView->model();
36 const auto count = model->columnCount();
75 const auto count = model->columnCount();
37 for (auto i = 0; i < count; ++i) {
76 for (auto i = 0; i < count; ++i) {
38 ui->tableView->setColumnWidth(
77 ui->tableView->setColumnWidth(
39 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
78 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
40 }
79 }
41
80
42 // Sets selection options
81 // Sets selection options
43 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
82 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
44 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
83 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
45
84
46 // Connection to show a menu when right clicking on the tree
85 // Connection to show a menu when right clicking on the tree
47 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
86 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
48 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
87 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
49 &VariableInspectorWidget::onTableMenuRequested);
88 &VariableInspectorWidget::onTableMenuRequested);
50 }
89 }
51
90
52 VariableInspectorWidget::~VariableInspectorWidget()
91 VariableInspectorWidget::~VariableInspectorWidget()
53 {
92 {
54 delete ui;
93 delete ui;
55 }
94 }
56
95
57 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
96 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
58 {
97 {
59 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
98 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
60
99
61 // Gets the model to retrieve the underlying selected variables
100 // Gets the model to retrieve the underlying selected variables
62 auto model = sqpApp->variableController().variableModel();
101 auto model = sqpApp->variableController().variableModel();
63 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
102 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
64 for (const auto &selectedRow : qAsConst(selectedRows)) {
103 for (const auto &selectedRow : qAsConst(selectedRows)) {
65 if (auto selectedVariable = model->variable(selectedRow.row())) {
104 if (auto selectedVariable = model->variable(selectedRow.row())) {
66 selectedVariables.push_back(selectedVariable);
105 selectedVariables.push_back(selectedVariable);
67 }
106 }
68 }
107 }
69
108
70 QMenu tableMenu{};
109 QMenu tableMenu{};
71
110
72 // Emits a signal so that potential receivers can populate the menu before displaying it
111 // Emits a signal so that potential receivers can populate the menu before displaying it
73 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
112 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
74
113
75 // Adds menu-specific actions
114 // Adds menu-specific actions
76 if (!selectedVariables.isEmpty()) {
115 if (!selectedVariables.isEmpty()) {
77 // 'Delete' action
116 // 'Delete' action
78 auto deleteFun = [&selectedVariables]() {
117 auto deleteFun = [&selectedVariables]() {
79 sqpApp->variableController().deleteVariables(selectedVariables);
118 sqpApp->variableController().deleteVariables(selectedVariables);
80 };
119 };
81
120
82 tableMenu.addSeparator();
121 tableMenu.addSeparator();
83 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
122 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
84 }
123 }
85
124
86 if (!tableMenu.isEmpty()) {
125 if (!tableMenu.isEmpty()) {
87 // Generates menu header (inserted before first action)
126 // Generates menu header (inserted before first action)
88 auto firstAction = tableMenu.actions().first();
127 auto firstAction = tableMenu.actions().first();
89 auto headerAction = new QWidgetAction{&tableMenu};
128 auto headerAction = new QWidgetAction{&tableMenu};
90 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
129 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
91 tableMenu.insertAction(firstAction, headerAction);
130 tableMenu.insertAction(firstAction, headerAction);
92
131
93 // Displays menu
132 // Displays menu
94 tableMenu.exec(mapToGlobal(pos));
133 tableMenu.exec(mapToGlobal(pos));
95 }
134 }
96 }
135 }
97
136
98 void VariableInspectorWidget::refresh() noexcept
137 void VariableInspectorWidget::refresh() noexcept
99 {
138 {
100 ui->tableView->viewport()->update();
139 ui->tableView->viewport()->update();
101 }
140 }
General Comments 0
You need to be logged in to leave comments. Login now