##// END OF EJS Templates
Merge branch 'feature/AbortDownload' into develop
perrinel -
r424:0d257d28af1c merge
parent child
Show More
@@ -34,6 +34,11 public:
34 */
34 */
35 virtual void requestDataLoading(QUuid identifier, const DataProviderParameters &parameters) = 0;
35 virtual void requestDataLoading(QUuid identifier, const DataProviderParameters &parameters) = 0;
36
36
37 /**
38 * @brief requestDataAborting stop data loading of the data identified by identifier
39 */
40 virtual void requestDataAborting(QUuid identifier) = 0;
41
37 signals:
42 signals:
38 /**
43 /**
39 * @brief dataProvided send dataSeries under dateTime and that corresponds of the data
44 * @brief dataProvided send dataSeries under dateTime and that corresponds of the data
@@ -50,6 +50,11 public:
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 /**
54 * @brief abort the variable retrieve data progression
55 */
56 void abortProgress(std::shared_ptr<Variable> variable);
57
53 signals:
58 signals:
54 /// Signal emitted when a variable is about to be deleted from the controller
59 /// Signal emitted when a variable is about to be deleted from the controller
55 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
60 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
@@ -72,6 +77,8 public slots:
72
77
73 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
78 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
74
79
80 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
81
75 void initialize();
82 void initialize();
76 void finalize();
83 void finalize();
77
84
@@ -22,6 +22,7 class Variable;
22 * @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
23 */
23 */
24 class VariableModel : public QAbstractTableModel {
24 class VariableModel : public QAbstractTableModel {
25 Q_OBJECT
25 public:
26 public:
26 explicit VariableModel(QObject *parent = nullptr);
27 explicit VariableModel(QObject *parent = nullptr);
27
28
@@ -46,6 +47,7 public:
46
47
47 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
48 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
48
49
50
49 // /////////////////////////// //
51 // /////////////////////////// //
50 // QAbstractTableModel methods //
52 // QAbstractTableModel methods //
51 // /////////////////////////// //
53 // /////////////////////////// //
@@ -56,6 +58,12 public:
56 virtual QVariant headerData(int section, Qt::Orientation orientation,
58 virtual QVariant headerData(int section, Qt::Orientation orientation,
57 int role = Qt::DisplayRole) const override;
59 int role = Qt::DisplayRole) const override;
58
60
61
62 void abortProgress(const QModelIndex &index);
63
64 signals:
65 void abortProgessRequested(std::shared_ptr<Variable> variable);
66
59 private:
67 private:
60 class VariableModelPrivate;
68 class VariableModelPrivate;
61 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
69 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
@@ -4,6 +4,7
4 #include <QNetworkAccessManager>
4 #include <QNetworkAccessManager>
5 #include <QNetworkReply>
5 #include <QNetworkReply>
6 #include <QNetworkRequest>
6 #include <QNetworkRequest>
7 #include <QReadWriteLock>
7 #include <QThread>
8 #include <QThread>
8
9
9 #include <unordered_map>
10 #include <unordered_map>
@@ -14,8 +15,13 struct NetworkController::NetworkControllerPrivate {
14 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 QMutex m_WorkingMutex;
16 QMutex m_WorkingMutex;
16
17
18 QReadWriteLock m_Lock;
17 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
19 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
18 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
20 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
21
22 void lockRead() { m_Lock.lockForRead(); }
23 void lockWrite() { m_Lock.lockForWrite(); }
24 void unlock() { m_Lock.unlock(); }
19 };
25 };
20
26
21 NetworkController::NetworkController(QObject *parent)
27 NetworkController::NetworkController(QObject *parent)
@@ -26,30 +32,50 NetworkController::NetworkController(QObject *parent)
26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
32 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
27 std::function<void(QNetworkReply *, QUuid)> callback)
33 std::function<void(QNetworkReply *, QUuid)> callback)
28 {
34 {
29 qCInfo(LOG_NetworkController()) << tr("NetworkController registered")
30 << QThread::currentThread();
31 auto reply = impl->m_AccessManager->get(request);
35 auto reply = impl->m_AccessManager->get(request);
36 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
37 << QThread::currentThread() << reply;
32
38
33 // Store the couple reply id
39 // Store the couple reply id
40 impl->lockWrite();
34 impl->m_NetworkReplyToVariableId[reply] = identifier;
41 impl->m_NetworkReplyToVariableId[reply] = identifier;
42 impl->unlock();
35
43
36 auto onReplyFinished = [reply, this, identifier, callback]() {
44 auto onReplyFinished = [reply, this, identifier, callback]() {
37
45
38 qCInfo(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
46 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
39 << QThread::currentThread();
47 << QThread::currentThread() << reply;
48 impl->lockRead();
40 auto it = impl->m_NetworkReplyToVariableId.find(reply);
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
50 impl->unlock();
41 if (it != impl->m_NetworkReplyToVariableId.cend()) {
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
52 impl->lockWrite();
53 impl->m_NetworkReplyToVariableId.erase(reply);
54 impl->unlock();
55 // Deletes reply
42 callback(reply, identifier);
56 callback(reply, identifier);
57 reply->deleteLater();
58
59 emit this->replyDownloadProgress(identifier, 0);
43 }
60 }
61
62 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
63 << QThread::currentThread() << reply;
44 };
64 };
45
65
46 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
66 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
47
67
48 double progress = (bytesRead * 100.0) / totalBytes;
68 double progress = (bytesRead * 100.0) / totalBytes;
69 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
70 << QThread::currentThread() << reply;
71 impl->lockRead();
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
72 auto it = impl->m_NetworkReplyToVariableId.find(reply);
73 impl->unlock();
50 if (it != impl->m_NetworkReplyToVariableId.cend()) {
74 if (it != impl->m_NetworkReplyToVariableId.cend()) {
51 emit this->replyDownloadProgress(it->second, progress);
75 emit this->replyDownloadProgress(it->second, progress);
52 }
76 }
77 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
78 << QThread::currentThread() << reply;
53 };
79 };
54
80
55
81
@@ -73,12 +99,19 void NetworkController::finalize()
73 void NetworkController::onReplyCanceled(QUuid identifier)
99 void NetworkController::onReplyCanceled(QUuid identifier)
74 {
100 {
75 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
101 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
102 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
103 << QThread::currentThread();
104
76
105
106 impl->lockRead();
77 auto end = impl->m_NetworkReplyToVariableId.cend();
107 auto end = impl->m_NetworkReplyToVariableId.cend();
78 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
108 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
109 impl->unlock();
79 if (it != end) {
110 if (it != end) {
80 it->first->abort();
111 it->first->abort();
81 }
112 }
113 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
114 << QThread::currentThread();
82 }
115 }
83
116
84 void NetworkController::waitForFinish()
117 void NetworkController::waitForFinish()
@@ -46,6 +46,9 VariableController::VariableController(QObject *parent)
46 {
46 {
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
48 << QThread::currentThread();
48 << QThread::currentThread();
49
50 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
51 &VariableController::onAbortProgressRequested);
49 }
52 }
50
53
51 VariableController::~VariableController()
54 VariableController::~VariableController()
@@ -105,6 +108,10 void VariableController::deleteVariables(
105 }
108 }
106 }
109 }
107
110
111 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
112 {
113 }
114
108 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
115 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
109 std::shared_ptr<IDataProvider> provider) noexcept
116 std::shared_ptr<IDataProvider> provider) noexcept
110 {
117 {
@@ -166,6 +173,22 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, doub
166 }
173 }
167 }
174 }
168
175
176 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
177 {
178 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
179 << QThread::currentThread()->objectName();
180
181 auto it = impl->m_VariableToIdentifierMap.find(variable);
182 if (it != impl->m_VariableToIdentifierMap.cend()) {
183 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
184 }
185 else {
186 qCWarning(LOG_VariableController())
187 << tr("Aborting progression of inexistant variable detected !!!")
188 << QThread::currentThread()->objectName();
189 }
190 }
191
169
192
170 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
193 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
171 const SqpDateTime &dateTime)
194 const SqpDateTime &dateTime)
@@ -205,6 +205,13 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int
205 return QVariant{};
205 return QVariant{};
206 }
206 }
207
207
208 void VariableModel::abortProgress(const QModelIndex &index)
209 {
210 if (auto variable = impl->m_Variables.at(index.row())) {
211 emit abortProgessRequested(variable);
212 }
213 }
214
208 void VariableModel::onVariableUpdated() noexcept
215 void VariableModel::onVariableUpdated() noexcept
209 {
216 {
210 // Finds variable that has been updated in the model
217 // Finds variable that has been updated in the model
@@ -5,6 +5,7
5
5
6 #include <ui_VariableInspectorWidget.h>
6 #include <ui_VariableInspectorWidget.h>
7
7
8 #include <QMouseEvent>
8 #include <QSortFilterProxyModel>
9 #include <QSortFilterProxyModel>
9 #include <QStyledItemDelegate>
10 #include <QStyledItemDelegate>
10 #include <QWidgetAction>
11 #include <QWidgetAction>
@@ -27,9 +28,12 public:
27 if (data.isValid() && progressData.isValid()) {
28 if (data.isValid() && progressData.isValid()) {
28 auto name = data.value<QString>();
29 auto name = data.value<QString>();
29 auto progress = progressData.value<double>();
30 auto progress = progressData.value<double>();
30 if (progress >= 0) {
31 if (progress > 0) {
32 auto cancelButtonWidth = 20;
31 auto progressBarOption = QStyleOptionProgressBar{};
33 auto progressBarOption = QStyleOptionProgressBar{};
32 progressBarOption.rect = option.rect;
34 auto progressRect = option.rect;
35 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
36 progressBarOption.rect = progressRect;
33 progressBarOption.minimum = 0;
37 progressBarOption.minimum = 0;
34 progressBarOption.maximum = 100;
38 progressBarOption.maximum = 100;
35 progressBarOption.progress = progress;
39 progressBarOption.progress = progress;
@@ -38,14 +42,70 public:
38 progressBarOption.textVisible = true;
42 progressBarOption.textVisible = true;
39 progressBarOption.textAlignment = Qt::AlignCenter;
43 progressBarOption.textAlignment = Qt::AlignCenter;
40
44
45
41 QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption,
46 QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption,
42 painter);
47 painter);
48
49 // Cancel button
50 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
51 option.rect.height());
52 auto buttonOption = QStyleOptionButton{};
53 buttonOption.rect = buttonRect;
54 buttonOption.text = "X";
55
56 QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
57 }
58 else {
59 QStyledItemDelegate::paint(painter, option, index);
43 }
60 }
44 }
61 }
45 else {
62 else {
46 QStyledItemDelegate::paint(painter, option, index);
63 QStyledItemDelegate::paint(painter, option, index);
47 }
64 }
48 }
65 }
66
67 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
68 const QModelIndex &index)
69 {
70 if (event->type() == QEvent::MouseButtonRelease) {
71 auto data = index.data(Qt::DisplayRole);
72 auto progressData = index.data(VariableRoles::ProgressRole);
73 if (data.isValid() && progressData.isValid()) {
74 auto cancelButtonWidth = 20;
75 auto progressRect = option.rect;
76 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
77 // Cancel button
78 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
79 option.rect.height());
80
81 QMouseEvent *e = (QMouseEvent *)event;
82 int clickX = e->x();
83 int clickY = e->y();
84
85 auto x = buttonRect.left(); // the X coordinate
86 auto y = buttonRect.top(); // the Y coordinate
87 auto w = buttonRect.width(); // button width
88 auto h = buttonRect.height(); // button height
89
90 if (clickX > x && clickX < x + w) {
91 if (clickY > y && clickY < y + h) {
92 qCritical(LOG_VariableInspectorWidget()) << tr("editorEvent CLIC");
93 auto variableModel = sqpApp->variableController().variableModel();
94 variableModel->abortProgress(index);
95 }
96 }
97 else {
98 QStyledItemDelegate::editorEvent(event, model, option, index);
99 }
100 }
101 else {
102 QStyledItemDelegate::editorEvent(event, model, option, index);
103 }
104 }
105 else {
106 QStyledItemDelegate::editorEvent(event, model, option, index);
107 }
108 }
49 };
109 };
50
110
51 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
111 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
@@ -21,6 +21,8 public:
21
21
22 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
22 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
23
23
24 void requestDataAborting(QUuid identifier) override;
25
24 private:
26 private:
25 void retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data);
27 void retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data);
26 };
28 };
@@ -61,6 +61,14 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters
61 }
61 }
62 }
62 }
63
63
64 void AmdaProvider::requestDataAborting(QUuid identifier)
65 {
66 if (auto app = sqpApp) {
67 auto &networkController = app->networkController();
68 networkController.onReplyCanceled(identifier);
69 }
70 }
71
64 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data)
72 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data)
65 {
73 {
66 // Retrieves product ID from data: if the value is invalid, no request is made
74 // Retrieves product ID from data: if the value is invalid, no request is made
@@ -102,16 +110,13 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const
102 }
110 }
103 }
111 }
104
112
105 // Deletes reply
113
106 reply->deleteLater();
107 reply = nullptr;
108 };
114 };
109 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
115 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
110 QUuid dataId) noexcept {
116 QUuid dataId) noexcept {
111
117
112 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
118 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
113 // Deletes old reply
119
114 reply->deleteLater();
115
120
116 // Executes request for downloading file //
121 // Executes request for downloading file //
117
122
@@ -17,6 +17,9 public:
17 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
17 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
18
18
19
19
20 void requestDataAborting(QUuid identifier) override;
21
22
20 private:
23 private:
21 /// @sa IDataProvider::retrieveData()
24 /// @sa IDataProvider::retrieveData()
22 std::shared_ptr<IDataSeries> retrieveData(const SqpDateTime &dateTime) const;
25 std::shared_ptr<IDataSeries> retrieveData(const SqpDateTime &dateTime) const;
@@ -46,3 +46,8 void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParamete
46 emit dataProvided(token, scalarSeries, dateTime);
46 emit dataProvided(token, scalarSeries, dateTime);
47 }
47 }
48 }
48 }
49
50 void CosinusProvider::requestDataAborting(QUuid identifier)
51 {
52 // TODO
53 }
General Comments 0
You need to be logged in to leave comments. Login now