QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsfloatingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfloatingwidget.cpp
3 ---------------------
4 begin : April 2016
5 copyright : (C) Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsfloatingwidget.h"
17
18#include <QEvent>
19#include <QPainter>
20#include <QStyleOption>
21
22#include "moc_qgsfloatingwidget.cpp"
23
24//
25// QgsFloatingWidget
26//
27
29 : QWidget( parent )
30{
31 if ( parent )
32 {
33 mParentEventFilter = new QgsFloatingWidgetEventFilter( parent );
34 parent->installEventFilter( mParentEventFilter );
35 connect( mParentEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged, this, &QgsFloatingWidget::onAnchorPointChanged );
36 }
37}
38
40{
41 if ( widget == mAnchorWidget )
42 return;
43
44 // remove existing event filter
45 if ( mAnchorWidget )
46 {
47 mAnchorWidget->removeEventFilter( mAnchorEventFilter );
48 delete mAnchorEventFilter;
49 mAnchorEventFilter = nullptr;
50 }
51
52 mAnchorWidget = widget;
53 if ( mAnchorWidget )
54 {
55 mAnchorEventFilter = new QgsFloatingWidgetEventFilter( mAnchorWidget );
56 mAnchorWidget->installEventFilter( mAnchorEventFilter );
57 connect( mAnchorEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged, this, &QgsFloatingWidget::onAnchorPointChanged );
58 }
59
60 onAnchorPointChanged();
61 emit anchorWidgetChanged( mAnchorWidget );
62}
63
65{
66 return mAnchorWidget;
67}
68
70{
71 if ( point == mFloatAnchorPoint )
72 return;
73
74 mFloatAnchorPoint = point;
75 onAnchorPointChanged();
76 emit anchorPointChanged( mFloatAnchorPoint );
77}
78
80{
81 if ( point == mAnchorWidgetAnchorPoint )
82 return;
83
84 mAnchorWidgetAnchorPoint = point;
85 onAnchorPointChanged();
86 emit anchorWidgetPointChanged( mAnchorWidgetAnchorPoint );
87}
88
89void QgsFloatingWidget::showEvent( QShowEvent *e )
90{
91 QWidget::showEvent( e );
92 onAnchorPointChanged();
93}
94
95void QgsFloatingWidget::paintEvent( QPaintEvent *e )
96{
97 Q_UNUSED( e )
98 QStyleOption opt;
99 opt.initFrom( this );
100 QPainter p( this );
101 style()->drawPrimitive( QStyle::PE_Widget, &opt, &p, this );
102}
103
104void QgsFloatingWidget::resizeEvent( QResizeEvent *e )
105{
106 QWidget::resizeEvent( e );
107 onAnchorPointChanged();
108}
109
110void QgsFloatingWidget::onAnchorPointChanged()
111{
112 if ( !parentWidget() )
113 return;
114
115 if ( mAnchorWidget )
116 {
117 QPoint anchorWidgetOrigin;
118
119 switch ( mAnchorWidgetAnchorPoint )
120 {
121 case TopLeft:
122 anchorWidgetOrigin = QPoint( 0, 0 );
123 break;
124 case TopMiddle:
125 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, 0 );
126 break;
127 case TopRight:
128 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), 0 );
129 break;
130 case MiddleLeft:
131 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() / 2 );
132 break;
133 case Middle:
134 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() / 2 );
135 break;
136 case MiddleRight:
137 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() / 2 );
138 break;
139 case BottomLeft:
140 anchorWidgetOrigin = QPoint( 0, mAnchorWidget->height() );
141 break;
142 case BottomMiddle:
143 anchorWidgetOrigin = QPoint( mAnchorWidget->width() / 2, mAnchorWidget->height() );
144 break;
145 case BottomRight:
146 anchorWidgetOrigin = QPoint( mAnchorWidget->width(), mAnchorWidget->height() );
147 break;
148 }
149
150 anchorWidgetOrigin = mAnchorWidget->mapTo( parentWidget(), anchorWidgetOrigin );
151 int anchorX = anchorWidgetOrigin.x();
152 int anchorY = anchorWidgetOrigin.y();
153
154 switch ( mFloatAnchorPoint )
155 {
156 case TopLeft:
157 break;
158 case TopMiddle:
159 anchorX = anchorX - width() / 2;
160 break;
161 case TopRight:
162 anchorX = anchorX - width();
163 break;
164 case MiddleLeft:
165 anchorY = anchorY - height() / 2;
166 break;
167 case Middle:
168 anchorY = anchorY - height() / 2;
169 anchorX = anchorX - width() / 2;
170 break;
171 case MiddleRight:
172 anchorX = anchorX - width();
173 anchorY = anchorY - height() / 2;
174 break;
175 case BottomLeft:
176 anchorY = anchorY - height();
177 break;
178 case BottomMiddle:
179 anchorX = anchorX - width() / 2;
180 anchorY = anchorY - height();
181 break;
182 case BottomRight:
183 anchorX = anchorX - width();
184 anchorY = anchorY - height();
185 break;
186 }
187
188 // constrain x so that widget floats within parent widget
189 anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() );
190
191 move( anchorX, anchorY );
192 }
193}
194
195//
196// QgsFloatingWidgetEventFilter
197//
198
200QgsFloatingWidgetEventFilter::QgsFloatingWidgetEventFilter( QWidget *parent )
201 : QObject( parent )
202{
203}
204
205bool QgsFloatingWidgetEventFilter::eventFilter( QObject *object, QEvent *event )
206{
207 Q_UNUSED( object )
208 switch ( event->type() )
209 {
210 case QEvent::Move:
211 case QEvent::Resize:
212 emit anchorPointChanged();
213 return false;
214 default:
215 return false;
216 }
217}
218
void anchorWidgetPointChanged(QgsFloatingWidget::AnchorPoint point)
Emitted when the anchor widget point changes.
void setAnchorWidget(QWidget *widget)
Sets the widget to "anchor" the floating widget to.
QgsFloatingWidget(QWidget *parent=nullptr)
Constructor for QgsFloatingWidget.
void setAnchorWidgetPoint(AnchorPoint point)
Returns the anchor widget's anchor point, which corresponds to the point on the anchor widget which t...
void resizeEvent(QResizeEvent *e) override
void anchorPointChanged(QgsFloatingWidget::AnchorPoint point)
Emitted when the anchor point changes.
AnchorPoint
Reference points for anchoring widget position.
@ BottomMiddle
Bottom center of widget.
@ TopMiddle
Top center of widget.
@ MiddleRight
Middle right of widget.
@ MiddleLeft
Middle left of widget.
@ BottomRight
Bottom-right of widget.
@ BottomLeft
Bottom-left of widget.
@ Middle
Middle of widget.
@ TopLeft
Top-left of widget.
@ TopRight
Top-right of widget.
void paintEvent(QPaintEvent *e) override
void setAnchorPoint(AnchorPoint point)
Sets the floating widget's anchor point, which corresponds to the point on the widget which should re...
void anchorWidgetChanged(QWidget *widget)
Emitted when the anchor widget changes.
void showEvent(QShowEvent *e) override