Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow layout elevation profile items to use the atlas feature as
the source of the profile curve

(Requires a line geometry type for the atlas layer)
  • Loading branch information
nyalldawson committed Jan 30, 2023
1 parent 982da52 commit ff447f0
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 18 deletions.
Expand Up @@ -126,6 +126,20 @@ actual profile curve a point can reside within to be included in the results. Ot
ignore this tolerance if it is not appropriate for the particular source.

.. seealso:: :py:func:`setTolerance`
%End

bool atlasDriven() const;
%Docstring
Returns whether the profile curve is set to follow the current atlas feature.

.. seealso:: :py:func:`setAtlasDriven`
%End

void setAtlasDriven( bool enabled );
%Docstring
Sets whether the profile curve will follow the current atlas feature.

.. seealso:: :py:func:`atlasDriven`
%End

QgsProfileRequest profileRequest() const;
Expand Down
23 changes: 23 additions & 0 deletions src/core/layout/qgslayoutitemelevationprofile.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsterrainprovider.h"
#include "qgsprofilerenderer.h"
#include "qgslayoututils.h"
#include "qgsvectorlayer.h"

#include <QTimer>

Expand Down Expand Up @@ -506,6 +507,11 @@ double QgsLayoutItemElevationProfile::tolerance() const
return mTolerance;
}

void QgsLayoutItemElevationProfile::setAtlasDriven( bool enabled )
{
mAtlasDriven = enabled;
}

QgsProfileRequest QgsLayoutItemElevationProfile::profileRequest() const
{
QgsProfileRequest req( mCurve ? mCurve.get()->clone() : nullptr );
Expand Down Expand Up @@ -670,6 +676,21 @@ void QgsLayoutItemElevationProfile::paint( QPainter *painter, const QStyleOption

void QgsLayoutItemElevationProfile::refresh()
{
if ( mAtlasDriven && mLayout && mLayout->reportContext().layer() )
{
if ( QgsVectorLayer *layer = mLayout->reportContext().layer() )
{
mCrs = layer->crs();
}
const QgsGeometry curveGeom( mLayout->reportContext().currentGeometry( mCrs ) );
if ( const QgsAbstractGeometry *geom = curveGeom.constGet() )
{
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom->simplifiedTypeRef() ) )
{
mCurve.reset( curve->clone() );
}
}
}
QgsLayoutItem::refresh();
invalidateCache();
}
Expand All @@ -696,6 +717,7 @@ bool QgsLayoutItemElevationProfile::writePropertiesToElement( QDomElement &layou
}

layoutProfileElem.setAttribute( QStringLiteral( "tolerance" ), mTolerance );
layoutProfileElem.setAttribute( QStringLiteral( "atlasDriven" ), mAtlasDriven ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
if ( mCrs.isValid() )
{
QDomElement crsElem = doc.createElement( QStringLiteral( "crs" ) );
Expand Down Expand Up @@ -756,6 +778,7 @@ bool QgsLayoutItemElevationProfile::readPropertiesFromElement( const QDomElement
}

mTolerance = itemElem.attribute( QStringLiteral( "tolerance" ) ).toDouble();
mAtlasDriven = static_cast< bool >( itemElem.attribute( QStringLiteral( "atlasDriven" ), QStringLiteral( "0" ) ).toInt() );

{
mLayers.clear();
Expand Down
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoutitemelevationprofile.h
Expand Up @@ -144,6 +144,20 @@ class CORE_EXPORT QgsLayoutItemElevationProfile: public QgsLayoutItem
*/
double tolerance() const;

/**
* Returns whether the profile curve is set to follow the current atlas feature.
* \see setAtlasDriven()
*/
bool atlasDriven() const { return mAtlasDriven; }

/**
* Sets whether the profile curve will follow the current atlas feature.
*
* \see atlasDriven()
*/
void setAtlasDriven( bool enabled );

/**
* Returns the profile request used to generate the elevation profile.
*/
Expand Down Expand Up @@ -173,6 +187,7 @@ class CORE_EXPORT QgsLayoutItemElevationProfile: public QgsLayoutItem

QgsCoordinateReferenceSystem mCrs;
std::unique_ptr< QgsCurve> mCurve;
bool mAtlasDriven = false;

double mTolerance = 0;

Expand All @@ -191,6 +206,7 @@ class CORE_EXPORT QgsLayoutItemElevationProfile: public QgsLayoutItem
std::unique_ptr< QgsProfilePlotRenderer > mRenderJob;
bool mPainterCancelWait = false;


};

#endif //QGSLAYOUTITEMELEVATIONPROFILE_H
58 changes: 58 additions & 0 deletions src/gui/layout/qgslayoutelevationprofilewidget.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgselevationprofilelayertreeview.h"
#include "qgselevationprofilecanvas.h"
#include "qgscurve.h"
#include "qgslayoutatlas.h"
#include <QMenu>

std::function< void( QgsLayoutElevationProfileWidget *, QMenu * ) > QgsLayoutElevationProfileWidget::sBuildCopyMenuFunction = []( QgsLayoutElevationProfileWidget *, QMenu * ) {};
Expand Down Expand Up @@ -90,6 +91,18 @@ QgsLayoutElevationProfileWidget::QgsLayoutElevationProfileWidget( QgsLayoutItemE
mProfile->endCommand();
} );

connect( mCheckControlledByAtlas, &QCheckBox::toggled, this, [ = ]
{
if ( !mProfile || mBlockChanges )
return;

mProfile->beginCommand( tr( "Change Profile Atlas Control" ) );
mProfile->setAtlasDriven( mCheckControlledByAtlas->isChecked() );
mProfile->invalidateCache();
mProfile->update();
mProfile->endCommand();
} );

mSpinMinDistance->setClearValue( 0 );
connect( mSpinMinDistance, qOverload< double >( &QDoubleSpinBox::valueChanged ), this, [ = ]( double value )
{
Expand Down Expand Up @@ -483,6 +496,15 @@ QgsLayoutElevationProfileWidget::QgsLayoutElevationProfileWidget( QgsLayoutItemE
mChartBackgroundSymbolButton->setLayer( layer );
mChartBorderSymbolButton->setLayer( layer );
} );

connect( &mProfile->layout()->reportContext(), &QgsLayoutReportContext::layerChanged,
this, &QgsLayoutElevationProfileWidget::atlasLayerChanged );
}

if ( QgsLayoutAtlas *atlas = layoutAtlas() )
{
connect( atlas, &QgsLayoutAtlas::toggled, this, &QgsLayoutElevationProfileWidget::layoutAtlasToggled );
layoutAtlasToggled( atlas->enabled() );
}
}

Expand All @@ -505,6 +527,11 @@ void QgsLayoutElevationProfileWidget::setDesignerInterface( QgsLayoutDesignerInt
QgsLayoutItemBaseWidget::setDesignerInterface( iface );
}

void QgsLayoutElevationProfileWidget::setReportTypeString( const QString &string )
{
mCheckControlledByAtlas->setText( tr( "Controlled by %1" ).arg( string == tr( "atlas" ) ? tr( "Atlas" ) : tr( "Report" ) ) );
}

void QgsLayoutElevationProfileWidget::copySettingsFromProfileCanvas( QgsElevationProfileCanvas *canvas )
{
mBlockChanges++;
Expand Down Expand Up @@ -609,6 +636,7 @@ void QgsLayoutElevationProfileWidget::setGuiElementValues()
mBlockChanges++;

mSpinTolerance->setValue( mProfile->tolerance() );
mCheckControlledByAtlas->setChecked( mProfile->atlasDriven() );

mSpinMinDistance->setValue( mProfile->plot()->xMinimum() );
mSpinMaxDistance->setValue( mProfile->plot()->xMaximum() );
Expand Down Expand Up @@ -688,3 +716,33 @@ void QgsLayoutElevationProfileWidget::updateItemLayers()
mProfile->setLayers( layers );
mProfile->update();
}

void QgsLayoutElevationProfileWidget::layoutAtlasToggled( bool atlasEnabled )
{
if ( atlasEnabled &&
mProfile && mProfile->layout() && mProfile->layout()->reportContext().layer()
&& mProfile->layout()->reportContext().layer()->geometryType() == QgsWkbTypes::LineGeometry )
{
mCheckControlledByAtlas->setEnabled( true );
}
else
{
mCheckControlledByAtlas->setEnabled( false );
mCheckControlledByAtlas->setChecked( false );
}
}

void QgsLayoutElevationProfileWidget::atlasLayerChanged( QgsVectorLayer *layer )
{
if ( !layer || layer->geometryType() != QgsWkbTypes::LineGeometry )
{
//non-line layer, disable atlas control
mCheckControlledByAtlas->setChecked( false );
mCheckControlledByAtlas->setEnabled( false );
return;
}
else
{
mCheckControlledByAtlas->setEnabled( true );
}
}
3 changes: 3 additions & 0 deletions src/gui/layout/qgslayoutelevationprofilewidget.h
Expand Up @@ -48,6 +48,7 @@ class GUI_EXPORT QgsLayoutElevationProfileWidget: public QgsLayoutItemBaseWidget
void setMasterLayout( QgsMasterLayoutInterface *masterLayout ) override;
QgsExpressionContext createExpressionContext() const override;
void setDesignerInterface( QgsLayoutDesignerInterface *iface ) override;
void setReportTypeString( const QString &string ) override;

/**
* Copies selected settings from a elevation profile \a canvas.
Expand All @@ -64,6 +65,8 @@ class GUI_EXPORT QgsLayoutElevationProfileWidget: public QgsLayoutItemBaseWidget

void setGuiElementValues();
void updateItemLayers();
void layoutAtlasToggled( bool atlasEnabled );
void atlasLayerChanged( QgsVectorLayer *layer );

private:

Expand Down
44 changes: 26 additions & 18 deletions src/ui/layout/qgslayoutelevationprofilewidgetbase.ui
Expand Up @@ -79,7 +79,7 @@
<x>0</x>
<y>0</y>
<width>548</width>
<height>1345</height>
<height>1375</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -111,10 +111,10 @@
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="groupChartRanges_2">
<property name="title">
<string>Profile Settings</string>
<string>Profile Curve</string>
</property>
<layout class="QGridLayout" name="gridLayout_5" columnstretch="1,2,0">
<item row="0" column="1">
<item row="1" column="1">
<widget class="QgsDoubleSpinBox" name="mSpinTolerance">
<property name="decimals">
<number>6</number>
Expand All @@ -124,17 +124,24 @@
</property>
</widget>
</item>
<item row="0" column="2">
<item row="1" column="0">
<widget class="QLabel" name="label_27">
<property name="text">
<string>Tolerance</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QgsPropertyOverrideButton" name="mDDBtnTolerance">
<property name="text">
<string>…</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_27">
<item row="0" column="0" colspan="3">
<widget class="QCheckBox" name="mCheckControlledByAtlas">
<property name="text">
<string>Tolerance</string>
<string>Controlled by atlas</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -745,17 +752,23 @@
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsScrollArea</class>
<extends>QScrollArea</extends>
<header>qgsscrollarea.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
<header location="global">qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsPropertyOverrideButton</class>
<extends>QToolButton</extends>
Expand All @@ -766,12 +779,6 @@
<extends>QToolButton</extends>
<header>qgssymbolbutton.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
<header location="global">qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFontButton</class>
<extends>QToolButton</extends>
Expand All @@ -783,6 +790,7 @@
</tabstops>
<resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit ff447f0

Please sign in to comment.