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