32#include <QInputDialog>
33#include <QStyledItemDelegate>
35#include "moc_qgsdatadefinedsizelegendwidget.cpp"
39 , mSizeProperty( ddSize )
40 , mMapCanvas( canvas )
47 std::unique_ptr< QgsMarkerSymbol > symbol;
51 radDisabled->setChecked(
true );
56 radSeparated->setChecked(
true );
58 radCollapsed->setChecked(
true );
61 cboAlignSymbols->setCurrentIndex( 0 );
63 cboAlignSymbols->setCurrentIndex( 1 );
70 groupBoxOptions->setEnabled( radSeparated->isChecked() );
74 symbol.reset( overrideSymbol );
75 mOverrideSymbol =
true;
82 mSourceSymbol = std::move( symbol );
84 btnChangeSymbol->setEnabled( !mOverrideSymbol );
87 btnChangeSymbol->setIcon( icon );
89 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
91 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
92 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
93 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
96 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
97 const auto constClasses = ddsLegend->
classes();
100 QStandardItem *item =
new QStandardItem( QLocale().toString( sc.size ) );
101 item->setData( sc.size );
102 QStandardItem *itemLabel =
new QStandardItem( sc.label );
103 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
105 mSizeClassesModel->sort( 0 );
108 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
109 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
111 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
112 viewSizeClasses->setModel( mSizeClassesModel );
113 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
117 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ), options );
119 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
123 viewLayerTree->setModel( mPreviewModel );
125 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [
this] { emit
widgetChanged(); } );
130 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
134 connect( radCollapsed, &QRadioButton::toggled,
this, [
this](
bool toggled ) { groupBoxOptions->setEnabled( toggled ); } );
140 delete mPreviewModel;
142 delete mPreviewLayer;
147 if ( radDisabled->isChecked() )
153 if ( !mOverrideSymbol )
155 ddsLegend->
setSymbol( mSourceSymbol->clone() );
158 ddsLegend->
setTitle( editTitle->text() );
160 if ( groupManualSizeClasses->isChecked() )
162 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
163 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
165 const double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
166 const QString label = mSizeClassesModel->item( i, 1 )->text();
176void QgsDataDefinedSizeLegendWidget::updatePreview()
184 viewLayerTree->expandAll();
187void QgsDataDefinedSizeLegendWidget::changeSymbol()
189 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
190 QgsSymbolWidgetContext context;
194 QgsExpressionContext ec;
197 ec = mMapCanvas->createExpressionContext();
208 const QString crsAuthId = mMapCanvas ? mMapCanvas->mapSettings().destinationCrs().authid() : QString();
210 const std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral(
"Point?crs=%1" ).arg( crsAuthId ), QStringLiteral(
"tmp" ), QStringLiteral(
"memory" ), options );
213 d.setContext( context );
215 if ( d.exec() != QDialog::Accepted )
218 mSourceSymbol = std::move( newSymbol );
220 btnChangeSymbol->setIcon( icon );
225void QgsDataDefinedSizeLegendWidget::addSizeClass()
228 const double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ), 0, -2147483647, 2147483647, 6, &ok );
232 QStandardItem *item =
new QStandardItem( QLocale().toString( v ) );
234 QStandardItem *itemLabel =
new QStandardItem( QLocale().toString( v ) );
235 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
236 mSizeClassesModel->sort( 0 );
240void QgsDataDefinedSizeLegendWidget::removeSizeClass()
242 const QModelIndex idx = viewSizeClasses->currentIndex();
243 if ( !idx.isValid() )
246 mSizeClassesModel->removeRow( idx.row() );
250void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
252 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
254 QStandardItem *item = mSizeClassesModel->item( row, 0 );
258 mSizeClassesModel->sort( 0 );
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
void setTitle(const QString &title)
Sets title label for data-defined size legend.
QList< QgsDataDefinedSizeLegend::SizeClass > classes() const
Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setSymbol(QgsMarkerSymbol *symbol SIP_TRANSFER)
Sets marker symbol that will be used to draw markers in legend.
void setVerticalAlignment(VerticalAlignment vAlign)
Sets vertical alignment of symbols - only valid for collapsed legend.
void setLineSymbol(QgsLineSymbol *symbol SIP_TRANSFER)
Sets the line symbol that will be used to draw callout lines in legend.
LegendType legendType() const
Returns how the legend should be rendered.
QgsMarkerSymbol * symbol() const
Returns marker symbol that will be used to draw markers in legend.
@ AlignCenter
Symbols are aligned to the center.
@ AlignBottom
Symbols are aligned to the bottom.
void setClasses(const QList< QgsDataDefinedSizeLegend::SizeClass > &classes)
Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label.
void setLegendType(LegendType type)
Sets how the legend should be rendered.
QString title() const
Returns title label for data-defined size legend.
@ LegendSeparated
Each class (size value) has a separate legend node.
@ LegendCollapsed
All classes are rendered within one legend node.
QgsLineSymbol * lineSymbol() const
Returns the line symbol that will be used to draw callout lines in legend.
VerticalAlignment verticalAlignment() const
Returns vertical alignment of symbols - only valid for collapsed legend.
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer.
A model representing the layer tree, including layers and groups of layers.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
Namespace with helper functions for layer tree operations.
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Map canvas is a class for displaying all GIS data types on a canvas.
double scale() const
Returns the last reported scale of the canvas.
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
A marker symbol type, for rendering Point and MultiPoint geometries.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
void setDataDefinedSize(const QgsProperty &property) const
Set data defined size for whole symbol (including all symbol layers).
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsCoordinateTransformContext transformContext
A store for object properties.
Stores properties relating to a screen.
A feature renderer which renders all features with the same symbol.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns an icon preview for a color ramp.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setExpressionContext(QgsExpressionContext *context)
Sets the optional expression context used for the widget.
Represents a vector layer which manages a vector based dataset.
void setRenderer(QgsFeatureRenderer *r)
Sets the feature renderer which will be invoked to represent this layer in 2D map views.
Definition of one class for the legend.
Setting options for loading vector layers.