QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgslabelsettingswidgetbase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelsettingswidgetbase.h
3 ----------------------
4 begin : December 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
18
19#include "qgsauxiliarystorage.h"
21#include "qgsgui.h"
25
26#include <QString>
27
28#include "moc_qgslabelsettingswidgetbase.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QgsPanelWidget( parent )
34 , mLayer( vl )
35{
36}
37
42
47
51
53{
54 if ( auto *lExpressionContext = mContext.expressionContext() )
55 return *lExpressionContext;
56
57 QgsExpressionContext expContext( mContext.globalProjectAtlasMapLayerScopes( mLayer ) );
60 expContext << symbolScope;
61
62 // additional scopes
63 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
64 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
65 {
66 expContext.appendScope( new QgsExpressionContextScope( scope ) );
67 }
68
69 //TODO - show actual value
70 expContext.setOriginalValueVariable( QVariant() );
71
73
74 return expContext;
75}
76
77void QgsLabelSettingsWidgetBase::createAuxiliaryField()
78{
79 auto vectorLayer = qobject_cast< QgsVectorLayer * >( mLayer );
80 if ( !vectorLayer )
81 return;
82
83 // try to create an auxiliary layer if not yet created
84 if ( !vectorLayer->auxiliaryLayer() )
85 {
86 QgsNewAuxiliaryLayerDialog dlg( vectorLayer, this );
87 dlg.exec();
88 }
89
90 // return if still not exists
91 if ( !vectorLayer->auxiliaryLayer() )
92 return;
93
94 QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
96 QgsPropertyDefinition def = QgsPalLayerSettings::propertyDefinitions()[static_cast<int>( key )];
97
98 // create property in auxiliary storage if necessary
99 if ( !vectorLayer->auxiliaryLayer()->exists( def ) )
100 {
101 QgsNewAuxiliaryFieldDialog dlg( def, vectorLayer, true, this );
102 if ( dlg.exec() == QDialog::Accepted )
103 def = dlg.propertyDefinition();
104 }
105
106 // return if still not exist
107 if ( !vectorLayer->auxiliaryLayer()->exists( def ) )
108 return;
109
110 // update property with join field name from auxiliary storage
111 QgsProperty property = button->toProperty();
112 property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) );
113 property.setActive( true );
114 button->updateFieldLists();
115 button->setToProperty( property );
116
117 mDataDefinedProperties.setProperty( key, button->toProperty() );
118
120
121 emit changed();
122}
123
124void QgsLabelSettingsWidgetBase::updateDataDefinedProperty()
125{
126 QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
127 const QgsPalLayerSettings::Property key = static_cast<QgsPalLayerSettings::Property>( button->propertyKey() );
128 mDataDefinedProperties.setProperty( key, button->toProperty() );
129 emit changed();
130}
131
136
138{
140
141 const QList<QgsPropertyOverrideButton *> overrideButtons = findChildren<QgsPropertyOverrideButton *>();
142 for ( QgsPropertyOverrideButton *button : overrideButtons )
143 {
144 const QgsPalLayerSettings::Property key = static_cast<QgsPalLayerSettings::Property>( button->propertyKey() );
145 button->setToProperty( mDataDefinedProperties.property( key ) );
146 }
147}
148
152
154{
155 auto vectorLayer = qobject_cast< QgsVectorLayer * >( mLayer );
156
157 button->init( static_cast<int>( key ), mDataDefinedProperties, QgsPalLayerSettings::propertyDefinitions(), vectorLayer, true );
158 connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLabelSettingsWidgetBase::updateDataDefinedProperty );
159 connect( button, &QgsPropertyOverrideButton::createAuxiliaryField, this, &QgsLabelSettingsWidgetBase::createAuxiliaryField );
160
162}
163
164
165//
166// QgsLabelSettingsWidgetDialog
167//
168
170 : QDialog( parent )
171{
172 setWindowTitle( widget->windowTitle() );
173 QVBoxLayout *vLayout = new QVBoxLayout();
174 vLayout->addWidget( widget );
175 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
176 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
177 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
178 vLayout->addWidget( mButtonBox );
179 setLayout( vLayout );
180
181 setObjectName( u"QgsLabelSettingsWidgetDialog"_s );
183}
184
186{
187 return mButtonBox;
188}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:365
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
Base class for widgets which allow customization of label engine properties, such as label placement ...
QgsLabelSettingsWidgetBase(QWidget *parent=nullptr, QgsMapLayer *vl=nullptr)
Constructor for QgsLabelSettingsWidgetBase.
virtual void updateDataDefinedProperties(QgsPropertyCollection &properties)
Updates a data defined properties collection, correctly setting the values for any properties related...
void changed()
Emitted when any of the settings described by the widget are changed.
QgsSymbolWidgetContext context() const
Returns the context in which the symbol widget is shown, e.g., the associated map canvas and expressi...
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key)
Registers a data defined override button.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
QgsPropertyCollection mDataDefinedProperties
Contains the data defined properties defined by the widget.
virtual void setGeometryType(Qgis::GeometryType type)
Sets the geometry type of the features to customize the widget accordingly.
void auxiliaryFieldCreated()
Emitted when an auxiliary field is created in the widget.
void setDataDefinedProperties(const QgsPropertyCollection &dataDefinedProperties)
Sets the current data defined properties to show in the widget.
QgsPropertyCollection dataDefinedProperties() const
Returns the current data defined properties state as specified in the widget.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsLabelSettingsWidgetDialog(QgsLabelSettingsWidgetBase *widget, QWidget *parent=nullptr)
Constructor for QgsLabelSettingsWidgetDialog.
QDialogButtonBox * buttonBox()
Returns the dialog's button box.
Base class for all map layer types.
Definition qgsmaplayer.h:83
A dialog to create a new auxiliary layer.
Property
Data definable properties.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the labeling property definitions.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
A button for controlling property overrides which may apply to a widget.
QgsProperty toProperty() const
Returns a QgsProperty object encapsulating the current state of the widget.
void updateFieldLists()
Updates list of fields.
void changed()
Emitted when property definition changes.
void init(int propertyKey, const QgsProperty &property, const QgsPropertiesDefinition &definitions, const QgsVectorLayer *layer=nullptr, bool auxiliaryStorageEnabled=false)
Initialize a newly constructed property button (useful if button was included in a UI layout).
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
int propertyKey() const
Returns the property key linked to the button.
void setToProperty(const QgsProperty &property)
Sets the widget to reflect the current state of a QgsProperty.
void createAuxiliaryField()
Emitted when creating a new auxiliary field.
void setField(const QString &field)
Sets the field name the property references.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Single variable definition for use within a QgsExpressionContextScope.