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