##// END OF EJS Templates
Updates data sources UI representation (1)...
Alexandre Leroux -
r1034:53c9cf446448
parent child
Show More
@@ -1,42 +1,47
1 1 #ifndef SCIQLOP_DATASOURCEWIDGET_H
2 2 #define SCIQLOP_DATASOURCEWIDGET_H
3 3
4 4 #include <QWidget>
5 5
6 #include <memory>
7
6 8 namespace Ui {
7 9 class DataSourceWidget;
8 10 } // Ui
9 11
10 12 class DataSourceItem;
11 13
12 14 /**
13 15 * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources
14 16 * attached to SciQlop.
15 17 */
16 18 class DataSourceWidget : public QWidget {
17 19 Q_OBJECT
18 20
19 21 public:
20 22 explicit DataSourceWidget(QWidget *parent = 0);
21 23 virtual ~DataSourceWidget() noexcept;
22 24
23 25 public slots:
24 26 /**
25 27 * Adds a data source. An item associated to the data source is created and then added to the
26 28 * representation tree
27 29 * @param dataSource the data source to add. The pointer has to be not null
28 30 */
29 31 void addDataSource(DataSourceItem *dataSource) noexcept;
30 32
31 33 private:
34 void updateTreeWidget() noexcept;
35
32 36 Ui::DataSourceWidget *ui;
37 std::unique_ptr<DataSourceItem> m_Root;
33 38
34 39 private slots:
35 40 /// Slot called when the filtering text has changed
36 41 void filterChanged(const QString &text) noexcept;
37 42
38 43 /// Slot called when right clicking on an item in the tree (displays a menu)
39 44 void onTreeMenuRequested(const QPoint &pos) noexcept;
40 45 };
41 46
42 47 #endif // SCIQLOP_DATASOURCEWIDGET_H
@@ -1,100 +1,120
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 DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget}
39 DataSourceWidget::DataSourceWidget(QWidget *parent)
40 : QWidget{parent},
41 ui{new Ui::DataSourceWidget},
42 m_Root{
43 std::make_unique<DataSourceItem>(DataSourceItemType::NODE, QStringLiteral("Sources"))}
40 44 {
41 45 ui->setupUi(this);
42 46
43 47 // Set tree properties
44 48 ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
45 49 ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
46 50 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
47 51
48 52 // Connection to show a menu when right clicking on the tree
49 53 connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
50 54 &DataSourceWidget::onTreeMenuRequested);
51 55
52 56 // Connection to filter tree
53 57 connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &DataSourceWidget::filterChanged);
58
59 // First init
60 updateTreeWidget();
54 61 }
55 62
56 63 DataSourceWidget::~DataSourceWidget() noexcept
57 64 {
58 65 delete ui;
59 66 }
60 67
61 68 void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept
62 69 {
63 70 // Creates the item associated to the source and adds it to the tree widget. The tree widget
64 71 // takes the ownership of the item
65 72 if (dataSource) {
66 73 ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource));
67 74 }
68 75 }
69 76
77 void DataSourceWidget::updateTreeWidget() noexcept
78 {
79 ui->treeWidget->clear();
80
81 auto rootItem = createTreeWidgetItem(m_Root.get());
82 ui->treeWidget->addTopLevelItem(rootItem);
83 rootItem->setExpanded(true);
84
85 // Sorts tree
86 ui->treeWidget->setSortingEnabled(true);
87 ui->treeWidget->sortByColumn(0, Qt::AscendingOrder);
88 }
89
70 90 void DataSourceWidget::filterChanged(const QString &text) noexcept
71 91 {
72 92 auto validateItem = [&text](const DataSourceTreeWidgetItem &item) {
73 93 auto regExp = QRegExp{text, Qt::CaseInsensitive, QRegExp::Wildcard};
74 94
75 95 // An item is valid if any of its metadata validates the text filter
76 96 auto itemMetadata = item.data()->data();
77 97 auto itemMetadataEnd = itemMetadata.cend();
78 98 auto acceptFilter
79 99 = [&regExp](const auto &variant) { return variant.toString().contains(regExp); };
80 100
81 101 return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter)
82 102 != itemMetadataEnd;
83 103 };
84 104
85 105 // Applies filter on tree widget
86 106 DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem);
87 107 }
88 108
89 109 void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept
90 110 {
91 111 // Retrieves the selected item in the tree, and build the menu from its actions
92 112 if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) {
93 113 QMenu treeMenu{};
94 114 treeMenu.addActions(selectedItem->actions());
95 115
96 116 if (!treeMenu.isEmpty()) {
97 117 treeMenu.exec(QCursor::pos());
98 118 }
99 119 }
100 120 }
General Comments 0
You need to be logged in to leave comments. Login now