@@ -1,210 +1,222 | |||||
|
1 | #include <Variable/RenameVariableDialog.h> | |||
|
2 | #include <Variable/Variable.h> | |||
1 | #include <Variable/VariableController.h> |
|
3 | #include <Variable/VariableController.h> | |
2 | #include <Variable/VariableInspectorWidget.h> |
|
4 | #include <Variable/VariableInspectorWidget.h> | |
3 | #include <Variable/VariableMenuHeaderWidget.h> |
|
5 | #include <Variable/VariableMenuHeaderWidget.h> | |
4 | #include <Variable/VariableModel.h> |
|
6 | #include <Variable/VariableModel.h> | |
5 |
|
7 | |||
6 | #include <ui_VariableInspectorWidget.h> |
|
8 | #include <ui_VariableInspectorWidget.h> | |
7 |
|
9 | |||
8 | #include <QMouseEvent> |
|
10 | #include <QMouseEvent> | |
9 | #include <QSortFilterProxyModel> |
|
11 | #include <QSortFilterProxyModel> | |
10 | #include <QStyledItemDelegate> |
|
12 | #include <QStyledItemDelegate> | |
11 | #include <QWidgetAction> |
|
13 | #include <QWidgetAction> | |
12 |
|
14 | |||
13 | #include <SqpApplication.h> |
|
15 | #include <SqpApplication.h> | |
14 |
|
16 | |||
15 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") |
|
17 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") | |
16 |
|
18 | |||
17 |
|
19 | |||
18 | class QProgressBarItemDelegate : public QStyledItemDelegate { |
|
20 | class QProgressBarItemDelegate : public QStyledItemDelegate { | |
19 |
|
21 | |||
20 | public: |
|
22 | public: | |
21 | QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {} |
|
23 | QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {} | |
22 |
|
24 | |||
23 | void paint(QPainter *painter, const QStyleOptionViewItem &option, |
|
25 | void paint(QPainter *painter, const QStyleOptionViewItem &option, | |
24 | const QModelIndex &index) const |
|
26 | const QModelIndex &index) const | |
25 | { |
|
27 | { | |
26 | auto data = index.data(Qt::DisplayRole); |
|
28 | auto data = index.data(Qt::DisplayRole); | |
27 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
29 | auto progressData = index.data(VariableRoles::ProgressRole); | |
28 | if (data.isValid() && progressData.isValid()) { |
|
30 | if (data.isValid() && progressData.isValid()) { | |
29 | auto name = data.value<QString>(); |
|
31 | auto name = data.value<QString>(); | |
30 | auto progress = progressData.value<double>(); |
|
32 | auto progress = progressData.value<double>(); | |
31 | if (progress > 0) { |
|
33 | if (progress > 0) { | |
32 | auto cancelButtonWidth = 20; |
|
34 | auto cancelButtonWidth = 20; | |
33 | auto progressBarOption = QStyleOptionProgressBar{}; |
|
35 | auto progressBarOption = QStyleOptionProgressBar{}; | |
34 | auto progressRect = option.rect; |
|
36 | auto progressRect = option.rect; | |
35 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
37 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); | |
36 | progressBarOption.rect = progressRect; |
|
38 | progressBarOption.rect = progressRect; | |
37 | progressBarOption.minimum = 0; |
|
39 | progressBarOption.minimum = 0; | |
38 | progressBarOption.maximum = 100; |
|
40 | progressBarOption.maximum = 100; | |
39 | progressBarOption.progress = progress; |
|
41 | progressBarOption.progress = progress; | |
40 | progressBarOption.text |
|
42 | progressBarOption.text | |
41 | = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%"); |
|
43 | = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%"); | |
42 | progressBarOption.textVisible = true; |
|
44 | progressBarOption.textVisible = true; | |
43 | progressBarOption.textAlignment = Qt::AlignCenter; |
|
45 | progressBarOption.textAlignment = Qt::AlignCenter; | |
44 |
|
46 | |||
45 |
|
47 | |||
46 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, |
|
48 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, | |
47 | painter); |
|
49 | painter); | |
48 |
|
50 | |||
49 | // Cancel button |
|
51 | // Cancel button | |
50 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
52 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, | |
51 | option.rect.height()); |
|
53 | option.rect.height()); | |
52 | auto buttonOption = QStyleOptionButton{}; |
|
54 | auto buttonOption = QStyleOptionButton{}; | |
53 | buttonOption.rect = buttonRect; |
|
55 | buttonOption.rect = buttonRect; | |
54 | buttonOption.text = "X"; |
|
56 | buttonOption.text = "X"; | |
55 |
|
57 | |||
56 | QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); |
|
58 | QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); | |
57 | } |
|
59 | } | |
58 | else { |
|
60 | else { | |
59 | QStyledItemDelegate::paint(painter, option, index); |
|
61 | QStyledItemDelegate::paint(painter, option, index); | |
60 | } |
|
62 | } | |
61 | } |
|
63 | } | |
62 | else { |
|
64 | else { | |
63 | QStyledItemDelegate::paint(painter, option, index); |
|
65 | QStyledItemDelegate::paint(painter, option, index); | |
64 | } |
|
66 | } | |
65 | } |
|
67 | } | |
66 |
|
68 | |||
67 | bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, |
|
69 | bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, | |
68 | const QModelIndex &index) |
|
70 | const QModelIndex &index) | |
69 | { |
|
71 | { | |
70 | if (event->type() == QEvent::MouseButtonRelease) { |
|
72 | if (event->type() == QEvent::MouseButtonRelease) { | |
71 | auto data = index.data(Qt::DisplayRole); |
|
73 | auto data = index.data(Qt::DisplayRole); | |
72 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
74 | auto progressData = index.data(VariableRoles::ProgressRole); | |
73 | if (data.isValid() && progressData.isValid()) { |
|
75 | if (data.isValid() && progressData.isValid()) { | |
74 | auto cancelButtonWidth = 20; |
|
76 | auto cancelButtonWidth = 20; | |
75 | auto progressRect = option.rect; |
|
77 | auto progressRect = option.rect; | |
76 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
78 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); | |
77 | // Cancel button |
|
79 | // Cancel button | |
78 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
80 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, | |
79 | option.rect.height()); |
|
81 | option.rect.height()); | |
80 |
|
82 | |||
81 | auto e = (QMouseEvent *)event; |
|
83 | auto e = (QMouseEvent *)event; | |
82 | auto clickX = e->x(); |
|
84 | auto clickX = e->x(); | |
83 | auto clickY = e->y(); |
|
85 | auto clickY = e->y(); | |
84 |
|
86 | |||
85 | auto x = buttonRect.left(); // the X coordinate |
|
87 | auto x = buttonRect.left(); // the X coordinate | |
86 | auto y = buttonRect.top(); // the Y coordinate |
|
88 | auto y = buttonRect.top(); // the Y coordinate | |
87 | auto w = buttonRect.width(); // button width |
|
89 | auto w = buttonRect.width(); // button width | |
88 | auto h = buttonRect.height(); // button height |
|
90 | auto h = buttonRect.height(); // button height | |
89 |
|
91 | |||
90 | if (clickX > x && clickX < x + w) { |
|
92 | if (clickX > x && clickX < x + w) { | |
91 | if (clickY > y && clickY < y + h) { |
|
93 | if (clickY > y && clickY < y + h) { | |
92 | auto variableModel = sqpApp->variableController().variableModel(); |
|
94 | auto variableModel = sqpApp->variableController().variableModel(); | |
93 | variableModel->abortProgress(index); |
|
95 | variableModel->abortProgress(index); | |
94 | } |
|
96 | } | |
95 | } |
|
97 | } | |
96 | else { |
|
98 | else { | |
97 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
99 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
98 | } |
|
100 | } | |
99 | } |
|
101 | } | |
100 | else { |
|
102 | else { | |
101 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
103 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
102 | } |
|
104 | } | |
103 | } |
|
105 | } | |
104 | else { |
|
106 | else { | |
105 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
107 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
106 | } |
|
108 | } | |
107 | } |
|
109 | } | |
108 | }; |
|
110 | }; | |
109 |
|
111 | |||
110 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) |
|
112 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) | |
111 | : QWidget{parent}, |
|
113 | : QWidget{parent}, | |
112 | ui{new Ui::VariableInspectorWidget}, |
|
114 | ui{new Ui::VariableInspectorWidget}, | |
113 | m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}} |
|
115 | m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}} | |
114 | { |
|
116 | { | |
115 | ui->setupUi(this); |
|
117 | ui->setupUi(this); | |
116 |
|
118 | |||
117 | // Sets model for table |
|
119 | // Sets model for table | |
118 | // auto sortFilterModel = new QSortFilterProxyModel{this}; |
|
120 | // auto sortFilterModel = new QSortFilterProxyModel{this}; | |
119 | // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); |
|
121 | // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); | |
120 |
|
122 | |||
121 | auto variableModel = sqpApp->variableController().variableModel(); |
|
123 | auto variableModel = sqpApp->variableController().variableModel(); | |
122 | ui->tableView->setModel(variableModel); |
|
124 | ui->tableView->setModel(variableModel); | |
123 |
|
125 | |||
124 | // Adds extra signal/slot between view and model, so the view can be updated instantly when |
|
126 | // Adds extra signal/slot between view and model, so the view can be updated instantly when | |
125 | // there is a change of data in the model |
|
127 | // there is a change of data in the model | |
126 | connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, |
|
128 | connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, | |
127 | SLOT(refresh())); |
|
129 | SLOT(refresh())); | |
128 |
|
130 | |||
129 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); |
|
131 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); | |
130 | ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate); |
|
132 | ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate); | |
131 |
|
133 | |||
132 | // Fixes column sizes |
|
134 | // Fixes column sizes | |
133 | auto model = ui->tableView->model(); |
|
135 | auto model = ui->tableView->model(); | |
134 | const auto count = model->columnCount(); |
|
136 | const auto count = model->columnCount(); | |
135 | for (auto i = 0; i < count; ++i) { |
|
137 | for (auto i = 0; i < count; ++i) { | |
136 | ui->tableView->setColumnWidth( |
|
138 | ui->tableView->setColumnWidth( | |
137 | i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width()); |
|
139 | i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width()); | |
138 | } |
|
140 | } | |
139 |
|
141 | |||
140 | // Sets selection options |
|
142 | // Sets selection options | |
141 | ui->tableView->setSelectionBehavior(QTableView::SelectRows); |
|
143 | ui->tableView->setSelectionBehavior(QTableView::SelectRows); | |
142 | ui->tableView->setSelectionMode(QTableView::ExtendedSelection); |
|
144 | ui->tableView->setSelectionMode(QTableView::ExtendedSelection); | |
143 |
|
145 | |||
144 | // Connection to show a menu when right clicking on the tree |
|
146 | // Connection to show a menu when right clicking on the tree | |
145 | ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); |
|
147 | ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); | |
146 | connect(ui->tableView, &QTableView::customContextMenuRequested, this, |
|
148 | connect(ui->tableView, &QTableView::customContextMenuRequested, this, | |
147 | &VariableInspectorWidget::onTableMenuRequested); |
|
149 | &VariableInspectorWidget::onTableMenuRequested); | |
148 | } |
|
150 | } | |
149 |
|
151 | |||
150 | VariableInspectorWidget::~VariableInspectorWidget() |
|
152 | VariableInspectorWidget::~VariableInspectorWidget() | |
151 | { |
|
153 | { | |
152 | delete ui; |
|
154 | delete ui; | |
153 | } |
|
155 | } | |
154 |
|
156 | |||
155 | void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept |
|
157 | void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept | |
156 | { |
|
158 | { | |
157 | auto selectedRows = ui->tableView->selectionModel()->selectedRows(); |
|
159 | auto selectedRows = ui->tableView->selectionModel()->selectedRows(); | |
158 |
|
160 | |||
159 | // Gets the model to retrieve the underlying selected variables |
|
161 | // Gets the model to retrieve the underlying selected variables | |
160 | auto model = sqpApp->variableController().variableModel(); |
|
162 | auto model = sqpApp->variableController().variableModel(); | |
161 | auto selectedVariables = QVector<std::shared_ptr<Variable> >{}; |
|
163 | auto selectedVariables = QVector<std::shared_ptr<Variable> >{}; | |
162 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
164 | for (const auto &selectedRow : qAsConst(selectedRows)) { | |
163 | if (auto selectedVariable = model->variable(selectedRow.row())) { |
|
165 | if (auto selectedVariable = model->variable(selectedRow.row())) { | |
164 | selectedVariables.push_back(selectedVariable); |
|
166 | selectedVariables.push_back(selectedVariable); | |
165 | } |
|
167 | } | |
166 | } |
|
168 | } | |
167 |
|
169 | |||
168 | QMenu tableMenu{}; |
|
170 | QMenu tableMenu{}; | |
169 |
|
171 | |||
170 | // Emits a signal so that potential receivers can populate the menu before displaying it |
|
172 | // Emits a signal so that potential receivers can populate the menu before displaying it | |
171 | emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); |
|
173 | emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); | |
172 |
|
174 | |||
173 | // Adds menu-specific actions |
|
175 | // Adds menu-specific actions | |
174 | if (!selectedVariables.isEmpty()) { |
|
176 | if (!selectedVariables.isEmpty()) { | |
175 | tableMenu.addSeparator(); |
|
177 | tableMenu.addSeparator(); | |
176 |
|
178 | |||
177 | // 'Rename' action (only if one variable selected) |
|
179 | // 'Rename' action (only if one variable selected) | |
178 | if (selectedVariables.size() == 1) { |
|
180 | if (selectedVariables.size() == 1) { | |
179 | auto selectedVariable = selectedVariables.front(); |
|
181 | auto selectedVariable = selectedVariables.front(); | |
180 |
|
182 | |||
181 | auto renameFun = [&selectedVariable, &model, this]() { |
|
183 | auto renameFun = [&selectedVariable, &model, this]() { | |
|
184 | // Generates forbidden names (names associated to existing variables) | |||
|
185 | auto allVariables = model->variables(); | |||
|
186 | auto forbiddenNames = QVector<QString>(allVariables.size()); | |||
|
187 | std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(), | |||
|
188 | [](const auto &variable) { return variable->name(); }); | |||
|
189 | ||||
|
190 | RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this}; | |||
|
191 | if (dialog.exec() == QDialog::Accepted) { | |||
|
192 | selectedVariable->setName(dialog.name()); | |||
|
193 | } | |||
182 | }; |
|
194 | }; | |
183 |
|
195 | |||
184 | tableMenu.addAction(tr("Rename..."), renameFun); |
|
196 | tableMenu.addAction(tr("Rename..."), renameFun); | |
185 | } |
|
197 | } | |
186 |
|
198 | |||
187 | // 'Delete' action |
|
199 | // 'Delete' action | |
188 | auto deleteFun = [&selectedVariables]() { |
|
200 | auto deleteFun = [&selectedVariables]() { | |
189 | sqpApp->variableController().deleteVariables(selectedVariables); |
|
201 | sqpApp->variableController().deleteVariables(selectedVariables); | |
190 | }; |
|
202 | }; | |
191 |
|
203 | |||
192 | tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); |
|
204 | tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); | |
193 | } |
|
205 | } | |
194 |
|
206 | |||
195 | if (!tableMenu.isEmpty()) { |
|
207 | if (!tableMenu.isEmpty()) { | |
196 | // Generates menu header (inserted before first action) |
|
208 | // Generates menu header (inserted before first action) | |
197 | auto firstAction = tableMenu.actions().first(); |
|
209 | auto firstAction = tableMenu.actions().first(); | |
198 | auto headerAction = new QWidgetAction{&tableMenu}; |
|
210 | auto headerAction = new QWidgetAction{&tableMenu}; | |
199 | headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu}); |
|
211 | headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu}); | |
200 | tableMenu.insertAction(firstAction, headerAction); |
|
212 | tableMenu.insertAction(firstAction, headerAction); | |
201 |
|
213 | |||
202 | // Displays menu |
|
214 | // Displays menu | |
203 | tableMenu.exec(QCursor::pos()); |
|
215 | tableMenu.exec(QCursor::pos()); | |
204 | } |
|
216 | } | |
205 | } |
|
217 | } | |
206 |
|
218 | |||
207 | void VariableInspectorWidget::refresh() noexcept |
|
219 | void VariableInspectorWidget::refresh() noexcept | |
208 | { |
|
220 | { | |
209 | ui->tableView->viewport()->update(); |
|
221 | ui->tableView->viewport()->update(); | |
210 | } |
|
222 | } |
General Comments 0
You need to be logged in to leave comments.
Login now