Skip to content

Commit

Permalink
Fix GPS toolbar width expansion when toolbar is floating
Browse files Browse the repository at this point in the history
Qt seems to have issues with adjusting the toolbar size when
a child label widget's text width changes, so we have to
force a fixed width for the toolbar
  • Loading branch information
nyalldawson committed Nov 8, 2022
1 parent 0e5e393 commit e41041b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/app/gps/qgsgpstoolbar.cpp
Expand Up @@ -121,9 +121,6 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mShowInfoAction->setCheckable( true );
addAction( mShowInfoAction );

mLocationLabel = new QLabel();
addWidget( mLocationLabel );

connect( mConnection, &QgsAppGpsConnection::positionChanged, this, &QgsGpsToolBar::updateLocationLabel );
updateLocationLabel( mConnection->lastValidLocation() );

Expand All @@ -133,7 +130,7 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
settingsButton->setMenu( QgisApp::instance()->gpsSettingsMenu() );
settingsButton->setPopupMode( QToolButton::InstantPopup );
settingsButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) ) );
addWidget( settingsButton );
mSettingsMenuAction = addWidget( settingsButton );

mRecenterAction->setEnabled( false );
mCreateFeatureAction->setEnabled( false );
Expand All @@ -152,6 +149,8 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mRecenterAction->setEnabled( false );
mCreateFeatureAction->setEnabled( false );
mAddTrackVertexAction->setEnabled( false );
delete mLocationLabel;
mLocationLabel = nullptr;
break;
case Qgis::GpsConnectionStatus::Connecting:
whileBlocking( mConnectAction )->setChecked( true );
Expand All @@ -161,6 +160,8 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mRecenterAction->setEnabled( false );
mCreateFeatureAction->setEnabled( false );
mAddTrackVertexAction->setEnabled( false );
delete mLocationLabel;
mLocationLabel = nullptr;
break;
case Qgis::GpsConnectionStatus::Connected:
whileBlocking( mConnectAction )->setChecked( true );
Expand All @@ -173,13 +174,18 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mAddTrackVertexAction->setEnabled( mEnableAddVertexButton );
break;
}
// this is necessary to ensure that the toolbar in floating mode correctly resizes to fit the label!
setFixedWidth( sizeHint().width() );
} );

connect( QgsProject::instance()->gpsSettings(), &QgsProjectGpsSettings::destinationLayerChanged,
this, &QgsGpsToolBar::destinationLayerChanged );

connect( QgsProject::instance()->gpsSettings(), &QgsProjectGpsSettings::automaticallyAddTrackVerticesChanged, this, [ = ]( bool enabled ) { setAddVertexButtonEnabled( !enabled ); } );
setAddVertexButtonEnabled( !QgsProject::instance()->gpsSettings()->automaticallyAddTrackVertices() );

// this is necessary to ensure that the toolbar in floating mode correctly resizes to fit the label!
setFixedWidth( sizeHint().width() );
}

void QgsGpsToolBar::setAddVertexButtonEnabled( bool enabled )
Expand All @@ -197,13 +203,22 @@ void QgsGpsToolBar::updateLocationLabel( const QgsPoint &point )
{
if ( point.isEmpty() )
{
mLocationLabel->clear();
delete mLocationLabel;
mLocationLabel = nullptr;
}
else
{
if ( !mLocationLabel )
{
mLocationLabel = new QLabel();
insertWidget( mSettingsMenuAction, mLocationLabel );
}
const QString pos = QgsCoordinateUtils::formatCoordinateForProject( QgsProject::instance(), point, QgsCoordinateReferenceSystem(), 8 );
mLocationLabel->setText( pos );
}

// this is necessary to ensure that the toolbar in floating mode correctly resizes to fit the label!
setFixedWidth( sizeHint().width() );
}

void QgsGpsToolBar::destinationLayerChanged( QgsVectorLayer *vlayer )
Expand Down

0 comments on commit e41041b

Please sign in to comment.