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