Skip to content

Commit

Permalink
[api] New class QgsLabelSettingsWidgetBase
Browse files Browse the repository at this point in the history
Base class for widgets which allow customisation of label engine properties,
such as label placement settings.
  • Loading branch information
nyalldawson committed Dec 4, 2019
1 parent 14c0d2c commit 060cfb4
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 0 deletions.
79 changes: 79 additions & 0 deletions python/gui/auto_generated/qgslabelsettingswidgetbase.sip.in
@@ -0,0 +1,79 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgslabelsettingswidgetbase.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLabelSettingsWidgetBase : QgsPanelWidget, protected QgsExpressionContextGenerator
{
%Docstring
Base class for widgets which allow customisation of label engine properties, such as label placement settings.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgslabelsettingswidgetbase.h"
%End
public:

QgsLabelSettingsWidgetBase( QWidget *parent /TransferThis/ = 0, QgsVectorLayer *vl = 0 );
%Docstring
Constructor for QgsLabelSettingsWidgetBase.

:param parent: parent widget
:param vl: associated vector layer
%End

virtual void setContext( const QgsSymbolWidgetContext &context );
%Docstring
Sets the ``context`` in which the symbol widget is shown, e.g., the associated map canvas and expression contexts.

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

QgsSymbolWidgetContext context() const;
%Docstring
Returns the context in which the symbol widget is shown, e.g., the associated map canvas and expression contexts.

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

virtual void setGeometryType( QgsWkbTypes::GeometryType type );
%Docstring
Sets the geometry ``type`` of the features to customize the widget accordingly.
%End

signals:

void changed();
%Docstring
Emitted when any of the settings described by the widget are changed.
%End

protected:

virtual QgsExpressionContext createExpressionContext() const;


void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key );
%Docstring
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.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgslabelsettingswidgetbase.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -96,6 +96,7 @@
%Include auto_generated/qgshistogramwidget.sip
%Include auto_generated/qgsidentifymenu.sip
%Include auto_generated/qgskeyvaluewidget.sip
%Include auto_generated/qgslabelsettingswidgetbase.sip
%Include auto_generated/qgslegendfilterbutton.sip
%Include auto_generated/qgslimitedrandomcolorrampdialog.sip
%Include auto_generated/qgslistwidget.sip
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -315,6 +315,7 @@ SET(QGIS_GUI_SRCS
qgsidentifymenu.cpp
qgskeyvaluewidget.cpp
qgslabelinggui.cpp
qgslabelsettingswidgetbase.cpp
qgslistwidget.cpp
qgslegendfilterbutton.cpp
qgslimitedrandomcolorrampdialog.cpp
Expand Down Expand Up @@ -516,6 +517,7 @@ SET(QGIS_GUI_HDRS
qgsidentifymenu.h
qgskeyvaluewidget.h
qgslabelinggui.h
qgslabelsettingswidgetbase.h
qgslegendfilterbutton.h
qgslimitedrandomcolorrampdialog.h
qgslistwidget.h
Expand Down
127 changes: 127 additions & 0 deletions src/gui/qgslabelsettingswidgetbase.cpp
@@ -0,0 +1,127 @@
/***************************************************************************
qgslabelsettingswidgetbase.h
----------------------
begin : December 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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 "qgslabelsettingswidgetbase.h"
#include "qgsexpressioncontextutils.h"
#include "qgsnewauxiliaryfielddialog.h"
#include "qgsnewauxiliarylayerdialog.h"
#include "qgspropertyoverridebutton.h"
#include "qgsauxiliarystorage.h"


QgsLabelSettingsWidgetBase::QgsLabelSettingsWidgetBase( QWidget *parent, QgsVectorLayer *vl )
: QgsPanelWidget( parent )
, mVectorLayer( vl )
{
}

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

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

void QgsLabelSettingsWidgetBase::setGeometryType( QgsWkbTypes::GeometryType )
{

}

QgsExpressionContext QgsLabelSettingsWidgetBase::createExpressionContext() const
{
if ( mContext.expressionContext() )
return *mContext.expressionContext();

QgsExpressionContext expContext( mContext.globalProjectAtlasMapLayerScopes( mVectorLayer ) );
QgsExpressionContextScope *symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr, new QgsExpressionContextScope() );
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, QColor(), true ) );
expContext << symbolScope;

// additional scopes
const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
{
expContext.appendScope( new QgsExpressionContextScope( scope ) );
}

//TODO - show actual value
expContext.setOriginalValueVariable( QVariant() );

expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR );

return expContext;
}

void QgsLabelSettingsWidgetBase::createAuxiliaryField()
{
// try to create an auxiliary layer if not yet created
if ( !mVectorLayer->auxiliaryLayer() )
{
QgsNewAuxiliaryLayerDialog dlg( mVectorLayer, this );
dlg.exec();
}

// return if still not exists
if ( !mVectorLayer->auxiliaryLayer() )
return;

QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
QgsPalLayerSettings::Property key = static_cast< QgsPalLayerSettings::Property >( button->propertyKey() );
QgsPropertyDefinition def = QgsPalLayerSettings::propertyDefinitions()[key];

// create property in auxiliary storage if necessary
if ( !mVectorLayer->auxiliaryLayer()->exists( def ) )
{
QgsNewAuxiliaryFieldDialog dlg( def, mVectorLayer, true, this );
if ( dlg.exec() == QDialog::Accepted )
def = dlg.propertyDefinition();
}

// return if still not exist
if ( !mVectorLayer->auxiliaryLayer()->exists( def ) )
return;

// update property with join field name from auxiliary storage
QgsProperty property = button->toProperty();
property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) );
property.setActive( true );
button->updateFieldLists();
button->setToProperty( property );

mDataDefinedProperties.setProperty( key, button->toProperty() );

emit changed();
}

void QgsLabelSettingsWidgetBase::updateDataDefinedProperty()
{
QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
QgsPalLayerSettings::Property key = static_cast< QgsPalLayerSettings::Property >( button->propertyKey() );
mDataDefinedProperties.setProperty( key, button->toProperty() );
emit changed();
}

void QgsLabelSettingsWidgetBase::registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key )
{
button->init( key, mDataDefinedProperties, QgsPalLayerSettings::propertyDefinitions(), mVectorLayer, true );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLabelSettingsWidgetBase::updateDataDefinedProperty );
connect( button, &QgsPropertyOverrideButton::createAuxiliaryField, this, &QgsLabelSettingsWidgetBase::createAuxiliaryField );

button->registerExpressionContextGenerator( this );
}
100 changes: 100 additions & 0 deletions src/gui/qgslabelsettingswidgetbase.h
@@ -0,0 +1,100 @@
/***************************************************************************
qgslabelsettingswidgetbase.h
----------------------
begin : December 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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 QGSLABELSETTINGSWIDGETBASE_H
#define QGSLABELSETTINGSWIDGETBASE_H

#include "qgssymbolwidgetcontext.h"
#include "qgspallabeling.h"
#include "qgspropertycollection.h"
#include "qgspanelwidget.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgis_gui.h"
#include "qgis_sip.h"

class QgsPropertyOverrideButton;

/**
* \ingroup gui
* \class QgsLabelSettingsWidgetBase
* Base class for widgets which allow customisation of label engine properties, such as label placement settings.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsLabelSettingsWidgetBase : public QgsPanelWidget, protected QgsExpressionContextGenerator
{
Q_OBJECT

public:

/**
* Constructor for QgsLabelSettingsWidgetBase.
* \param parent parent widget
* \param vl associated vector layer
*/
QgsLabelSettingsWidgetBase( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsVectorLayer *vl = nullptr );

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

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

/**
* Sets the geometry \a type of the features to customize the widget accordingly.
*/
virtual void setGeometryType( QgsWkbTypes::GeometryType type );

signals:

/**
* Emitted when any of the settings described by the widget are changed.
*/
void changed();

protected:

QgsExpressionContext createExpressionContext() const override;

/**
* Registers a data defined override \a 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, QgsPalLayerSettings::Property key );

private slots:

void createAuxiliaryField();
void updateDataDefinedProperty();

private:

QgsVectorLayer *mVectorLayer = nullptr;

bool mBlockSignals = false;

QgsSymbolWidgetContext mContext;

QgsPropertyCollection mDataDefinedProperties;

};

#endif // QGSLABELSETTINGSWIDGETBASE_H

0 comments on commit 060cfb4

Please sign in to comment.