19#include <QStandardItemModel>
37 mLegendTreeView =
new QTreeView;
38 mLegendTreeView->setRootIsDecorated(
false );
40 mTextOnSymbolFormatButton =
new QgsFontButton(
nullptr, tr(
"Legend Text Format" ) );
41 mTextOnSymbolFormatButton->setText( tr(
"Text Format" ) );
44 mTextOnSymbolFromExpressionButton =
new QPushButton( tr(
"Set Labels from Expression…" ) );
45 connect( mTextOnSymbolFromExpressionButton, &QPushButton::clicked,
this, &QgsVectorLayerLegendWidget::labelsFromExpression );
49 QHBoxLayout *buttonsLayout =
new QHBoxLayout;
50 buttonsLayout->addWidget( mTextOnSymbolFormatButton );
51 buttonsLayout->addWidget( mTextOnSymbolFromExpressionButton );
52 buttonsLayout->addStretch();
54 QVBoxLayout *groupLayout =
new QVBoxLayout;
55 groupLayout->addWidget( mLegendTreeView );
56 groupLayout->addLayout( buttonsLayout );
58 mTextOnSymbolGroupBox->setTitle( tr(
"Text on Symbols" ) );
59 mTextOnSymbolGroupBox->setCheckable(
true );
60 mTextOnSymbolGroupBox->setLayout( groupLayout );
64 mLabelLegendGroupBox->setCheckable(
true );
65 mLabelLegendGroupBox->setTitle( tr(
"Show Label Legend" ) );
67 mLabelLegendTreeWidget =
new QTreeWidget;
68 connect( mLabelLegendTreeWidget, &QTreeWidget::itemDoubleClicked,
this, &QgsVectorLayerLegendWidget::labelLegendTreeWidgetItemDoubleClicked );
69 QVBoxLayout *labelLegendLayout =
new QVBoxLayout;
70 labelLegendLayout->addWidget( mLabelLegendTreeWidget );
71 mLabelLegendGroupBox->setLayout( labelLegendLayout );
73 mPlaceholderImageLabel =
new QLabel( tr(
"Legend placeholder image" ) );
81 QHBoxLayout *placeholderLayout =
new QHBoxLayout;
82 placeholderLayout->addWidget( mPlaceholderImageLabel );
83 placeholderLayout->addWidget( mImageSourceLineEdit );
85 QVBoxLayout *layout =
new QVBoxLayout;
86 layout->setContentsMargins( 0, 0, 0, 0 );
87 layout->addLayout( placeholderLayout );
88 layout->addWidget( mLabelLegendGroupBox );
89 layout->addWidget( mTextOnSymbolGroupBox );
94void QgsVectorLayerLegendWidget::labelLegendTreeWidgetItemDoubleClicked( QTreeWidgetItem *item,
int column )
96 const Qt::ItemFlags flags = item->flags();
99 item->setFlags( flags | Qt::ItemIsEditable );
103 item->setFlags( flags & ( ~Qt::ItemIsEditable ) );
122 populateLabelLegendTreeWidget();
132void QgsVectorLayerLegendWidget::populateLabelLegendTreeWidget()
134 mLabelLegendTreeWidget->clear();
135 mLabelLegendTreeWidget->setColumnCount( 2 );
136 QTreeWidgetItem *headerItem =
new QTreeWidgetItem( QStringList() << tr(
"Description" ) << tr(
"Legend Text" ) );
137 mLabelLegendTreeWidget->setHeaderItem( headerItem );
143 for (
int i = 0; i < pList.size(); ++i )
148 if ( ruleBasedLabeling && ruleBasedLabeling->
rootRule() )
157 QTreeWidgetItem *labelItem =
new QTreeWidgetItem( QStringList() << description << s.
legendString() );
158 labelItem->setData( 0, Qt::UserRole, pList.at( i ) );
159 mLabelLegendTreeWidget->addTopLevelItem( labelItem );
165void QgsVectorLayerLegendWidget::populateLegendTreeView(
const QHash<QString, QString> &content )
167 QStandardItemModel *model =
new QStandardItemModel(
this );
168 model->setColumnCount( 2 );
169 model->setHorizontalHeaderLabels( QStringList() << tr(
"Symbol" ) << tr(
"Text" ) );
174 if ( !symbolItem.symbol() )
181 QStandardItem *item1 =
new QStandardItem( icon, symbolItem.label() );
182 item1->setEditable(
false );
183 QStandardItem *item2 =
new QStandardItem;
184 if ( symbolItem.ruleKey().isEmpty() )
186 item1->setEnabled(
false );
187 item2->setEnabled(
false );
191 item1->setData( symbolItem.ruleKey() );
192 if ( content.contains( symbolItem.ruleKey() ) )
193 item2->setText( content.value( symbolItem.ruleKey() ) );
195 model->appendRow( QList<QStandardItem *>() << item1 << item2 );
197 mLegendTreeView->setModel( model );
198 mLegendTreeView->resizeColumnToContents( 0 );
208 QHash<QString, QString> content;
209 if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mLegendTreeView->model() ) )
211 for (
int i = 0; i < model->rowCount(); ++i )
213 const QString ruleKey = model->item( i, 0 )->data().toString();
214 const QString label = model->item( i, 1 )->text();
215 if ( !label.isEmpty() )
216 content[ruleKey] = label;
221 const bool showLabelLegend = mLabelLegendGroupBox->isChecked();
223 if ( showLabelLegend )
233void QgsVectorLayerLegendWidget::labelsFromExpression()
235 QHash<QString, QString> content;
240 if ( !dlgExpression.exec() )
246 std::unique_ptr< QgsFeatureRenderer > r( mLayer->
renderer()->
clone() );
253 r->startRender( context, mLayer->
fields() );
257 const QSet<QString> keys = r->legendKeysForFeature( f, context );
258 for (
const QString &key : keys )
260 if ( content.contains( key ) )
264 if ( !label.isEmpty() )
265 content[key] = label;
268 r->stopRender( context );
270 populateLegendTreeView( content );
273void QgsVectorLayerLegendWidget::applyLabelLegend()
276 if ( !layerLabeling )
283 const int nIterations = std::min< int >( ids.size(), mLabelLegendTreeWidget->topLevelItemCount() );
285 for (
int i = 0; i < nIterations; ++i )
287 QTreeWidgetItem *item = mLabelLegendTreeWidget->topLevelItem( i );
290 const QString legendText = item->text( 1 );
void setLastPathSettingsKey(const QString &key)
Sets a specific settings key to use when storing the last used path for the file source.
void setSource(const QString &source)
Sets a new source to show in the widget.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
virtual void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString())=0
Set pal settings for a specific provider (takes ownership).
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
virtual QgsAbstractVectorLayerLabeling * clone() const =0
Returns a new copy of the object.
void setCollapsed(bool collapse)
Collapse or uncollapse this groupbox.
A groupbox that collapses/expands when toggled and can save its collapsed and checked states.
Default legend implementation for vector layers.
void setTextOnSymbolEnabled(bool enabled)
Sets whether the "text on symbol" functionality is enabled.
void setTextOnSymbolContent(const QHash< QString, QString > &content)
Sets per-symbol content of labels for "text on symbol" functionality.
void setShowLabelLegend(bool enabled)
Sets if a legend for the labeling should be shown.
bool textOnSymbolEnabled() const
Returns whether the "text on symbol" functionality is enabled.
QgsTextFormat textOnSymbolTextFormat() const
Returns text format of symbol labels for "text on symbol" functionality.
bool showLabelLegend() const
Returns whether the legend for the labeling is shown.
void setTextOnSymbolTextFormat(const QgsTextFormat &format)
Sets text format of symbol labels for "text on symbol" functionality.
QHash< QString, QString > textOnSymbolContent() const
Returns per-symbol content of labels for "text on symbol" functionality.
A generic dialog for building expression strings.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
A line edit widget with toolbutton for setting a raster image path.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsMapLayerLegend * legend() const
Can be nullptr.
QString legendPlaceholderImage() const
Returns path to the placeholder image or an empty string if a generated legend is shown.
void setLegendPlaceholderImage(const QString &imgPath)
Set placeholder image for legend.
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
Contains settings for how a map layer will be labeled.
QString legendString() const
legendString
void setLegendString(const QString &legendString)
setLegendString
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
A child rule for QgsRuleBasedLabeling.
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
QString description() const
A human readable description for this rule.
Rule based labeling for a vector layer.
QgsRuleBasedLabeling::Rule * rootRule()
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for a color ramp.
Represents a vector layer which manages a vector based data sets.
void setLabeling(QgsAbstractVectorLayerLabeling *labeling)
Sets labeling configuration.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QList< QgsLegendSymbolItem > QgsLegendSymbolList