18 #include <QInputDialog>
19 #include <QStyledItemDelegate>
36 , mSizeProperty( ddSize )
37 , mMapCanvas( canvas )
46 radDisabled->setChecked(
true );
51 radSeparated->setChecked(
true );
53 radCollapsed->setChecked(
true );
56 cboAlignSymbols->setCurrentIndex( 0 );
58 cboAlignSymbols->setCurrentIndex( 1 );
65 symbol = overrideSymbol;
66 mOverrideSymbol =
true;
73 mSourceSymbol.reset( symbol );
75 btnChangeSymbol->setEnabled( !mOverrideSymbol );
78 btnChangeSymbol->setIcon( icon );
80 editTitle->setText( ddsLegend ? ddsLegend->
title() : QString() );
82 mSizeClassesModel =
new QStandardItemModel( viewSizeClasses );
83 mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr(
"Value" ) << tr(
"Label" ) );
84 mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
87 groupManualSizeClasses->setChecked( !ddsLegend->
classes().isEmpty() );
88 const auto constClasses = ddsLegend->
classes();
91 QStandardItem *item =
new QStandardItem( QString::number( sc.size ) );
92 item->setData( sc.size );
93 QStandardItem *itemLabel =
new QStandardItem( sc.label );
94 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
96 mSizeClassesModel->sort( 0 );
99 connect( btnAddClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::addSizeClass );
100 connect( btnRemoveClass, &QToolButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::removeSizeClass );
102 viewSizeClasses->setItemDelegateForColumn( 0,
new SizeClassDelegate( viewSizeClasses ) );
103 viewSizeClasses->setModel( mSizeClassesModel );
104 connect( mSizeClassesModel, &QStandardItemModel::dataChanged,
this, &QgsDataDefinedSizeLegendWidget::onSizeClassesChanged );
108 mPreviewLayer =
new QgsVectorLayer( QStringLiteral(
"Point?crs=EPSG:4326" ), QStringLiteral(
"Preview" ), QStringLiteral(
"memory" ), options );
110 mPreviewLayerNode = mPreviewTree->
addLayer( mPreviewLayer );
114 viewLayerTree->setModel( mPreviewModel );
116 connect( cboAlignSymbols,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ] { emit
widgetChanged(); } );
121 connect( btnChangeSymbol, &QPushButton::clicked,
this, &QgsDataDefinedSizeLegendWidget::changeSymbol );
129 delete mPreviewModel;
131 delete mPreviewLayer;
136 if ( radDisabled->isChecked() )
142 if ( !mOverrideSymbol )
144 ddsLegend->
setSymbol( mSourceSymbol->clone() );
147 ddsLegend->
setTitle( editTitle->text() );
149 if ( groupManualSizeClasses->isChecked() )
151 QList<QgsDataDefinedSizeLegend::SizeClass> classes;
152 for (
int i = 0; i < mSizeClassesModel->rowCount(); ++i )
154 double value = mSizeClassesModel->item( i, 0 )->data().toDouble();
155 QString label = mSizeClassesModel->item( i, 1 )->text();
163 void QgsDataDefinedSizeLegendWidget::updatePreview()
169 mPreviewLayer->setRenderer( r );
170 mPreviewModel->refreshLayerLegend( mPreviewLayerNode );
171 viewLayerTree->expandAll();
174 void QgsDataDefinedSizeLegendWidget::changeSymbol()
176 std::unique_ptr<QgsMarkerSymbol> newSymbol( mSourceSymbol->clone() );
189 QString crsAuthId = mMapCanvas ? mMapCanvas->mapSettings().destinationCrs().authid() : QString();
191 std::unique_ptr<QgsVectorLayer> layer = qgis::make_unique<QgsVectorLayer>( QStringLiteral(
"Point?crs=%1" ).arg( crsAuthId ),
192 QStringLiteral(
"tmp" ),
193 QStringLiteral(
"memory" ),
197 d.setContext( context );
199 if ( d.exec() != QDialog::Accepted )
202 mSourceSymbol = std::move( newSymbol );
204 btnChangeSymbol->setIcon( icon );
206 emit widgetChanged();
209 void QgsDataDefinedSizeLegendWidget::addSizeClass()
212 double v = QInputDialog::getDouble(
this, tr(
"Add Size Class" ), tr(
"Enter value for a new class" ),
213 0, -2147483647, 2147483647, 6, &ok );
217 QStandardItem *item =
new QStandardItem( QString::number( v ) );
219 QStandardItem *itemLabel =
new QStandardItem( QString::number( v ) );
220 mSizeClassesModel->appendRow( QList<QStandardItem *>() << item << itemLabel );
221 mSizeClassesModel->sort( 0 );
222 emit widgetChanged();
225 void QgsDataDefinedSizeLegendWidget::removeSizeClass()
227 QModelIndex idx = viewSizeClasses->currentIndex();
228 if ( !idx.isValid() )
231 mSizeClassesModel->removeRow( idx.row() );
232 emit widgetChanged();
235 void QgsDataDefinedSizeLegendWidget::onSizeClassesChanged()
237 for (
int row = 0; row < mSizeClassesModel->rowCount(); ++row )
239 QStandardItem *item = mSizeClassesModel->item( row, 0 );
240 item->setData( item->text().toDouble() );
243 mSizeClassesModel->sort( 0 );
244 emit widgetChanged();