##// END OF EJS Templates
Switched to cpp_utils package...
Switched to cpp_utils package Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r1478:6e3f56cd8c8b
r1486:8539a975891c
Show More
DataSourceWidget.cpp
124 lines | 3.8 KiB | text/x-c | CppLexer
/ gui / src / DataSource / DataSourceWidget.cpp
Alexandre Leroux
Creates the data source widget...
r82 #include <DataSource/DataSourceWidget.h>
#include <ui_DataSourceWidget.h>
#include <DataSource/DataSourceItem.h>
Alexandre Leroux
Implements filter function for data source...
r477 #include <DataSource/DataSourceTreeWidgetHelper.h>
Alexandre Leroux
Creates the data source widget...
r82 #include <DataSource/DataSourceTreeWidgetItem.h>
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143 #include <QMenu>
PySide2 bindings + some GUI clean...
r1478 namespace
{
Alexandre Leroux
Creates the data source widget...
r82
/// Number of columns displayed in the tree
const auto TREE_NB_COLUMNS = 1;
/// Header labels for the tree
PySide2 bindings + some GUI clean...
r1478 const auto TREE_HEADER_LABELS = QStringList { QObject::tr("Name") };
Alexandre Leroux
Creates the data source widget...
r82
Alexandre Leroux
Implements method to add a data source in the widget...
r83 /**
* Creates the item associated to a data source
* @param dataSource the data source for which to create the item
* @return the new item
*/
PySide2 bindings + some GUI clean...
r1478 DataSourceTreeWidgetItem* createTreeWidgetItem(DataSourceItem* dataSource)
Alexandre Leroux
Implements method to add a data source in the widget...
r83 {
// Creates item for the data source
PySide2 bindings + some GUI clean...
r1478 auto item = new DataSourceTreeWidgetItem { dataSource };
Alexandre Leroux
Implements method to add a data source in the widget...
r83 // Generates items for the children of the data source
PySide2 bindings + some GUI clean...
r1478 std::for_each(dataSource->cbegin(), dataSource->cend(),
[&item](const std::unique_ptr<DataSourceItem>& child) {
item->addChild(createTreeWidgetItem(child.get()));
});
Alexandre Leroux
Implements method to add a data source in the widget...
r83 return item;
}
Alexandre Leroux
Creates the data source widget...
r82 } // namespace
PySide2 bindings + some GUI clean...
r1478 DataSourceWidget::DataSourceWidget(QWidget* parent)
: QWidget { parent }
, ui { new Ui::DataSourceWidget }
, m_Root { std::make_unique<DataSourceItem>(
DataSourceItemType::NODE, QStringLiteral("Sources")) }
Alexandre Leroux
Creates the data source widget...
r82 {
Alexandre Leroux
(Minor) Extracts ui of DataSourceWidget
r109 ui->setupUi(this);
// Set tree properties
ui->treeWidget->setColumnCount(TREE_NB_COLUMNS);
ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS);
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143 ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
// Connection to show a menu when right clicking on the tree
connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this,
PySide2 bindings + some GUI clean...
r1478 &DataSourceWidget::onTreeMenuRequested);
Alexandre Leroux
Adds line edit as a search box above data source tree
r475
// Connection to filter tree
connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &DataSourceWidget::filterChanged);
Alexandre Leroux
Updates data sources UI representation (1)...
r1034
// First init
updateTreeWidget();
Alexandre Leroux
Creates the data source widget...
r82 }
Alexandre Leroux
Implements method to add a data source in the widget...
r83
add missing delete ui
r232 DataSourceWidget::~DataSourceWidget() noexcept
{
delete ui;
}
PySide2 bindings + some GUI clean...
r1478 void DataSourceWidget::addDataSource(DataSourceItem* dataSource) noexcept
Alexandre Leroux
Implements method to add a data source in the widget...
r83 {
Alexandre Leroux
Updates data sources UI representation (1)...
r1035 // Merges the data source (without taking its root)
PySide2 bindings + some GUI clean...
r1478 if (dataSource)
{
std::for_each(std::cbegin(*dataSource), std::cend(*dataSource),
[this](const auto& child) { this->m_Root->merge(*child.get()); });
Alexandre Leroux
Updates data sources UI representation (1)...
r1035 updateTreeWidget();
Alexandre Leroux
Change signal/slot signature for data source
r92 }
Alexandre Leroux
Implements method to add a data source in the widget...
r83 }
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143
Alexandre Leroux
Updates data sources UI representation (1)...
r1034 void DataSourceWidget::updateTreeWidget() noexcept
{
ui->treeWidget->clear();
auto rootItem = createTreeWidgetItem(m_Root.get());
ui->treeWidget->addTopLevelItem(rootItem);
rootItem->setExpanded(true);
// Sorts tree
ui->treeWidget->setSortingEnabled(true);
ui->treeWidget->sortByColumn(0, Qt::AscendingOrder);
}
PySide2 bindings + some GUI clean...
r1478 void DataSourceWidget::filterChanged(const QString& text) noexcept
Alexandre Leroux
Adds line edit as a search box above data source tree
r475 {
PySide2 bindings + some GUI clean...
r1478 auto validateItem = [&text](const DataSourceTreeWidgetItem& item) {
auto regExp = QRegExp { text, Qt::CaseInsensitive, QRegExp::Wildcard };
Alexandre Leroux
Implements filter function for data source...
r477
// An item is valid if any of its metadata validates the text filter
auto itemMetadata = item.data()->data();
auto itemMetadataEnd = itemMetadata.cend();
auto acceptFilter
PySide2 bindings + some GUI clean...
r1478 = [&regExp](const auto& variant) { return variant.toString().contains(regExp); };
Alexandre Leroux
Implements filter function for data source...
r477
return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter)
PySide2 bindings + some GUI clean...
r1478 != itemMetadataEnd;
Alexandre Leroux
Implements filter function for data source...
r477 };
// Applies filter on tree widget
DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem);
Alexandre Leroux
Adds line edit as a search box above data source tree
r475 }
PySide2 bindings + some GUI clean...
r1478 void DataSourceWidget::onTreeMenuRequested(const QPoint& pos) noexcept
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143 {
// Retrieves the selected item in the tree, and build the menu from its actions
PySide2 bindings + some GUI clean...
r1478 if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem*>(ui->treeWidget->itemAt(pos)))
{
QMenu treeMenu {};
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143 treeMenu.addActions(selectedItem->actions());
PySide2 bindings + some GUI clean...
r1478 if (!treeMenu.isEmpty())
{
Alexandre Leroux
Fixed menus position
r655 treeMenu.exec(QCursor::pos());
Alexandre Leroux
Handles right clicking on the tree of the data sources...
r143 }
}
}