##// END OF EJS Templates
Adds header in the menu
Alexandre Leroux -
r290:c7a8f2384d1c
parent child
Show More
@@ -1,69 +1,77
1 1 #include <Variable/VariableController.h>
2 2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableMenuHeaderWidget.h>
3 4 #include <Variable/VariableModel.h>
4 5
5 6 #include <ui_VariableInspectorWidget.h>
6 7
7 8 #include <QSortFilterProxyModel>
9 #include <QWidgetAction>
8 10
9 11 #include <SqpApplication.h>
10 12
11 13 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
12 14
13 15 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
14 16 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
15 17 {
16 18 ui->setupUi(this);
17 19
18 20 // Sets model for table
19 21 auto sortFilterModel = new QSortFilterProxyModel{this};
20 22 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
21 23
22 24 ui->tableView->setModel(sortFilterModel);
23 25
24 26 // Fixes column sizes
25 27 auto model = ui->tableView->model();
26 28 const auto count = model->columnCount();
27 29 for (auto i = 0; i < count; ++i) {
28 30 ui->tableView->setColumnWidth(
29 31 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
30 32 }
31 33
32 34 // Sets selection options
33 35 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
34 36 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
35 37
36 38 // Connection to show a menu when right clicking on the tree
37 39 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
38 40 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
39 41 &VariableInspectorWidget::onTableMenuRequested);
40 42 }
41 43
42 44 VariableInspectorWidget::~VariableInspectorWidget()
43 45 {
44 46 delete ui;
45 47 }
46 48
47 49 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
48 50 {
49 51 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
50 52
51 53 // Gets the model to retrieve the underlying selected variables
52 54 auto model = sqpApp->variableController().variableModel();
53 55 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
54 56 for (const auto &selectedRow : qAsConst(selectedRows)) {
55 57 if (auto selectedVariable = model->variable(selectedRow.row())) {
56 58 selectedVariables.push_back(selectedVariable);
57 59 }
58 60 }
59 61
60 62 QMenu tableMenu{};
61 63
62 64 // Emits a signal so that potential receivers can populate the menu before displaying it
63 65 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
64 66
65 67 if (!tableMenu.isEmpty()) {
68 // Generates menu header (inserted before first action)
69 auto firstAction = tableMenu.actions().first();
70 auto headerAction = new QWidgetAction{&tableMenu};
71 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
72 tableMenu.insertAction(firstAction, headerAction);
73
66 74 // Displays menu
67 75 tableMenu.exec(mapToGlobal(pos));
68 76 }
69 77 }
General Comments 0
You need to be logged in to leave comments. Login now