QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
22 #include "qgsauxiliarystorage.h"
23 #include "qgsgui.h"
24 
25 
27  : QgsPanelWidget( parent )
28  , mVectorLayer( vl )
29 {
30 }
31 
33 {
34  mContext = context;
35 }
36 
38 {
39  return mContext;
40 }
41 
43 {
44 
45 }
46 
48 {
49  if ( auto *lExpressionContext = mContext.expressionContext() )
50  return *lExpressionContext;
51 
52  QgsExpressionContext expContext( mContext.globalProjectAtlasMapLayerScopes( mVectorLayer ) );
55  expContext << symbolScope;
56 
57  // additional scopes
58  const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
59  for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
60  {
61  expContext.appendScope( new QgsExpressionContextScope( scope ) );
62  }
63 
64  //TODO - show actual value
65  expContext.setOriginalValueVariable( QVariant() );
66 
68 
69  return expContext;
70 }
71 
72 void QgsLabelSettingsWidgetBase::createAuxiliaryField()
73 {
74  // try to create an auxiliary layer if not yet created
75  if ( !mVectorLayer->auxiliaryLayer() )
76  {
77  QgsNewAuxiliaryLayerDialog dlg( mVectorLayer, this );
78  dlg.exec();
79  }
80 
81  // return if still not exists
82  if ( !mVectorLayer->auxiliaryLayer() )
83  return;
84 
85  QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
86  const QgsPalLayerSettings::Property key = static_cast< QgsPalLayerSettings::Property >( button->propertyKey() );
88 
89  // create property in auxiliary storage if necessary
90  if ( !mVectorLayer->auxiliaryLayer()->exists( def ) )
91  {
92  QgsNewAuxiliaryFieldDialog dlg( def, mVectorLayer, true, this );
93  if ( dlg.exec() == QDialog::Accepted )
94  def = dlg.propertyDefinition();
95  }
96 
97  // return if still not exist
98  if ( !mVectorLayer->auxiliaryLayer()->exists( def ) )
99  return;
100 
101  // update property with join field name from auxiliary storage
102  QgsProperty property = button->toProperty();
103  property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) );
104  property.setActive( true );
105  button->updateFieldLists();
106  button->setToProperty( property );
107 
109 
110  emit auxiliaryFieldCreated();
111 
112  emit changed();
113 }
114 
115 void QgsLabelSettingsWidgetBase::updateDataDefinedProperty()
116 {
117  QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
118  const QgsPalLayerSettings::Property key = static_cast< QgsPalLayerSettings::Property >( button->propertyKey() );
120  emit changed();
121 }
122 
124 {
125  return mDataDefinedProperties;
126 }
127 
129 {
131 
132  const auto overrideButtons = findChildren<QgsPropertyOverrideButton *>();
133  for ( QgsPropertyOverrideButton *button : overrideButtons )
134  {
135  const QgsPalLayerSettings::Property key = static_cast< QgsPalLayerSettings::Property >( button->propertyKey() );
137  }
138 }
139 
141 {
142 
143 }
144 
146 {
147  button->init( key, mDataDefinedProperties, QgsPalLayerSettings::propertyDefinitions(), mVectorLayer, true );
148  connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLabelSettingsWidgetBase::updateDataDefinedProperty );
149  connect( button, &QgsPropertyOverrideButton::createAuxiliaryField, this, &QgsLabelSettingsWidgetBase::createAuxiliaryField );
150 
151  button->registerExpressionContextGenerator( this );
152 }
153 
154 
155 //
156 // QgsLabelSettingsWidgetDialog
157 //
158 
160  : QDialog( parent )
161 {
162  setWindowTitle( widget->windowTitle() );
163  QVBoxLayout *vLayout = new QVBoxLayout();
164  vLayout->addWidget( widget );
165  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
166  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
167  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
168  vLayout->addWidget( mButtonBox );
169  setLayout( vLayout );
170 
171  setObjectName( QStringLiteral( "QgsLabelSettingsWidgetDialog" ) );
173 }
174 
176 {
177  return mButtonBox;
178 }
QgsSymbolWidgetContext::globalProjectAtlasMapLayerScopes
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
Definition: qgssymbolwidgetcontext.cpp:92
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgsexpressioncontextutils.h
qgslabelsettingswidgetbase.h
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsExpressionContext::EXPR_ORIGINAL_VALUE
static const QString EXPR_ORIGINAL_VALUE
Inbuilt variable name for value original value variable.
Definition: qgsexpressioncontext.h:805
QgsExpressionContextScope::addVariable
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Definition: qgsexpressioncontext.cpp:97
QgsLabelSettingsWidgetBase::auxiliaryFieldCreated
void auxiliaryFieldCreated()
Emitted when an auxiliary field is created in the widget.
QgsLabelSettingsWidgetBase::dataDefinedProperties
QgsPropertyCollection dataDefinedProperties() const
Returns the current data defined properties state as specified in the widget.
Definition: qgslabelsettingswidgetbase.cpp:123
QgsPropertyOverrideButton::changed
void changed()
Emitted when property definition changes.
qgsgui.h
QgsAuxiliaryLayer::exists
bool exists(const QgsPropertyDefinition &definition) const
Returns true if the property is stored in the layer already, false otherwise.
Definition: qgsauxiliarystorage.cpp:135
QgsExpressionContext::setOriginalValueVariable
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Definition: qgsexpressioncontext.cpp:600
QgsSymbolWidgetContext
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Definition: qgssymbolwidgetcontext.h:35
QgsLabelSettingsWidgetDialog::buttonBox
QDialogButtonBox * buttonBox()
Returns the dialog's button box.
Definition: qgslabelsettingswidgetbase.cpp:175
qgsnewauxiliaryfielddialog.h
QgsVectorLayer::auxiliaryLayer
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
Definition: qgsvectorlayer.cpp:5520
QgsPropertyOverrideButton::createAuxiliaryField
void createAuxiliaryField()
Emitted when creating a new auxiliary field.
QgsPropertyOverrideButton::propertyKey
int propertyKey() const
Returns the property key linked to the button.
Definition: qgspropertyoverridebutton.h:123
QgsLabelSettingsWidgetBase::setGeometryType
virtual void setGeometryType(QgsWkbTypes::GeometryType type)
Sets the geometry type of the features to customize the widget accordingly.
Definition: qgslabelsettingswidgetbase.cpp:42
QgsExpressionContext::EXPR_SYMBOL_COLOR
static const QString EXPR_SYMBOL_COLOR
Inbuilt variable name for symbol color variable.
Definition: qgsexpressioncontext.h:807
QgsPropertyOverrideButton::setToProperty
void setToProperty(const QgsProperty &property)
Sets the widget to reflect the current state of a QgsProperty.
Definition: qgspropertyoverridebutton.cpp:270
QgsPropertyCollection::property
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
Definition: qgspropertycollection.cpp:214
QgsPropertyOverrideButton
A button for controlling property overrides which may apply to a widget.
Definition: qgspropertyoverridebutton.h:50
QgsLabelSettingsWidgetBase::QgsLabelSettingsWidgetBase
QgsLabelSettingsWidgetBase(QWidget *parent=nullptr, QgsVectorLayer *vl=nullptr)
Constructor for QgsLabelSettingsWidgetBase.
Definition: qgslabelsettingswidgetbase.cpp:26
qgsauxiliarystorage.h
qgsnewauxiliarylayerdialog.h
QgsGui::enableAutoGeometryRestore
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:180
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsProperty::setField
void setField(const QString &field)
Sets the field name the property references.
Definition: qgsproperty.cpp:349
QgsLabelSettingsWidgetBase::setContext
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
Definition: qgslabelsettingswidgetbase.cpp:32
QgsPropertyOverrideButton::updateFieldLists
void updateFieldLists()
Updates list of fields.
Definition: qgspropertyoverridebutton.cpp:152
QgsNewAuxiliaryFieldDialog
A dialog to create a new auxiliary field.
Definition: qgsnewauxiliaryfielddialog.h:33
QgsLabelSettingsWidgetBase::updateDataDefinedProperties
virtual void updateDataDefinedProperties(QgsPropertyCollection &properties)
Updates a data defined properties collection, correctly setting the values for any properties related...
Definition: qgslabelsettingswidgetbase.cpp:140
QgsPropertyOverrideButton::toProperty
QgsProperty toProperty() const
Returns a QgsProperty object encapsulating the current state of the widget.
Definition: qgspropertyoverridebutton.cpp:192
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsSymbolWidgetContext::expressionContext
QgsExpressionContext * expressionContext() const
Returns the expression context used for the widget, if set.
Definition: qgssymbolwidgetcontext.cpp:77
QgsPalLayerSettings::propertyDefinitions
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the labeling property definitions.
Definition: qgspallabeling.cpp:602
QgsPropertyOverrideButton::init
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).
Definition: qgspropertyoverridebutton.cpp:96
QgsLabelSettingsWidgetBase::setDataDefinedProperties
void setDataDefinedProperties(const QgsPropertyCollection &dataDefinedProperties)
Sets the current data defined properties to show in the widget.
Definition: qgslabelsettingswidgetbase.cpp:128
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsLabelSettingsWidgetDialog::QgsLabelSettingsWidgetDialog
QgsLabelSettingsWidgetDialog(QgsLabelSettingsWidgetBase *widget, QWidget *parent=nullptr)
Constructor for QgsLabelSettingsWidgetDialog.
Definition: qgslabelsettingswidgetbase.cpp:159
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:494
QgsLabelSettingsWidgetBase::registerDataDefinedButton
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key)
Registers a data defined override button.
Definition: qgslabelsettingswidgetbase.cpp:145
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:318
QgsLabelSettingsWidgetBase
Base class for widgets which allow customization of label engine properties, such as label placement ...
Definition: qgslabelsettingswidgetbase.h:38
QgsExpressionContextUtils::updateSymbolScope
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
Definition: qgsexpressioncontextutils.cpp:538
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsPropertyOverrideButton::registerExpressionContextGenerator
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
Definition: qgspropertyoverridebutton.cpp:945
QgsAuxiliaryLayer::nameFromProperty
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
Definition: qgsauxiliarystorage.cpp:440
QgsExpressionContext::setHighlightedVariables
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
Definition: qgsexpressioncontext.cpp:328
QgsPropertyCollection::setProperty
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Definition: qgspropertycollection.cpp:187
QgsLabelSettingsWidgetBase::mDataDefinedProperties
QgsPropertyCollection mDataDefinedProperties
Contains the data defined properties defined by the widget.
Definition: qgslabelsettingswidgetbase.h:118
QgsPalLayerSettings::Property
Property
Data definable properties.
Definition: qgspallabeling.h:144
QgsLabelSettingsWidgetBase::changed
void changed()
Emitted when any of the settings described by the widget are changed.
QgsExpressionContextScope::StaticVariable
Single variable definition for use within a QgsExpressionContextScope.
Definition: qgsexpressioncontext.h:120
QgsLabelSettingsWidgetBase::context
QgsSymbolWidgetContext context() const
Returns the context in which the symbol widget is shown, e.g., the associated map canvas and expressi...
Definition: qgslabelsettingswidgetbase.cpp:37
QgsNewAuxiliaryLayerDialog
A dialog to create a new auxiliary layer.
Definition: qgsnewauxiliarylayerdialog.h:34
qgspropertyoverridebutton.h
QgsSymbolWidgetContext::additionalExpressionContextScopes
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
Definition: qgssymbolwidgetcontext.cpp:87
QgsLabelSettingsWidgetBase::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgslabelsettingswidgetbase.cpp:47