Skip to content

Commit

Permalink
Gui: add missing PropertyItemDelegate::eventFilter()
Browse files Browse the repository at this point in the history
For handling focus change when editing property
  • Loading branch information
realthunder authored and wwmayer committed Feb 25, 2022
1 parent d5b0329 commit 75dd3d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Gui/propertyeditor/PropertyItemDelegate.cpp
Expand Up @@ -37,6 +37,8 @@
#include "PropertyItemDelegate.h"
#include "PropertyItem.h"
#include "PropertyEditor.h"
#include "MDIView.h"
#include "Tree.h"

FC_LOG_LEVEL_INIT("PropertyView",true,true)

Expand Down Expand Up @@ -124,6 +126,40 @@ bool PropertyItemDelegate::editorEvent (QEvent * event, QAbstractItemModel* mode
return QItemDelegate::editorEvent(event, model, option, index);
}

bool PropertyItemDelegate::eventFilter(QObject *o, QEvent *ev)
{
if (ev->type() == QEvent::FocusOut) {
PropertyEditor *parentEditor = qobject_cast<PropertyEditor*>(this->parent());
auto widget = qobject_cast<QWidget*>(o);
if (widget && parentEditor && parentEditor->activeEditor
&& widget != parentEditor->activeEditor)
{
// We event filter child QAbstractButton and QLabel of an editor,
// which requires special focus change in order to not mess up with
// QItemDelegate's logic.
QWidget *w = QApplication::focusWidget();
// For some reason, Qt (5.15) on Windows will remove current focus
// before bringing up a modal dialog.
if (!w)
return false;
while (w) { // don't worry about focus changes internally in the editor
if (w == widget || w == parentEditor->activeEditor)
return false;

// ignore focus change to 3D view or tree view, because, for
// example DlgPropertyLink is implemented as modeless dialog
// to allow selection in 3D and tree view.
if (qobject_cast<MDIView*>(w))
return false;
if (qobject_cast<TreeWidget*>(w))
return false;
w = w->parentWidget();
}
}
}
return QItemDelegate::eventFilter(o, ev);
}

QWidget * PropertyItemDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & /*option*/,
const QModelIndex & index ) const
{
Expand Down
3 changes: 3 additions & 0 deletions src/Gui/propertyeditor/PropertyItemDelegate.h
Expand Up @@ -44,6 +44,9 @@ class PropertyItemDelegate : public QItemDelegate
virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
virtual bool editorEvent (QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem& option, const QModelIndex& index);
protected:
bool eventFilter(QObject *, QEvent *);

public Q_SLOTS:
void valueChanged();

Expand Down

0 comments on commit 75dd3d6

Please sign in to comment.