13#include "core/Utils_p.h"
14#include "core/View_p.h"
15#include "ViewWrapper_p.h"
16#include "core/layouting/Item_p.h"
17#include "core/ScopedValueRollback_p.h"
18#include "qtquick/Window_p.h"
22#include <QtQuick/private/qquickitem_p.h>
23#include <qpa/qplatformwindow.h>
24#include <QtGui/private/qhighdpiscaling_p.h>
25#include <QGuiApplication>
39class MouseEventRedirector :
public QObject
43 explicit MouseEventRedirector(QQuickItem *eventSource,
View *eventTarget)
45 , m_eventSource(eventSource)
46 , m_eventTarget(eventTarget)
51 auto oldRedirector = s_mouseEventRedirectors.take(eventSource);
53 eventSource->removeEventFilter(oldRedirector);
54 oldRedirector->deleteLater();
57 s_mouseEventRedirectors.insert(eventSource,
this);
60 static MouseEventRedirector *redirectorForSource(
QObject *eventSource)
62 return s_mouseEventRedirectors.value(eventSource);
65 ~MouseEventRedirector()
override;
71 m_eventTarget->onHoverEvent(
72 hev, m_eventSource->mapToGlobal(Qt5Qt6Compat::eventPos(hev)).toPoint());
87 m_eventTarget->setProperty(
"cursorPosition", m_eventSource->property(
"cursorPosition"));
89 qGuiApp->sendEvent(m_eventTarget, me);
101 QQuickItem *
const m_eventSource;
108MouseEventRedirector::~MouseEventRedirector()
110 s_mouseEventRedirectors.
remove(m_eventSource);
130 , View_qt(controller, type, this)
131 , m_windowFlags(flags)
139 connect(
this, &QQuickItem::widthChanged,
this, [
this] {
146 connect(
this, &QQuickItem::heightChanged,
this, [
this] {
153 _setSize({ 800, 800 });
164 QQmlComponent component(engine, filename);
167 qWarning() << Q_FUNC_INFO << component.errorString();
171 return qobject_cast<QQuickItem *>(obj);
176 if (
auto existingRedirector = MouseEventRedirector::redirectorForSource(source)) {
177 if (existingRedirector->m_eventTarget ==
this) {
186void View::sendVisibleChangeEvent()
201void View::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
206 QQuickItem::itemChange(change, data);
209 if (change == QQuickItem::ItemVisibleHasChanged)
210 sendVisibleChangeEvent();
213void View::updateNormalGeometry()
221 if (
const QPlatformWindow *pw =
window->handle()) {
222 normalGeometry = QHighDpi::fromNativePixels(pw->normalGeometry(), pw->window());
237 if (
QWindow *w = QQuickItem::window()) {
238 w->setPosition(
x,
y);
253 return QQuickItem::event(ev);
258 if (qobject_cast<QWindow *>(watched)) {
259 if (m_mouseTrackingEnabled) {
260 switch (ev->
type()) {
265 qGuiApp->sendEvent(
this, ev);
276 updateNormalGeometry();
282 return QQuickItem::eventFilter(watched, ev);
287 if (
auto viewqtquick = qobject_cast<View *>(item)) {
289 viewqtquick->Core::View::d->closeRequested.emit(&ev);
292 viewqtquick->setVisible(
false);
308 QQuickItem::QQUICKITEMgeometryChanged(newGeometry, oldGeometry);
312 if (newGeometry.
size() != oldGeometry.
size()) {
314 qGuiApp->sendEvent(
this, &ev);
319 qGuiApp->sendEvent(
this, &ev);
327 if (
QWindow *w = QQuickItem::window()) {
337 auto priv = QQuickItemPrivate::get(
this);
338 return priv->explicitVisible;
344 if (
QWindow *w = QQuickItem::window()) {
345 if (
is && !w->isVisible()) {
347 }
else if (!
is && w->isVisible()) {
353 QQuickItem::setVisible(
is);
355#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
357 if (
is && !parentItem() && !QQuickItem::isVisible()) {
358 sendVisibleChangeEvent();
365 auto priv = QQuickItemPrivate::get(
this);
366 return !priv->explicitVisible;
375void View::_setSize(
QSize newSize)
382 if (
window->size() != newSize) {
385 window->setGeometry(windowGeo);
390 QQuickItem::setSize(newSize);
396 return window->rootView();
398 auto thisNonConst =
const_cast<View *
>(
this);
405 qWarning() << Q_FUNC_INFO <<
"Invalid item";
409 QQuickItem *parentItem = item->parentItem();
411 qWarning() << Q_FUNC_INFO <<
"Invalid parentItem for" << item;
417 qWarning() << Q_FUNC_INFO <<
"Invalid anchors for" << item;
427 m_widgetAttributes |= attr;
429 m_widgetAttributes &= ~attr;
434 return m_widgetAttributes & attr;
448 return m_windowFlags;
453 const QSize min = property(
"kddockwidgets_min_size").toSize();
454 return min.
expandedTo(Core::Item::hardcodedMinimumSize);
459 const QSize max = property(
"kddockwidgets_max_size").toSize();
460 return max.
isEmpty() ? Core::Item::hardcodedMaximumSize
461 : max.
boundedTo(Core::Item::hardcodedMaximumSize);
467 if (
QWindow *w = QQuickItem::window()) {
468 return w->geometry();
472 return QRect(
QPointF(QQuickItem::x(), QQuickItem::y()).toPoint(), QQuickItem::size().toSize());
477 return m_normalGeometry;
482 m_normalGeometry = geo;
488 setProperty(
"kddockwidgets_max_size", sz);
496 QQuickItem::setWidth(w);
501 QQuickItem::setHeight(h);
547 ScopedValueRollback guard(m_inSetParent,
true);
548 QQuickItem::setParent(parentItem);
549 QQuickItem::setParentItem(parentItem);
566 if (
QWindow *w = item->window()) {
568 w->requestActivate();
579 if (
QWindow *w = QQuickItem::window())
580 w->requestActivate();
586 if (
QWindow *w = QQuickItem::window())
588 }
else if (
auto p = QQuickItem::parentItem()) {
590 const auto siblings = p->childItems();
591 QQuickItem *last = siblings.last();
599 QQuickItem *parent = item->parentItem();
603 if (
auto *w = qobject_cast<QQuickWindow *>(item->window())) {
604 if (parent == w->contentItem() || item == w->contentItem())
606 if (
auto *qv = qobject_cast<QQuickView *>(item->window())) {
607 if (parent == qv->rootObject() || item == qv->rootObject())
622 return qobject_cast<QQuickView *>(QQuickItem::window());
627 return QQuickItem::mapToGlobal(localPt).toPoint();
632 return QQuickItem::mapFromGlobal(globalPt).toPoint();
641 return parentItem->mapFromGlobal(QQuickItem::mapToGlobal(
pos)).toPoint();
646 if (
QWindow *w = QQuickItem::window())
652 if (
QWindow *w = QQuickItem::window())
658 if (
QWindow *w = QQuickItem::window())
664 if (
QWindow *w = QQuickItem::window())
665 return w->isActive();
672 if (
QWindow *w = QQuickItem::window())
678 if (
QWindow *w = QQuickItem::window())
684 if (
QWindow *w = QQuickItem::window())
690 if (
QWindow *w = QQuickItem::window())
698 if (
QWindow *w = QQuickItem::window())
708 if (
auto p = parentItem()) {
709 const auto siblings = p->childItems();
718 if (
QWindow *w = QQuickItem::window()) {
719 auto windowqtquick =
new QtQuick::Window(w);
720 return std::shared_ptr<Core::Window>(windowqtquick);
728 auto child = QQuickItem::childAt(p.
x(), p.
y());
735 auto p = item->parentItem();
736 if (QQuickWindow *
window = item->window()) {
737 if (p ==
window->contentItem()) {
749 return ViewWrapper::create(item);
759 return ViewWrapper::create(
this);
764 QQuickItem::grabMouse();
769 QQuickItem::ungrabMouse();
779 QQuickItem::setFocus(
true, reason);
780 forceActiveFocus(reason);
785 return QQuickItem::hasActiveFocus();
790 return m_focusPolicy;
795 m_focusPolicy = policy;
800 return QQuickItem::objectName();
806 setProperty(
"kddockwidgets_min_size", sz);
814 if (QQuickWindow *w = QQuickItem::window()) {
815 const QImage image = w->grabWindow();
817 const QPoint pos = mapToScene({ 0, 0 }).toPoint();
825 QQuickItem::setCursor(shape);
830 m_mouseTrackingEnabled = enable;
836 const auto childItems = QQuickItem::childItems();
837 result.
reserve(childItems.size());
838 for (QQuickItem *child : childItems) {
848 m_oldWindowState =
window->windowState();
867 if (filename.
startsWith(QStringLiteral(
"qrc:/")))
868 return filename.
right(filename.
size() - 3);
877 QQmlEngine *engine =
nullptr;
878 while (p && !engine) {
879 engine = qmlEngine(p);
887 qWarning() << Q_FUNC_INFO <<
"No engine found";
892 qWarning() << Q_FUNC_INFO <<
"File not found" << filename;
896 QQmlComponent component(engine, filename);
897 auto qquickitem = qobject_cast<QQuickItem *>(component.create(ctx));
899 qWarning() << Q_FUNC_INFO << component.errorString();
903 qquickitem->setParentItem(parent);
904 qquickitem->QObject::setParent(parent);
916 qWarning() << Q_FUNC_INFO
917 <<
"Base class called, please implement in your derived class if needed";
924 if (
auto ctx = qmlContext(item))
926 item = item->parentItem();
bool isAccepted() const const
QEvent::Type type() const const
bool exists() const const
int remove(const Key &key)
qreal devicePixelRatio() const const
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
void setParent(QObject *parent)
bool setProperty(const char *name, const QVariant &value)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
QRect window() const const
bool isValid() const const
void setSize(const QSize &size)
QSizeF size() const const
QPointF topLeft() const const
QSize boundedTo(const QSize &otherSize) const const
QSize expandedTo(const QSize &otherSize) const const
bool isEmpty() const const
QString right(int n) const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
static bool flagsAreTopLevelFlags(Qt::WindowFlags flags)
static QQuickItem * actualParentItem(QQuickItem *candidateParentItem, Qt::WindowFlags flags)
QVariant fromValue(const T &value)
bool isValid() const const
bool toBool() const const