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