18 #include <QStyleOption>
30 mParentEventFilter =
new QgsFloatingWidgetEventFilter( parent );
31 parent->installEventFilter( mParentEventFilter );
32 connect( mParentEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
38 if ( widget == mAnchorWidget )
44 mAnchorWidget->removeEventFilter( mAnchorEventFilter );
45 delete mAnchorEventFilter;
46 mAnchorEventFilter =
nullptr;
49 mAnchorWidget = widget;
52 mAnchorEventFilter =
new QgsFloatingWidgetEventFilter( mAnchorWidget );
53 mAnchorWidget->installEventFilter( mAnchorEventFilter );
54 connect( mAnchorEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
57 onAnchorPointChanged();
68 if ( point == mFloatAnchorPoint )
71 mFloatAnchorPoint = point;
72 onAnchorPointChanged();
78 if ( point == mAnchorWidgetAnchorPoint )
81 mAnchorWidgetAnchorPoint = point;
82 onAnchorPointChanged();
88 QWidget::showEvent( e );
89 onAnchorPointChanged();
98 style()->drawPrimitive( QStyle::PE_Widget, &opt, &p,
this );
103 QWidget::resizeEvent( e );
104 onAnchorPointChanged();
107 void QgsFloatingWidget::onAnchorPointChanged()
109 if ( !parentWidget() )
114 QPoint anchorWidgetOrigin;
116 switch ( mAnchorWidgetAnchorPoint )
119 anchorWidgetOrigin = QPoint( 0, 0 );
122 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, 0 );
125 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), 0 );
128 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() / 2 );
131 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() / 2 );
134 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() / 2 );
137 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() );
140 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() );
143 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() );
147 anchorWidgetOrigin = mAnchorWidget->mapTo( parentWidget(), anchorWidgetOrigin );
148 int anchorX = anchorWidgetOrigin.x();
149 int anchorY = anchorWidgetOrigin.y();
151 switch ( mFloatAnchorPoint )
156 anchorX = anchorX - width() / 2;
159 anchorX = anchorX - width();
162 anchorY = anchorY - height() / 2;
165 anchorY = anchorY - height() / 2;
166 anchorX = anchorX - width() / 2;
169 anchorX = anchorX - width();
170 anchorY = anchorY - height() / 2;
173 anchorY = anchorY - height();
176 anchorX = anchorX - width() / 2;
177 anchorY = anchorY - height();
180 anchorX = anchorX - width();
181 anchorY = anchorY - height();
186 anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() );
188 move( anchorX, anchorY );
197 QgsFloatingWidgetEventFilter::QgsFloatingWidgetEventFilter( QWidget *parent )
203 bool QgsFloatingWidgetEventFilter::eventFilter( QObject *
object, QEvent *event )
206 switch ( event->type() )
210 emit anchorPointChanged();