6#include "QskInputGrabber.h"
13#include <private/qquickitem_p.h>
14#include <private/qquickitemchangelistener_p.h>
17class QskInputGrabber::PrivateData final :
public QQuickItemChangeListener
21 : m_grabber( grabber )
25 inline QRectF grabberRect()
const
27 if ( itemBelow && itemAbove )
29 const auto pos = itemBelow->mapToItem( itemAbove, QPointF() );
30 return QRectF( pos.x(), pos.y(), itemBelow->width(), itemBelow->height() );
36 void enableListener( QQuickItem* item,
37 QQuickItemPrivate::ChangeTypes types,
bool on )
41 auto d = QQuickItemPrivate::get( item );
44 d->addItemChangeListener(
this, types );
46 d->removeItemChangeListener(
this, types );
50 void setup( QQuickItem* item )
52 QQuickItem* newItemBelow = item ? item->parentItem() :
nullptr;
53 QQuickItem* newItemAbove = item;
55 if ( newItemBelow != itemBelow )
57 const auto changeTypes = QQuickItemPrivate::Geometry;
59 enableListener( itemBelow, changeTypes,
false );
60 enableListener( newItemBelow, changeTypes,
true );
62 itemBelow = newItemBelow;
65 if ( newItemAbove != itemAbove )
67 const auto changeTypes = QQuickItemPrivate::Geometry | QQuickItemPrivate::Parent;
69 enableListener( itemAbove, changeTypes,
false );
70 enableListener( newItemAbove, changeTypes,
true );
72 itemAbove = newItemAbove;
77 void itemGeometryChanged( QQuickItem* item,
78 QQuickGeometryChange change,
const QRectF& )
override
80 bool doUpdate =
false;
82 if ( item == itemAbove )
83 doUpdate = change.positionChange();
85 doUpdate = change.sizeChange();
88 m_grabber->updateGeometry();
91 void itemParentChanged( QQuickItem* item, QQuickItem* parentItem )
override
93 if ( item == m_grabber && parentItem )
96 m_grabber->updateGeometry();
103 QPointer< QQuickItem > itemAbove;
104 QPointer< QQuickItem > itemBelow;
107QskInputGrabber::QskInputGrabber( QQuickItem* parent )
108 : Inherited( parent )
109 , m_data( new PrivateData( this ) )
111 setAcceptedMouseButtons( Qt::AllButtons );
112 setAcceptTouchEvents(
true );
113 setAcceptHoverEvents(
true );
116 setFlag( QQuickItem::ItemHasContents,
false );
118 m_data->setup( parent );
122QskInputGrabber::~QskInputGrabber()
124 m_data->setup(
nullptr );
127bool QskInputGrabber::event( QEvent* event )
129 bool doBlock =
false;
131 switch (
static_cast< int >( event->type() ) )
133 case QEvent::MouseButtonPress:
134 case QEvent::MouseMove:
135 case QEvent::MouseButtonRelease:
137 const auto mouseEvent =
static_cast< QMouseEvent*
>( event );
138 doBlock = isBlocking( qskMousePosition( mouseEvent ) );
141#ifndef QT_NO_WHEELEVENT
144 const auto wheelEvent =
static_cast< QWheelEvent*
>( event );
145 doBlock = isBlocking( qskWheelPosition( wheelEvent ) );
149 case QEvent::HoverEnter:
150 case QEvent::HoverLeave:
152 const auto ev =
static_cast< QHoverEvent*
>( event );
153 doBlock = isBlocking( qskHoverPosition( ev ) );
162 if (
auto w = qobject_cast< QskWindow* >( window() ) )
163 w->setEventAcceptance( QskWindow::EventPropagationStopped );
168 return Inherited::event( event );
171bool QskInputGrabber::isBlocking(
const QPointF& pos )
const
173 if (
const auto item = parentItem() )
174 return !item->contains( position() + pos );
179QRectF QskInputGrabber::grabberRect()
const
181 return m_data->grabberRect();
184void QskInputGrabber::updateGeometry()
186 const QRectF
rect = grabberRect();
191#include "moc_QskInputGrabber.cpp"
void setGeometry(qreal x, qreal y, qreal width, qreal height)