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();
99#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
102 opt.initFrom(
this );
105 style()->drawPrimitive( QStyle::PE_Widget, &opt, &p,
this );
110 QWidget::resizeEvent( e );
111 onAnchorPointChanged();
114void QgsFloatingWidget::onAnchorPointChanged()
116 if ( !parentWidget() )
121 QPoint anchorWidgetOrigin;
123 switch ( mAnchorWidgetAnchorPoint )
126 anchorWidgetOrigin = QPoint( 0, 0 );
129 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, 0 );
132 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), 0 );
135 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() / 2 );
138 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() / 2 );
141 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() / 2 );
144 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() );
147 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() );
150 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() );
154 anchorWidgetOrigin = mAnchorWidget->mapTo( parentWidget(), anchorWidgetOrigin );
155 int anchorX = anchorWidgetOrigin.x();
156 int anchorY = anchorWidgetOrigin.y();
158 switch ( mFloatAnchorPoint )
163 anchorX = anchorX - width() / 2;
166 anchorX = anchorX - width();
169 anchorY = anchorY - height() / 2;
172 anchorY = anchorY - height() / 2;
173 anchorX = anchorX - width() / 2;
176 anchorX = anchorX - width();
177 anchorY = anchorY - height() / 2;
180 anchorY = anchorY - height();
183 anchorX = anchorX - width() / 2;
184 anchorY = anchorY - height();
187 anchorX = anchorX - width();
188 anchorY = anchorY - height();
193 anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() );
195 move( anchorX, anchorY );
204QgsFloatingWidgetEventFilter::QgsFloatingWidgetEventFilter( QWidget *parent )
209bool QgsFloatingWidgetEventFilter::eventFilter( QObject *
object, QEvent *event )
212 switch ( event->type() )
216 emit anchorPointChanged();