@@ -1,72 +1,77 | |||
|
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 | 8 | #include <QMenu> |
|
9 | 9 | |
|
10 | 10 | namespace { |
|
11 | 11 | |
|
12 | 12 | /// Number of columns displayed in the tree |
|
13 | 13 | const auto TREE_NB_COLUMNS = 1; |
|
14 | 14 | |
|
15 | 15 | /// Header labels for the tree |
|
16 | 16 | const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")}; |
|
17 | 17 | |
|
18 | 18 | /** |
|
19 | 19 | * Creates the item associated to a data source |
|
20 | 20 | * @param dataSource the data source for which to create the item |
|
21 | 21 | * @return the new item |
|
22 | 22 | */ |
|
23 | 23 | DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource) |
|
24 | 24 | { |
|
25 | 25 | // Creates item for the data source |
|
26 | 26 | auto item = new DataSourceTreeWidgetItem{dataSource}; |
|
27 | 27 | |
|
28 | 28 | // Generates items for the children of the data source |
|
29 | 29 | for (auto i = 0; i < dataSource->childCount(); ++i) { |
|
30 | 30 | item->addChild(createTreeWidgetItem(dataSource->child(i))); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | return item; |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | } // namespace |
|
37 | 37 | |
|
38 | 38 | DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget} |
|
39 | 39 | { |
|
40 | 40 | ui->setupUi(this); |
|
41 | 41 | |
|
42 | 42 | // Set tree properties |
|
43 | 43 | ui->treeWidget->setColumnCount(TREE_NB_COLUMNS); |
|
44 | 44 | ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS); |
|
45 | 45 | ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
46 | 46 | |
|
47 | 47 | // Connection to show a menu when right clicking on the tree |
|
48 | 48 | connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, |
|
49 | 49 | &DataSourceWidget::onTreeMenuRequested); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | DataSourceWidget::~DataSourceWidget() noexcept | |
|
53 | { | |
|
54 | delete ui; | |
|
55 | } | |
|
56 | ||
|
52 | 57 | void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept |
|
53 | 58 | { |
|
54 | 59 | // Creates the item associated to the source and adds it to the tree widget. The tree widget |
|
55 | 60 | // takes the ownership of the item |
|
56 | 61 | if (dataSource) { |
|
57 | 62 | ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource)); |
|
58 | 63 | } |
|
59 | 64 | } |
|
60 | 65 | |
|
61 | 66 | void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept |
|
62 | 67 | { |
|
63 | 68 | // Retrieves the selected item in the tree, and build the menu from its actions |
|
64 | 69 | if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) { |
|
65 | 70 | QMenu treeMenu{}; |
|
66 | 71 | treeMenu.addActions(selectedItem->actions()); |
|
67 | 72 | |
|
68 | 73 | if (!treeMenu.isEmpty()) { |
|
69 | 74 | treeMenu.exec(mapToGlobal(pos)); |
|
70 | 75 | } |
|
71 | 76 | } |
|
72 | 77 | } |
General Comments 0
You need to be logged in to leave comments.
Login now