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