20#include <QStyleOption>
22#include "moc_qgsfloatingwidget.cpp"
33 mParentEventFilter =
new QgsFloatingWidgetEventFilter( parent );
34 parent->installEventFilter( mParentEventFilter );
35 connect( mParentEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
41 if ( widget == mAnchorWidget )
47 mAnchorWidget->removeEventFilter( mAnchorEventFilter );
48 delete mAnchorEventFilter;
49 mAnchorEventFilter =
nullptr;
52 mAnchorWidget = widget;
55 mAnchorEventFilter =
new QgsFloatingWidgetEventFilter( mAnchorWidget );
56 mAnchorWidget->installEventFilter( mAnchorEventFilter );
57 connect( mAnchorEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged,
this, &QgsFloatingWidget::onAnchorPointChanged );
60 onAnchorPointChanged();
71 if ( point == mFloatAnchorPoint )
74 mFloatAnchorPoint = point;
75 onAnchorPointChanged();
81 if ( point == mAnchorWidgetAnchorPoint )
84 mAnchorWidgetAnchorPoint = point;
85 onAnchorPointChanged();
91 QWidget::showEvent( e );
92 onAnchorPointChanged();
101 style()->drawPrimitive( QStyle::PE_Widget, &opt, &p,
this );
106 QWidget::resizeEvent( e );
107 onAnchorPointChanged();
110void QgsFloatingWidget::onAnchorPointChanged()
112 if ( !parentWidget() )
117 QPoint anchorWidgetOrigin;
119 switch ( mAnchorWidgetAnchorPoint )
122 anchorWidgetOrigin = QPoint( 0, 0 );
125 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, 0 );
128 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), 0 );
131 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() / 2 );
134 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() / 2 );
137 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() / 2 );
140 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() );
143 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() );
146 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() );
150 anchorWidgetOrigin = mAnchorWidget->mapTo( parentWidget(), anchorWidgetOrigin );
151 int anchorX = anchorWidgetOrigin.x();
152 int anchorY = anchorWidgetOrigin.y();
154 switch ( mFloatAnchorPoint )
159 anchorX = anchorX - width() / 2;
162 anchorX = anchorX - width();
165 anchorY = anchorY - height() / 2;
168 anchorY = anchorY - height() / 2;
169 anchorX = anchorX - width() / 2;
172 anchorX = anchorX - width();
173 anchorY = anchorY - height() / 2;
176 anchorY = anchorY - height();
179 anchorX = anchorX - width() / 2;
180 anchorY = anchorY - height();
183 anchorX = anchorX - width();
184 anchorY = anchorY - height();
189 anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() );
191 move( anchorX, anchorY );
200QgsFloatingWidgetEventFilter::QgsFloatingWidgetEventFilter( QWidget *parent )
205bool QgsFloatingWidgetEventFilter::eventFilter( QObject *
object, QEvent *event )
208 switch ( event->type() )
212 emit anchorPointChanged();