##// END OF EJS Templates
Add implementation of progress bar on variable inspector connected to...
perrinel -
r401:5601a2d8f3ef
parent child
Show More
@@ -25,13 +25,16 public:
25 25 void finalize();
26 26
27 27 public slots:
28 /// Execute request and call callback when the reply is finished. Identifier is attached to the
29 /// callback
28 30 void onProcessRequested(const QNetworkRequest &request, QUuid identifier,
29 31 std::function<void(QNetworkReply *, QUuid)> callback);
32 /// Cancel the request of identifier
30 33 void onReplyCanceled(QUuid identifier);
31 34
32 35 signals:
33 36 void replyFinished(QNetworkReply *reply, QUuid identifier);
34 void replyDownloadProgress(QUuid identifier);
37 void replyDownloadProgress(QUuid identifier, double progress);
35 38
36 39 private:
37 40 void waitForFinish();
@@ -67,6 +67,9 public slots:
67 67 /// Update the temporal parameters of every selected variable to dateTime
68 68 void onDateTimeOnSelection(const SqpDateTime &dateTime);
69 69
70
71 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
72
70 73 void initialize();
71 74 void finalize();
72 75
@@ -12,6 +12,9
12 12
13 13 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
14 14
15 enum VariableRoles { progressRole = Qt::UserRole };
16
17
15 18 class IDataSeries;
16 19 class Variable;
17 20
@@ -37,11 +40,15 public:
37 40 */
38 41 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
39 42
43
40 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 49 // QAbstractTableModel methods //
44 50 // /////////////////////////// //
51
45 52 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
46 53 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
47 54 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
@@ -26,8 +26,8 NetworkController::NetworkController(QObject *parent)
26 26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
27 27 std::function<void(QNetworkReply *, QUuid)> callback)
28 28 {
29 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
30 << QThread::currentThread();
29 qCInfo(LOG_NetworkController()) << tr("NetworkController registered")
30 << QThread::currentThread();
31 31 auto reply = impl->m_AccessManager->get(request);
32 32
33 33 // Store the couple reply id
@@ -35,27 +35,26 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid
35 35
36 36 auto onReplyFinished = [reply, this, identifier, callback]() {
37 37
38 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
39 << QThread::currentThread();
38 qCInfo(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
39 << QThread::currentThread();
40 40 auto it = impl->m_NetworkReplyToVariableId.find(reply);
41 41 if (it != impl->m_NetworkReplyToVariableId.cend()) {
42 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")
49 << QThread::currentThread();
48 double progress = (bytesRead * 100.0) / totalBytes;
50 49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
51 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 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 60 void NetworkController::initialize()
@@ -157,6 +157,17 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
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 172 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
162 173 const SqpDateTime &dateTime)
@@ -5,6 +5,7
5 5
6 6 #include <QDateTime>
7 7 #include <QSize>
8 #include <unordered_map>
8 9
9 10 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
10 11
@@ -40,11 +41,17 const auto COLUMN_PROPERTIES
40 41 /// Format for datetimes
41 42 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42 43
44
43 45 } // namespace
44 46
45 47 struct VariableModel::VariableModelPrivate {
46 48 /// Variables created in SciQlop
47 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 57 VariableModel::VariableModel(QObject *parent)
@@ -96,11 +103,21 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
96 103 }
97 104 }
98 105
106
99 107 std::shared_ptr<Variable> VariableModel::variable(int index) const
100 108 {
101 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 121 int VariableModel::columnCount(const QModelIndex &parent) const
105 122 {
106 123 Q_UNUSED(parent);
@@ -152,6 +169,15 QVariant VariableModel::data(const QModelIndex &index, int role) const
152 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 182 return QVariant{};
157 183 }
@@ -183,18 +209,28 void VariableModel::onVariableUpdated() noexcept
183 209 {
184 210 // Finds variable that has been updated in the model
185 211 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
186 auto begin = std::cbegin(impl->m_Variables);
187 auto end = std::cend(impl->m_Variables);
188 auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) {
189 return variable.get() == updatedVariable;
190 });
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));
212 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
213
214 if (updatedVariableIndex > -1) {
215 emit dataChanged(createIndex(updatedVariableIndex, 0),
216 createIndex(updatedVariableIndex, 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 }
@@ -11,6 +11,8 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableInspectorWidget)
11 11
12 12 class Variable;
13 13
14 class QProgressBarItemDelegate;
15
14 16 namespace Ui {
15 17 class VariableInspectorWidget;
16 18 } // Ui
@@ -42,6 +44,8 signals:
42 44 private:
43 45 Ui::VariableInspectorWidget *ui;
44 46
47 QProgressBarItemDelegate *m_ProgressBarItemDelegate;
48
45 49 private slots:
46 50 /// Slot called when right clicking on an variable in the table (displays a menu)
47 51 void onTableMenuRequested(const QPoint &pos) noexcept;
@@ -104,6 +104,11 SqpApplication::SqpApplication(int &argc, char **argv)
104 104 impl->m_NetworkControllerThread.start();
105 105 impl->m_VariableControllerThread.start();
106 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 114 SqpApplication::~SqpApplication()
@@ -6,14 +6,52
6 6 #include <ui_VariableInspectorWidget.h>
7 7
8 8 #include <QSortFilterProxyModel>
9 #include <QStyledItemDelegate>
9 10 #include <QWidgetAction>
10 11
11 12 #include <SqpApplication.h>
12 13
13 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 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 56 ui->setupUi(this);
19 57
@@ -30,6 +68,7 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
30 68 SLOT(refresh()));
31 69
32 70 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
71 ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate);
33 72
34 73 // Fixes column sizes
35 74 auto model = ui->tableView->model();
General Comments 0
You need to be logged in to leave comments. Login now