18 #include <QInputDialog>
19 #include <QStyledItemDelegate>
38 , mSizeProperty( ddSize )
39 , mMapCanvas( canvas )
50 radDisabled->setChecked(
true );
55 radSeparated->setChecked(
true );
57 radCollapsed->setChecked(
true );
60 cboAlignSymbols->setCurrentIndex( 0 );
62 cboAlignSymbols->setCurrentIndex( 1 );
69 groupBoxOptions->setEnabled( radSeparated->isChecked() );
73 symbol = overrideSymbol;
74 mOverrideSymbol =
true;
81 mSourceSymbol.reset( symbol );
83 btnChangeSymbol->setEnabled( !mOverrideSymbol );
86 btnChangeSymbol->setIcon( icon );
88 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
90 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
91 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
92 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
95 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
96 const auto constClasses = ddsLegend->
classes();
99 QStandardItem *item =
new QStandardItem( QLocale().toString( sc.size ) );
100 item->setData( sc.size );
101 QStandardItem *itemLabel =
new QStandardItem( sc.label );
102 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
104 mSizeClassesModel->sort( 0 );
107 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
108 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
110 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
111 viewSizeClasses->setModel( mSizeClassesModel );
112 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
116 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ), options );
118 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
122 viewLayerTree->setModel( mPreviewModel );
124 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
129 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
133 connect( radCollapsed, &QRadioButton::toggled,
this, [ = ](
bool toggled ) {groupBoxOptions->setEnabled( toggled );} );
139 delete mPreviewModel;
141 delete mPreviewLayer;
146 if ( radDisabled->isChecked() )
152 if ( !mOverrideSymbol )
154 ddsLegend->
setSymbol( mSourceSymbol->clone() );
157 ddsLegend->
setTitle( editTitle->text() );
159 if ( groupManualSizeClasses->isChecked() )
161 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
162 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
164 const double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
165 const QString label = mSizeClassesModel->item( i, 1 )->text();
175 void QgsDataDefinedSizeLegendWidget::updatePreview()
181 mPreviewLayer->setRenderer( r );
182 mPreviewModel->refreshLayerLegend( mPreviewLayerNode );
183 viewLayerTree->expandAll();
186 void QgsDataDefinedSizeLegendWidget::changeSymbol()
188 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
201 const QString crsAuthId = mMapCanvas ? mMapCanvas->mapSettings().destinationCrs().authid() : QString();
203 const std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral(
"Point?crs=%1" ).arg( crsAuthId ),
204 QStringLiteral(
"tmp" ),
205 QStringLiteral(
"memory" ),
209 d.setContext( context );
211 if ( d.exec() != QDialog::Accepted )
214 mSourceSymbol = std::move( newSymbol );
216 btnChangeSymbol->setIcon( icon );
218 emit widgetChanged();
221 void QgsDataDefinedSizeLegendWidget::addSizeClass()
224 const double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
225 0, -2147483647, 2147483647, 6, &ok );
229 QStandardItem *item =
new QStandardItem( QLocale().toString( v ) );
231 QStandardItem *itemLabel =
new QStandardItem( QLocale().toString( v ) );
232 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
233 mSizeClassesModel->sort( 0 );
234 emit widgetChanged();
237 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
239 const QModelIndex idx = viewSizeClasses->currentIndex();
240 if ( !idx.isValid() )
243 mSizeClassesModel->removeRow( idx.row() );
244 emit widgetChanged();
247 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
249 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
251 QStandardItem *item = mSizeClassesModel->item( row, 0 );
255 mSizeClassesModel->sort( 0 );
256 emit widgetChanged();