24#include "../DockRegistry_p.h"
25#include "../Utils_p.h"
26#include "../FloatingWindow_p.h"
27#include "../multisplitter/Item_p.h"
29#include <QResizeEvent>
31#include <QQmlComponent>
35#include <QScopedValueRollback>
37#include <qpa/qplatformwindow.h>
38#include <QtGui/private/qhighdpiscaling_p.h>
49class MouseEventRedirector :
public QObject
55 , m_eventSource(eventSource)
61 auto oldRedirector = s_mouseEventRedirectors.take(eventSource);
67 s_mouseEventRedirectors.insert(eventSource,
this);
70 static MouseEventRedirector *redirectorForSource(
QObject *eventSource)
72 return s_mouseEventRedirectors.value(eventSource);
75 ~MouseEventRedirector()
override;
86 if (
v.isValid() && !
v.toBool())
90 m_eventTarget->setProperty(
"cursorPosition", m_eventSource->property(
"cursorPosition"));
91 qApp->sendEvent(m_eventTarget,
me);
104MouseEventRedirector::~MouseEventRedirector()
106 s_mouseEventRedirectors.
remove(m_eventSource);
121 : candidateParentItem;
124QWidgetAdapter::QWidgetAdapter(QQuickItem *parent,
Qt::WindowFlags flags)
126 , m_windowFlags(flags)
133 connect(
this, &QQuickItem::widthChanged,
this, [
this] {
138 connect(
this, &QQuickItem::heightChanged,
this, [
this] {
139 if (!m_windowIsBeingDestroyed) {
145 qApp->installEventFilter(
this);
147 setSize(
QSize(800, 800));
150QWidgetAdapter::~QWidgetAdapter()
154void QWidgetAdapter::raiseAndActivate()
156 if (
QWindow *w = windowHandle()) {
158 w->requestActivate();
162void QWidgetAdapter::setWindowOpacity(qreal level)
164 if (
QWindow *w = windowHandle())
165 w->setOpacity(level);
168bool QWidgetAdapter::onResize(
QSize)
172void QWidgetAdapter::onLayoutRequest()
175void QWidgetAdapter::onMousePress()
178void QWidgetAdapter::onMouseMove(
QPoint)
181void QWidgetAdapter::onMouseRelease()
189 updateNormalGeometry();
193 updateNormalGeometry();
196void QWidgetAdapter::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
198 QQuickItem::itemChange(change, data);
202 case QQuickItem::ItemParentHasChanged: {
204 qApp->sendEvent(
this, &ev);
205 Q_EMIT parentChanged(
this);
208 case QQuickItem::ItemVisibleHasChanged: {
224void QWidgetAdapter::updateNormalGeometry()
226 QWindow *window = windowHandle();
231 QRect normalGeometry;
232 if (
const QPlatformWindow *pw = window->handle()) {
233 normalGeometry = QHighDpi::fromNativePixels(pw->normalGeometry(), pw->window());
237 normalGeometry = window->
geometry();
240 if (normalGeometry.
isValid()) {
241 setNormalGeometry(normalGeometry);
245void QWidgetAdapter::QQUICKITEMgeometryChanged(
const QRectF &newGeometry,
const QRectF &oldGeometry)
248 QQuickItem::QQUICKITEMgeometryChanged(newGeometry, oldGeometry);
252 if (newGeometry.
size() != oldGeometry.
size()) {
254 qApp->sendEvent(
this, &ev);
259 qApp->sendEvent(
this, &ev);
262 Q_EMIT widgetGeometryChanged();
265void QWidgetAdapter::raise()
268 if (
QWindow *w = windowHandle())
270 }
else if (
auto p = QQuickItem::parentItem()) {
272 const auto siblings = p->childItems();
273 QQuickItem *last = siblings.last();
279QSize QWidgetAdapter::minimumSize()
const
282 const auto children = childItems();
283 if (!children.isEmpty()) {
284 const QSize min = children.constFirst()->property(
"kddockwidgets_min_size").toSize();
285 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
289 const QSize min = property(
"kddockwidgets_min_size").toSize();
290 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
293QSize QWidgetAdapter::maximumSize()
const
296 const auto children = childItems();
297 if (!children.isEmpty()) {
298 const QSize max = children.constFirst()->property(
"kddockwidgets_max_size").toSize();
299 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
300 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
304 const QSize max = property(
"kddockwidgets_max_size").toSize();
305 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
306 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
309WId QWidgetAdapter::winId()
const
311 if (
QWindow *w = windowHandle())
317FloatingWindow *QWidgetAdapter::floatingWindow()
const
319 if (
auto fw = qobject_cast<FloatingWindow *>(window()))
325QRect QWidgetAdapter::geometry()
const
328 if (
QWindow *w = windowHandle()) {
329 return w->geometry();
333 return KDDockWidgets::Private::geometry(
this);
336QRect QWidgetAdapter::normalGeometry()
const
338 return m_normalGeometry;
341void QWidgetAdapter::setNormalGeometry(
QRect geo)
343 m_normalGeometry = geo;
346QRect QWidgetAdapter::rect()
const
351QPoint QWidgetAdapter::pos()
const
353 return geometry().topLeft();
356void QWidgetAdapter::show()
361void QWidgetAdapter::setFixedHeight(
int height)
366void QWidgetAdapter::setFixedWidth(
int width)
371void QWidgetAdapter::setGeometry(
QRect rect)
373 setWidth(rect.
width());
378QRect QWidgetAdapter::frameGeometry()
const
380 if (
QWindow *w = windowHandle())
381 return w->frameGeometry();
386void QWidgetAdapter::grabMouse()
388 QQuickItem::grabMouse();
391void QWidgetAdapter::releaseMouse()
393 QQuickItem::ungrabMouse();
396void QWidgetAdapter::releaseKeyboard()
401void QWidgetAdapter::setMinimumSize(
QSize sz)
403 if (minimumSize() != sz) {
404 setProperty(
"kddockwidgets_min_size", sz);
409void QWidgetAdapter::setMaximumSize(
QSize sz)
411 if (maximumSize() != sz) {
412 setProperty(
"kddockwidgets_max_size", sz);
417void QWidgetAdapter::setMaximumSize(
int w,
int h)
419 QWidgetAdapter::setMaximumSize(
QSize(w, h));
422void QWidgetAdapter::setMinimumSize(
int w,
int h)
424 QWidgetAdapter::setMinimumSize(
QSize(w, h));
427void QWidgetAdapter::updateGeometry()
429 Q_EMIT geometryUpdated();
432void QWidgetAdapter::resize(
QSize sz)
434 setWidth(sz.
width());
438void QWidgetAdapter::resize(
int w,
int h)
443bool QWidgetAdapter::isWindow()
const
445 QQuickItem *parent = parentItem();
449 if (QQuickView *w = quickView()) {
450 if (parent == w->contentItem() || parent == w->rootObject())
457bool QWidgetAdapter::isMaximized()
const
459 if (
QWindow *w = windowHandle())
465bool QWidgetAdapter::isFullScreen()
const
467 if (
QWindow *w = windowHandle())
473bool QWidgetAdapter::isMinimized()
const
475 if (
QWindow *w = windowHandle())
481bool KDDockWidgets::QWidgetAdapter::isActiveWindow()
const
483 if (
QWindow *w = windowHandle())
484 return w->isActive();
489void QWidgetAdapter::showMaximized()
491 if (
QWindow *w = windowHandle())
495void QWidgetAdapter::showMinimized()
497 if (
QWindow *w = windowHandle())
501void QWidgetAdapter::showNormal()
503 if (
QWindow *w = windowHandle())
507QQuickView *QWidgetAdapter::quickView()
const
509 return qobject_cast<QQuickView *>(QQuickItem::window());
512QWindow *QWidgetAdapter::windowHandle()
const
514 return QQuickItem::window();
517QWidgetAdapter *QWidgetAdapter::window()
const
521 if (QWidgetAdapter *w = parentWidget(
false))
524 return const_cast<QWidgetAdapter *
>(
this);
527QWidgetAdapter *QWidgetAdapter::parentWidget(
bool includeTransient)
const
529 QQuickItem *p = parentItem();
531 if (
auto qa = qobject_cast<QWidgetAdapter *>(p))
537 if (includeTransient) {
538 if (QQuickView *w = quickView()) {
540 MainWindowBase *mw = DockRegistry::self()->mainWindowForHandle(w->transientParent());
552 return QQuickItem::mapToGlobal(pt).toPoint();
557 return QQuickItem::mapFromGlobal(pt).toPoint();
560QPoint QWidgetAdapter::mapTo(
const QQuickItem *parent,
QPoint pos)
const
565 return parent->mapFromGlobal(QQuickItem::mapToGlobal(pos)).toPoint();
570 return m_widgetAttributes & attr;
576 m_widgetAttributes |= attr;
578 m_widgetAttributes &= ~attr;
581void QWidgetAdapter::setWindowTitle(
const QString &title)
583 if (
QWindow *window = windowHandle())
587void QWidgetAdapter::setWindowIcon(
const QIcon &icon)
589 if (
QWindow *window = windowHandle())
593bool QWidgetAdapter::close()
606QQuickItem *QWidgetAdapter::childAt(
QPoint p)
const
608 return QQuickItem::childAt(p.
x(), p.
y());
611void QWidgetAdapter::move(
QPoint pt)
613 move(pt.
x(), pt.
y());
616void QWidgetAdapter::move(
int x,
int y)
619 if (
QWindow *w = windowHandle()) {
620 w->setPosition(x, y);
630void QWidgetAdapter::setSize(
QSize size)
632 QQuickItem::setSize(
QSizeF(size));
635void QWidgetAdapter::setParent(QQuickItem *p)
640 QQuickItem::setParent(p);
641 QQuickItem::setParentItem(p);
649void QWidgetAdapter::activateWindow()
651 if (
QWindow *w = windowHandle())
652 w->requestActivate();
665QSize QWidgetAdapter::sizeHint()
const
670bool QWidgetAdapter::hasFocus()
const
672 return hasActiveFocus();
675void QWidgetAdapter::setWindowFlag(
int flag,
bool enable)
683 return m_windowFlags;
687QQuickItem *QWidgetAdapter::createItem(QQmlEngine *engine,
const QString &filename)
689 QQmlComponent component(engine, filename);
690 QObject *obj = component.create();
692 qWarning() << Q_FUNC_INFO << component.errorString();
696 return qobject_cast<QQuickItem *>(obj);
699void QWidgetAdapter::makeItemFillParent(QQuickItem *item)
704 qWarning() << Q_FUNC_INFO <<
"Invalid item";
708 QQuickItem *parentItem = item->parentItem();
710 qWarning() << Q_FUNC_INFO <<
"Invalid parentItem for" << item;
716 qWarning() << Q_FUNC_INFO <<
"Invalid anchors for" << item;
734 return m_focusPolicy;
739 m_focusPolicy = policy;
744 QQuickItem::setFocus(
true, reason);
745 forceActiveFocus(reason);
748void QWidgetAdapter::render(
QPainter *)
750 qWarning() << Q_FUNC_INFO <<
"Implement me";
753void QWidgetAdapter::setMouseTracking(
bool enabled)
755 m_mouseTrackingEnabled = enabled;
758bool QWidgetAdapter::event(
QEvent *ev)
763 return QQuickItem::event(ev);
766bool QWidgetAdapter::eventFilter(
QObject *watched,
QEvent *ev)
768 if (qobject_cast<QWindow *>(watched)) {
769 if (m_mouseTrackingEnabled) {
770 switch (ev->
type()) {
775 qApp->sendEvent(
this, ev);
792 return QQuickItem::eventFilter(watched, ev);
795QScreen *QWidgetAdapter::screen()
const
797 if (QQuickView *w = quickView()) {
804void QWidgetAdapter::setWindowIsBeingDestroyed(
bool is)
806 m_windowIsBeingDestroyed = is;
809void QWidgetAdapter::create()
814QQuickItem *KDDockWidgets::Private::widgetForWindow(
QWindow *window)
819 return window->
property(
"kddockwidgets_qwidget").
value<QQuickItem *>();
822void QWidgetAdapter::redirectMouseEvents(
QObject *source)
824 if (
auto existingRedirector = MouseEventRedirector::redirectorForSource(source)) {
825 if (existingRedirector->m_eventTarget ==
this) {
831 new MouseEventRedirector(source,
this);
834void QWidgetAdapter::setIsWrapper()
839bool QWidgetAdapter::isWrapper()
const
846#include "QWidgetAdapter_quick.moc"
The MainWindow base-class that's shared between QtWidgets and QtQuick stack.
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
bool isAccepted() const const
QEvent::Type type() const const
int remove(const Key &key)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
void removeEventFilter(QObject *obj)
void setParent(QObject *parent)
bool setProperty(const char *name, const QVariant &value)
bool isValid() const const
QPoint topLeft() const const
QSizeF size() const const
QRect toRect() const const
QPointF topLeft() const const
QSize boundedTo(const QSize &otherSize) const const
QSize expandedTo(const QSize &otherSize) const const
bool isEmpty() const const
QVariant fromValue(const T &value)
QRect geometry() const const
void setIcon(const QIcon &icon)
void setTitle(const QString &)
Qt::WindowState windowState() const const