CatalogueSideBarWidget.cpp
113 lines
| 4.0 KiB
| text/x-c
|
CppLexer
r1130 | #include "Catalogue/CatalogueSideBarWidget.h" | |||
#include "ui_CatalogueSideBarWidget.h" | ||||
r1162 | #include <SqpApplication.h> | |||
#include <Catalogue/CatalogueController.h> | ||||
#include <Catalogue/CatalogueTreeWidgetItem.h> | ||||
#include <CatalogueDao.h> | ||||
#include <ComparaisonPredicate.h> | ||||
#include <DBCatalogue.h> | ||||
r1130 | ||||
r1138 | constexpr auto ALL_EVENT_ITEM_TYPE = QTreeWidgetItem::UserType; | |||
constexpr auto TRASH_ITEM_TYPE = QTreeWidgetItem::UserType + 1; | ||||
constexpr auto CATALOGUE_ITEM_TYPE = QTreeWidgetItem::UserType + 2; | ||||
constexpr auto DATABASE_ITEM_TYPE = QTreeWidgetItem::UserType + 3; | ||||
r1133 | ||||
struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate { | ||||
r1162 | ||||
QHash<QTreeWidgetItem *, DBCatalogue> m_CatalogueMap; | ||||
r1133 | void configureTreeWidget(QTreeWidget *treeWidget); | |||
r1137 | QTreeWidgetItem *addDatabaseItem(const QString &name, QTreeWidget *treeWidget); | |||
r1162 | void addCatalogueItem(const DBCatalogue &catalogue, QTreeWidgetItem *parentDatabaseItem); | |||
r1133 | }; | |||
r1130 | CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent) | |||
r1133 | : QWidget(parent), | |||
ui(new Ui::CatalogueSideBarWidget), | ||||
impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()} | ||||
r1130 | { | |||
ui->setupUi(this); | ||||
r1133 | impl->configureTreeWidget(ui->treeWidget); | |||
r1138 | ||||
r1140 | auto emitSelection = [this](auto item) { | |||
r1138 | switch (item->type()) { | |||
case CATALOGUE_ITEM_TYPE: | ||||
r1162 | emit this->catalogueSelected( | |||
static_cast<CatalogueTreeWidgetItem *>(item)->catalogue()); | ||||
r1138 | break; | |||
case ALL_EVENT_ITEM_TYPE: | ||||
emit this->allEventsSelected(); | ||||
break; | ||||
case TRASH_ITEM_TYPE: | ||||
emit this->trashSelected(); | ||||
break; | ||||
case DATABASE_ITEM_TYPE: | ||||
default: | ||||
break; | ||||
} | ||||
r1140 | }; | |||
connect(ui->treeWidget, &QTreeWidget::itemClicked, emitSelection); | ||||
connect(ui->treeWidget, &QTreeWidget::currentItemChanged, emitSelection); | ||||
r1130 | } | |||
CatalogueSideBarWidget::~CatalogueSideBarWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r1133 | ||||
void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget( | ||||
QTreeWidget *treeWidget) | ||||
{ | ||||
auto allEventsItem = new QTreeWidgetItem({"All Events"}, ALL_EVENT_ITEM_TYPE); | ||||
allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png")); | ||||
treeWidget->addTopLevelItem(allEventsItem); | ||||
auto trashItem = new QTreeWidgetItem({"Trash"}, TRASH_ITEM_TYPE); | ||||
trashItem->setIcon(0, QIcon(":/icones/trash.png")); | ||||
treeWidget->addTopLevelItem(trashItem); | ||||
auto separator = new QFrame(treeWidget); | ||||
separator->setFrameShape(QFrame::HLine); | ||||
auto separatorItem = new QTreeWidgetItem(); | ||||
separatorItem->setFlags(Qt::NoItemFlags); | ||||
treeWidget->addTopLevelItem(separatorItem); | ||||
treeWidget->setItemWidget(separatorItem, 0, separator); | ||||
r1137 | ||||
// Test | ||||
r1162 | auto &dao = sqpApp->catalogueController().getDao(); | |||
auto allPredicate = std::make_shared<ComparaisonPredicate>(QString{"uniqId"}, "-1", | ||||
ComparaisonOperation::DIFFERENT); | ||||
auto db = addDatabaseItem("Default", treeWidget); | ||||
r1137 | ||||
r1162 | auto catalogues = dao.getCatalogues(allPredicate); | |||
for (auto catalogue : catalogues) { | ||||
addCatalogueItem(catalogue, db); | ||||
} | ||||
r1137 | ||||
treeWidget->expandAll(); | ||||
} | ||||
QTreeWidgetItem * | ||||
CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name, | ||||
QTreeWidget *treeWidget) | ||||
{ | ||||
auto databaseItem = new QTreeWidgetItem({name}, DATABASE_ITEM_TYPE); | ||||
databaseItem->setIcon(0, QIcon(":/icones/database.png")); | ||||
treeWidget->addTopLevelItem(databaseItem); | ||||
return databaseItem; | ||||
} | ||||
void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem( | ||||
r1162 | const DBCatalogue &catalogue, QTreeWidgetItem *parentDatabaseItem) | |||
r1137 | { | |||
r1162 | auto catalogueItem = new CatalogueTreeWidgetItem(catalogue, CATALOGUE_ITEM_TYPE); | |||
r1137 | catalogueItem->setIcon(0, QIcon(":/icones/catalogue.png")); | |||
parentDatabaseItem->addChild(catalogueItem); | ||||
r1133 | } | |||