Gui: Add Property View delete key binding

This commit adds a binding for the delete key to delete properties from
property containers.
This commit is contained in:
Pieter Hijma
2025-09-01 17:18:48 +02:00
parent 0ac71c5e80
commit c0f440fad4
2 changed files with 79 additions and 16 deletions

View File

@@ -24,6 +24,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <algorithm>
#include <boost/algorithm/string/predicate.hpp>
#include <QApplication>
#include <QClipboard>
@@ -222,6 +223,54 @@ bool PropertyEditor::event(QEvent* event)
return QTreeView::event(event);
}
bool PropertyEditor::removeSelectedDynamicProperties()
{
std::unordered_set<App::Property*> props = acquireSelectedProperties();
if (props.empty()) {
return false;
}
bool canRemove = std::ranges::all_of(props, [](auto prop) {
return prop->testStatus(App::Property::PropDynamic)
&& !prop->testStatus(App::Property::LockDynamic);
});
if (!canRemove) {
return false;
}
removeProperties(props);
return true;
}
void PropertyEditor::keyPressEvent(QKeyEvent* event)
{
if (state() == QAbstractItemView::EditingState) {
QTreeView::keyPressEvent(event);
return;
}
const auto key = event->key();
const auto mods = event->modifiers();
const bool allowedModifiers =
mods == Qt::NoModifier ||
mods == Qt::KeypadModifier;
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
const bool isDeleteKey = key == Qt::Key_Backspace || key == Qt::Key_Delete;
#else
const bool isDeleteKey = key == Qt::Key_Delete;
#endif
if (allowedModifiers && isDeleteKey) {
if (removeSelectedDynamicProperties()) {
event->accept();
return;
}
}
QTreeView::keyPressEvent(event);
}
void PropertyEditor::commitData(QWidget* editor)
{
committing = true;
@@ -726,14 +775,8 @@ enum MenuAction
MA_Copy,
};
void PropertyEditor::contextMenuEvent(QContextMenuEvent*)
std::unordered_set<App::Property*> PropertyEditor::acquireSelectedProperties() const
{
QMenu menu;
QAction* autoExpand = nullptr;
auto contextIndex = currentIndex();
// acquiring the selected properties
std::unordered_set<App::Property*> props;
const auto indexes = selectedIndexes();
for (const auto& index : indexes) {
@@ -752,6 +795,30 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent*)
continue;
}
}
return props;
}
void PropertyEditor::removeProperties(const std::unordered_set<App::Property*>& props)
{
App::AutoTransaction committer("Remove property");
for (auto prop : props) {
try {
prop->getContainer()->removeDynamicProperty(prop->getName());
}
catch (Base::Exception& e) {
e.reportException();
}
}
}
void PropertyEditor::contextMenuEvent(QContextMenuEvent*)
{
QMenu menu;
QAction* autoExpand = nullptr;
auto contextIndex = currentIndex();
std::unordered_set<App::Property*> props = acquireSelectedProperties();
// copy value to clipboard
if (props.size() == 1) {
@@ -990,15 +1057,7 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent*)
return;
}
case MA_RemoveProp: {
App::AutoTransaction committer("Remove property");
for (auto prop : props) {
try {
prop->getContainer()->removeDynamicProperty(prop->getName());
}
catch (Base::Exception& e) {
e.reportException();
}
}
removeProperties(props);
break;
}
default:

View File

@@ -128,11 +128,15 @@ protected:
#endif
void contextMenuEvent(QContextMenuEvent* event) override;
bool event(QEvent*) override;
void keyPressEvent(QKeyEvent* event) override;
private:
void setEditorMode(const QModelIndex& parent, int start, int end);
void closeTransaction();
void recomputeDocument(App::Document*);
std::unordered_set<App::Property*> acquireSelectedProperties() const;
void removeProperties(const std::unordered_set<App::Property*>& props);
bool removeSelectedDynamicProperties();
// check if mouse_pos is around right or bottom side of a cell
// and return the index of that cell if found