CatalogueSideBarWidget.cpp
247 lines
| 9.2 KiB
| text/x-c
|
CppLexer
r1095 | #include "Catalogue/CatalogueSideBarWidget.h" | |||
#include "ui_CatalogueSideBarWidget.h" | ||||
r1129 | #include <SqpApplication.h> | |||
#include <Catalogue/CatalogueController.h> | ||||
#include <Catalogue/CatalogueTreeWidgetItem.h> | ||||
#include <CatalogueDao.h> | ||||
#include <ComparaisonPredicate.h> | ||||
#include <DBCatalogue.h> | ||||
r1141 | #include <QMenu> | |||
r1147 | Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget") | |||
r1095 | ||||
r1103 | 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; | ||||
r1098 | ||||
struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate { | ||||
r1129 | ||||
r1098 | void configureTreeWidget(QTreeWidget *treeWidget); | |||
r1102 | QTreeWidgetItem *addDatabaseItem(const QString &name, QTreeWidget *treeWidget); | |||
r1134 | QTreeWidgetItem *getDatabaseItem(const QString &name, QTreeWidget *treeWidget); | |||
r1143 | void addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue, | |||
QTreeWidgetItem *parentDatabaseItem); | ||||
r1147 | ||||
CatalogueTreeWidgetItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue, | ||||
QTreeWidget *treeWidget) const; | ||||
r1098 | }; | |||
r1095 | CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent) | |||
r1098 | : QWidget(parent), | |||
ui(new Ui::CatalogueSideBarWidget), | ||||
impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()} | ||||
r1095 | { | |||
ui->setupUi(this); | ||||
r1098 | impl->configureTreeWidget(ui->treeWidget); | |||
r1103 | ||||
r1142 | ui->treeWidget->setColumnCount(2); | |||
ui->treeWidget->header()->setStretchLastSection(false); | ||||
ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); | ||||
ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch); | ||||
r1132 | auto emitSelection = [this]() { | |||
auto selectedItems = ui->treeWidget->selectedItems(); | ||||
if (selectedItems.isEmpty()) { | ||||
emit this->selectionCleared(); | ||||
} | ||||
else { | ||||
r1143 | QVector<std::shared_ptr<DBCatalogue> > catalogues; | |||
r1132 | QStringList databases; | |||
int selectionType = selectedItems.first()->type(); | ||||
for (auto item : ui->treeWidget->selectedItems()) { | ||||
if (item->type() == selectionType) { | ||||
switch (selectionType) { | ||||
case CATALOGUE_ITEM_TYPE: | ||||
catalogues.append( | ||||
static_cast<CatalogueTreeWidgetItem *>(item)->catalogue()); | ||||
break; | ||||
case DATABASE_ITEM_TYPE: | ||||
selectionType = DATABASE_ITEM_TYPE; | ||||
databases.append(item->text(0)); | ||||
case ALL_EVENT_ITEM_TYPE: // fallthrough | ||||
case TRASH_ITEM_TYPE: // fallthrough | ||||
default: | ||||
break; | ||||
} | ||||
} | ||||
else { | ||||
// Incoherent multi selection | ||||
selectionType = -1; | ||||
break; | ||||
} | ||||
} | ||||
switch (selectionType) { | ||||
case CATALOGUE_ITEM_TYPE: | ||||
emit this->catalogueSelected(catalogues); | ||||
break; | ||||
case DATABASE_ITEM_TYPE: | ||||
emit this->databaseSelected(databases); | ||||
break; | ||||
case ALL_EVENT_ITEM_TYPE: | ||||
emit this->allEventsSelected(); | ||||
break; | ||||
case TRASH_ITEM_TYPE: | ||||
emit this->trashSelected(); | ||||
break; | ||||
default: | ||||
emit this->selectionCleared(); | ||||
break; | ||||
} | ||||
r1103 | } | |||
r1132 | ||||
r1105 | }; | |||
connect(ui->treeWidget, &QTreeWidget::itemClicked, emitSelection); | ||||
connect(ui->treeWidget, &QTreeWidget::currentItemChanged, emitSelection); | ||||
r1147 | connect(ui->treeWidget, &QTreeWidget::itemChanged, | |||
[emitSelection, this](auto item, auto column) { | ||||
auto selectedItems = ui->treeWidget->selectedItems(); | ||||
qDebug() << "ITEM CHANGED" << column; | ||||
if (selectedItems.contains(item) && column == 0) { | ||||
emitSelection(); | ||||
} | ||||
}); | ||||
r1141 | ||||
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); | ||||
connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, | ||||
&CatalogueSideBarWidget::onContextMenuRequested); | ||||
r1095 | } | |||
CatalogueSideBarWidget::~CatalogueSideBarWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r1098 | ||||
r1147 | void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue, | |||
bool hasChanges) | ||||
{ | ||||
if (auto catalogueItem = impl->getCatalogueItem(catalogue, ui->treeWidget)) { | ||||
catalogueItem->setHasChanges(hasChanges); | ||||
catalogueItem->refresh(); | ||||
} | ||||
} | ||||
r1141 | void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos) | |||
{ | ||||
QMenu menu{this}; | ||||
auto currentItem = ui->treeWidget->currentItem(); | ||||
switch (currentItem->type()) { | ||||
case CATALOGUE_ITEM_TYPE: | ||||
menu.addAction("Rename", | ||||
[this, currentItem]() { ui->treeWidget->editItem(currentItem); }); | ||||
break; | ||||
case DATABASE_ITEM_TYPE: | ||||
break; | ||||
case ALL_EVENT_ITEM_TYPE: | ||||
break; | ||||
case TRASH_ITEM_TYPE: | ||||
menu.addAction("Empty Trash", []() { | ||||
// TODO | ||||
}); | ||||
break; | ||||
default: | ||||
break; | ||||
} | ||||
if (!menu.isEmpty()) { | ||||
menu.exec(ui->treeWidget->mapToGlobal(pos)); | ||||
} | ||||
} | ||||
r1098 | void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget( | |||
QTreeWidget *treeWidget) | ||||
{ | ||||
r1133 | auto allEventsItem = new QTreeWidgetItem{{"All Events"}, ALL_EVENT_ITEM_TYPE}; | |||
r1098 | allEventsItem->setIcon(0, QIcon(":/icones/allEvents.png")); | |||
treeWidget->addTopLevelItem(allEventsItem); | ||||
r1133 | auto trashItem = new QTreeWidgetItem{{"Trash"}, TRASH_ITEM_TYPE}; | |||
r1098 | trashItem->setIcon(0, QIcon(":/icones/trash.png")); | |||
treeWidget->addTopLevelItem(trashItem); | ||||
r1133 | auto separator = new QFrame{treeWidget}; | |||
r1098 | separator->setFrameShape(QFrame::HLine); | |||
r1133 | auto separatorItem = new QTreeWidgetItem{}; | |||
r1098 | separatorItem->setFlags(Qt::NoItemFlags); | |||
treeWidget->addTopLevelItem(separatorItem); | ||||
treeWidget->setItemWidget(separatorItem, 0, separator); | ||||
r1102 | ||||
r1129 | auto db = addDatabaseItem("Default", treeWidget); | |||
r1102 | ||||
r1159 | auto catalogues = sqpApp->catalogueController().retrieveCatalogues("Default"); | |||
r1129 | for (auto catalogue : catalogues) { | |||
addCatalogueItem(catalogue, db); | ||||
} | ||||
r1102 | ||||
treeWidget->expandAll(); | ||||
} | ||||
QTreeWidgetItem * | ||||
CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name, | ||||
QTreeWidget *treeWidget) | ||||
{ | ||||
r1133 | auto databaseItem = new QTreeWidgetItem{{name}, DATABASE_ITEM_TYPE}; | |||
databaseItem->setIcon(0, QIcon{":/icones/database.png"}); | ||||
r1102 | treeWidget->addTopLevelItem(databaseItem); | |||
return databaseItem; | ||||
} | ||||
r1134 | QTreeWidgetItem * | |||
CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name, | ||||
QTreeWidget *treeWidget) | ||||
{ | ||||
for (auto i = 0; i < treeWidget->topLevelItemCount(); ++i) { | ||||
auto item = treeWidget->topLevelItem(i); | ||||
if (item->type() == DATABASE_ITEM_TYPE && item->text(0) == name) { | ||||
return item; | ||||
} | ||||
} | ||||
return nullptr; | ||||
} | ||||
r1102 | void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem( | |||
r1143 | const std::shared_ptr<DBCatalogue> &catalogue, QTreeWidgetItem *parentDatabaseItem) | |||
r1102 | { | |||
r1133 | auto catalogueItem = new CatalogueTreeWidgetItem{catalogue, CATALOGUE_ITEM_TYPE}; | |||
catalogueItem->setIcon(0, QIcon{":/icones/catalogue.png"}); | ||||
r1102 | parentDatabaseItem->addChild(catalogueItem); | |||
r1098 | } | |||
r1147 | ||||
CatalogueTreeWidgetItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem( | ||||
const std::shared_ptr<DBCatalogue> &catalogue, QTreeWidget *treeWidget) const | ||||
{ | ||||
for (auto i = 0; i < treeWidget->topLevelItemCount(); ++i) { | ||||
auto item = treeWidget->topLevelItem(i); | ||||
if (item->type() == DATABASE_ITEM_TYPE) { | ||||
for (auto j = 0; j < item->childCount(); ++j) { | ||||
auto childItem = item->child(j); | ||||
if (childItem->type() == CATALOGUE_ITEM_TYPE) { | ||||
auto catalogueItem = static_cast<CatalogueTreeWidgetItem *>(childItem); | ||||
if (catalogueItem->catalogue() == catalogue) { | ||||
return catalogueItem; | ||||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogueItem: Invalid tree " | ||||
"structure. A database item should " | ||||
"only contain catalogues."; | ||||
Q_ASSERT(false); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
return nullptr; | ||||
} | ||||