Skip to content

Commit

Permalink
Small cleanups to QgsFloatingWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 22, 2016
1 parent b19278c commit e27822b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
11 changes: 11 additions & 0 deletions python/gui/qgsfloatingwidget.sip
Expand Up @@ -73,6 +73,17 @@ class QgsFloatingWidget : QWidget
*/
void setAnchorWidgetPoint( AnchorPoint point );

signals:

//! Emitted when the anchor widget changes
void anchorWidgetChanged( QWidget* widget );

//! Emitted when the anchor point changes
void anchorPointChanged( AnchorPoint point );

//! Emitted when the anchor widget point changes
void anchorWidgetPointChanged( AnchorPoint point );

protected:
void showEvent( QShowEvent* e );
virtual void paintEvent( QPaintEvent* e );
Expand Down
37 changes: 32 additions & 5 deletions src/gui/qgsfloatingwidget.cpp
Expand Up @@ -34,12 +34,15 @@ QgsFloatingWidget::QgsFloatingWidget( QWidget *parent )
{
mParentEventFilter = new QgsFloatingWidgetEventFilter( parent );
parent->installEventFilter( mParentEventFilter );
connect( mParentEventFilter, SIGNAL( anchorPointChanged() ), this, SLOT( anchorPointChanged() ) );
connect( mParentEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged, this, &QgsFloatingWidget::onAnchorPointChanged );
}
}

void QgsFloatingWidget::setAnchorWidget( QWidget *widget )
{
if ( widget == mAnchorWidget )
return;

// remove existing event filter
if ( mAnchorWidget )
{
Expand All @@ -53,20 +56,41 @@ void QgsFloatingWidget::setAnchorWidget( QWidget *widget )
{
mAnchorEventFilter = new QgsFloatingWidgetEventFilter( mAnchorWidget );
mAnchorWidget->installEventFilter( mAnchorEventFilter );
connect( mAnchorEventFilter, SIGNAL( anchorPointChanged() ), this, SLOT( anchorPointChanged() ) );
connect( mAnchorEventFilter, &QgsFloatingWidgetEventFilter::anchorPointChanged, this, &QgsFloatingWidget::onAnchorPointChanged );
}

anchorPointChanged();
onAnchorPointChanged();
emit anchorWidgetChanged( mAnchorWidget );
}

QWidget *QgsFloatingWidget::anchorWidget()
{
return mAnchorWidget;
}

void QgsFloatingWidget::setAnchorPoint( QgsFloatingWidget::AnchorPoint point )
{
if ( point == mFloatAnchorPoint )
return;

mFloatAnchorPoint = point;
onAnchorPointChanged();
emit anchorPointChanged( mFloatAnchorPoint );
}

void QgsFloatingWidget::setAnchorWidgetPoint( QgsFloatingWidget::AnchorPoint point )
{
if ( point == mAnchorWidgetAnchorPoint )
return;

mAnchorWidgetAnchorPoint = point;
onAnchorPointChanged();
emit anchorWidgetPointChanged( mAnchorWidgetAnchorPoint );
}

void QgsFloatingWidget::showEvent( QShowEvent *e )
{
anchorPointChanged();
onAnchorPointChanged();
QWidget::showEvent( e );
}

Expand All @@ -79,8 +103,11 @@ void QgsFloatingWidget::paintEvent( QPaintEvent* e )
style()->drawPrimitive( QStyle::PE_Widget, &opt, &p, this );
}

void QgsFloatingWidget::anchorPointChanged()
void QgsFloatingWidget::onAnchorPointChanged()
{
if ( !parentWidget() )
return;

if ( mAnchorWidget )
{
QPoint anchorWidgetOrigin;
Expand Down
28 changes: 22 additions & 6 deletions src/gui/qgsfloatingwidget.h
Expand Up @@ -17,18 +17,23 @@

#include <QWidget>

class QgsFloatingWidgetEventFilter;

/** \ingroup gui
* \class QgsFloatingWidget
* A QWidget subclass for creating widgets which float outside of the normal Qt layout
* system. Floating widgets use an "anchor widget" to determine how they are anchored
* within their parent widget.
* \note Added in version 2.16
* \note Added in version 3.0
*/

class GUI_EXPORT QgsFloatingWidget: public QWidget
{
Q_OBJECT
Q_ENUMS( AnchorPoint )
Q_PROPERTY( QWidget* anchorWidget READ anchorWidget WRITE setAnchorWidget NOTIFY anchorWidgetChanged )
Q_PROPERTY( AnchorPoint anchorPoint READ anchorPoint WRITE setAnchorPoint NOTIFY anchorPointChanged )
Q_PROPERTY( AnchorPoint anchorWidgetPoint READ anchorWidgetPoint WRITE setAnchorWidgetPoint NOTIFY anchorWidgetPointChanged )

public:

Expand Down Expand Up @@ -75,7 +80,7 @@ class GUI_EXPORT QgsFloatingWidget: public QWidget
* @param point anchor point
* @see anchorPoint()
*/
void setAnchorPoint( AnchorPoint point ) { mFloatAnchorPoint = point; anchorPointChanged(); }
void setAnchorPoint( AnchorPoint point );

/** Returns the anchor widget's anchor point, which corresponds to the point on the anchor widget which
* the floating widget should "attach" to. The floating widget should remain fixed in the same relative position
Expand All @@ -89,7 +94,18 @@ class GUI_EXPORT QgsFloatingWidget: public QWidget
* to this anchor widget whenever the widget's parent is resized or moved.
* @see setAnchorWidgetPoint()
*/
void setAnchorWidgetPoint( AnchorPoint point ) { mAnchorWidgetAnchorPoint = point; anchorPointChanged(); }
void setAnchorWidgetPoint( AnchorPoint point );

signals:

//! Emitted when the anchor widget changes
void anchorWidgetChanged( QWidget* widget );

//! Emitted when the anchor point changes
void anchorPointChanged( AnchorPoint point );

//! Emitted when the anchor widget point changes
void anchorWidgetPointChanged( AnchorPoint point );

protected:
void showEvent( QShowEvent* e ) override;
Expand All @@ -98,13 +114,13 @@ class GUI_EXPORT QgsFloatingWidget: public QWidget
private slots:

//! Repositions the floating widget to a changed anchor point
void anchorPointChanged();
void onAnchorPointChanged();

private:

QWidget* mAnchorWidget;
QObject* mParentEventFilter;
QObject* mAnchorEventFilter;
QgsFloatingWidgetEventFilter* mParentEventFilter;
QgsFloatingWidgetEventFilter* mAnchorEventFilter;
AnchorPoint mFloatAnchorPoint;
AnchorPoint mAnchorWidgetAnchorPoint;

Expand Down

0 comments on commit e27822b

Please sign in to comment.