DataSourceWidget.cpp
123 lines
| 3.7 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r82 | #include <DataSource/DataSourceWidget.h> | ||
#include <ui_DataSourceWidget.h> | ||||
#include <DataSource/DataSourceItem.h> | ||||
Alexandre Leroux
|
r477 | #include <DataSource/DataSourceTreeWidgetHelper.h> | ||
Alexandre Leroux
|
r82 | #include <DataSource/DataSourceTreeWidgetItem.h> | ||
Alexandre Leroux
|
r143 | #include <QMenu> | ||
Alexandre Leroux
|
r82 | namespace { | ||
/// Number of columns displayed in the tree | ||||
const auto TREE_NB_COLUMNS = 1; | ||||
/// Header labels for the tree | ||||
const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")}; | ||||
Alexandre Leroux
|
r83 | /** | ||
* Creates the item associated to a data source | ||||
* @param dataSource the data source for which to create the item | ||||
* @return the new item | ||||
*/ | ||||
DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource) | ||||
{ | ||||
// Creates item for the data source | ||||
auto item = new DataSourceTreeWidgetItem{dataSource}; | ||||
// Generates items for the children of the data source | ||||
for (auto i = 0; i < dataSource->childCount(); ++i) { | ||||
item->addChild(createTreeWidgetItem(dataSource->child(i))); | ||||
} | ||||
return item; | ||||
} | ||||
Alexandre Leroux
|
r82 | } // namespace | ||
Alexandre Leroux
|
r1074 | DataSourceWidget::DataSourceWidget(QWidget *parent) | ||
: QWidget{parent}, | ||||
ui{new Ui::DataSourceWidget}, | ||||
m_Root{ | ||||
std::make_unique<DataSourceItem>(DataSourceItemType::NODE, QStringLiteral("Sources"))} | ||||
Alexandre Leroux
|
r82 | { | ||
Alexandre Leroux
|
r109 | ui->setupUi(this); | ||
// Set tree properties | ||||
ui->treeWidget->setColumnCount(TREE_NB_COLUMNS); | ||||
ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS); | ||||
Alexandre Leroux
|
r143 | ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); | ||
// Connection to show a menu when right clicking on the tree | ||||
connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, | ||||
&DataSourceWidget::onTreeMenuRequested); | ||||
Alexandre Leroux
|
r475 | |||
// Connection to filter tree | ||||
connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &DataSourceWidget::filterChanged); | ||||
Alexandre Leroux
|
r1074 | |||
// First init | ||||
updateTreeWidget(); | ||||
Alexandre Leroux
|
r82 | } | ||
Alexandre Leroux
|
r83 | |||
r232 | DataSourceWidget::~DataSourceWidget() noexcept | |||
{ | ||||
delete ui; | ||||
} | ||||
Alexandre Leroux
|
r92 | void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept | ||
Alexandre Leroux
|
r83 | { | ||
Alexandre Leroux
|
r1075 | // Merges the data source (without taking its root) | ||
Alexandre Leroux
|
r92 | if (dataSource) { | ||
Alexandre Leroux
|
r1075 | for (auto i = 0, count = dataSource->childCount(); i < count; ++i) { | ||
m_Root->merge(*dataSource->child(i)); | ||||
} | ||||
updateTreeWidget(); | ||||
Alexandre Leroux
|
r92 | } | ||
Alexandre Leroux
|
r83 | } | ||
Alexandre Leroux
|
r143 | |||
Alexandre Leroux
|
r1074 | 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); | ||||
} | ||||
Alexandre Leroux
|
r475 | void DataSourceWidget::filterChanged(const QString &text) noexcept | ||
{ | ||||
Alexandre Leroux
|
r477 | auto validateItem = [&text](const DataSourceTreeWidgetItem &item) { | ||
auto regExp = QRegExp{text, Qt::CaseInsensitive, QRegExp::Wildcard}; | ||||
// An item is valid if any of its metadata validates the text filter | ||||
auto itemMetadata = item.data()->data(); | ||||
auto itemMetadataEnd = itemMetadata.cend(); | ||||
auto acceptFilter | ||||
= [®Exp](const auto &variant) { return variant.toString().contains(regExp); }; | ||||
return std::find_if(itemMetadata.cbegin(), itemMetadataEnd, acceptFilter) | ||||
!= itemMetadataEnd; | ||||
}; | ||||
// Applies filter on tree widget | ||||
DataSourceTreeWidgetHelper::filter(*ui->treeWidget, validateItem); | ||||
Alexandre Leroux
|
r475 | } | ||
Alexandre Leroux
|
r143 | void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept | ||
{ | ||||
// Retrieves the selected item in the tree, and build the menu from its actions | ||||
if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) { | ||||
QMenu treeMenu{}; | ||||
treeMenu.addActions(selectedItem->actions()); | ||||
if (!treeMenu.isEmpty()) { | ||||
Alexandre Leroux
|
r655 | treeMenu.exec(QCursor::pos()); | ||
Alexandre Leroux
|
r143 | } | ||
} | ||||
} | ||||