CatalogueTreeWidgetItem.cpp
91 lines
| 2.6 KiB
| text/x-c
|
CppLexer
r1129 | #include "Catalogue/CatalogueTreeWidgetItem.h" | |||
r1162 | #include <Catalogue/CatalogueExplorerHelper.h> | |||
r1129 | ||||
r1164 | #include <Catalogue/CatalogueController.h> | |||
#include <SqpApplication.h> | ||||
r1142 | #include <memory> | |||
r1143 | #include <DBCatalogue.h> | |||
r1142 | ||||
r1147 | /// Column in the tree widget where the apply and cancel buttons must appear | |||
const auto APPLY_CANCEL_BUTTONS_COLUMN = 1; | ||||
r1129 | struct CatalogueTreeWidgetItem::CatalogueTreeWidgetItemPrivate { | |||
r1143 | std::shared_ptr<DBCatalogue> m_Catalogue; | |||
r1129 | ||||
r1143 | CatalogueTreeWidgetItemPrivate(std::shared_ptr<DBCatalogue> catalogue) : m_Catalogue(catalogue) | |||
{ | ||||
} | ||||
r1129 | }; | |||
r1143 | CatalogueTreeWidgetItem::CatalogueTreeWidgetItem(std::shared_ptr<DBCatalogue> catalogue, int type) | |||
r1129 | : QTreeWidgetItem(type), | |||
impl{spimpl::make_unique_impl<CatalogueTreeWidgetItemPrivate>(catalogue)} | ||||
{ | ||||
r1140 | setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); | |||
r1129 | } | |||
QVariant CatalogueTreeWidgetItem::data(int column, int role) const | ||||
{ | ||||
r1142 | if (column == 0) { | |||
switch (role) { | ||||
case Qt::EditRole: // fallthrough | ||||
case Qt::DisplayRole: | ||||
r1143 | return impl->m_Catalogue->getName(); | |||
r1142 | default: | |||
break; | ||||
} | ||||
r1129 | } | |||
return QTreeWidgetItem::data(column, role); | ||||
} | ||||
r1140 | void CatalogueTreeWidgetItem::setData(int column, int role, const QVariant &value) | |||
{ | ||||
if (role == Qt::EditRole && column == 0) { | ||||
auto newName = value.toString(); | ||||
r1143 | if (newName != impl->m_Catalogue->getName()) { | |||
r1142 | setText(0, newName); | |||
r1143 | impl->m_Catalogue->setName(newName); | |||
r1164 | sqpApp->catalogueController().updateCatalogue(impl->m_Catalogue); | |||
r1142 | setHasChanges(true); | |||
} | ||||
r1140 | } | |||
else { | ||||
QTreeWidgetItem::setData(column, role, value); | ||||
} | ||||
} | ||||
r1143 | std::shared_ptr<DBCatalogue> CatalogueTreeWidgetItem::catalogue() const | |||
r1129 | { | |||
return impl->m_Catalogue; | ||||
} | ||||
r1142 | ||||
void CatalogueTreeWidgetItem::setHasChanges(bool value) | ||||
{ | ||||
if (value) { | ||||
r1162 | if (!hasChanges()) { | |||
auto widget = CatalogueExplorerHelper::buildValidationWidget( | ||||
treeWidget(), [this]() { setHasChanges(false); }, | ||||
[this]() { setHasChanges(false); }); | ||||
treeWidget()->setItemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN, widget); | ||||
r1147 | } | |||
r1142 | } | |||
else { | ||||
// Note: the widget is destroyed | ||||
r1147 | treeWidget()->setItemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN, nullptr); | |||
r1142 | } | |||
} | ||||
r1147 | ||||
r1162 | bool CatalogueTreeWidgetItem::hasChanges() | |||
{ | ||||
return treeWidget()->itemWidget(this, APPLY_CANCEL_BUTTONS_COLUMN) != nullptr; | ||||
} | ||||
r1147 | void CatalogueTreeWidgetItem::refresh() | |||
{ | ||||
emitDataChanged(); | ||||
} | ||||