From f5b4fc9a0b2c9518c73ddbc3b87a59ce5c4dbece Mon Sep 17 00:00:00 2001 From: Paddle Date: Mon, 8 Jan 2024 10:33:17 +0100 Subject: [PATCH] Property editor: Hide headers and enable resizing on all cells. --- src/Gui/propertyeditor/PropertyEditor.cpp | 59 +++++++++++++++++++++++ src/Gui/propertyeditor/PropertyEditor.h | 11 +++++ 2 files changed, 70 insertions(+) diff --git a/src/Gui/propertyeditor/PropertyEditor.cpp b/src/Gui/propertyeditor/PropertyEditor.cpp index d86e2629d9..b0b0e3489b 100644 --- a/src/Gui/propertyeditor/PropertyEditor.cpp +++ b/src/Gui/propertyeditor/PropertyEditor.cpp @@ -27,6 +27,7 @@ # include # include # include +# include # include # include #endif @@ -59,6 +60,7 @@ PropertyEditor::PropertyEditor(QWidget *parent) , binding(false) , checkDocument(false) , closingEditor(false) + , dragInProgress(false) { propertyModel = new PropertyModel(this); setModel(propertyModel); @@ -93,6 +95,10 @@ PropertyEditor::PropertyEditor(QWidget *parent) connect(this, &QTreeView::collapsed, this, &PropertyEditor::onItemCollapsed); connect(propertyModel, &QAbstractItemModel::rowsMoved, this, &PropertyEditor::onRowsMoved); connect(propertyModel, &QAbstractItemModel::rowsRemoved, this, &PropertyEditor::onRowsRemoved); + + setHeaderHidden(true); + viewport()->installEventFilter(this); + viewport()->setMouseTracking(true); } PropertyEditor::~PropertyEditor() @@ -827,4 +833,57 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent *) { } } + +bool PropertyEditor::eventFilter(QObject* object, QEvent* event) { + if (object == viewport()) { + QMouseEvent* mouse_event = dynamic_cast(event); + if (mouse_event) { + if (mouse_event->type() == QEvent::MouseMove) { + if (dragInProgress) { // apply dragging + QHeaderView* header_view = header(); + int delta = mouse_event->pos().x() - dragPreviousPos; + dragPreviousPos = mouse_event->pos().x(); + //using minimal size = dragSensibility * 2 to prevent collapsing + header_view->resizeSection(dragSection, + qMax(dragSensibility * 2, header_view->sectionSize(dragSection) + delta)); + return true; + } + else { // set mouse cursor shape + if (indexResizable(mouse_event->pos()).isValid()) { + viewport()->setCursor(Qt::SplitHCursor); + } + else { + viewport()->setCursor(QCursor()); + } + } + } + else if (mouse_event->type() == QEvent::MouseButtonPress && mouse_event->button() == Qt::LeftButton && !dragInProgress) { + if (indexResizable(mouse_event->pos()).isValid()) { + dragInProgress = true; + dragPreviousPos = mouse_event->x(); + dragSection = indexResizable(mouse_event->pos()).column(); + return true; + } + } + else if (mouse_event->type() == QEvent::MouseButtonRelease && + mouse_event->button() == Qt::LeftButton && dragInProgress) { + dragInProgress = false; + return true; + } + } + } + return false; +} + +QModelIndex PropertyEditor::indexResizable(QPoint mouse_pos) { + QModelIndex index = indexAt(mouse_pos - QPoint(dragSensibility + 1, 0)); + if (index.isValid()) { + if (qAbs(visualRect(index).right() - mouse_pos.x()) < dragSensibility && + header()->sectionResizeMode(index.column()) == QHeaderView::Interactive) { + return index; + } + } + return QModelIndex(); +} + #include "moc_PropertyEditor.cpp" diff --git a/src/Gui/propertyeditor/PropertyEditor.h b/src/Gui/propertyeditor/PropertyEditor.h index 6f67e71766..bd01c2f99e 100644 --- a/src/Gui/propertyeditor/PropertyEditor.h +++ b/src/Gui/propertyeditor/PropertyEditor.h @@ -100,6 +100,7 @@ protected Q_SLOTS: void onRowsRemoved(const QModelIndex &parent, int start, int end); protected: + bool eventFilter(QObject* object, QEvent* event); void closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint) override; void commitData (QWidget * editor) override; void editorDestroyed (QObject * editor) override; @@ -120,6 +121,10 @@ private: void closeTransaction(); void recomputeDocument(App::Document*); + // check if mouse_pos is around right or bottom side of a cell + // and return the index of that cell if found + QModelIndex indexResizable(QPoint mouse_pos); + private: PropertyItemDelegate *delegate; PropertyModel* propertyModel; @@ -133,6 +138,12 @@ private: bool binding; bool checkDocument; bool closingEditor; + bool dragInProgress; + + //max distance between mouse and a cell, small enough to trigger resize + int dragSensibility = 5; // NOLINT + int dragSection = 0; + int dragPreviousPos = 0; int transactionID = 0;