Skip to content

Commit

Permalink
Add a toolbar to elevation profile widget with button to copy settings
Browse files Browse the repository at this point in the history
from a elevation profile dock

(Slightly hacky approach is required to allow the gui based item widget
access to the docks which live in app, without creating an interface
api for a single use case)
  • Loading branch information
nyalldawson committed Jan 27, 2023
1 parent c1e0a4a commit 9caa96f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 15 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -975,6 +975,7 @@
<file>themes/default/gpsicons/mIconGpsDestinationLayer.svg</file>
<file>themes/default/mLayoutItemElevationProfile.svg</file>
<file>themes/default/mActionElevationProfile.svg</file>
<file>themes/default/mActionCopyProfileSettings.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/mActionCopyProfileSettings.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/app/elevation/qgselevationprofilewidget.h
Expand Up @@ -80,6 +80,8 @@ class QgsElevationProfileWidget : public QWidget

void setMainCanvas( QgsMapCanvas *canvas );

QgsElevationProfileCanvas *profileCanvas() { return mCanvas; }

/**
* Cancel any rendering job, in a blocking way. Used for application closing.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -288,6 +288,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgslayoutqptdrophandler.h"
#include "qgslayoutimagedrophandler.h"
#include "qgslayoutguiutils.h"
#include "qgslayoutelevationprofilewidget.h"
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgsactionlocatorfilter.h"
Expand Down Expand Up @@ -13108,6 +13109,31 @@ void QgisApp::initLayouts()
registerCustomLayoutDropHandler( mLayoutQptDropHandler );
mLayoutImageDropHandler = new QgsLayoutImageDropHandler( this );
registerCustomLayoutDropHandler( mLayoutImageDropHandler );

QgsLayoutElevationProfileWidget::sBuildCopyMenuFunction = [ = ]( QgsLayoutElevationProfileWidget * layoutWidget, QMenu * menu )
{
menu->clear();
const QList<QgsElevationProfileWidget *> elevationProfileWidgets = findChildren< QgsElevationProfileWidget * >();

if ( elevationProfileWidgets.empty() )
{
QAction *action = new QAction( tr( "No Elevation Profiles Found" ), menu );
action->setEnabled( false );
menu->addAction( action );
}
else
{
for ( QgsElevationProfileWidget *widget : elevationProfileWidgets )
{
QAction *action = new QAction( tr( "Copy From %1" ).arg( widget->canvasName() ), menu );
connect( action, &QAction::triggered, widget, [ = ]
{
layoutWidget->copySettingsFromProfileCanvas( widget->profileCanvas() );
} );
menu->addAction( action );
}
}
};
}

Qgs3DMapCanvasWidget *QgisApp::createNew3DMapCanvasDock( const QString &name, bool isDocked )
Expand Down
91 changes: 91 additions & 0 deletions src/gui/layout/qgslayoutelevationprofilewidget.cpp
Expand Up @@ -28,6 +28,11 @@
#include "qgslayertree.h"
#include "qgslayertreeregistrybridge.h"
#include "qgselevationprofilelayertreeview.h"
#include "qgselevationprofilecanvas.h"
#include "qgscurve.h"
#include <QMenu>

std::function< void( QgsLayoutElevationProfileWidget *, QMenu * ) > QgsLayoutElevationProfileWidget::sBuildCopyMenuFunction = []( QgsLayoutElevationProfileWidget *, QMenu * ) {};

QgsLayoutElevationProfileWidget::QgsLayoutElevationProfileWidget( QgsLayoutItemElevationProfile *profile )
: QgsLayoutItemBaseWidget( nullptr, profile )
Expand All @@ -40,6 +45,21 @@ QgsLayoutElevationProfileWidget::QgsLayoutElevationProfileWidget( QgsLayoutItemE
setupUi( this );
setPanelTitle( tr( "Elevation Profile Properties" ) );

mCopyFromDockMenu = new QMenu( this );
connect( mCopyFromDockMenu, &QMenu::aboutToShow, this, [ = ]
{
sBuildCopyMenuFunction( this, mCopyFromDockMenu );
} );

QToolButton *copyFromDockButton = new QToolButton();
copyFromDockButton->setAutoRaise( true );
copyFromDockButton->setToolTip( tr( "Copy From Profile" ) );
copyFromDockButton->setMenu( mCopyFromDockMenu );
copyFromDockButton->setPopupMode( QToolButton::InstantPopup );
copyFromDockButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionCopyProfileSettings.svg" ) ) );

mDockToolbar->addWidget( copyFromDockButton );

//add widget for general composer item properties
mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, profile );
mainLayout->addWidget( mItemPropertiesWidget );
Expand Down Expand Up @@ -431,6 +451,77 @@ QgsExpressionContext QgsLayoutElevationProfileWidget::createExpressionContext()
return mProfile->createExpressionContext();
}

void QgsLayoutElevationProfileWidget::setDesignerInterface( QgsLayoutDesignerInterface *iface )
{
mInterface = iface;
QgsLayoutItemBaseWidget::setDesignerInterface( iface );
}

void QgsLayoutElevationProfileWidget::copySettingsFromProfileCanvas( QgsElevationProfileCanvas *canvas )
{
mBlockChanges++;

mProfile->setCrs( canvas->crs() );
mProfile->setTolerance( canvas->tolerance() );
if ( const QgsCurve *curve = canvas->profileCurve() )
mProfile->setProfileCurve( curve->clone() );

mSpinMinDistance->setValue( canvas->plot().xMinimum() );
mSpinMinDistance->setClearValue( canvas->plot().xMinimum() );
mProfile->plot()->setXMinimum( canvas->plot().xMinimum() );

mSpinMaxDistance->setValue( canvas->plot().xMaximum() );
mSpinMaxDistance->setClearValue( canvas->plot().xMaximum() );
mProfile->plot()->setXMaximum( canvas->plot().xMaximum() );

mDistanceAxisMajorIntervalSpin->setValue( canvas->plot().xAxis().gridIntervalMajor() );
mDistanceAxisMajorIntervalSpin->setClearValue( canvas->plot().xAxis().gridIntervalMajor() );
mProfile->plot()->xAxis().setGridIntervalMajor( canvas->plot().xAxis().gridIntervalMajor() );

mDistanceAxisMinorIntervalSpin->setValue( canvas->plot().xAxis().gridIntervalMinor() );
mDistanceAxisMinorIntervalSpin->setClearValue( canvas->plot().xAxis().gridIntervalMinor() );
mProfile->plot()->xAxis().setGridIntervalMinor( canvas->plot().xAxis().gridIntervalMinor() );

mDistanceAxisLabelIntervalSpin->setValue( canvas->plot().xAxis().labelInterval() );
mDistanceAxisLabelIntervalSpin->setClearValue( canvas->plot().xAxis().labelInterval() );
mProfile->plot()->xAxis().setLabelInterval( canvas->plot().xAxis().labelInterval() );

mSpinMinElevation->setValue( canvas->plot().xMinimum() );
mSpinMinElevation->setClearValue( canvas->plot().yMinimum() );
mProfile->plot()->setYMinimum( canvas->plot().yMinimum() );

mSpinMaxElevation->setValue( canvas->plot().yMaximum() );
mSpinMaxElevation->setClearValue( canvas->plot().yMaximum() );
mProfile->plot()->setYMaximum( canvas->plot().yMaximum() );

mElevationAxisMajorIntervalSpin->setValue( canvas->plot().yAxis().gridIntervalMajor() );
mElevationAxisMajorIntervalSpin->setClearValue( canvas->plot().yAxis().gridIntervalMajor() );
mProfile->plot()->yAxis().setGridIntervalMajor( canvas->plot().yAxis().gridIntervalMajor() );

mElevationAxisMinorIntervalSpin->setValue( canvas->plot().yAxis().gridIntervalMinor() );
mElevationAxisMinorIntervalSpin->setClearValue( canvas->plot().yAxis().gridIntervalMinor() );
mProfile->plot()->yAxis().setGridIntervalMinor( canvas->plot().yAxis().gridIntervalMinor() );

mElevationAxisLabelIntervalSpin->setValue( canvas->plot().yAxis().labelInterval() );
mElevationAxisLabelIntervalSpin->setClearValue( canvas->plot().yAxis().labelInterval() );
mProfile->plot()->yAxis().setLabelInterval( canvas->plot().yAxis().labelInterval() );

QList<QgsMapLayer *> canvasLayers = canvas->layers();
// canvas layers are in opposite direction to what the layout item requires
std::reverse( canvasLayers.begin(), canvasLayers.end() );
mProfile->setLayers( canvasLayers );
const QList<QgsLayerTreeLayer *> layers = mLayerTree->findLayers();
for ( QgsLayerTreeLayer *layer : layers )
{
layer->setItemVisibilityChecked( mProfile->layers().contains( layer->layer() ) );
}
mLayerTree->reorderGroupLayers( mProfile->layers() );

mProfile->invalidateCache();
mProfile->update();
mBlockChanges--;
}

bool QgsLayoutElevationProfileWidget::setNewItem( QgsLayoutItem *item )
{
if ( item->type() != QgsLayoutItemRegistry::LayoutElevationProfile )
Expand Down
12 changes: 12 additions & 0 deletions src/gui/layout/qgslayoutelevationprofilewidget.h
Expand Up @@ -29,6 +29,7 @@
#include <QPointer>

class QgsElevationProfileLayerTreeView;
class QgsElevationProfileCanvas;

/**
* \ingroup gui
Expand All @@ -46,6 +47,14 @@ class GUI_EXPORT QgsLayoutElevationProfileWidget: public QgsLayoutItemBaseWidget
~QgsLayoutElevationProfileWidget() override;
void setMasterLayout( QgsMasterLayoutInterface *masterLayout ) override;
QgsExpressionContext createExpressionContext() const override;
void setDesignerInterface( QgsLayoutDesignerInterface *iface ) override;

/**
* Copies selected settings from a elevation profile \a canvas.
*/
void copySettingsFromProfileCanvas( QgsElevationProfileCanvas *canvas );

static std::function< void( QgsLayoutElevationProfileWidget *, QMenu * ) > sBuildCopyMenuFunction;

protected:

Expand All @@ -60,13 +69,16 @@ class GUI_EXPORT QgsLayoutElevationProfileWidget: public QgsLayoutItemBaseWidget

int mBlockChanges = 0;

QgsLayoutDesignerInterface *mInterface = nullptr;

QPointer< QgsLayoutItemElevationProfile > mProfile = nullptr;

QgsLayoutItemPropertiesWidget *mItemPropertiesWidget = nullptr;

std::unique_ptr< QgsLayerTree > mLayerTree;
QgsLayerTreeRegistryBridge *mLayerTreeBridge = nullptr;
QgsElevationProfileLayerTreeView *mLayerTreeView = nullptr;
QMenu *mCopyFromDockMenu = nullptr;
};

#endif //QGSLAYOUTELEVATIONPROFILEWIDGET_H
52 changes: 37 additions & 15 deletions src/ui/layout/qgslayoutelevationprofilewidgetbase.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>506</width>
<height>705</height>
<width>564</width>
<height>749</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -17,9 +17,9 @@
</sizepolicy>
</property>
<property name="windowTitle">
<string>Label Options</string>
<string>Elevation Profile Options</string>
</property>
<layout class="QVBoxLayout" name="_2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
Expand All @@ -36,7 +36,7 @@
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="mLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
Expand All @@ -51,8 +51,24 @@
</property>
</widget>
</item>
<item>
<widget class="QToolBar" name="mDockToolbar">
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="floatable">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QgsScrollArea" name="scrollArea">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
Expand All @@ -61,10 +77,16 @@
<rect>
<x>0</x>
<y>0</y>
<width>490</width>
<width>548</width>
<height>1267</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="mainLayout">
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="groupBox">
Expand Down Expand Up @@ -689,9 +711,14 @@
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFontButton</class>
<class>QgsPropertyOverrideButton</class>
<extends>QToolButton</extends>
<header>qgsfontbutton.h</header>
<header>qgspropertyoverridebutton.h</header>
</customwidget>
<customwidget>
<class>QgsSymbolButton</class>
<extends>QToolButton</extends>
<header>qgssymbolbutton.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
Expand All @@ -700,14 +727,9 @@
<container>1</container>
</customwidget>
<customwidget>
<class>QgsSymbolButton</class>
<extends>QToolButton</extends>
<header>qgssymbolbutton.h</header>
</customwidget>
<customwidget>
<class>QgsPropertyOverrideButton</class>
<class>QgsFontButton</class>
<extends>QToolButton</extends>
<header>qgspropertyoverridebutton.h</header>
<header>qgsfontbutton.h</header>
</customwidget>
</customwidgets>
<tabstops>
Expand Down

0 comments on commit 9caa96f

Please sign in to comment.