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