6#include "QskInputGrabber.h"
9#include "QskInternalMacros.h"
14#include <private/qquickitem_p.h>
15#include <private/qquickitemchangelistener_p.h>
18class QskInputGrabber::PrivateData final :
public QQuickItemChangeListener
22 : m_grabber( grabber )
26 inline QRectF grabberRect()
const
28 if ( itemBelow && itemAbove )
30 const auto pos = itemBelow->mapToItem( itemAbove, QPointF() );
31 return QRectF( pos.x(), pos.y(), itemBelow->width(), itemBelow->height() );
37 void enableListener( QQuickItem* item,
38 QQuickItemPrivate::ChangeTypes types,
bool on )
42 auto d = QQuickItemPrivate::get( item );
45 d->addItemChangeListener(
this, types );
47 d->removeItemChangeListener(
this, types );
51 void setup( QQuickItem* item )
53 QQuickItem* newItemBelow = item ? item->parentItem() :
nullptr;
54 QQuickItem* newItemAbove = item;
56 if ( newItemBelow != itemBelow )
58 const auto changeTypes = QQuickItemPrivate::Geometry;
60 enableListener( itemBelow, changeTypes,
false );
61 enableListener( newItemBelow, changeTypes,
true );
63 itemBelow = newItemBelow;
66 if ( newItemAbove != itemAbove )
68 const auto changeTypes = QQuickItemPrivate::Geometry | QQuickItemPrivate::Parent;
70 enableListener( itemAbove, changeTypes,
false );
71 enableListener( newItemAbove, changeTypes,
true );
73 itemAbove = newItemAbove;
78 void itemGeometryChanged( QQuickItem* item,
79 QQuickGeometryChange change,
const QRectF& )
override
81 bool doUpdate =
false;
83 if ( item == itemAbove )
84 doUpdate = change.positionChange();
86 doUpdate = change.sizeChange();
89 m_grabber->updateGeometry();
92 void itemParentChanged( QQuickItem* item, QQuickItem* parentItem )
override
94 if ( item == m_grabber && parentItem )
97 m_grabber->updateGeometry();
104 QPointer< QQuickItem > itemAbove;
105 QPointer< QQuickItem > itemBelow;
108QskInputGrabber::QskInputGrabber( QQuickItem* parent )
109 : Inherited( parent )
110 , m_data( new PrivateData( this ) )
112 setAcceptedMouseButtons( Qt::AllButtons );
113 setAcceptTouchEvents(
true );
114 setAcceptHoverEvents(
true );
117 setFlag( QQuickItem::ItemHasContents,
false );
119 m_data->setup( parent );
123QskInputGrabber::~QskInputGrabber()
125 m_data->setup(
nullptr );
128bool QskInputGrabber::event( QEvent* event )
130 bool doBlock =
false;
132 switch (
static_cast< int >( event->type() ) )
134 case QEvent::MouseButtonPress:
135 case QEvent::MouseMove:
136 case QEvent::MouseButtonRelease:
138 const auto mouseEvent =
static_cast< QMouseEvent*
>( event );
139 doBlock = isBlocking( qskMousePosition( mouseEvent ) );
142#ifndef QT_NO_WHEELEVENT
145 const auto wheelEvent =
static_cast< QWheelEvent*
>( event );
146 doBlock = isBlocking( qskWheelPosition( wheelEvent ) );
150 case QEvent::HoverEnter:
151 case QEvent::HoverLeave:
153 const auto ev =
static_cast< QHoverEvent*
>( event );
154 doBlock = isBlocking( qskHoverPosition( ev ) );
163 if (
auto w = qobject_cast< QskWindow* >( window() ) )
164 w->setEventAcceptance( QskWindow::EventPropagationStopped );
169 return Inherited::event( event );
172bool QskInputGrabber::isBlocking(
const QPointF& pos )
const
174 if (
const auto item = parentItem() )
175 return !item->contains( position() + pos );
180QRectF QskInputGrabber::grabberRect()
const
182 return m_data->grabberRect();
185void QskInputGrabber::updateGeometry()
187 const QRectF
rect = grabberRect();
192#include "moc_QskInputGrabber.cpp"
void setGeometry(qreal x, qreal y, qreal width, qreal height)