17#include "moc_qgsvectorlayerlegendwidget.cpp"
20#include <QStandardItemModel>
38 mLegendTreeView =
new QTreeView;
39 mLegendTreeView->setRootIsDecorated(
false );
41 mTextOnSymbolFormatButton =
new QgsFontButton(
nullptr, tr(
"Legend Text Format" ) );
42 mTextOnSymbolFormatButton->setText( tr(
"Text Format" ) );
45 mTextOnSymbolFromExpressionButton =
new QPushButton( tr(
"Set Labels from Expression…" ) );
46 connect( mTextOnSymbolFromExpressionButton, &QPushButton::clicked,
this, &QgsVectorLayerLegendWidget::labelsFromExpression );
50 QHBoxLayout *buttonsLayout =
new QHBoxLayout;
51 buttonsLayout->addWidget( mTextOnSymbolFormatButton );
52 buttonsLayout->addWidget( mTextOnSymbolFromExpressionButton );
53 buttonsLayout->addStretch();
55 QVBoxLayout *groupLayout =
new QVBoxLayout;
56 groupLayout->addWidget( mLegendTreeView );
57 groupLayout->addLayout( buttonsLayout );
59 mTextOnSymbolGroupBox->setTitle( tr(
"Text on Symbols" ) );
60 mTextOnSymbolGroupBox->setCheckable(
true );
61 mTextOnSymbolGroupBox->setLayout( groupLayout );
65 mLabelLegendGroupBox->setCheckable(
true );
66 mLabelLegendGroupBox->setTitle( tr(
"Show Label Legend" ) );
68 mLabelLegendTreeWidget =
new QTreeWidget;
69 connect( mLabelLegendTreeWidget, &QTreeWidget::itemDoubleClicked,
this, &QgsVectorLayerLegendWidget::labelLegendTreeWidgetItemDoubleClicked );
70 QVBoxLayout *labelLegendLayout =
new QVBoxLayout;
71 labelLegendLayout->addWidget( mLabelLegendTreeWidget );
72 mLabelLegendGroupBox->setLayout( labelLegendLayout );
74 mPlaceholderImageLabel =
new QLabel( tr(
"Legend placeholder image" ) );
82 QHBoxLayout *placeholderLayout =
new QHBoxLayout;
83 placeholderLayout->addWidget( mPlaceholderImageLabel );
84 placeholderLayout->addWidget( mImageSourceLineEdit );
86 QVBoxLayout *layout =
new QVBoxLayout;
87 layout->setContentsMargins( 0, 0, 0, 0 );
88 layout->addLayout( placeholderLayout );
89 layout->addWidget( mLabelLegendGroupBox );
90 layout->addWidget( mTextOnSymbolGroupBox );
95void QgsVectorLayerLegendWidget::labelLegendTreeWidgetItemDoubleClicked( QTreeWidgetItem *item,
int column )
97 const Qt::ItemFlags flags = item->flags();
100 item->setFlags( flags | Qt::ItemIsEditable );
104 item->setFlags( flags & ( ~Qt::ItemIsEditable ) );
123 populateLabelLegendTreeWidget();
133void QgsVectorLayerLegendWidget::populateLabelLegendTreeWidget()
135 mLabelLegendTreeWidget->clear();
136 mLabelLegendTreeWidget->setColumnCount( 2 );
137 QTreeWidgetItem *headerItem =
new QTreeWidgetItem( QStringList() << tr(
"Description" ) << tr(
"Legend Text" ) );
138 mLabelLegendTreeWidget->setHeaderItem( headerItem );
144 for (
int i = 0; i < pList.size(); ++i )
149 if ( ruleBasedLabeling && ruleBasedLabeling->
rootRule() )
158 QTreeWidgetItem *labelItem =
new QTreeWidgetItem( QStringList() << description << s.
legendString() );
159 labelItem->setData( 0, Qt::UserRole, pList.at( i ) );
160 mLabelLegendTreeWidget->addTopLevelItem( labelItem );
166void QgsVectorLayerLegendWidget::populateLegendTreeView(
const QHash<QString, QString> &content )
168 QStandardItemModel *model =
new QStandardItemModel(
this );
169 model->setColumnCount( 2 );
170 model->setHorizontalHeaderLabels( QStringList() << tr(
"Symbol" ) << tr(
"Text" ) );
175 if ( !symbolItem.symbol() )
182 QStandardItem *item1 =
new QStandardItem( icon, symbolItem.label() );
183 item1->setEditable(
false );
184 QStandardItem *item2 =
new QStandardItem;
185 if ( symbolItem.ruleKey().isEmpty() )
187 item1->setEnabled(
false );
188 item2->setEnabled(
false );
192 item1->setData( symbolItem.ruleKey() );
193 if ( content.contains( symbolItem.ruleKey() ) )
194 item2->setText( content.value( symbolItem.ruleKey() ) );
196 model->appendRow( QList<QStandardItem *>() << item1 << item2 );
198 mLegendTreeView->setModel( model );
199 mLegendTreeView->resizeColumnToContents( 0 );
209 QHash<QString, QString> content;
210 if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mLegendTreeView->model() ) )
212 for (
int i = 0; i < model->rowCount(); ++i )
214 const QString ruleKey = model->item( i, 0 )->data().toString();
215 const QString label = model->item( i, 1 )->text();
216 if ( !label.isEmpty() )
217 content[ruleKey] = label;
222 const bool showLabelLegend = mLabelLegendGroupBox->isChecked();
224 if ( showLabelLegend )
234void QgsVectorLayerLegendWidget::labelsFromExpression()
236 QHash<QString, QString> content;
241 if ( !dlgExpression.exec() )
247 std::unique_ptr< QgsFeatureRenderer > r( mLayer->
renderer()->
clone() );
254 r->startRender( context, mLayer->
fields() );
258 const QSet<QString> keys = r->legendKeysForFeature( f, context );
259 for (
const QString &key : keys )
261 if ( content.contains( key ) )
265 if ( !label.isEmpty() )
266 content[key] = label;
269 r->stopRender( context );
271 populateLegendTreeView( content );
274void QgsVectorLayerLegendWidget::applyLabelLegend()
277 if ( !layerLabeling )
284 const int nIterations = std::min< int >( ids.size(), mLabelLegendTreeWidget->topLevelItemCount() );
286 for (
int i = 0; i < nIterations; ++i )
288 QTreeWidgetItem *item = mLabelLegendTreeWidget->topLevelItem( i );
291 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