24 #include "../DockRegistry_p.h"
25 #include "../Utils_p.h"
26 #include "../FloatingWindow_p.h"
27 #include "../multisplitter/Item_p.h"
29 #include <QResizeEvent>
30 #include <QMouseEvent>
31 #include <QQmlComponent>
35 #include <QScopedValueRollback>
37 #include <qpa/qplatformwindow.h>
48 class MouseEventRedirector :
public QObject
52 explicit MouseEventRedirector(
QObject *eventSource,
QObject *eventTarget)
54 , m_eventSource(eventSource)
55 , m_eventTarget(eventTarget)
60 auto oldRedirector = s_mouseEventRedirectors.take(eventSource);
63 oldRedirector->deleteLater();
66 s_mouseEventRedirectors.insert(eventSource,
this);
69 static MouseEventRedirector *redirectorForSource(
QObject *eventSource)
71 return s_mouseEventRedirectors.value(eventSource);
74 ~MouseEventRedirector()
override;
89 m_eventTarget->setProperty(
"cursorPosition", m_eventSource->property(
"cursorPosition"));
90 qApp->sendEvent(m_eventTarget, me);
103 MouseEventRedirector::~MouseEventRedirector()
105 s_mouseEventRedirectors.
remove(m_eventSource);
120 : candidateParentItem;
123 QWidgetAdapter::QWidgetAdapter(QQuickItem *parent,
Qt::WindowFlags flags)
125 , m_windowFlags(flags)
132 connect(
this, &QQuickItem::widthChanged,
this, [
this] {
137 connect(
this, &QQuickItem::heightChanged,
this, [
this] {
138 if (!m_windowIsBeingDestroyed) {
144 qApp->installEventFilter(
this);
146 setSize(
QSize(800, 800));
149 QWidgetAdapter::~QWidgetAdapter()
153 void QWidgetAdapter::raiseAndActivate()
155 if (
QWindow *w = windowHandle()) {
157 w->requestActivate();
161 void QWidgetAdapter::setWindowOpacity(qreal level)
163 if (
QWindow *w = windowHandle())
164 w->setOpacity(level);
167 bool QWidgetAdapter::onResize(
QSize)
171 void QWidgetAdapter::onLayoutRequest()
174 void QWidgetAdapter::onMousePress()
177 void QWidgetAdapter::onMouseMove(
QPoint)
180 void QWidgetAdapter::onMouseRelease()
188 updateNormalGeometry();
190 void QWidgetAdapter::onMoveEvent(
QMoveEvent *)
192 updateNormalGeometry();
195 void QWidgetAdapter::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
197 QQuickItem::itemChange(change, data);
201 case QQuickItem::ItemParentHasChanged: {
203 qApp->sendEvent(
this, &ev);
204 Q_EMIT parentChanged(
this);
207 case QQuickItem::ItemVisibleHasChanged: {
223 void QWidgetAdapter::updateNormalGeometry()
225 QWindow *window = windowHandle();
230 QRect normalGeometry;
231 if (
const QPlatformWindow *pw = window->handle()) {
232 normalGeometry = pw->normalGeometry();
236 normalGeometry = window->
geometry();
239 if (normalGeometry.
isValid()) {
240 setNormalGeometry(normalGeometry);
244 void QWidgetAdapter::QQUICKITEMgeometryChanged(
const QRectF &newGeometry,
const QRectF &oldGeometry)
247 QQuickItem::QQUICKITEMgeometryChanged(newGeometry, oldGeometry);
251 if (newGeometry.
size() != oldGeometry.
size()) {
253 qApp->sendEvent(
this, &ev);
258 qApp->sendEvent(
this, &ev);
261 Q_EMIT itemGeometryChanged();
264 void QWidgetAdapter::raise()
267 if (
QWindow *w = windowHandle())
269 }
else if (
auto p = QQuickItem::parentItem()) {
271 const auto siblings = p->childItems();
272 QQuickItem *last = siblings.last();
278 QSize QWidgetAdapter::minimumSize()
const
281 const auto children = childItems();
282 if (!children.isEmpty()) {
283 const QSize min = children.constFirst()->property(
"kddockwidgets_min_size").toSize();
284 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
288 const QSize min = property(
"kddockwidgets_min_size").toSize();
289 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
292 QSize QWidgetAdapter::maximumSize()
const
295 const auto children = childItems();
296 if (!children.isEmpty()) {
297 const QSize max = children.constFirst()->property(
"kddockwidgets_max_size").toSize();
298 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
299 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
303 const QSize max = property(
"kddockwidgets_max_size").toSize();
304 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
305 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
308 WId QWidgetAdapter::winId()
const
310 if (
QWindow *w = windowHandle())
316 FloatingWindow *QWidgetAdapter::floatingWindow()
const
318 if (
auto fw = qobject_cast<FloatingWindow *>(window()))
324 QRect QWidgetAdapter::geometry()
const
327 if (
QWindow *w = windowHandle()) {
328 return w->geometry();
332 return KDDockWidgets::Private::geometry(
this);
335 QRect QWidgetAdapter::normalGeometry()
const
337 return m_normalGeometry;
340 void QWidgetAdapter::setNormalGeometry(
QRect geo)
342 m_normalGeometry = geo;
345 QRect QWidgetAdapter::rect()
const
350 QPoint QWidgetAdapter::pos()
const
352 return geometry().topLeft();
355 void QWidgetAdapter::show()
360 void QWidgetAdapter::setFixedHeight(
int height)
365 void QWidgetAdapter::setFixedWidth(
int width)
370 void QWidgetAdapter::setGeometry(
QRect rect)
372 setWidth(rect.
width());
377 QRect QWidgetAdapter::frameGeometry()
const
379 if (
QWindow *w = windowHandle())
380 return w->frameGeometry();
385 void QWidgetAdapter::grabMouse()
387 QQuickItem::grabMouse();
390 void QWidgetAdapter::releaseMouse()
392 QQuickItem::ungrabMouse();
395 void QWidgetAdapter::releaseKeyboard()
400 void QWidgetAdapter::setMinimumSize(
QSize sz)
402 if (minimumSize() != sz) {
403 setProperty(
"kddockwidgets_min_size", sz);
408 void QWidgetAdapter::setMaximumSize(
QSize sz)
410 if (maximumSize() != sz) {
411 setProperty(
"kddockwidgets_max_size", sz);
416 void QWidgetAdapter::setMaximumSize(
int w,
int h)
418 QWidgetAdapter::setMaximumSize(
QSize(w, h));
421 void QWidgetAdapter::setMinimumSize(
int w,
int h)
423 QWidgetAdapter::setMinimumSize(
QSize(w, h));
426 void QWidgetAdapter::updateGeometry()
428 Q_EMIT geometryUpdated();
431 void QWidgetAdapter::resize(
QSize sz)
433 setWidth(sz.
width());
437 void QWidgetAdapter::resize(
int w,
int h)
442 bool QWidgetAdapter::isWindow()
const
444 QQuickItem *parent = parentItem();
448 if (QQuickView *w = quickView()) {
449 if (parent == w->contentItem() || parent == w->rootObject())
456 bool QWidgetAdapter::isMaximized()
const
458 if (
QWindow *w = windowHandle())
464 bool QWidgetAdapter::isFullScreen()
const
466 if (
QWindow *w = windowHandle())
474 if (
QWindow *w = windowHandle())
480 bool KDDockWidgets::QWidgetAdapter::isActiveWindow()
const
482 if (
QWindow *w = windowHandle())
483 return w->isActive();
488 void QWidgetAdapter::showMaximized()
490 if (
QWindow *w = windowHandle())
494 void QWidgetAdapter::showMinimized()
496 if (
QWindow *w = windowHandle())
500 void QWidgetAdapter::showNormal()
502 if (
QWindow *w = windowHandle())
506 QQuickView *QWidgetAdapter::quickView()
const
508 return qobject_cast<QQuickView *>(QQuickItem::window());
511 QWindow *QWidgetAdapter::windowHandle()
const
513 return QQuickItem::window();
516 QWidgetAdapter *QWidgetAdapter::window()
const
520 if (QWidgetAdapter *w = parentWidget(
false))
523 return const_cast<QWidgetAdapter *
>(
this);
526 QWidgetAdapter *QWidgetAdapter::parentWidget(
bool includeTransient)
const
528 QQuickItem *p = parentItem();
530 if (
auto qa = qobject_cast<QWidgetAdapter *>(p))
536 if (includeTransient) {
537 if (QQuickView *w = quickView()) {
539 MainWindowBase *mw = DockRegistry::self()->mainWindowForHandle(w->transientParent());
551 return QQuickItem::mapToGlobal(pt).toPoint();
556 return QQuickItem::mapFromGlobal(pt).toPoint();
559 QPoint QWidgetAdapter::mapTo(
const QQuickItem *parent,
QPoint pos)
const
564 return parent->mapFromGlobal(QQuickItem::mapToGlobal(pos)).toPoint();
569 return m_widgetAttributes & attr;
575 m_widgetAttributes |= attr;
577 m_widgetAttributes &= ~attr;
580 void QWidgetAdapter::setWindowTitle(
const QString &title)
582 if (
QWindow *window = windowHandle())
586 void QWidgetAdapter::setWindowIcon(
const QIcon &icon)
588 if (
QWindow *window = windowHandle())
592 bool QWidgetAdapter::close()
605 QQuickItem *QWidgetAdapter::childAt(
QPoint p)
const
607 return QQuickItem::childAt(p.
x(), p.
y());
610 void QWidgetAdapter::move(
QPoint pt)
612 move(pt.
x(), pt.
y());
615 void QWidgetAdapter::move(
int x,
int y)
618 if (
QWindow *w = windowHandle()) {
619 w->setPosition(x, y);
629 void QWidgetAdapter::setSize(
QSize size)
631 QQuickItem::setSize(
QSizeF(size));
634 void QWidgetAdapter::setParent(QQuickItem *p)
639 QQuickItem::setParent(p);
640 QQuickItem::setParentItem(p);
648 void QWidgetAdapter::activateWindow()
650 if (
QWindow *w = windowHandle())
651 w->requestActivate();
664 QSize QWidgetAdapter::sizeHint()
const
669 bool QWidgetAdapter::hasFocus()
const
671 return hasActiveFocus();
674 void QWidgetAdapter::setWindowFlag(
int flag,
bool enable)
682 return m_windowFlags;
686 QQuickItem *QWidgetAdapter::createItem(QQmlEngine *engine,
const QString &filename)
688 QQmlComponent component(engine, filename);
689 QObject *obj = component.create();
691 qWarning() << Q_FUNC_INFO << component.errorString();
695 return qobject_cast<QQuickItem *>(obj);
698 void QWidgetAdapter::makeItemFillParent(QQuickItem *item)
703 qWarning() << Q_FUNC_INFO <<
"Invalid item";
707 QQuickItem *parentItem = item->parentItem();
709 qWarning() << Q_FUNC_INFO <<
"Invalid parentItem for" << item;
715 qWarning() << Q_FUNC_INFO <<
"Invalid anchors for" << item;
733 return m_focusPolicy;
738 m_focusPolicy = policy;
743 QQuickItem::setFocus(
true, reason);
744 forceActiveFocus(reason);
747 void QWidgetAdapter::render(
QPainter *)
749 qWarning() << Q_FUNC_INFO <<
"Implement me";
752 void QWidgetAdapter::setMouseTracking(
bool enabled)
754 m_mouseTrackingEnabled = enabled;
757 bool QWidgetAdapter::event(
QEvent *ev)
762 return QQuickItem::event(ev);
765 bool QWidgetAdapter::eventFilter(
QObject *watched,
QEvent *ev)
767 if (qobject_cast<QWindow *>(watched)) {
768 if (m_mouseTrackingEnabled) {
769 switch (ev->
type()) {
774 qApp->sendEvent(
this, ev);
791 return QQuickItem::eventFilter(watched, ev);
794 QScreen *QWidgetAdapter::screen()
const
796 if (QQuickView *w = quickView()) {
803 void QWidgetAdapter::setWindowIsBeingDestroyed(
bool is)
805 m_windowIsBeingDestroyed = is;
808 void QWidgetAdapter::create()
813 QQuickItem *KDDockWidgets::Private::widgetForWindow(
QWindow *window)
818 return window->
property(
"kddockwidgets_qwidget").
value<QQuickItem *>();
821 void QWidgetAdapter::redirectMouseEvents(
QObject *source)
823 if (
auto existingRedirector = MouseEventRedirector::redirectorForSource(source)) {
824 if (existingRedirector->m_eventTarget ==
this) {
830 new MouseEventRedirector(source,
this);
833 void QWidgetAdapter::setIsWrapper()
838 bool QWidgetAdapter::isWrapper()
const
843 LayoutGuestWidget::~LayoutGuestWidget() =
default;
845 #include "QWidgetAdapter_quick.moc"