Skip to content

Commit

Permalink
Merge pull request #5252 from opengisch/canvas_margins
Browse files Browse the repository at this point in the history
Implement margins concept for map canvas
  • Loading branch information
nirvn committed May 21, 2024
2 parents fad8561 + 07ddbca commit ef39edd
Show file tree
Hide file tree
Showing 28 changed files with 343 additions and 149 deletions.
2 changes: 1 addition & 1 deletion src/core/bookmarkmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void BookmarkModel::setExtentFromBookmark( const QModelIndex &index )
return;
}

mMapSettings->setExtent( transformedRect );
mMapSettings->setExtent( transformedRect, true );
}

QString BookmarkModel::addBookmarkAtPoint( QgsPoint point, const QString &name, const QString &group )
Expand Down
4 changes: 2 additions & 2 deletions src/core/featurelistextentcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void FeatureListExtentController::zoomToSelected( bool skipIfIntersects ) const

if ( layer && layer->geometryType() != Qgis::GeometryType::Unknown && layer->geometryType() != Qgis::GeometryType::Null )
{
QgsRectangle extent = FeatureUtils::extent( mMapSettings, layer, feat, mFeatureForm ? mFeatureForm->x() : 0.0, mFeatureForm ? mFeatureForm->y() : 0.0 );
QgsRectangle extent = FeatureUtils::extent( mMapSettings, layer, feat );
if ( !skipIfIntersects || !mMapSettings->extent().intersects( extent ) )
mMapSettings->setExtent( extent );
mMapSettings->setExtent( extent, true );
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/core/featurelistextentcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class FeatureListExtentController : public QObject
Q_PROPERTY( FeatureListModelSelection *selection MEMBER mSelection NOTIFY selectionChanged )
Q_PROPERTY( bool autoZoom MEMBER mAutoZoom NOTIFY autoZoomChanged )
Q_PROPERTY( QgsQuickMapSettings *mapSettings MEMBER mMapSettings NOTIFY mapSettingsChanged )
Q_PROPERTY( QQuickItem *featureForm MEMBER mFeatureForm NOTIFY featureFormChanged )

public:
explicit FeatureListExtentController( QObject *parent = nullptr );
Expand All @@ -56,7 +55,6 @@ class FeatureListExtentController : public QObject
void selectionChanged();
void modelChanged();
void mapSettingsChanged();
void featureFormChanged();
void featureFormStateRequested();

private slots:
Expand All @@ -67,7 +65,6 @@ class FeatureListExtentController : public QObject
MultiFeatureListModel *mModel = nullptr;
FeatureListModelSelection *mSelection = nullptr;
QgsQuickMapSettings *mMapSettings = nullptr;
QQuickItem *mFeatureForm = nullptr;
bool mAutoZoom = false;
};

Expand Down
4 changes: 2 additions & 2 deletions src/core/locator/activelayerfeatureslocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,11 @@ void ActiveLayerFeaturesLocatorFilter::triggerResultFromAction( const QgsLocator

if ( r.isEmpty() || mLocatorBridge->keepScale() )
{
mLocatorBridge->mapSettings()->setCenter( QgsPoint( r.center() ) );
mLocatorBridge->mapSettings()->setCenter( QgsPoint( r.center() ), true );
}
else
{
mLocatorBridge->mapSettings()->setExtent( r );
mLocatorBridge->mapSettings()->setExtent( r, true );
}

mLocatorBridge->locatorHighlightGeometry()->setProperty( "qgsGeometry", geom );
Expand Down
4 changes: 2 additions & 2 deletions src/core/locator/featureslocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ void FeaturesLocatorFilter::triggerResultFromAction( const QgsLocatorResult &res
}

if ( r.isEmpty() || mLocatorBridge->keepScale() )
mLocatorBridge->mapSettings()->setCenter( QgsPoint( r.center() ) );
mLocatorBridge->mapSettings()->setCenter( QgsPoint( r.center() ), true );
else
mLocatorBridge->mapSettings()->setExtent( r );
mLocatorBridge->mapSettings()->setExtent( r, true );


mLocatorBridge->locatorHighlightGeometry()->setProperty( "qgsGeometry", geom );
Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/finlandlocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void FinlandLocatorFilter::handleGeocodeResult( const QgsGeocoderResult &result
return;
}

mLocatorBridge->mapSettings()->setCenter( transformedGeometry.centroid().vertexAt( 0 ) );
mLocatorBridge->mapSettings()->setCenter( transformedGeometry.centroid().vertexAt( 0 ), true );

mLocatorBridge->locatorHighlightGeometry()->setProperty( "qgsGeometry", result.geometry() );
mLocatorBridge->locatorHighlightGeometry()->setProperty( "crs", result.crs() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/gotolocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void GotoLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result,
}
else
{
mLocatorBridge->mapSettings()->setCenter( geom.vertexAt( 0 ) );
mLocatorBridge->mapSettings()->setCenter( geom.vertexAt( 0 ), true );

mLocatorBridge->locatorHighlightGeometry()->setProperty( "qgsGeometry", geom );
mLocatorBridge->locatorHighlightGeometry()->setProperty( "crs", mLocatorBridge->mapSettings()->mapSettings().destinationCrs() );
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsquick/qgsquickmapcanvasmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,34 @@ void QgsQuickMapCanvasMap::setQuality( double quality )
refresh();
}

double QgsQuickMapCanvasMap::bottomMargin() const
{
return mMapSettings->bottomMargin();
}

void QgsQuickMapCanvasMap::setBottomMargin( double bottomMargin )
{
if ( mMapSettings->bottomMargin() == bottomMargin )
return;

mMapSettings->setBottomMargin( bottomMargin );
emit bottomMarginChanged();
}

double QgsQuickMapCanvasMap::rightMargin() const
{
return mMapSettings->rightMargin();
}

void QgsQuickMapCanvasMap::setRightMargin( double rightMargin )
{
if ( mMapSettings->rightMargin() == rightMargin )
return;

mMapSettings->setRightMargin( rightMargin );
emit rightMarginChanged();
}

bool QgsQuickMapCanvasMap::forceDeferredLayersRepaint() const
{
return mForceDeferredLayersRepaint;
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsquick/qgsquickmapcanvasmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class QgsQuickMapCanvasMap : public QQuickItem
*/
Q_PROPERTY( QgsQuickMapSettings *mapSettings READ mapSettings )

/**
* The bottom margin used by the map settings when calculating map extent or center.
*/
Q_PROPERTY( double bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY bottomMarginChanged )

/**
* The right margin used by the map settings when calculating map extent or center.
*/
Q_PROPERTY( double rightMargin READ rightMargin WRITE setRightMargin NOTIFY rightMarginChanged )

/**
* When freeze property is set to true, the map canvas does not refresh.
* The value temporary changes during the rendering process.
Expand Down Expand Up @@ -141,6 +151,18 @@ class QgsQuickMapCanvasMap : public QQuickItem
//!\copydoc QgsQuickMapCanvasMap::forceDeferredLayersRepaint
void setForceDeferredLayersRepaint( bool deferred );

//!\copydoc QgsQuickMapCanvasMap::bottomMargin
double bottomMargin() const;

//!\copydoc QgsQuickMapCanvasMap::bottomMargin
void setBottomMargin( double bottomMargin );

//!\copydoc QgsQuickMapCanvasMap::rightMargin
double rightMargin() const;

//!\copydoc QgsQuickMapCanvasMap::rightMargin
void setRightMargin( double rightMargin );

/**
* Returns an image of the last successful map canvas rendering
*/
Expand Down Expand Up @@ -176,6 +198,12 @@ class QgsQuickMapCanvasMap : public QQuickItem
//!\copydoc QgsQuickMapCanvasMap::forceDeferredLayersRepaint
void forceDeferredLayersRepaintChanged();

//!\copydoc QgsQuickMapCanvasMap::bottomMargin
void bottomMarginChanged();

//!\copydoc QgsQuickMapCanvasMap::rightMargin
void rightMarginChanged();

protected:
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
void geometryChanged( const QRectF &newGeometry, const QRectF &oldGeometry ) override;
Expand Down
106 changes: 92 additions & 14 deletions src/core/qgsquick/qgsquickmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,50 @@ QgsRectangle QgsQuickMapSettings::extent() const
return mMapSettings.extent();
}

void QgsQuickMapSettings::setExtent( const QgsRectangle &extent )
void QgsQuickMapSettings::setExtent( const QgsRectangle &extent, bool handleMargins )
{
if ( mMapSettings.extent() == extent || extent.isNull() )
return;
if ( handleMargins )
{
const double rightMargin = mRightMargin * devicePixelRatio();
const double bottomMargin = mBottomMargin * devicePixelRatio();
const double outputSizeRatio = ( outputSize().width() - rightMargin ) / ( outputSize().height() - bottomMargin );
const double extentRatio = extent.width() / extent.height();

mMapSettings.setExtent( extent );
QgsRectangle modifiedExtent = extent;
if ( extentRatio > outputSizeRatio )
{
const double heightAdjustment = ( extent.width() / outputSizeRatio - extent.height() ) / 2;
modifiedExtent.setYMinimum( extent.yMinimum() - heightAdjustment );
modifiedExtent.setYMaximum( extent.yMaximum() + heightAdjustment );
}
else
{
const double widthAdjustment = ( extent.height() * outputSizeRatio - extent.width() ) / 2;
modifiedExtent.setXMinimum( extent.xMinimum() - widthAdjustment );
modifiedExtent.setXMaximum( extent.xMaximum() + widthAdjustment );
}

if ( rightMargin > 0 )
{
modifiedExtent.setXMaximum( modifiedExtent.xMaximum() + ( modifiedExtent.width() / ( outputSize().width() - rightMargin ) ) * rightMargin );
}
if ( bottomMargin > 0 )
{
modifiedExtent.setYMinimum( modifiedExtent.yMinimum() - ( modifiedExtent.height() / ( outputSize().height() - bottomMargin ) ) * bottomMargin );
}

if ( mMapSettings.extent() == modifiedExtent || modifiedExtent.isNull() )
return;

mMapSettings.setExtent( modifiedExtent );
}
else
{
if ( mMapSettings.extent() == extent || extent.isNull() )
return;

mMapSettings.setExtent( extent );
}
emit extentChanged();
}

Expand All @@ -95,18 +133,30 @@ QgsPoint QgsQuickMapSettings::center() const
return QgsPoint( extent().center() );
}

void QgsQuickMapSettings::setCenter( const QgsPoint &center )
void QgsQuickMapSettings::setCenter( const QgsPoint &center, bool handleMargins )
{
if ( center.isEmpty() )
return;

QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();

const QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
QgsRectangle e = mMapSettings.extent();
e.setXMinimum( e.xMinimum() + delta.x() );
e.setXMaximum( e.xMaximum() + delta.x() );
e.setYMinimum( e.yMinimum() + delta.y() );
e.setYMaximum( e.yMaximum() + delta.y() );
if ( handleMargins )
{
const double xAdjustment = mRightMargin * devicePixelRatio() * mMapSettings.mapUnitsPerPixel() / 2;
const double yAdjustment = mBottomMargin * devicePixelRatio() * mMapSettings.mapUnitsPerPixel() / 2;

e.setXMinimum( e.xMinimum() + delta.x() - xAdjustment );
e.setXMaximum( e.xMaximum() + delta.x() - xAdjustment );
e.setYMinimum( e.yMinimum() + delta.y() - yAdjustment );
e.setYMaximum( e.yMaximum() + delta.y() - yAdjustment );
}
else
{
e.setXMinimum( e.xMinimum() + delta.x() );
e.setXMaximum( e.xMaximum() + delta.x() );
e.setYMinimum( e.yMinimum() + delta.y() );
e.setYMaximum( e.yMaximum() + delta.y() );
}

setExtent( e );
}
Expand All @@ -126,7 +176,7 @@ void QgsQuickMapSettings::setCenterToLayer( QgsMapLayer *layer, bool shouldZoom
}
}

void QgsQuickMapSettings::setExtentFromPoints( const QVariantList &points, const double &minimumScale )
void QgsQuickMapSettings::setExtentFromPoints( const QVariantList &points, const double &minimumScale, bool handleMargins )
{
if ( points.isEmpty() )
return;
Expand All @@ -135,7 +185,7 @@ void QgsQuickMapSettings::setExtentFromPoints( const QVariantList &points, const
{
QgsPoint p = points.at( 0 ).value<QgsPoint>();
if ( !p.isEmpty() )
setCenter( p );
setCenter( p, handleMargins );
return;
}

Expand Down Expand Up @@ -174,7 +224,7 @@ void QgsQuickMapSettings::setExtentFromPoints( const QVariantList &points, const
}
}

setExtent( extent.buffered( buffer ) );
setExtent( extent.buffered( buffer ), handleMargins );
}

double QgsQuickMapSettings::mapUnitsPerPoint() const
Expand Down Expand Up @@ -415,3 +465,31 @@ void QgsQuickMapSettings::setTemporalEnd( const QDateTime &end )
mMapSettings.setTemporalRange( QgsDateTimeRange( range.begin(), end ) );
emit temporalStateChanged();
}

double QgsQuickMapSettings::bottomMargin() const
{
return mBottomMargin;
}

void QgsQuickMapSettings::setBottomMargin( double bottomMargin )
{
if ( mBottomMargin == bottomMargin )
return;

mBottomMargin = bottomMargin;
emit bottomMarginChanged();
}

double QgsQuickMapSettings::rightMargin() const
{
return mRightMargin;
}

void QgsQuickMapSettings::setRightMargin( double rightMargin )
{
if ( mRightMargin == rightMargin )
return;

mRightMargin = rightMargin;
emit rightMarginChanged();
}
Loading

1 comment on commit ef39edd

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.