##// END OF EJS Templates
Handles right clicking on the tree of the data sources...
Alexandre Leroux -
r143:7b4ea0e1482b
parent child
Show More
@@ -29,6 +29,10 public slots:
29
29
30 private:
30 private:
31 Ui::DataSourceWidget *ui;
31 Ui::DataSourceWidget *ui;
32
33 private slots:
34 /// Slot called when right clicking on an item in the tree (displays a menu)
35 void onTreeMenuRequested(const QPoint &pos) noexcept;
32 };
36 };
33
37
34 #endif // SCIQLOP_DATASOURCEWIDGET_H
38 #endif // SCIQLOP_DATASOURCEWIDGET_H
@@ -5,6 +5,8
5 #include <DataSource/DataSourceItem.h>
5 #include <DataSource/DataSourceItem.h>
6 #include <DataSource/DataSourceTreeWidgetItem.h>
6 #include <DataSource/DataSourceTreeWidgetItem.h>
7
7
8 #include <QMenu>
9
8 namespace {
10 namespace {
9
11
10 /// Number of columns displayed in the tree
12 /// Number of columns displayed in the tree
@@ -40,6 +42,11 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui
40 // Set tree properties
42 // Set tree properties
41 ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
43 ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
42 ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
44 ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
45 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
46
47 // Connection to show a menu when right clicking on the tree
48 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
49 &DataSourceWidget::onTreeMenuRequested);
43 }
50 }
44
51
45 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
52 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
@@ -50,3 +57,16 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
50 ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource));
57 ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource));
51 }
58 }
52 }
59 }
60
61 void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept
62 {
63 // Retrieves the selected item in the tree, and build the menu from its actions
64 if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) {
65 QMenu treeMenu{};
66 treeMenu.addActions(selectedItem->actions());
67
68 if (!treeMenu.isEmpty()) {
69 treeMenu.exec(mapToGlobal(pos));
70 }
71 }
72 }
General Comments 0
You need to be logged in to leave comments. Login now