##// END OF EJS Templates
Fix bug time precision with catalogue
Fix bug time precision with catalogue

File last commit:

r1368:2f97b648f297
r1369:b20dcec1c57b
Show More
CatalogueSideBarWidget.cpp
442 lines | 16.8 KiB | text/x-c | CppLexer
/ gui / src / Catalogue / CatalogueSideBarWidget.cpp
Sub widget classes
r1130 #include "Catalogue/CatalogueSideBarWidget.h"
#include "ui_CatalogueSideBarWidget.h"
Display catalogues and events with CatalogueAPI
r1162 #include <SqpApplication.h>
#include <Catalogue/CatalogueController.h>
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 #include <Catalogue/CatalogueExplorerHelper.h>
Refactoring of catalogue: use a custom item class
r1284 #include <Catalogue/CatalogueTreeItems/CatalogueTextTreeItem.h>
#include <Catalogue/CatalogueTreeItems/CatalogueTreeItem.h>
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 #include <Catalogue/CatalogueTreeModel.h>
Display catalogues and events with CatalogueAPI
r1162 #include <CatalogueDao.h>
Move event in catalogue with Drag&Drop
r1362 #include <Common/MimeTypesDef.h>
Display catalogues and events with CatalogueAPI
r1162 #include <ComparaisonPredicate.h>
#include <DBCatalogue.h>
Add supp shortcut for Catalogues
r1368 #include <QKeyEvent>
Basic context menu on a catalogue item
r1174 #include <QMenu>
Add catalogue handling
r1356 #include <QMessageBox>
Move event in catalogue with Drag&Drop
r1362 #include <QMimeData>
Basic context menu on a catalogue item
r1174
Edition of catalogues from the inspector
r1180 Q_LOGGING_CATEGORY(LOG_CatalogueSideBarWidget, "CatalogueSideBarWidget")
Sub widget classes
r1130
Refactoring of catalogue: use a custom item class
r1284 constexpr auto ALL_EVENT_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 1;
constexpr auto TRASH_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 2;
constexpr auto CATALOGUE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 3;
constexpr auto DATABASE_ITEM_TYPE = CatalogueAbstractTreeItem::DEFAULT_TYPE + 4;
Catalog side bar
r1133
struct CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate {
Display catalogues and events with CatalogueAPI
r1162
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 CatalogueTreeModel *m_TreeModel = nullptr;
void configureTreeWidget(QTreeView *treeView);
QModelIndex addDatabaseItem(const QString &name);
Refactoring of catalogue: use a custom item class
r1284 CatalogueAbstractTreeItem *getDatabaseItem(const QString &name);
Add addCatalogue method
r1357 CatalogueAbstractTreeItem *addCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue,
const QModelIndex &databaseIndex);
Edition of catalogues from the inspector
r1180
Refactoring of catalogue: use a custom item class
r1284 CatalogueTreeItem *getCatalogueItem(const std::shared_ptr<DBCatalogue> &catalogue) const;
Add catalogue handling
r1356 void setHasChanges(bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 bool hasChanges(const QModelIndex &index, QTreeView *treeView);
Drop of events on a catalogue
r1285
int selectionType(QTreeView *treeView) const
{
auto selectedItems = treeView->selectionModel()->selectedRows();
if (selectedItems.isEmpty()) {
return CatalogueAbstractTreeItem::DEFAULT_TYPE;
}
else {
auto firstIndex = selectedItems.first();
auto firstItem = m_TreeModel->item(firstIndex);
if (!firstItem) {
Q_ASSERT(false);
return CatalogueAbstractTreeItem::DEFAULT_TYPE;
}
auto selectionType = firstItem->type();
for (auto itemIndex : selectedItems) {
auto item = m_TreeModel->item(itemIndex);
if (!item || item->type() != selectionType) {
// Incoherent multi selection
selectionType = CatalogueAbstractTreeItem::DEFAULT_TYPE;
break;
}
}
return selectionType;
}
}
QVector<std::shared_ptr<DBCatalogue> > selectedCatalogues(QTreeView *treeView) const
{
QVector<std::shared_ptr<DBCatalogue> > catalogues;
auto selectedItems = treeView->selectionModel()->selectedRows();
for (auto itemIndex : selectedItems) {
auto item = m_TreeModel->item(itemIndex);
if (item && item->type() == CATALOGUE_ITEM_TYPE) {
catalogues.append(static_cast<CatalogueTreeItem *>(item)->catalogue());
}
}
return catalogues;
}
QStringList selectedRepositories(QTreeView *treeView) const
{
QStringList repositories;
auto selectedItems = treeView->selectionModel()->selectedRows();
for (auto itemIndex : selectedItems) {
auto item = m_TreeModel->item(itemIndex);
if (item && item->type() == DATABASE_ITEM_TYPE) {
repositories.append(item->text());
}
}
return repositories;
}
Catalog side bar
r1133 };
Sub widget classes
r1130 CatalogueSideBarWidget::CatalogueSideBarWidget(QWidget *parent)
Catalog side bar
r1133 : QWidget(parent),
ui(new Ui::CatalogueSideBarWidget),
impl{spimpl::make_unique_impl<CatalogueSideBarWidgetPrivate>()}
Sub widget classes
r1130 {
ui->setupUi(this);
Basic interactions
r1138
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 impl->m_TreeModel = new CatalogueTreeModel(this);
ui->treeView->setModel(impl->m_TreeModel);
impl->configureTreeWidget(ui->treeView);
ui->treeView->header()->setStretchLastSection(false);
ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
Display of the save & cancel button next to a catalogue
r1175
Add catalogue handling
r1356 connect(ui->treeView, &QTreeView::clicked, this, &CatalogueSideBarWidget::emitSelection);
connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this,
&CatalogueSideBarWidget::emitSelection);
Add addCatalogue method
r1357 connect(ui->btnAdd, &QToolButton::clicked, [this]() {
auto catalogue = std::make_shared<DBCatalogue>();
catalogue->setName(QString("Cat"));
sqpApp->catalogueController().addCatalogue(catalogue);
auto item = this->addCatalogue(catalogue, REPOSITORY_DEFAULT);
this->setCatalogueChanges(catalogue, true);
ui->treeView->edit(impl->m_TreeModel->indexOf(item));
Add catalogue handling
r1356
Add addCatalogue method
r1357 });
Add catalogue handling
r1356
Move event in catalogue with Drag&Drop
r1362 connect(impl->m_TreeModel, &CatalogueTreeModel::itemDropped,
[this](auto index, auto mimeData, auto action) {
auto item = impl->m_TreeModel->item(index);
if (item && item->type() == CATALOGUE_ITEM_TYPE) {
auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
this->setCatalogueChanges(catalogue, true);
}
if (action == Qt::MoveAction) {
/// Display a save button on source catalogues
auto sourceCatalogues = sqpApp->catalogueController().cataloguesForMimeData(
mimeData->data(MIME_TYPE_SOURCE_CATALOGUE_LIST));
for (auto catalogue : sourceCatalogues) {
if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
this->setCatalogueChanges(catalogue, true);
}
}
}
});
Add catalogue handling
r1356 connect(ui->btnRemove, &QToolButton::clicked, [this]() {
QVector<QPair<std::shared_ptr<DBCatalogue>, CatalogueAbstractTreeItem *> >
cataloguesToItems;
auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
for (auto index : selectedIndexes) {
auto item = impl->m_TreeModel->item(index);
if (item && item->type() == CATALOGUE_ITEM_TYPE) {
auto catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
cataloguesToItems << qMakePair(catalogue, item);
}
}
if (!cataloguesToItems.isEmpty()) {
if (QMessageBox::warning(this, tr("Remove Catalogue(s)"),
tr("The selected catalogues(s) will be completly removed "
"from the repository!\nAre you sure you want to continue?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
== QMessageBox::Yes) {
for (auto catalogueToItem : cataloguesToItems) {
sqpApp->catalogueController().removeCatalogue(catalogueToItem.first);
impl->m_TreeModel->removeChildItem(
catalogueToItem.second,
impl->m_TreeModel->indexOf(catalogueToItem.second->parent()));
}
}
}
});
Skeleton to fill the inspector with the selection
r1140
Add catalogue handling
r1356 connect(impl->m_TreeModel, &CatalogueTreeModel::itemRenamed, [this](auto index) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto selectedIndexes = ui->treeView->selectionModel()->selectedRows();
if (selectedIndexes.contains(index)) {
Add catalogue handling
r1356 this->emitSelection();
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 }
Add catalogue handling
r1356 impl->setHasChanges(true, index, this);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 });
Basic context menu on a catalogue item
r1174
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
Refactoring of catalogue: use a custom item class
r1284 connect(ui->treeView, &QTreeView::customContextMenuRequested, this,
Basic context menu on a catalogue item
r1174 &CatalogueSideBarWidget::onContextMenuRequested);
Sub widget classes
r1130 }
CatalogueSideBarWidget::~CatalogueSideBarWidget()
{
delete ui;
}
Catalog side bar
r1133
Add addCatalogue method
r1357 CatalogueAbstractTreeItem *
CatalogueSideBarWidget::addCatalogue(const std::shared_ptr<DBCatalogue> &catalogue,
const QString &repository)
Updates model after an event has been created through the colored zone
r1286 {
auto repositoryItem = impl->getDatabaseItem(repository);
Add addCatalogue method
r1357 return impl->addCatalogueItem(catalogue, impl->m_TreeModel->indexOf(repositoryItem));
Updates model after an event has been created through the colored zone
r1286 }
Edition of catalogues from the inspector
r1180 void CatalogueSideBarWidget::setCatalogueChanges(const std::shared_ptr<DBCatalogue> &catalogue,
bool hasChanges)
{
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 if (auto catalogueItem = impl->getCatalogueItem(catalogue)) {
auto index = impl->m_TreeModel->indexOf(catalogueItem);
Add catalogue handling
r1356 impl->setHasChanges(hasChanges, index, this);
Refactoring of catalogue: use a custom item class
r1284 // catalogueItem->refresh();
Edition of catalogues from the inspector
r1180 }
}
Updates model after an event has been created through the colored zone
r1286 QVector<std::shared_ptr<DBCatalogue> >
CatalogueSideBarWidget::getCatalogues(const QString &repository) const
{
QVector<std::shared_ptr<DBCatalogue> > result;
auto repositoryItem = impl->getDatabaseItem(repository);
for (auto child : repositoryItem->children()) {
if (child->type() == CATALOGUE_ITEM_TYPE) {
auto catalogueItem = static_cast<CatalogueTreeItem *>(child);
result << catalogueItem->catalogue();
}
else {
qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogues: invalid structure";
}
}
return result;
}
Add catalogue handling
r1356 void CatalogueSideBarWidget::emitSelection()
{
auto selectionType = impl->selectionType(ui->treeView);
switch (selectionType) {
case CATALOGUE_ITEM_TYPE:
emit this->catalogueSelected(impl->selectedCatalogues(ui->treeView));
break;
case DATABASE_ITEM_TYPE:
emit this->databaseSelected(impl->selectedRepositories(ui->treeView));
break;
case ALL_EVENT_ITEM_TYPE:
emit this->allEventsSelected();
break;
case TRASH_ITEM_TYPE:
emit this->trashSelected();
break;
default:
emit this->selectionCleared();
break;
}
}
Basic context menu on a catalogue item
r1174 void CatalogueSideBarWidget::onContextMenuRequested(const QPoint &pos)
{
QMenu menu{this};
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto currentIndex = ui->treeView->currentIndex();
auto currentItem = impl->m_TreeModel->item(currentIndex);
if (!currentItem) {
return;
}
Basic context menu on a catalogue item
r1174 switch (currentItem->type()) {
case CATALOGUE_ITEM_TYPE:
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 menu.addAction("Rename", [this, currentIndex]() { ui->treeView->edit(currentIndex); });
Basic context menu on a catalogue item
r1174 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()) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 menu.exec(ui->treeView->mapToGlobal(pos));
Basic context menu on a catalogue item
r1174 }
}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::configureTreeWidget(QTreeView *treeView)
Catalog side bar
r1133 {
Refactoring of catalogue: use a custom item class
r1284 auto allEventsItem = new CatalogueTextTreeItem{QIcon{":/icones/allEvents.png"}, "All Events",
ALL_EVENT_ITEM_TYPE};
Some fixes
r1290 auto allEventIndex = m_TreeModel->addTopLevelItem(allEventsItem);
treeView->setCurrentIndex(allEventIndex);
Catalog side bar
r1133
Refactoring of catalogue: use a custom item class
r1284 auto trashItem
= new CatalogueTextTreeItem{QIcon{":/icones/trash.png"}, "Trash", TRASH_ITEM_TYPE};
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 m_TreeModel->addTopLevelItem(trashItem);
Catalog side bar
r1133
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto separator = new QFrame{treeView};
Catalog side bar
r1133 separator->setFrameShape(QFrame::HLine);
Refactoring of catalogue: use a custom item class
r1284 auto separatorItem
= new CatalogueTextTreeItem{QIcon{}, QString{}, CatalogueAbstractTreeItem::DEFAULT_TYPE};
separatorItem->setEnabled(false);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto separatorIndex = m_TreeModel->addTopLevelItem(separatorItem);
treeView->setIndexWidget(separatorIndex, separator);
Methods to add some catalogue items
r1137
Adaptation to last version of catalogue controller
r1196 auto repositories = sqpApp->catalogueController().getRepositories();
for (auto dbname : repositories) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto dbIndex = addDatabaseItem(dbname);
Adaptation to last version of catalogue controller
r1196 auto catalogues = sqpApp->catalogueController().retrieveCatalogues(dbname);
for (auto catalogue : catalogues) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 addCatalogueItem(catalogue, dbIndex);
Adaptation to last version of catalogue controller
r1196 }
Display catalogues and events with CatalogueAPI
r1162 }
Methods to add some catalogue items
r1137
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 treeView->expandAll();
Methods to add some catalogue items
r1137 }
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 QModelIndex
CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addDatabaseItem(const QString &name)
Methods to add some catalogue items
r1137 {
Refactoring of catalogue: use a custom item class
r1284 auto databaseItem
= new CatalogueTextTreeItem{QIcon{":/icones/database.png"}, {name}, DATABASE_ITEM_TYPE};
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto databaseIndex = m_TreeModel->addTopLevelItem(databaseItem);
Methods to add some catalogue items
r1137
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 return databaseIndex;
Methods to add some catalogue items
r1137 }
Refactoring of catalogue: use a custom item class
r1284 CatalogueAbstractTreeItem *
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getDatabaseItem(const QString &name)
Methods to facilitate add/remove operations
r1167 {
Refactoring of catalogue: use a custom item class
r1284 for (auto item : m_TreeModel->topLevelItems()) {
if (item->type() == DATABASE_ITEM_TYPE && item->text() == name) {
Methods to facilitate add/remove operations
r1167 return item;
}
}
return nullptr;
}
Add addCatalogue method
r1357 CatalogueAbstractTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::addCatalogueItem(
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 const std::shared_ptr<DBCatalogue> &catalogue, const QModelIndex &databaseIndex)
Methods to add some catalogue items
r1137 {
Refactoring of catalogue: use a custom item class
r1284 auto catalogueItem
= new CatalogueTreeItem{catalogue, QIcon{":/icones/catalogue.png"}, CATALOGUE_ITEM_TYPE};
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 m_TreeModel->addChildItem(catalogueItem, databaseIndex);
Add addCatalogue method
r1357
return catalogueItem;
Catalog side bar
r1133 }
Edition of catalogues from the inspector
r1180
Refactoring of catalogue: use a custom item class
r1284 CatalogueTreeItem *CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::getCatalogueItem(
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 const std::shared_ptr<DBCatalogue> &catalogue) const
Edition of catalogues from the inspector
r1180 {
Refactoring of catalogue: use a custom item class
r1284 for (auto item : m_TreeModel->topLevelItems()) {
Edition of catalogues from the inspector
r1180 if (item->type() == DATABASE_ITEM_TYPE) {
Refactoring of catalogue: use a custom item class
r1284 for (auto childItem : item->children()) {
Edition of catalogues from the inspector
r1180 if (childItem->type() == CATALOGUE_ITEM_TYPE) {
Refactoring of catalogue: use a custom item class
r1284 auto catalogueItem = static_cast<CatalogueTreeItem *>(childItem);
Move event in catalogue with Drag&Drop
r1362 if (catalogueItem->catalogue()->getUniqId() == catalogue->getUniqId()) {
Edition of catalogues from the inspector
r1180 return catalogueItem;
}
}
else {
qCWarning(LOG_CatalogueSideBarWidget()) << "getCatalogueItem: Invalid tree "
"structure. A database item should "
"only contain catalogues.";
Q_ASSERT(false);
}
}
}
}
return nullptr;
}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283
Add catalogue handling
r1356 void CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::setHasChanges(
bool value, const QModelIndex &index, CatalogueSideBarWidget *sideBarWidget)
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 {
Add catalogue handling
r1356 std::shared_ptr<DBCatalogue> catalogue = nullptr;
auto item = m_TreeModel->item(index);
if (item && item->type() == CATALOGUE_ITEM_TYPE) {
catalogue = static_cast<CatalogueTreeItem *>(item)->catalogue();
}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
if (value) {
Add catalogue handling
r1356 if (!hasChanges(validationIndex, sideBarWidget->ui->treeView)) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 auto widget = CatalogueExplorerHelper::buildValidationWidget(
Add catalogue handling
r1356 sideBarWidget->ui->treeView,
[this, validationIndex, sideBarWidget, catalogue]() {
if (catalogue) {
sqpApp->catalogueController().saveCatalogue(catalogue);
}
setHasChanges(false, validationIndex, sideBarWidget);
},
[this, validationIndex, sideBarWidget, catalogue, item]() {
if (catalogue) {
bool removed;
sqpApp->catalogueController().discardCatalogue(catalogue, removed);
if (removed) {
m_TreeModel->removeChildItem(item,
m_TreeModel->indexOf(item->parent()));
}
else {
m_TreeModel->refresh(m_TreeModel->indexOf(item));
setHasChanges(false, validationIndex, sideBarWidget);
}
sideBarWidget->emitSelection();
}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 });
Add catalogue handling
r1356 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, widget);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 }
}
else {
// Note: the widget is destroyed
Add catalogue handling
r1356 sideBarWidget->ui->treeView->setIndexWidget(validationIndex, nullptr);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1283 }
}
bool CatalogueSideBarWidget::CatalogueSideBarWidgetPrivate::hasChanges(const QModelIndex &index,
QTreeView *treeView)
{
auto validationIndex = index.sibling(index.row(), (int)CatalogueTreeModel::Column::Validation);
return treeView->indexWidget(validationIndex) != nullptr;
}
Add supp shortcut for Catalogues
r1368
void CatalogueSideBarWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Delete: {
ui->btnRemove->click();
}
default:
break;
}
}