Skip to content

Commit

Permalink
Enable editing extent buffer in the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhoErvasti committed Dec 4, 2024
1 parent 984a947 commit 1db7781
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ set(QGIS_GUI_SRCS
symbology/qgssymbolbuffersettingswidget.cpp
symbology/qgssymbollayerwidget.cpp
symbology/qgssymbollevelsdialog.cpp
symbology/qgsextentbufferdialog.cpp
symbology/qgssymbolslistwidget.cpp
symbology/qgssymbolselectordialog.cpp
symbology/qgssymbolwidgetcontext.cpp
Expand Down Expand Up @@ -1551,6 +1552,7 @@ set(QGIS_GUI_HDRS
symbology/qgssymbolbuffersettingswidget.h
symbology/qgssymbollayerwidget.h
symbology/qgssymbollevelsdialog.h
symbology/qgsextentbufferdialog.h
symbology/qgssymbolselectordialog.h
symbology/qgssymbolslistwidget.h
symbology/qgssymbolwidgetcontext.h
Expand Down
127 changes: 127 additions & 0 deletions src/gui/symbology/qgsextentbufferdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/***************************************************************************
qgsextentbufferdialog.cpp
---------------------
begin : December 2024
copyright : (C) 2024 by Juho Ervasti
email : juho dot ervasti at gispo dot fi
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsextentbufferdialog.h"
#include "qdialogbuttonbox.h"
#include "qgsexpressioncontext.h"
#include "qgshelp.h"
#include "qgspanelwidget.h"
#include "qgssymbol.h"
#include "qgssymbolwidgetcontext.h"
#include "qgsvectorlayer.h"

QgsExtentBufferWidget::QgsExtentBufferWidget( QgsSymbol *symbol, QgsVectorLayer *layer, QWidget *parent )
: QgsPanelWidget( parent ), mSymbol( symbol ), mLayer( layer )
{
setupUi( this );

mExtentBufferSpinBox->setValue( mSymbol-> extentBuffer() );

connect( mExtentBufferSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), [ = ]()
{
emit widgetChanged();
} );

registerDataDefinedButton( mExtentBufferDDButton, QgsSymbol::Property::ExtentBuffer );
}

QgsSymbolWidgetContext QgsExtentBufferWidget::context() const
{
return mContext;
}

void QgsExtentBufferWidget::setContext( const QgsSymbolWidgetContext &context )
{
mContext = context;
}

void QgsExtentBufferWidget::registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsSymbol::Property key )
{
// pass in nullptr to avoid id, feature and geometry variables being added
// since the buffer is not evaluated per-feature
button->init( static_cast< int >( key ), mSymbol->dataDefinedProperties(), QgsSymbol::propertyDefinitions(), nullptr );
connect( button, &QgsPropertyOverrideButton::changed, [ = ]()
{
emit widgetChanged();
} );

button->registerExpressionContextGenerator( this );
}

QgsExpressionContext QgsExtentBufferWidget::createExpressionContext() const
{
QList<QgsExpressionContextScope *> scopes = mContext.globalProjectAtlasMapLayerScopes( mLayer );
QgsExpressionContext expContext( scopes );

return expContext;
}

double QgsExtentBufferWidget::extentBuffer() const
{
return mExtentBufferSpinBox->value();
}

QgsProperty QgsExtentBufferWidget::dataDefinedProperty() const
{
return mExtentBufferDDButton->toProperty();
}


/// QgsExtentBufferDialog

QgsExtentBufferDialog::QgsExtentBufferDialog( QgsSymbol *symbol, QgsVectorLayer *layer, QWidget *parent )
: QDialog( parent )
{
QVBoxLayout *vLayout = new QVBoxLayout();
mWidget = new QgsExtentBufferWidget( symbol, layer );
vLayout->addWidget( mWidget );

QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
connect( bbox, &QDialogButtonBox::accepted, this, &QgsExtentBufferDialog::accept );
connect( bbox, &QDialogButtonBox::rejected, this, &QgsExtentBufferDialog::reject );
connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsExtentBufferDialog::showHelp );

vLayout->addWidget( bbox );
setLayout( vLayout );

setWindowTitle( tr( "Extent buffer" ) );
}

double QgsExtentBufferDialog::extentBuffer() const
{
if ( !mWidget )
return 0;

return mWidget->extentBuffer();
}

QgsProperty QgsExtentBufferDialog::dataDefinedProperty() const
{
if ( !mWidget )
return QgsProperty();

return mWidget->dataDefinedProperty();
}

QgsExtentBufferWidget *QgsExtentBufferDialog::widget() const
{
return mWidget;
}

void QgsExtentBufferDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#extent-buffer" ) );
}

131 changes: 131 additions & 0 deletions src/gui/symbology/qgsextentbufferdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/***************************************************************************
qgsextentbufferdialog.h
---------------------
begin : December 2024
copyright : (C) 2024 by Juho Ervasti
email : juho dot ervasti at gispo dot fi
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSEXTENTBUFFERDIALOG_H
#define QGSEXTENTBUFFERDIALOG_H

#include <QDialog>
#include "qgis_sip.h"

#include "qgssymbol.h"
#include "qgssymbolwidgetcontext.h"
#include "ui_qgsextentbufferdialogbase.h"
#include "qgis_gui.h"

class QgsVectorLayer;

/**
* \class QgsExtentBufferWidget
* \ingroup gui
* \brief A widget which allows the user to modify the rendering order of extent buffers.
* \see QgsExtentBufferDialog
*/
class GUI_EXPORT QgsExtentBufferWidget : public QgsPanelWidget, public QgsExpressionContextGenerator, private Ui::QgsExtentBufferDialogBase
{
Q_OBJECT
public:

/**
* Constructor for QgsExtentBufferWidget
*/
QgsExtentBufferWidget( QgsSymbol *symbol, QgsVectorLayer *layer, QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Returns the extent buffer value currently set in the widget.
*
* \returns extent buffer value
*/
double extentBuffer() const;

/**
* Returns the data defined property currently set in the widget.
*
* \returns property
*/
QgsProperty dataDefinedProperty() const;

/**
* Sets the context in which widget is shown, e.g., the associated map canvas and expression contexts.
* \param context symbol widget context
* \see context()
*/
void setContext( const QgsSymbolWidgetContext &context );

/**
* Returns the context in which the widget is shown, e.g., the associated map canvas and expression contexts.
* \see setContext()
*/
QgsSymbolWidgetContext context() const;

private:
QgsSymbol *mSymbol = nullptr;
QgsVectorLayer *mLayer = nullptr;
QgsSymbolWidgetContext mContext;

QgsExpressionContext createExpressionContext() const override;

/**
* Registers a data defined override button. Handles setting up connections
* for the button and initializing the button to show the correct descriptions
* and help text for the associated property.
*/
void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsSymbol::Property key );
};

/**
* \class QgsExtentBufferDialog
* \ingroup gui
* \brief A dialog which allows the user to modify the extent buffer of a symbol.
*/
class GUI_EXPORT QgsExtentBufferDialog : public QDialog
{
Q_OBJECT
public:

//! Constructor for QgsExtentBufferDialog.
QgsExtentBufferDialog( QgsSymbol *symbol, QgsVectorLayer *layer, QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Returns the extent buffer value currently set in the widget.
*/
double extentBuffer() const;

/**
* Returns the extent buffer value currently set in the widget.
*
* \note returns 0 if widget does not exist
*
* \returns extent buffer value
*/
QgsProperty dataDefinedProperty() const;

/**
* Returns the data defined property currently set in the widget.
*
* \note returns empty property if widget does not exist
*
* \returns property
*/
QgsExtentBufferWidget *widget() const;

private:
QgsExtentBufferWidget *mWidget;

private slots:

void showHelp();

};

#endif // QGSEXTENTBUFFERDIALOG_H
46 changes: 46 additions & 0 deletions src/gui/symbology/qgssymbolslistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "qgssymbolslistwidget.h"
#include "moc_qgssymbolslistwidget.cpp"
#include "qgsextentbufferdialog.h"
#include "qgsstylesavedialog.h"
#include "qgsstyleitemslistwidget.h"
#include "qgsvectorlayer.h"
Expand Down Expand Up @@ -59,6 +60,9 @@ QgsSymbolsListWidget::QgsSymbolsListWidget( QgsSymbol *symbol, QgsStyle *style,
mAnimationSettingsAction = new QAction( tr( "Animation Settings…" ), this );
connect( mAnimationSettingsAction, &QAction::triggered, this, &QgsSymbolsListWidget::showAnimationSettings );

mExtentBufferAction = new QAction( tr( "Extent buffer…" ), this );
connect( mExtentBufferAction, &QAction::triggered, this, &QgsSymbolsListWidget::showExtentBufferSettings );

// select correct page in stacked widget
QgsPropertyOverrideButton *opacityDDBtn = nullptr;
switch ( symbol->type() )
Expand Down Expand Up @@ -148,6 +152,7 @@ QgsSymbolsListWidget::~QgsSymbolsListWidget()
mStyleItemsListWidget->advancedMenu()->removeAction( mClipFeaturesAction );
mStyleItemsListWidget->advancedMenu()->removeAction( mStandardizeRingsAction );
mStyleItemsListWidget->advancedMenu()->removeAction( mAnimationSettingsAction );
mStyleItemsListWidget->advancedMenu()->removeAction( mExtentBufferAction );
mStyleItemsListWidget->advancedMenu()->removeAction( mBufferSettingsAction );
}

Expand Down Expand Up @@ -299,6 +304,45 @@ void QgsSymbolsListWidget::showAnimationSettings()
}
}

void QgsSymbolsListWidget::showExtentBufferSettings()
{
QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
if ( panel && panel->dockMode() )
{
QgsExtentBufferWidget *widget = new QgsExtentBufferWidget( mSymbol, mLayer, panel );
widget->setPanelTitle( tr( "Extent Buffer" ) );
widget->setContext( mContext );

connect( widget, &QgsPanelWidget::widgetChanged, this, [ = ]()
{
mSymbol->setExtentBuffer( widget->extentBuffer() );
mSymbol->setDataDefinedProperty( QgsSymbol::Property::ExtentBuffer, widget->dataDefinedProperty() );

emit changed();
} );

panel->openPanel( widget );

}
else
{
QgsExtentBufferDialog dlg( mSymbol, mLayer, panel );

if ( dlg.widget() )
{
dlg.widget()->setContext( mContext );
}

if ( dlg.exec() == QDialog::Accepted )
{
mSymbol->setExtentBuffer( dlg.extentBuffer() );
mSymbol->setDataDefinedProperty( QgsSymbol::Property::ExtentBuffer, dlg.dataDefinedProperty() );

emit changed();
}
}
}

void QgsSymbolsListWidget::showBufferSettings()
{
QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
Expand Down Expand Up @@ -613,6 +657,7 @@ void QgsSymbolsListWidget::updateSymbolInfo()
mClipFeaturesAction,
mStandardizeRingsAction,
mAnimationSettingsAction,
mExtentBufferAction,
mBufferSettingsAction
} )
{
Expand All @@ -638,6 +683,7 @@ void QgsSymbolsListWidget::updateSymbolInfo()
mStyleItemsListWidget->advancedMenu()->addAction( mBufferSettingsAction );
}
mStyleItemsListWidget->advancedMenu()->addAction( mAnimationSettingsAction );
mStyleItemsListWidget->advancedMenu()->addAction( mExtentBufferAction );

mStyleItemsListWidget->showAdvancedButton( mAdvancedMenu || !mStyleItemsListWidget->advancedMenu()->isEmpty() );

Expand Down
2 changes: 2 additions & 0 deletions src/gui/symbology/qgssymbolslistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class GUI_EXPORT QgsSymbolsListWidget : public QWidget, private Ui::SymbolsListW
void createSymbolAuxiliaryField();
void forceRHRToggled( bool checked );
void showAnimationSettings();
void showExtentBufferSettings();
void showBufferSettings();
void saveSymbol();
void updateSymbolDataDefinedProperty();
Expand All @@ -114,6 +115,7 @@ class GUI_EXPORT QgsSymbolsListWidget : public QWidget, private Ui::SymbolsListW
QAction *mStandardizeRingsAction = nullptr;
QAction *mBufferSettingsAction = nullptr;
QAction *mAnimationSettingsAction = nullptr;
QAction *mExtentBufferAction = nullptr;
QgsVectorLayer *mLayer = nullptr;

QgsColorButton *mSymbolColorButton = nullptr;
Expand Down
Loading

0 comments on commit 1db7781

Please sign in to comment.