24 #include "../DockRegistry_p.h"
25 #include "../Utils_p.h"
26 #include "../FloatingWindow_p.h"
28 #include <QResizeEvent>
29 #include <QMouseEvent>
30 #include <QQmlComponent>
34 #include <QScopedValueRollback>
45 class MouseEventRedirector :
public QObject
49 explicit MouseEventRedirector(
QObject *eventSource,
QObject *eventTarget)
51 , m_eventSource(eventSource)
52 , m_eventTarget(eventTarget)
57 auto oldRedirector = s_mouseEventRedirectors.take(eventSource);
60 oldRedirector->deleteLater();
63 s_mouseEventRedirectors.insert(eventSource,
this);
66 static MouseEventRedirector *redirectorForSource(
QObject *eventSource)
68 return s_mouseEventRedirectors.value(eventSource);
71 ~MouseEventRedirector()
override;
86 m_eventTarget->setProperty(
"cursorPosition", m_eventSource->property(
"cursorPosition"));
87 qApp->sendEvent(m_eventTarget, me);
100 MouseEventRedirector::~MouseEventRedirector()
102 s_mouseEventRedirectors.
remove(m_eventSource);
117 : candidateParentItem;
120 QWidgetAdapter::QWidgetAdapter(QQuickItem *parent,
Qt::WindowFlags flags)
122 , m_windowFlags(flags)
129 connect(
this, &QQuickItem::widthChanged,
this, [
this] {
134 connect(
this, &QQuickItem::heightChanged,
this, [
this] {
135 if (!m_windowIsBeingDestroyed) {
141 setSize(
QSize(800, 800));
144 QWidgetAdapter::~QWidgetAdapter()
148 void QWidgetAdapter::raiseAndActivate()
150 if (
QWindow *w = windowHandle()) {
152 w->requestActivate();
156 void QWidgetAdapter::setWindowOpacity(qreal level)
158 if (
QWindow *w = windowHandle())
159 w->setOpacity(level);
162 bool QWidgetAdapter::onResize(
QSize)
166 void QWidgetAdapter::onLayoutRequest()
169 void QWidgetAdapter::onMousePress()
172 void QWidgetAdapter::onMouseMove(
QPoint)
175 void QWidgetAdapter::onMouseRelease()
182 void QWidgetAdapter::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
184 QQuickItem::itemChange(change, data);
188 case QQuickItem::ItemParentHasChanged: {
190 qApp->sendEvent(
this, &ev);
191 Q_EMIT parentChanged(
this);
194 case QQuickItem::ItemVisibleHasChanged: {
210 void QWidgetAdapter::QQUICKITEMgeometryChanged(
const QRectF &newGeometry,
const QRectF &oldGeometry)
213 QQuickItem::QQUICKITEMgeometryChanged(newGeometry, oldGeometry);
217 if (newGeometry.
size() != oldGeometry.
size()) {
219 qApp->sendEvent(
this, &ev);
224 qApp->sendEvent(
this, &ev);
227 Q_EMIT itemGeometryChanged();
230 void QWidgetAdapter::raise()
233 if (
QWindow *w = windowHandle())
235 }
else if (
auto p = QQuickItem::parentItem()) {
237 const auto siblings = p->childItems();
238 QQuickItem *last = siblings.last();
244 QSize QWidgetAdapter::minimumSize()
const
247 const auto children = childItems();
248 if (!children.isEmpty()) {
249 const QSize min = children.constFirst()->property(
"kddockwidgets_min_size").toSize();
250 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
254 const QSize min = property(
"kddockwidgets_min_size").toSize();
255 return min.
expandedTo(Layouting::Item::hardcodedMinimumSize);
258 QSize QWidgetAdapter::maximumSize()
const
261 const auto children = childItems();
262 if (!children.isEmpty()) {
263 const QSize max = children.constFirst()->property(
"kddockwidgets_max_size").toSize();
264 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
265 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
269 const QSize max = property(
"kddockwidgets_max_size").toSize();
270 return max.
isEmpty() ? Layouting::Item::hardcodedMaximumSize
271 : max.
boundedTo(Layouting::Item::hardcodedMaximumSize);
274 WId QWidgetAdapter::winId()
const
276 if (
QWindow *w = windowHandle())
282 FloatingWindow *QWidgetAdapter::floatingWindow()
const
284 if (
auto fw = qobject_cast<FloatingWindow *>(window()))
290 QRect QWidgetAdapter::geometry()
const
293 if (
QWindow *w = windowHandle()) {
294 return w->geometry();
298 return KDDockWidgets::Private::geometry(
this);
301 QRect QWidgetAdapter::normalGeometry()
const
304 return QWidgetAdapter::geometry();
307 void QWidgetAdapter::setNormalGeometry(
QRect geo)
312 if (
QWindow *w = windowHandle()) {
313 if (isNormalWindowState(w->windowStates())) {
317 qDebug() << Q_FUNC_INFO <<
"TODO";
322 QRect QWidgetAdapter::rect()
const
327 QPoint QWidgetAdapter::pos()
const
329 return geometry().topLeft();
332 void QWidgetAdapter::show()
337 void QWidgetAdapter::setFixedHeight(
int height)
342 void QWidgetAdapter::setFixedWidth(
int width)
347 void QWidgetAdapter::setGeometry(
QRect rect)
349 setWidth(rect.
width());
354 QRect QWidgetAdapter::frameGeometry()
const
356 if (
QWindow *w = windowHandle())
357 return w->frameGeometry();
362 void QWidgetAdapter::grabMouse()
364 QQuickItem::grabMouse();
367 void QWidgetAdapter::releaseMouse()
369 QQuickItem::ungrabMouse();
372 void QWidgetAdapter::releaseKeyboard()
377 void QWidgetAdapter::setMinimumSize(
QSize sz)
379 if (minimumSize() != sz) {
380 setProperty(
"kddockwidgets_min_size", sz);
385 void QWidgetAdapter::setMaximumSize(
QSize sz)
387 if (maximumSize() != sz) {
388 setProperty(
"kddockwidgets_max_size", sz);
393 void QWidgetAdapter::setMaximumSize(
int w,
int h)
395 QWidgetAdapter::setMaximumSize(
QSize(w, h));
398 void QWidgetAdapter::setMinimumSize(
int w,
int h)
400 QWidgetAdapter::setMinimumSize(
QSize(w, h));
403 void QWidgetAdapter::updateGeometry()
405 Q_EMIT geometryUpdated();
408 void QWidgetAdapter::resize(
QSize sz)
410 setWidth(sz.
width());
414 void QWidgetAdapter::resize(
int w,
int h)
419 bool QWidgetAdapter::isWindow()
const
421 QQuickItem *parent = parentItem();
425 if (QQuickView *w = quickView()) {
426 if (parent == w->contentItem() || parent == w->rootObject())
433 bool QWidgetAdapter::isMaximized()
const
435 if (
QWindow *w = windowHandle())
443 if (
QWindow *w = windowHandle())
449 bool KDDockWidgets::QWidgetAdapter::isActiveWindow()
const
451 if (
QWindow *w = windowHandle())
452 return w->isActive();
457 void QWidgetAdapter::showMaximized()
459 if (
QWindow *w = windowHandle())
463 void QWidgetAdapter::showMinimized()
465 if (
QWindow *w = windowHandle())
469 void QWidgetAdapter::showNormal()
471 if (
QWindow *w = windowHandle())
475 QQuickView *QWidgetAdapter::quickView()
const
477 return qobject_cast<QQuickView *>(QQuickItem::window());
480 QWindow *QWidgetAdapter::windowHandle()
const
482 return QQuickItem::window();
485 QWidgetAdapter *QWidgetAdapter::window()
const
489 if (QWidgetAdapter *w = parentWidget(
false))
492 return const_cast<QWidgetAdapter *
>(
this);
495 QWidgetAdapter *QWidgetAdapter::parentWidget(
bool includeTransient)
const
497 QQuickItem *p = parentItem();
499 if (
auto qa = qobject_cast<QWidgetAdapter *>(p))
505 if (includeTransient) {
506 if (QQuickView *w = quickView()) {
508 MainWindowBase *mw = DockRegistry::self()->mainWindowForHandle(w->transientParent());
520 return QQuickItem::mapToGlobal(pt).toPoint();
525 return QQuickItem::mapFromGlobal(pt).toPoint();
528 QPoint QWidgetAdapter::mapTo(
const QQuickItem *parent,
QPoint pos)
const
533 return parent->mapFromGlobal(QQuickItem::mapToGlobal(pos)).toPoint();
538 return m_widgetAttributes & attr;
544 m_widgetAttributes |= attr;
546 m_widgetAttributes &= ~attr;
549 void QWidgetAdapter::setWindowTitle(
const QString &title)
551 if (
QWindow *window = windowHandle())
552 window->setTitle(title);
555 void QWidgetAdapter::setWindowIcon(
const QIcon &icon)
557 if (
QWindow *window = windowHandle())
558 window->setIcon(icon);
561 bool QWidgetAdapter::close()
574 QQuickItem *QWidgetAdapter::childAt(
QPoint p)
const
576 return QQuickItem::childAt(p.
x(), p.
y());
579 void QWidgetAdapter::move(
QPoint pt)
581 move(pt.
x(), pt.
y());
584 void QWidgetAdapter::move(
int x,
int y)
587 if (
QWindow *w = windowHandle()) {
588 w->setPosition(x, y);
598 void QWidgetAdapter::setSize(
QSize size)
600 QQuickItem::setSize(
QSizeF(size));
603 void QWidgetAdapter::setParent(QQuickItem *p)
608 QQuickItem::setParent(p);
609 QQuickItem::setParentItem(p);
617 void QWidgetAdapter::activateWindow()
619 if (
QWindow *w = windowHandle())
620 w->requestActivate();
633 QSize QWidgetAdapter::sizeHint()
const
638 bool QWidgetAdapter::hasFocus()
const
640 return hasActiveFocus();
643 void QWidgetAdapter::setWindowFlag(
int flag,
bool enable)
651 return m_windowFlags;
655 QQuickItem *QWidgetAdapter::createItem(QQmlEngine *engine,
const QString &filename)
657 QQmlComponent component(engine, filename);
658 QObject *obj = component.create();
660 qWarning() << Q_FUNC_INFO << component.errorString();
664 return qobject_cast<QQuickItem *>(obj);
667 void QWidgetAdapter::makeItemFillParent(QQuickItem *item)
672 qWarning() << Q_FUNC_INFO <<
"Invalid item";
676 QQuickItem *parentItem = item->parentItem();
678 qWarning() << Q_FUNC_INFO <<
"Invalid parentItem for" << item;
684 qWarning() << Q_FUNC_INFO <<
"Invalid anchors for" << item;
702 return m_focusPolicy;
707 m_focusPolicy = policy;
712 QQuickItem::setFocus(
true, reason);
713 forceActiveFocus(reason);
716 void QWidgetAdapter::render(
QPainter *)
718 qWarning() << Q_FUNC_INFO <<
"Implement me";
721 void QWidgetAdapter::setMouseTracking(
bool enabled)
723 m_mouseTrackingEnabled = enabled;
726 bool QWidgetAdapter::event(
QEvent *ev)
731 return QQuickItem::event(ev);
734 bool QWidgetAdapter::eventFilter(
QObject *watched,
QEvent *ev)
736 if (qobject_cast<QWindow *>(watched)) {
737 if (m_mouseTrackingEnabled) {
738 switch (ev->
type()) {
743 qApp->sendEvent(
this, ev);
754 return QQuickItem::eventFilter(watched, ev);
757 QScreen *QWidgetAdapter::screen()
const
759 if (QQuickView *w = quickView()) {
766 void QWidgetAdapter::setWindowIsBeingDestroyed(
bool is)
768 m_windowIsBeingDestroyed = is;
771 void QWidgetAdapter::create()
776 QQuickItem *KDDockWidgets::Private::widgetForWindow(
QWindow *window)
781 return window->
property(
"kddockwidgets_qwidget").
value<QQuickItem *>();
784 void QWidgetAdapter::redirectMouseEvents(
QObject *source)
786 if (
auto existingRedirector = MouseEventRedirector::redirectorForSource(source)) {
787 if (existingRedirector->m_eventTarget ==
this) {
793 new MouseEventRedirector(source,
this);
796 void QWidgetAdapter::setIsWrapper()
801 bool QWidgetAdapter::isWrapper()
const
806 LayoutGuestWidget::~LayoutGuestWidget() =
default;
808 #include "QWidgetAdapter_quick.moc"