##// END OF EJS Templates
Handles right clicking on the tree of the data sources...
Alexandre Leroux -
r143:7b4ea0e1482b
parent child
Show More
@@ -1,34 +1,38
1 #ifndef SCIQLOP_DATASOURCEWIDGET_H
1 #ifndef SCIQLOP_DATASOURCEWIDGET_H
2 #define SCIQLOP_DATASOURCEWIDGET_H
2 #define SCIQLOP_DATASOURCEWIDGET_H
3
3
4 #include <QWidget>
4 #include <QWidget>
5
5
6 namespace Ui {
6 namespace Ui {
7 class DataSourceWidget;
7 class DataSourceWidget;
8 } // Ui
8 } // Ui
9
9
10 class DataSourceItem;
10 class DataSourceItem;
11
11
12 /**
12 /**
13 * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources
13 * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources
14 * attached to SciQlop.
14 * attached to SciQlop.
15 */
15 */
16 class DataSourceWidget : public QWidget {
16 class DataSourceWidget : public QWidget {
17 Q_OBJECT
17 Q_OBJECT
18
18
19 public:
19 public:
20 explicit DataSourceWidget(QWidget *parent = 0);
20 explicit DataSourceWidget(QWidget *parent = 0);
21
21
22 public slots:
22 public slots:
23 /**
23 /**
24 * Adds a data source. An item associated to the data source is created and then added to the
24 * Adds a data source. An item associated to the data source is created and then added to the
25 * representation tree
25 * representation tree
26 * @param dataSource the data source to add. The pointer has to be not null
26 * @param dataSource the data source to add. The pointer has to be not null
27 */
27 */
28 void addDataSource(DataSourceItem *dataSource) noexcept;
28 void addDataSource(DataSourceItem *dataSource) noexcept;
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
@@ -1,52 +1,72
1 #include <DataSource/DataSourceWidget.h>
1 #include <DataSource/DataSourceWidget.h>
2
2
3 #include <ui_DataSourceWidget.h>
3 #include <ui_DataSourceWidget.h>
4
4
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
11 const auto TREE_NB_COLUMNS = 1;
13 const auto TREE_NB_COLUMNS = 1;
12
14
13 /// Header labels for the tree
15 /// Header labels for the tree
14 const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")};
16 const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")};
15
17
16 /**
18 /**
17 * Creates the item associated to a data source
19 * Creates the item associated to a data source
18 * @param dataSource the data source for which to create the item
20 * @param dataSource the data source for which to create the item
19 * @return the new item
21 * @return the new item
20 */
22 */
21 DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource)
23 DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource)
22 {
24 {
23 // Creates item for the data source
25 // Creates item for the data source
24 auto item = new DataSourceTreeWidgetItem{dataSource};
26 auto item = new DataSourceTreeWidgetItem{dataSource};
25
27
26 // Generates items for the children of the data source
28 // Generates items for the children of the data source
27 for (auto i = 0; i < dataSource->childCount(); ++i) {
29 for (auto i = 0; i < dataSource->childCount(); ++i) {
28 item->addChild(createTreeWidgetItem(dataSource->child(i)));
30 item->addChild(createTreeWidgetItem(dataSource->child(i)));
29 }
31 }
30
32
31 return item;
33 return item;
32 }
34 }
33
35
34 } // namespace
36 } // namespace
35
37
36 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget}
38 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget}
37 {
39 {
38 ui->setupUi(this);
40 ui->setupUi(this);
39
41
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
46 {
53 {
47 // Creates the item associated to the source and adds it to the tree widget. The tree widget
54 // Creates the item associated to the source and adds it to the tree widget. The tree widget
48 // takes the ownership of the item
55 // takes the ownership of the item
49 if (dataSource) {
56 if (dataSource) {
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