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