@@ -1,120 +1,123 | |||
|
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/DataSourceTreeWidgetHelper.h> |
|
7 | 7 | #include <DataSource/DataSourceTreeWidgetItem.h> |
|
8 | 8 | |
|
9 | 9 | #include <QMenu> |
|
10 | 10 | |
|
11 | 11 | namespace { |
|
12 | 12 | |
|
13 | 13 | /// Number of columns displayed in the tree |
|
14 | 14 | const auto TREE_NB_COLUMNS = 1; |
|
15 | 15 | |
|
16 | 16 | /// Header labels for the tree |
|
17 | 17 | const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")}; |
|
18 | 18 | |
|
19 | 19 | /** |
|
20 | 20 | * Creates the item associated to a data source |
|
21 | 21 | * @param dataSource the data source for which to create the item |
|
22 | 22 | * @return the new item |
|
23 | 23 | */ |
|
24 | 24 | DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource) |
|
25 | 25 | { |
|
26 | 26 | // Creates item for the data source |
|
27 | 27 | auto item = new DataSourceTreeWidgetItem{dataSource}; |
|
28 | 28 | |
|
29 | 29 | // Generates items for the children of the data source |
|
30 | 30 | for (auto i = 0; i < dataSource->childCount(); ++i) { |
|
31 | 31 | item->addChild(createTreeWidgetItem(dataSource->child(i))); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | return item; |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | } // namespace |
|
38 | 38 | |
|
39 | 39 | DataSourceWidget::DataSourceWidget(QWidget *parent) |
|
40 | 40 | : QWidget{parent}, |
|
41 | 41 | ui{new Ui::DataSourceWidget}, |
|
42 | 42 | m_Root{ |
|
43 | 43 | std::make_unique<DataSourceItem>(DataSourceItemType::NODE, QStringLiteral("Sources"))} |
|
44 | 44 | { |
|
45 | 45 | ui->setupUi(this); |
|
46 | 46 | |
|
47 | 47 | // Set tree properties |
|
48 | 48 | ui->treeWidget->setColumnCount(TREE_NB_COLUMNS); |
|
49 | 49 | ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS); |
|
50 | 50 | ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
51 | 51 | |
|
52 | 52 | // Connection to show a menu when right clicking on the tree |
|
53 | 53 | connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, |
|
54 | 54 | &DataSourceWidget::onTreeMenuRequested); |
|
55 | 55 | |
|
56 | 56 | // Connection to filter tree |
|
57 | 57 | connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &DataSourceWidget::filterChanged); |
|
58 | 58 | |
|
59 | 59 | // First init |
|
60 | 60 | updateTreeWidget(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | DataSourceWidget::~DataSourceWidget() noexcept |
|
64 | 64 | { |
|
65 | 65 | delete ui; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept |
|
69 | 69 | { |
|
70 | // Creates the item associated to the source and adds it to the tree widget. The tree widget | |
|
71 | // takes the ownership of the item | |
|
70 | // Merges the data source (without taking its root) | |
|
72 | 71 | if (dataSource) { |
|
73 | ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource)); | |
|
72 | for (auto i = 0, count = dataSource->childCount(); i < count; ++i) { | |
|
73 | m_Root->merge(*dataSource->child(i)); | |
|
74 | } | |
|
75 | ||
|
76 | updateTreeWidget(); | |
|
74 | 77 | } |
|
75 | 78 | } |
|
76 | 79 | |
|
77 | 80 | void DataSourceWidget::updateTreeWidget() noexcept |
|
78 | 81 | { |
|
79 | 82 | ui->treeWidget->clear(); |
|
80 | 83 | |
|
81 | 84 | auto rootItem = createTreeWidgetItem(m_Root.get()); |
|
82 | 85 | ui->treeWidget->addTopLevelItem(rootItem); |
|
83 | 86 | rootItem->setExpanded(true); |
|
84 | 87 | |
|
85 | 88 | // Sorts tree |
|
86 | 89 | ui->treeWidget->setSortingEnabled(true); |
|
87 | 90 | ui->treeWidget->sortByColumn(0, Qt::AscendingOrder); |
|
88 | 91 | } |
|
89 | 92 | |
|
90 | 93 | void DataSourceWidget::filterChanged(const QString &text) noexcept |
|
91 | 94 | { |
|
92 | 95 | auto validateItem = [&text](const DataSourceTreeWidgetItem &item) { |
|
93 | 96 | auto regExp = QRegExp{text, Qt::CaseInsensitive, QRegExp::Wildcard}; |
|
94 | 97 | |
|
95 | 98 | // An item is valid if any of its metadata validates the text filter |
|
96 | 99 | auto itemMetadata = item.data()->data(); |
|
97 | 100 | auto itemMetadataEnd = itemMetadata.cend(); |
|
98 | 101 | auto acceptFilter |
|
99 | 102 | = [®Exp](const auto &variant) { return variant.toString().contains(regExp); }; |
|
100 | 103 | |
|
101 | 104 | return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter) |
|
102 | 105 | != itemMetadataEnd; |
|
103 | 106 | }; |
|
104 | 107 | |
|
105 | 108 | // Applies filter on tree widget |
|
106 | 109 | DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem); |
|
107 | 110 | } |
|
108 | 111 | |
|
109 | 112 | void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept |
|
110 | 113 | { |
|
111 | 114 | // Retrieves the selected item in the tree, and build the menu from its actions |
|
112 | 115 | if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) { |
|
113 | 116 | QMenu treeMenu{}; |
|
114 | 117 | treeMenu.addActions(selectedItem->actions()); |
|
115 | 118 | |
|
116 | 119 | if (!treeMenu.isEmpty()) { |
|
117 | 120 | treeMenu.exec(QCursor::pos()); |
|
118 | 121 | } |
|
119 | 122 | } |
|
120 | 123 | } |
General Comments 0
You need to be logged in to leave comments.
Login now