qtquick: Move the event redirector to the base class

Instead of having it in TitleBarQuick.
It will be needed by the TabWidget too
This commit is contained in:
Sergio Martins
2020-12-20 17:10:46 +00:00
parent add577c46f
commit b73c526c57
5 changed files with 47 additions and 22 deletions

View File

@@ -22,6 +22,7 @@
#include "FloatingWindow_p.h"
#include "MainWindowBase.h"
#include "DockRegistry_p.h"
#include "Utils_p.h"
#include <QResizeEvent>
#include <QMouseEvent>
@@ -32,6 +33,37 @@
using namespace KDDockWidgets;
namespace KDDockWidgets {
/**
* @brief Event filter which redirects mouse events from one QObject to another.
* Needed for QtQuick to redirect the events from MouseArea to our KDDW classes which derive from Draggable.
* For QtWidgets it's not needed, as the Draggables are QWidgets themselves.
*/
class MouseEventRedirector : public QObject
{
public:
explicit MouseEventRedirector(QObject *eventSource, QObject *eventTarget)
: QObject(eventTarget)
, m_eventTarget(eventTarget)
{
eventSource->installEventFilter(this);
}
bool eventFilter(QObject *, QEvent *ev) override
{
if (QMouseEvent *me = mouseEvent(ev))
qApp->sendEvent(m_eventTarget, me);
return false;
}
QObject *const m_eventTarget;
};
}
static bool flagsAreTopLevelFlags(Qt::WindowFlags flags)
{
return flags & (Qt::Window | Qt::Tool);
@@ -614,4 +646,14 @@ QQuickItem* KDDockWidgets::Private::widgetForWindow(QWindow *window)
return window->property("kddockwidgets_qwidget").value<QQuickItem*>();
}
void QWidgetAdapter::redirectMouseEvents(QObject *source)
{
if (m_mouseEventRedirector) {
qWarning() << Q_FUNC_INFO << "Redirector already installed";
} else {
m_mouseEventRedirector = new MouseEventRedirector(source, this);
}
}
LayoutGuestWidget::~LayoutGuestWidget() = default;

View File

@@ -37,6 +37,8 @@ QT_END_NAMESPACE
namespace KDDockWidgets {
class MouseEventRedirector;
namespace Private {
DOCKS_EXPORT QQuickItem* widgetForWindow(QWindow *window);
@@ -134,6 +136,7 @@ public:
Q_INVOKABLE void showMaximized();
Q_INVOKABLE void showMinimized();
Q_INVOKABLE void showNormal();
Q_INVOKABLE void redirectMouseEvents(QObject *from);
QQuickView *quickView() const;
QWindow *windowHandle() const;
@@ -196,6 +199,7 @@ private:
Qt::FocusPolicy m_focusPolicy = Qt::NoFocus;
bool m_windowIsBeingDestroyed = false;
bool m_mouseTrackingEnabled = false;
MouseEventRedirector *m_mouseEventRedirector = nullptr;
};
}

View File

@@ -35,22 +35,6 @@ TitleBarQuick::~TitleBarQuick()
{
}
void TitleBarQuick::filterEvents(QObject *obj)
{
if (!m_eventFilterInstalled) {
m_eventFilterInstalled = true;
obj->installEventFilter(this);
}
}
bool TitleBarQuick::eventFilter(QObject *, QEvent *ev)
{
if (QMouseEvent *me = mouseEvent(ev))
qApp->sendEvent(this, me);
return false;
}
#ifdef DOCKS_DEVELOPER_MODE
bool TitleBarQuick::isCloseButtonEnabled() const
{

View File

@@ -32,10 +32,6 @@ public:
~TitleBarQuick() override;
protected:
Q_INVOKABLE void filterEvents(QObject *);
bool eventFilter(QObject *, QEvent *) override;
#ifdef DOCKS_DEVELOPER_MODE
// These 4 just for unit-tests
bool isCloseButtonEnabled() const override;
@@ -58,7 +54,6 @@ private:
QQuickItem *floatButton() const;
QQuickItem *closeButton() const;
bool m_eventFilterInstalled = false;
QPointer<QQuickItem> m_titleBarQmlItem;
};

View File

@@ -58,7 +58,7 @@ Rectangle {
onTitleBarCppChanged: {
if (titleBarCpp) {
titleBarCpp.filterEvents(dragMouseArea)
titleBarCpp.redirectMouseEvents(dragMouseArea)
// Setting just so the unit-tests can access the buttons
titleBarCpp.titleBarQmlItem = this;