17#include "moc_qgsfloatingwidget.cpp"
19#include <QStyleOption>
31 mParentEventFilter =
new QgsFloatingWidgetEventFilter( parent );
32 parent->installEventFilter( mParentEventFilter );
33 connect( mParentEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
39 if ( widget == mAnchorWidget )
45 mAnchorWidget->removeEventFilter( mAnchorEventFilter );
46 delete mAnchorEventFilter;
47 mAnchorEventFilter =
nullptr;
50 mAnchorWidget = widget;
53 mAnchorEventFilter =
new QgsFloatingWidgetEventFilter( mAnchorWidget );
54 mAnchorWidget->installEventFilter( mAnchorEventFilter );
55 connect( mAnchorEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
58 onAnchorPointChanged();
69 if ( point == mFloatAnchorPoint )
72 mFloatAnchorPoint = point;
73 onAnchorPointChanged();
79 if ( point == mAnchorWidgetAnchorPoint )
82 mAnchorWidgetAnchorPoint = point;
83 onAnchorPointChanged();
89 QWidget::showEvent( e );
90 onAnchorPointChanged();
97#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
100 opt.initFrom(
this );
103 style()->drawPrimitive( QStyle::PE_Widget, &opt, &p,
this );
108 QWidget::resizeEvent( e );
109 onAnchorPointChanged();
112void QgsFloatingWidget::onAnchorPointChanged()
114 if ( !parentWidget() )
119 QPoint anchorWidgetOrigin;
121 switch ( mAnchorWidgetAnchorPoint )
124 anchorWidgetOrigin = QPoint( 0, 0 );
127 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, 0 );
130 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), 0 );
133 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() / 2 );
136 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() / 2 );
139 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() / 2 );
142 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() );
145 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() );
148 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() );
152 anchorWidgetOrigin = mAnchorWidget->mapTo( parentWidget(), anchorWidgetOrigin );
153 int anchorX = anchorWidgetOrigin.x();
154 int anchorY = anchorWidgetOrigin.y();
156 switch ( mFloatAnchorPoint )
161 anchorX = anchorX - width() / 2;
164 anchorX = anchorX - width();
167 anchorY = anchorY - height() / 2;
170 anchorY = anchorY - height() / 2;
171 anchorX = anchorX - width() / 2;
174 anchorX = anchorX - width();
175 anchorY = anchorY - height() / 2;
178 anchorY = anchorY - height();
181 anchorX = anchorX - width() / 2;
182 anchorY = anchorY - height();
185 anchorX = anchorX - width();
186 anchorY = anchorY - height();
191 anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() );
193 move( anchorX, anchorY );
202QgsFloatingWidgetEventFilter::QgsFloatingWidgetEventFilter( QWidget *parent )
208bool QgsFloatingWidgetEventFilter::eventFilter( QObject *
object, QEvent *event )
211 switch ( event->type() )
215 emit anchorPointChanged();