QGIS API Documentation 3.28.12-Firenze (exported)
Loading...
Searching...
No Matches
qgsgraduatedsymbolrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraduatedsymbolrendererwidget.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <QKeyEvent>
17#include <QMenu>
18#include <QMessageBox>
19#include <QStandardItemModel>
20#include <QStandardItem>
21#include <QPen>
22#include <QPainter>
23#include <QClipboard>
24#include <QCompleter>
25
27#include "qgspanelwidget.h"
28
31#include "qgssymbol.h"
32#include "qgssymbollayerutils.h"
33#include "qgscolorrampimpl.h"
34#include "qgscolorrampbutton.h"
35#include "qgsstyle.h"
37#include "qgsvectorlayer.h"
40#include "qgslogger.h"
41#include "qgsludialog.h"
42#include "qgsproject.h"
44#include "qgsmapcanvas.h"
46#include "qgsapplication.h"
50#include "qgsgui.h"
51#include "qgsprocessinggui.h"
56#include "qgsdoublevalidator.h"
57#include "qgsmarkersymbol.h"
58
59
60// ------------------------------ Model ------------------------------------
61
63
64QgsGraduatedSymbolRendererModel::QgsGraduatedSymbolRendererModel( QObject *parent ) : QAbstractItemModel( parent )
65 , mMimeFormat( QStringLiteral( "application/x-qgsgraduatedsymbolrendererv2model" ) )
66{
67}
68
69void QgsGraduatedSymbolRendererModel::setRenderer( QgsGraduatedSymbolRenderer *renderer )
70{
71 if ( mRenderer )
72 {
73 if ( !mRenderer->ranges().isEmpty() )
74 {
75 beginRemoveRows( QModelIndex(), 0, mRenderer->ranges().size() - 1 );
76 mRenderer = nullptr;
77 endRemoveRows();
78 }
79 else
80 {
81 mRenderer = nullptr;
82 }
83 }
84 if ( renderer )
85 {
86 if ( !renderer->ranges().isEmpty() )
87 {
88 beginInsertRows( QModelIndex(), 0, renderer->ranges().size() - 1 );
89 mRenderer = renderer;
90 endInsertRows();
91 }
92 else
93 {
94 mRenderer = renderer;
95 }
96 }
97}
98
99void QgsGraduatedSymbolRendererModel::addClass( QgsSymbol *symbol )
100{
101 if ( !mRenderer ) return;
102 int idx = mRenderer->ranges().size();
103 beginInsertRows( QModelIndex(), idx, idx );
104 mRenderer->addClass( symbol );
105 endInsertRows();
106}
107
108void QgsGraduatedSymbolRendererModel::addClass( const QgsRendererRange &range )
109{
110 if ( !mRenderer )
111 {
112 return;
113 }
114 int idx = mRenderer->ranges().size();
115 beginInsertRows( QModelIndex(), idx, idx );
116 mRenderer->addClass( range );
117 endInsertRows();
118}
119
120QgsRendererRange QgsGraduatedSymbolRendererModel::rendererRange( const QModelIndex &index )
121{
122 if ( !index.isValid() || !mRenderer || mRenderer->ranges().size() <= index.row() )
123 {
124 return QgsRendererRange();
125 }
126
127 return mRenderer->ranges().value( index.row() );
128}
129
130Qt::ItemFlags QgsGraduatedSymbolRendererModel::flags( const QModelIndex &index ) const
131{
132 if ( !index.isValid() )
133 {
134 return Qt::ItemIsDropEnabled;
135 }
136
137 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
138
139 if ( index.column() == 2 )
140 {
141 flags |= Qt::ItemIsEditable;
142 }
143
144 return flags;
145}
146
147Qt::DropActions QgsGraduatedSymbolRendererModel::supportedDropActions() const
148{
149 return Qt::MoveAction;
150}
151
152QVariant QgsGraduatedSymbolRendererModel::data( const QModelIndex &index, int role ) const
153{
154 if ( !index.isValid() || !mRenderer ) return QVariant();
155
156 const QgsRendererRange range = mRenderer->ranges().value( index.row() );
157
158 if ( role == Qt::CheckStateRole && index.column() == 0 )
159 {
160 return range.renderState() ? Qt::Checked : Qt::Unchecked;
161 }
162 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
163 {
164 switch ( index.column() )
165 {
166 case 1:
167 {
168 int decimalPlaces = mRenderer->classificationMethod()->labelPrecision() + 2;
169 if ( decimalPlaces < 0 ) decimalPlaces = 0;
170 return QString( QLocale().toString( range.lowerValue(), 'f', decimalPlaces ) + " - " + QLocale().toString( range.upperValue(), 'f', decimalPlaces ) );
171 }
172 case 2:
173 return range.label();
174 default:
175 return QVariant();
176 }
177 }
178 else if ( role == Qt::DecorationRole && index.column() == 0 && range.symbol() )
179 {
180 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
181 return QgsSymbolLayerUtils::symbolPreviewIcon( range.symbol(), QSize( iconSize, iconSize ) );
182 }
183 else if ( role == Qt::TextAlignmentRole )
184 {
185 return ( index.column() == 0 ) ? static_cast<Qt::Alignment::Int>( Qt::AlignHCenter ) : static_cast<Qt::Alignment::Int>( Qt::AlignLeft );
186 }
187 else if ( role == Qt::EditRole )
188 {
189 switch ( index.column() )
190 {
191 // case 1: return rangeStr;
192 case 2:
193 return range.label();
194 default:
195 return QVariant();
196 }
197 }
198
199 return QVariant();
200}
201
202bool QgsGraduatedSymbolRendererModel::setData( const QModelIndex &index, const QVariant &value, int role )
203{
204 if ( !index.isValid() )
205 return false;
206
207 if ( index.column() == 0 && role == Qt::CheckStateRole )
208 {
209 mRenderer->updateRangeRenderState( index.row(), value == Qt::Checked );
210 emit dataChanged( index, index );
211 return true;
212 }
213
214 if ( role != Qt::EditRole )
215 return false;
216
217 switch ( index.column() )
218 {
219 case 1: // range
220 return false; // range is edited in popup dialog
221 case 2: // label
222 mRenderer->updateRangeLabel( index.row(), value.toString() );
223 break;
224 default:
225 return false;
226 }
227
228 emit dataChanged( index, index );
229 return true;
230}
231
232QVariant QgsGraduatedSymbolRendererModel::headerData( int section, Qt::Orientation orientation, int role ) const
233{
234 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
235 {
236 QStringList lst;
237 lst << tr( "Symbol" ) << tr( "Values" ) << tr( "Legend" );
238 return lst.value( section );
239 }
240 return QVariant();
241}
242
243int QgsGraduatedSymbolRendererModel::rowCount( const QModelIndex &parent ) const
244{
245 if ( parent.isValid() || !mRenderer )
246 {
247 return 0;
248 }
249 return mRenderer->ranges().size();
250}
251
252int QgsGraduatedSymbolRendererModel::columnCount( const QModelIndex &index ) const
253{
254 Q_UNUSED( index )
255 return 3;
256}
257
258QModelIndex QgsGraduatedSymbolRendererModel::index( int row, int column, const QModelIndex &parent ) const
259{
260 if ( hasIndex( row, column, parent ) )
261 {
262 return createIndex( row, column );
263 }
264 return QModelIndex();
265}
266
267QModelIndex QgsGraduatedSymbolRendererModel::parent( const QModelIndex &index ) const
268{
269 Q_UNUSED( index )
270 return QModelIndex();
271}
272
273QStringList QgsGraduatedSymbolRendererModel::mimeTypes() const
274{
275 QStringList types;
276 types << mMimeFormat;
277 return types;
278}
279
280QMimeData *QgsGraduatedSymbolRendererModel::mimeData( const QModelIndexList &indexes ) const
281{
282 QMimeData *mimeData = new QMimeData();
283 QByteArray encodedData;
284
285 QDataStream stream( &encodedData, QIODevice::WriteOnly );
286
287 // Create list of rows
288 const auto constIndexes = indexes;
289 for ( const QModelIndex &index : constIndexes )
290 {
291 if ( !index.isValid() || index.column() != 0 )
292 continue;
293
294 stream << index.row();
295 }
296 mimeData->setData( mMimeFormat, encodedData );
297 return mimeData;
298}
299
300bool QgsGraduatedSymbolRendererModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent )
301{
302 Q_UNUSED( row )
303 Q_UNUSED( column )
304 if ( action != Qt::MoveAction ) return true;
305
306 if ( !data->hasFormat( mMimeFormat ) ) return false;
307
308 QByteArray encodedData = data->data( mMimeFormat );
309 QDataStream stream( &encodedData, QIODevice::ReadOnly );
310
311 QVector<int> rows;
312 while ( !stream.atEnd() )
313 {
314 int r;
315 stream >> r;
316 rows.append( r );
317 }
318
319 int to = parent.row();
320 // to is -1 if dragged outside items, i.e. below any item,
321 // then move to the last position
322 if ( to == -1 ) to = mRenderer->ranges().size(); // out of rang ok, will be decreased
323 for ( int i = rows.size() - 1; i >= 0; i-- )
324 {
325 QgsDebugMsg( QStringLiteral( "move %1 to %2" ).arg( rows[i] ).arg( to ) );
326 int t = to;
327 // moveCategory first removes and then inserts
328 if ( rows[i] < t ) t--;
329 mRenderer->moveClass( rows[i], t );
330 // current moved under another, shift its index up
331 for ( int j = 0; j < i; j++ )
332 {
333 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
334 }
335 // removed under 'to' so the target shifted down
336 if ( rows[i] < to ) to--;
337 }
338 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->ranges().size(), 0 ) );
339 emit rowsMoved();
340 return false;
341}
342
343void QgsGraduatedSymbolRendererModel::deleteRows( QList<int> rows )
344{
345 for ( int i = rows.size() - 1; i >= 0; i-- )
346 {
347 beginRemoveRows( QModelIndex(), rows[i], rows[i] );
348 mRenderer->deleteClass( rows[i] );
349 endRemoveRows();
350 }
351}
352
353void QgsGraduatedSymbolRendererModel::removeAllRows()
354{
355 beginRemoveRows( QModelIndex(), 0, mRenderer->ranges().size() - 1 );
356 mRenderer->deleteAllClasses();
357 endRemoveRows();
358}
359
360void QgsGraduatedSymbolRendererModel::sort( int column, Qt::SortOrder order )
361{
362 if ( column == 0 )
363 {
364 return;
365 }
366 if ( column == 1 )
367 {
368 mRenderer->sortByValue( order );
369 }
370 else if ( column == 2 )
371 {
372 mRenderer->sortByLabel( order );
373 }
374 emit rowsMoved();
375 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->ranges().size(), 0 ) );
376}
377
378void QgsGraduatedSymbolRendererModel::updateSymbology()
379{
380 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->ranges().size(), 0 ) );
381}
382
383void QgsGraduatedSymbolRendererModel::updateLabels()
384{
385 emit dataChanged( createIndex( 0, 2 ), createIndex( mRenderer->ranges().size(), 2 ) );
386}
387
388// ------------------------------ View style --------------------------------
389QgsGraduatedSymbolRendererViewStyle::QgsGraduatedSymbolRendererViewStyle( QWidget *parent )
390 : QgsProxyStyle( parent )
391{}
392
393void QgsGraduatedSymbolRendererViewStyle::drawPrimitive( PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget ) const
394{
395 if ( element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull() )
396 {
397 QStyleOption opt( *option );
398 opt.rect.setLeft( 0 );
399 // draw always as line above, because we move item to that index
400 opt.rect.setHeight( 0 );
401 if ( widget ) opt.rect.setRight( widget->width() );
402 QProxyStyle::drawPrimitive( element, &opt, painter, widget );
403 return;
404 }
405 QProxyStyle::drawPrimitive( element, option, painter, widget );
406}
407
409
410// ------------------------------ Widget ------------------------------------
411
416
417QgsExpressionContext QgsGraduatedSymbolRendererWidget::createExpressionContext() const
418{
419 QgsExpressionContext expContext;
423
424 if ( auto *lMapCanvas = mContext.mapCanvas() )
425 {
426 expContext << QgsExpressionContextUtils::mapSettingsScope( lMapCanvas->mapSettings() )
427 << new QgsExpressionContextScope( lMapCanvas->expressionContextScope() );
428 if ( const QgsExpressionContextScopeGenerator *generator = dynamic_cast< const QgsExpressionContextScopeGenerator * >( lMapCanvas->temporalController() ) )
429 {
430 expContext << generator->createExpressionContextScope();
431 }
432 }
433 else
434 {
436 }
437
438 if ( auto *lVectorLayer = vectorLayer() )
439 expContext << QgsExpressionContextUtils::layerScope( lVectorLayer );
440
441 // additional scopes
442 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
443 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
444 {
445 expContext.appendScope( new QgsExpressionContextScope( scope ) );
446 }
447
448 return expContext;
449}
450
452 : QgsRendererWidget( layer, style )
453{
454 // try to recognize the previous renderer
455 // (null renderer means "no previous renderer")
456 if ( renderer )
457 {
459 }
460 if ( !mRenderer )
461 {
462 mRenderer = std::make_unique< QgsGraduatedSymbolRenderer >( QString(), QgsRangeList() );
463 if ( renderer )
464 renderer->copyRendererData( mRenderer.get() );
465 }
466
467 // setup user interface
468 setupUi( this );
469
470 mSymmetryPointValidator = new QgsDoubleValidator( this );
471 cboSymmetryPoint->setEditable( true );
472 cboSymmetryPoint->setValidator( mSymmetryPointValidator );
473
474 const QMap<QString, QString> methods = QgsApplication::classificationMethodRegistry()->methodNames();
475 for ( QMap<QString, QString>::const_iterator it = methods.constBegin(); it != methods.constEnd(); ++it )
476 {
477 QIcon icon = QgsApplication::classificationMethodRegistry()->icon( it.value() );
478 cboGraduatedMode->addItem( icon, it.key(), it.value() );
479 }
480
481 connect( methodComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::methodComboBox_currentIndexChanged );
482 this->layout()->setContentsMargins( 0, 0, 0, 0 );
483
484 mModel = new QgsGraduatedSymbolRendererModel( this );
485
486 mExpressionWidget->setFilters( QgsFieldProxyModel::Numeric | QgsFieldProxyModel::Date );
487 mExpressionWidget->setLayer( mLayer );
488
489 btnChangeGraduatedSymbol->setLayer( mLayer );
490 btnChangeGraduatedSymbol->registerExpressionContextGenerator( this );
491
494
495 spinPrecision->setMinimum( QgsClassificationMethod::MIN_PRECISION );
496 spinPrecision->setMaximum( QgsClassificationMethod::MAX_PRECISION );
497 spinPrecision->setClearValue( 4 );
498
499 spinGraduatedClasses->setShowClearButton( false );
500
501 btnColorRamp->setShowRandomColorRamp( true );
502
503 // set project default color ramp
504 std::unique_ptr< QgsColorRamp > colorRamp( QgsProject::instance()->styleSettings()->defaultColorRamp() );
505 if ( colorRamp )
506 {
507 btnColorRamp->setColorRamp( colorRamp.get() );
508 }
509 else
510 {
511 QgsColorRamp *ramp = new QgsGradientColorRamp( QColor( 255, 255, 255 ), QColor( 255, 0, 0 ) );
512 btnColorRamp->setColorRamp( ramp );
513 delete ramp;
514 }
515
516
517 viewGraduated->setStyle( new QgsGraduatedSymbolRendererViewStyle( viewGraduated ) );
518
519 mGraduatedSymbol.reset( QgsSymbol::defaultSymbol( mLayer->geometryType() ) );
520 if ( mGraduatedSymbol )
521 {
522 btnChangeGraduatedSymbol->setSymbolType( mGraduatedSymbol->type() );
523 btnChangeGraduatedSymbol->setSymbol( mGraduatedSymbol->clone() );
524
525 methodComboBox->blockSignals( true );
526 methodComboBox->addItem( tr( "Color" ), ColorMode );
527 switch ( mGraduatedSymbol->type() )
528 {
530 {
531 methodComboBox->addItem( tr( "Size" ), SizeMode );
532 minSizeSpinBox->setValue( 1 );
533 maxSizeSpinBox->setValue( 8 );
534 break;
535 }
537 {
538 methodComboBox->addItem( tr( "Size" ), SizeMode );
539 minSizeSpinBox->setValue( .1 );
540 maxSizeSpinBox->setValue( 2 );
541 break;
542 }
544 {
545 //set button and label invisible to avoid display of a single item combobox
546 methodComboBox->hide();
547 labelMethod->hide();
548 break;
549 }
551 break;
552 }
553 methodComboBox->blockSignals( false );
554 }
555
556 connect( mExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsGraduatedSymbolRendererWidget::graduatedColumnChanged );
557 connect( viewGraduated, &QAbstractItemView::doubleClicked, this, &QgsGraduatedSymbolRendererWidget::rangesDoubleClicked );
558 connect( viewGraduated, &QAbstractItemView::clicked, this, &QgsGraduatedSymbolRendererWidget::rangesClicked );
559 connect( viewGraduated, &QTreeView::customContextMenuRequested, this, &QgsGraduatedSymbolRendererWidget::contextMenuViewCategories );
560
561 connect( btnGraduatedClassify, &QAbstractButton::clicked, this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
562 connect( btnChangeGraduatedSymbol, &QgsSymbolButton::changed, this, &QgsGraduatedSymbolRendererWidget::changeGraduatedSymbol );
563 connect( btnGraduatedDelete, &QAbstractButton::clicked, this, &QgsGraduatedSymbolRendererWidget::deleteClasses );
564 connect( btnDeleteAllClasses, &QAbstractButton::clicked, this, &QgsGraduatedSymbolRendererWidget::deleteAllClasses );
565 connect( btnGraduatedAdd, &QAbstractButton::clicked, this, &QgsGraduatedSymbolRendererWidget::addClass );
566 connect( cbxLinkBoundaries, &QAbstractButton::toggled, this, &QgsGraduatedSymbolRendererWidget::toggleBoundariesLink );
567 connect( mSizeUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsGraduatedSymbolRendererWidget::mSizeUnitWidget_changed );
568
569 connect( cboGraduatedMode, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::updateMethodParameters );
570
571 // need to update widget according to current graduated mode
572 updateMethodParameters();
573
575
576 // initialize from previously set renderer
578
579 // default to collapsed symmetric group for ui simplicity
580 mGroupBoxSymmetric->setCollapsed( true ); //
581
582 // menus for data-defined rotation/size
583 QMenu *advMenu = new QMenu( this );
584
585 mActionLevels = advMenu->addAction( tr( "Symbol Levels…" ), this, &QgsGraduatedSymbolRendererWidget::showSymbolLevels );
586 if ( mGraduatedSymbol && mGraduatedSymbol->type() == Qgis::SymbolType::Marker )
587 {
588 QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
589 // only from Qt 5.6 there is convenience addAction() with new style connection
590 connect( actionDdsLegend, &QAction::triggered, this, &QgsGraduatedSymbolRendererWidget::dataDefinedSizeLegend );
591 }
592
593 btnAdvanced->setMenu( advMenu );
594
595 mHistogramWidget->setLayer( mLayer );
596 mHistogramWidget->setRenderer( mRenderer.get() );
598 connect( mExpressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) >( &QgsFieldExpressionWidget::fieldChanged ), mHistogramWidget, &QgsHistogramWidget::setSourceFieldExp );
599
600 mExpressionWidget->registerExpressionContextGenerator( this );
601}
602
603void QgsGraduatedSymbolRendererWidget::mSizeUnitWidget_changed()
604{
605 if ( !mGraduatedSymbol )
606 return;
607 mGraduatedSymbol->setOutputUnit( mSizeUnitWidget->unit() );
608 mGraduatedSymbol->setMapUnitScale( mSizeUnitWidget->getMapUnitScale() );
609 mRenderer->updateSymbols( mGraduatedSymbol.get() );
611}
612
614{
615 delete mModel;
616 mParameterWidgetWrappers.clear();
617}
618
620{
621 return mRenderer.get();
622}
623
625{
627 btnChangeGraduatedSymbol->setMapCanvas( context.mapCanvas() );
628 btnChangeGraduatedSymbol->setMessageBar( context.messageBar() );
629}
630
632{
633 delete mActionLevels;
634 mActionLevels = nullptr;
635}
636
637// Connect/disconnect event handlers which trigger updating renderer
639{
640 connect( spinGraduatedClasses, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
641 connect( cboGraduatedMode, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
643 connect( spinPrecision, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
644 connect( cbxTrimTrailingZeroes, &QAbstractButton::toggled, this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
645 connect( txtLegendFormat, &QLineEdit::textChanged, this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
646 connect( minSizeSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::reapplySizes );
647 connect( maxSizeSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::reapplySizes );
648
649 connect( mModel, &QgsGraduatedSymbolRendererModel::rowsMoved, this, &QgsGraduatedSymbolRendererWidget::rowsMoved );
650 connect( mModel, &QAbstractItemModel::dataChanged, this, &QgsGraduatedSymbolRendererWidget::modelDataChanged );
651
652 connect( mGroupBoxSymmetric, &QGroupBox::toggled, this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
653 connect( cbxAstride, &QAbstractButton::toggled, this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
654 connect( cboSymmetryPoint, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
655 connect( cboSymmetryPoint->lineEdit(), &QLineEdit::editingFinished, this, &QgsGraduatedSymbolRendererWidget::symmetryPointEditingFinished );
656
657 for ( const auto &ppww : std::as_const( mParameterWidgetWrappers ) )
658 {
660 }
661}
662
663// Connect/disconnect event handlers which trigger updating renderer
665{
666 disconnect( spinGraduatedClasses, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
667 disconnect( cboGraduatedMode, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
669 disconnect( spinPrecision, qOverload<int>( &QSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
670 disconnect( cbxTrimTrailingZeroes, &QAbstractButton::toggled, this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
671 disconnect( txtLegendFormat, &QLineEdit::textChanged, this, &QgsGraduatedSymbolRendererWidget::labelFormatChanged );
672 disconnect( minSizeSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::reapplySizes );
673 disconnect( maxSizeSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsGraduatedSymbolRendererWidget::reapplySizes );
674
675 disconnect( mModel, &QgsGraduatedSymbolRendererModel::rowsMoved, this, &QgsGraduatedSymbolRendererWidget::rowsMoved );
676 disconnect( mModel, &QAbstractItemModel::dataChanged, this, &QgsGraduatedSymbolRendererWidget::modelDataChanged );
677
678 disconnect( mGroupBoxSymmetric, &QGroupBox::toggled, this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
679 disconnect( cbxAstride, &QAbstractButton::toggled, this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
680 disconnect( cboSymmetryPoint, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsGraduatedSymbolRendererWidget::classifyGraduated );
681 disconnect( cboSymmetryPoint->lineEdit(), &QLineEdit::editingFinished, this, &QgsGraduatedSymbolRendererWidget::symmetryPointEditingFinished );
682
683 for ( const auto &ppww : std::as_const( mParameterWidgetWrappers ) )
684 {
686 }
687}
688
690{
692 mBlockUpdates++;
693
694 const QgsClassificationMethod *method = mRenderer->classificationMethod();
695
696 const QgsRangeList ranges = mRenderer->ranges();
697
698 // use the breaks for symmetry point
699 int precision = spinPrecision->value() + 2;
700 while ( cboSymmetryPoint->count() )
701 cboSymmetryPoint->removeItem( 0 );
702 for ( int i = 0; i < ranges.count() - 1; i++ )
703 cboSymmetryPoint->addItem( QLocale().toString( ranges.at( i ).upperValue(), 'f', precision ), ranges.at( i ).upperValue() );
704
705 if ( method )
706 {
707 int idx = cboGraduatedMode->findData( method->id() );
708 if ( idx >= 0 )
709 cboGraduatedMode->setCurrentIndex( idx );
710
711 mGroupBoxSymmetric->setVisible( method->symmetricModeAvailable() );
712 mGroupBoxSymmetric->setChecked( method->symmetricModeEnabled() );
713 cbxAstride->setChecked( method->symmetryAstride() );
714 if ( method->symmetricModeEnabled() )
715 cboSymmetryPoint->setItemText( cboSymmetryPoint->currentIndex(), QLocale().toString( method->symmetryPoint(), 'f', method->labelPrecision() + 2 ) );
716
717 txtLegendFormat->setText( method->labelFormat() );
718 spinPrecision->setValue( method->labelPrecision() );
719 cbxTrimTrailingZeroes->setChecked( method->labelTrimTrailingZeroes() );
720
722 for ( const auto &ppww : std::as_const( mParameterWidgetWrappers ) )
723 {
724 const QgsProcessingParameterDefinition *def = ppww->parameterDefinition();
725 QVariant value = method->parameterValues().value( def->name(), def->defaultValueForGui() );
726 ppww->setParameterValue( value, context );
727 }
728 }
729
730 // Only update class count if different - otherwise typing value gets very messy
731 int nclasses = ranges.count();
732 if ( nclasses && ( updateCount || ( method && ( method->flags() & QgsClassificationMethod::MethodProperty::IgnoresClassCount ) ) ) )
733 {
734 spinGraduatedClasses->setValue( ranges.count() );
735 }
736 if ( method )
737 {
738 spinGraduatedClasses->setEnabled( !( method->flags() & QgsClassificationMethod::MethodProperty::IgnoresClassCount ) );
739 }
740 else
741 {
742 spinGraduatedClasses->setEnabled( true );
743 }
744
745 // set column
746 QString attrName = mRenderer->classAttribute();
747 mExpressionWidget->setField( attrName );
748 mHistogramWidget->setSourceFieldExp( attrName );
749
750 // set source symbol
751 if ( mRenderer->sourceSymbol() )
752 {
753 mGraduatedSymbol.reset( mRenderer->sourceSymbol()->clone() );
754 whileBlocking( btnChangeGraduatedSymbol )->setSymbol( mGraduatedSymbol->clone() );
755 }
756
757 mModel->setRenderer( mRenderer.get() );
758 viewGraduated->setModel( mModel );
759
760 connect( viewGraduated->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsGraduatedSymbolRendererWidget::selectionChanged );
761
762 if ( mGraduatedSymbol )
763 {
764 mSizeUnitWidget->blockSignals( true );
765 mSizeUnitWidget->setUnit( mGraduatedSymbol->outputUnit() );
766 mSizeUnitWidget->setMapUnitScale( mGraduatedSymbol->mapUnitScale() );
767 mSizeUnitWidget->blockSignals( false );
768 }
769
770 // set source color ramp
771 methodComboBox->blockSignals( true );
772 switch ( mRenderer->graduatedMethod() )
773 {
775 {
776 methodComboBox->setCurrentIndex( methodComboBox->findData( ColorMode ) );
777 if ( mRenderer->sourceColorRamp() )
778 {
779 btnColorRamp->setColorRamp( mRenderer->sourceColorRamp() );
780 }
781 break;
782 }
784 {
785 methodComboBox->setCurrentIndex( methodComboBox->findData( SizeMode ) );
786 if ( !mRenderer->ranges().isEmpty() ) // avoid overriding default size with zeros
787 {
788 minSizeSpinBox->setValue( mRenderer->minSymbolSize() );
789 maxSizeSpinBox->setValue( mRenderer->maxSymbolSize() );
790 }
791 break;
792 }
793 }
794 toggleMethodWidgets( static_cast< MethodMode>( methodComboBox->currentData().toInt() ) );
795 methodComboBox->blockSignals( false );
796
797 viewGraduated->resizeColumnToContents( 0 );
798 viewGraduated->resizeColumnToContents( 1 );
799 viewGraduated->resizeColumnToContents( 2 );
800
801 mHistogramWidget->refresh();
802
804 mBlockUpdates--;
805
806 emit widgetChanged();
807}
808
810{
811 mRenderer->setClassAttribute( field );
812}
813
814void QgsGraduatedSymbolRendererWidget::methodComboBox_currentIndexChanged( int )
815{
816 const MethodMode newMethod = static_cast< MethodMode >( methodComboBox->currentData().toInt() );
817 toggleMethodWidgets( newMethod );
818 switch ( newMethod )
819 {
820 case ColorMode:
821 {
822 mRenderer->setGraduatedMethod( Qgis::GraduatedMethod::Color );
823 QgsColorRamp *ramp = btnColorRamp->colorRamp();
824
825 if ( !ramp )
826 {
827 QMessageBox::critical( this, tr( "Select Method" ), tr( "No color ramp defined." ) );
828 return;
829 }
830 mRenderer->setSourceColorRamp( ramp );
832 break;
833 }
834
835 case SizeMode:
836 {
837 lblColorRamp->setVisible( false );
838 btnColorRamp->setVisible( false );
839 lblSize->setVisible( true );
840 minSizeSpinBox->setVisible( true );
841 lblSize->setVisible( true );
842 maxSizeSpinBox->setVisible( true );
843 mSizeUnitWidget->setVisible( true );
844
845 mRenderer->setGraduatedMethod( Qgis::GraduatedMethod::Size );
846 reapplySizes();
847 break;
848 }
849 }
850}
851
852void QgsGraduatedSymbolRendererWidget::updateMethodParameters()
853{
854 clearParameterWidgets();
855
856 const QString methodId = cboGraduatedMode->currentData().toString();
858 Q_ASSERT( method );
859
860 // need more context?
862
863 for ( const QgsProcessingParameterDefinition *def : method->parameterDefinitions() )
864 {
866 mParametersLayout->addRow( ppww->createWrappedLabel(), ppww->createWrappedWidget( context ) );
867
868 QVariant value = method->parameterValues().value( def->name(), def->defaultValueForGui() );
869 ppww->setParameterValue( value, context );
870
872
873 mParameterWidgetWrappers.push_back( std::unique_ptr<QgsAbstractProcessingParameterWidgetWrapper>( ppww ) );
874 }
875
876 spinGraduatedClasses->setEnabled( !( method->flags() & QgsClassificationMethod::MethodProperty::IgnoresClassCount ) );
877}
878
879void QgsGraduatedSymbolRendererWidget::toggleMethodWidgets( MethodMode mode )
880{
881 switch ( mode )
882 {
883 case ColorMode:
884 {
885 lblColorRamp->setVisible( true );
886 btnColorRamp->setVisible( true );
887 lblSize->setVisible( false );
888 minSizeSpinBox->setVisible( false );
889 lblSizeTo->setVisible( false );
890 maxSizeSpinBox->setVisible( false );
891 mSizeUnitWidget->setVisible( false );
892 break;
893 }
894
895 case SizeMode:
896 {
897 lblColorRamp->setVisible( false );
898 btnColorRamp->setVisible( false );
899 lblSize->setVisible( true );
900 minSizeSpinBox->setVisible( true );
901 lblSizeTo->setVisible( true );
902 maxSizeSpinBox->setVisible( true );
903 mSizeUnitWidget->setVisible( true );
904 break;
905 }
906 }
907}
908
909void QgsGraduatedSymbolRendererWidget::clearParameterWidgets()
910{
911 while ( mParametersLayout->rowCount() )
912 {
913 QFormLayout::TakeRowResult row = mParametersLayout->takeRow( 0 );
914 for ( QLayoutItem *item : QList<QLayoutItem *>( {row.labelItem, row.fieldItem} ) )
915 if ( item )
916 {
917 if ( item->widget() )
918 item->widget()->deleteLater();
919 delete item;
920 }
921 }
922 mParameterWidgetWrappers.clear();
923}
924
926{
927 if ( !mModel )
928 return;
929
930 mModel->updateSymbology();
931
933 spinGraduatedClasses->setValue( mRenderer->ranges().count() );
935
936 emit widgetChanged();
937}
938
940{
941 for ( const QgsLegendSymbolItem &legendSymbol : levels )
942 {
943 QgsSymbol *sym = legendSymbol.symbol();
944 for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
945 {
946 mRenderer->setLegendSymbolItem( legendSymbol.ruleKey(), sym->clone() );
947 }
948 }
949 mRenderer->setUsingSymbolLevels( enabled );
950 mModel->updateSymbology();
951 emit widgetChanged();
952}
953
954void QgsGraduatedSymbolRendererWidget::cleanUpSymbolSelector( QgsPanelWidget *container )
955{
956 QgsSymbolSelectorWidget *dlg = qobject_cast<QgsSymbolSelectorWidget *>( container );
957 if ( !dlg )
958 return;
959
960 delete dlg->symbol();
961}
962
963void QgsGraduatedSymbolRendererWidget::updateSymbolsFromWidget()
964{
965 QgsSymbolSelectorWidget *dlg = qobject_cast<QgsSymbolSelectorWidget *>( sender() );
966 mGraduatedSymbol.reset( dlg->symbol()->clone() );
967
969}
970
972{
973 mSizeUnitWidget->blockSignals( true );
974 mSizeUnitWidget->setUnit( mGraduatedSymbol->outputUnit() );
975 mSizeUnitWidget->setMapUnitScale( mGraduatedSymbol->mapUnitScale() );
976 mSizeUnitWidget->blockSignals( false );
977
978 QItemSelectionModel *m = viewGraduated->selectionModel();
979 QModelIndexList selectedIndexes = m->selectedRows( 1 );
980 if ( !selectedIndexes.isEmpty() )
981 {
982 const auto constSelectedIndexes = selectedIndexes;
983 for ( const QModelIndex &idx : constSelectedIndexes )
984 {
985 if ( idx.isValid() )
986 {
987 int rangeIdx = idx.row();
988 QgsSymbol *newRangeSymbol = mGraduatedSymbol->clone();
989 if ( selectedIndexes.count() > 1 )
990 {
991 //if updating multiple ranges, retain the existing range colors
992 newRangeSymbol->setColor( mRenderer->ranges().at( rangeIdx ).symbol()->color() );
993 }
994 mRenderer->updateRangeSymbol( rangeIdx, newRangeSymbol );
995 }
996 }
997 }
998 else
999 {
1000 mRenderer->updateSymbols( mGraduatedSymbol.get() );
1001 }
1002
1004 emit widgetChanged();
1005}
1006
1007void QgsGraduatedSymbolRendererWidget::symmetryPointEditingFinished( )
1008{
1009 const QString text = cboSymmetryPoint->lineEdit()->text();
1010 int index = cboSymmetryPoint->findText( text );
1011 if ( index != -1 )
1012 {
1013 cboSymmetryPoint->setCurrentIndex( index );
1014 }
1015 else
1016 {
1017 cboSymmetryPoint->setItemText( cboSymmetryPoint->currentIndex(), text );
1019 }
1020}
1021
1022
1024{
1025 if ( mBlockUpdates )
1026 return;
1027
1028 QgsTemporaryCursorOverride override( Qt::WaitCursor );
1029 QString attrName = mExpressionWidget->currentField();
1030 int nclasses = spinGraduatedClasses->value();
1031
1032 const QString methodId = cboGraduatedMode->currentData().toString();
1034 Q_ASSERT( method );
1035
1036 int attrNum = mLayer->fields().lookupField( attrName );
1037
1038 QVariant minVal;
1039 QVariant maxVal;
1040 mLayer->minimumAndMaximumValue( attrNum, minVal, maxVal );
1041
1042 double minimum = minVal.toDouble();
1043 double maximum = maxVal.toDouble();
1044 mSymmetryPointValidator->setBottom( minimum );
1045 mSymmetryPointValidator->setTop( maximum );
1046 mSymmetryPointValidator->setMaxDecimals( spinPrecision->value() );
1047
1050 {
1051 // knowing that spinSymmetryPointForOtherMethods->value() is automatically put at minimum when out of min-max
1052 // using "(maximum-minimum)/100)" to avoid direct comparison of doubles
1053 double currentValue = QgsDoubleValidator::toDouble( cboSymmetryPoint->currentText() );
1054 if ( currentValue < ( minimum + ( maximum - minimum ) / 100. ) || currentValue > ( maximum - ( maximum - minimum ) / 100. ) )
1055 cboSymmetryPoint->setItemText( cboSymmetryPoint->currentIndex(), QLocale().toString( minimum + ( maximum - minimum ) / 2., 'f', method->labelPrecision() + 2 ) );
1056 }
1057
1058 if ( mGroupBoxSymmetric->isChecked() )
1059 {
1060 double symmetryPoint = QgsDoubleValidator::toDouble( cboSymmetryPoint->currentText() );
1061 bool astride = cbxAstride->isChecked();
1062 method->setSymmetricMode( true, symmetryPoint, astride );
1063 }
1064
1065 QVariantMap parameterValues;
1066 for ( const auto &ppww : std::as_const( mParameterWidgetWrappers ) )
1067 parameterValues.insert( ppww->parameterDefinition()->name(), ppww->parameterValue() );
1068 method->setParameterValues( parameterValues );
1069
1070 // set method to renderer
1071 mRenderer->setClassificationMethod( method );
1072
1073 // create and set new renderer
1074 mRenderer->setClassAttribute( attrName );
1075
1076 // If complexity >= oN^2, warn for big dataset (more than 50k records)
1077 // and give the user the chance to cancel
1078 if ( method->codeComplexity() > 1 && mLayer->featureCount() > 50000 )
1079 {
1080 if ( QMessageBox::Cancel == QMessageBox::question( this, tr( "Apply Classification" ), tr( "Natural break classification (Jenks) is O(n2) complexity, your classification may take a long time.\nPress cancel to abort breaks calculation or OK to continue." ), QMessageBox::Cancel, QMessageBox::Ok ) )
1081 {
1082 return;
1083 }
1084 }
1085
1086 if ( methodComboBox->currentData() == ColorMode )
1087 {
1088 std::unique_ptr<QgsColorRamp> ramp( btnColorRamp->colorRamp() );
1089 if ( !ramp )
1090 {
1091 QMessageBox::critical( this, tr( "Apply Classification" ), tr( "No color ramp defined." ) );
1092 return;
1093 }
1094 mRenderer->setSourceColorRamp( ramp.release() );
1095 }
1096 else
1097 {
1098 mRenderer->setSourceColorRamp( nullptr );
1099 }
1100
1101 mRenderer->updateClasses( mLayer, nclasses );
1102
1103 if ( methodComboBox->currentData() == SizeMode )
1104 mRenderer->setSymbolSizes( minSizeSpinBox->value(), maxSizeSpinBox->value() );
1105
1106 mRenderer->calculateLabelPrecision();
1107 // PrettyBreaks and StdDev calculation don't generate exact
1108 // number of classes - leave user interface unchanged for these
1109 updateUiFromRenderer( false );
1110}
1111
1113{
1114 std::unique_ptr< QgsColorRamp > ramp( btnColorRamp->colorRamp() );
1115 if ( !ramp )
1116 return;
1117
1118 mRenderer->updateColorRamp( ramp.release() );
1119 mRenderer->updateSymbols( mGraduatedSymbol.get() );
1121}
1122
1124{
1125 mRenderer->setSymbolSizes( minSizeSpinBox->value(), maxSizeSpinBox->value() );
1126 mRenderer->updateSymbols( mGraduatedSymbol.get() );
1128}
1129
1130#if 0
1131int QgsRendererPropertiesDialog::currentRangeRow()
1132{
1133 QModelIndex idx = viewGraduated->selectionModel()->currentIndex();
1134 if ( !idx.isValid() )
1135 return -1;
1136 return idx.row();
1137}
1138#endif
1139
1141{
1142 QList<int> rows;
1143 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
1144
1145 const auto constSelectedRows = selectedRows;
1146 for ( const QModelIndex &r : constSelectedRows )
1147 {
1148 if ( r.isValid() )
1149 {
1150 rows.append( r.row() );
1151 }
1152 }
1153 return rows;
1154}
1155
1157{
1159 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
1160 QModelIndexList::const_iterator sIt = selectedRows.constBegin();
1161
1162 for ( ; sIt != selectedRows.constEnd(); ++sIt )
1163 {
1164 selectedRanges.append( mModel->rendererRange( *sIt ) );
1165 }
1166 return selectedRanges;
1167}
1168
1170{
1171 if ( idx.isValid() && idx.column() == 0 )
1172 changeRangeSymbol( idx.row() );
1173 if ( idx.isValid() && idx.column() == 1 )
1174 changeRange( idx.row() );
1175}
1176
1178{
1179 if ( !idx.isValid() )
1180 mRowSelected = -1;
1181 else
1182 mRowSelected = idx.row();
1183}
1184
1188
1190{
1191 const QgsRendererRange &range = mRenderer->ranges()[rangeIdx];
1192 std::unique_ptr< QgsSymbol > newSymbol( range.symbol()->clone() );
1194 if ( panel && panel->dockMode() )
1195 {
1196 // bit tricky here - the widget doesn't take ownership of the symbol. So we need it to last for the duration of the
1197 // panel's existence. Accordingly, just kinda give it ownership here, and clean up in cleanUpSymbolSelector
1198 QgsSymbolSelectorWidget *dlg = new QgsSymbolSelectorWidget( newSymbol.release(), mStyle, mLayer, panel );
1199 dlg->setContext( mContext );
1200 dlg->setPanelTitle( range.label() );
1201 connect( dlg, &QgsPanelWidget::widgetChanged, this, &QgsGraduatedSymbolRendererWidget::updateSymbolsFromWidget );
1202 connect( dlg, &QgsPanelWidget::panelAccepted, this, &QgsGraduatedSymbolRendererWidget::cleanUpSymbolSelector );
1203 openPanel( dlg );
1204 }
1205 else
1206 {
1207 QgsSymbolSelectorDialog dlg( newSymbol.get(), mStyle, mLayer, panel );
1208 dlg.setContext( mContext );
1209 if ( !dlg.exec() || !newSymbol )
1210 {
1211 return;
1212 }
1213
1214 mGraduatedSymbol = std::move( newSymbol );
1215 whileBlocking( btnChangeGraduatedSymbol )->setSymbol( mGraduatedSymbol->clone() );
1217 }
1218}
1219
1221{
1222 QgsLUDialog dialog( this );
1223
1224 const QgsRendererRange &range = mRenderer->ranges()[rangeIdx];
1225 // Add arbitrary 2 to number of decimal places to retain a bit extra.
1226 // Ensures users can see if legend is not completely honest!
1227 int decimalPlaces = mRenderer->classificationMethod()->labelPrecision() + 2;
1228 if ( decimalPlaces < 0 ) decimalPlaces = 0;
1229 dialog.setLowerValue( QLocale().toString( range.lowerValue(), 'f', decimalPlaces ) );
1230 dialog.setUpperValue( QLocale().toString( range.upperValue(), 'f', decimalPlaces ) );
1231
1232 if ( dialog.exec() == QDialog::Accepted )
1233 {
1234 mRenderer->updateRangeUpperValue( rangeIdx, dialog.upperValueDouble() );
1235 mRenderer->updateRangeLowerValue( rangeIdx, dialog.lowerValueDouble() );
1236
1237 //If the boundaries have to stay linked, we update the ranges above and below, as well as their label if needed
1238 if ( cbxLinkBoundaries->isChecked() )
1239 {
1240 if ( rangeIdx > 0 )
1241 {
1242 mRenderer->updateRangeUpperValue( rangeIdx - 1, dialog.lowerValueDouble() );
1243 }
1244
1245 if ( rangeIdx < mRenderer->ranges().size() - 1 )
1246 {
1247 mRenderer->updateRangeLowerValue( rangeIdx + 1, dialog.upperValueDouble() );
1248 }
1249 }
1250 }
1251 mHistogramWidget->refresh();
1252 emit widgetChanged();
1253}
1254
1256{
1257 mModel->addClass( mGraduatedSymbol.get() );
1258 mHistogramWidget->refresh();
1259 emit widgetChanged();
1260
1261}
1262
1264{
1265 QList<int> classIndexes = selectedClasses();
1266 mModel->deleteRows( classIndexes );
1267 mHistogramWidget->refresh();
1268 emit widgetChanged();
1269}
1270
1272{
1273 mModel->removeAllRows();
1274 mHistogramWidget->refresh();
1275 emit widgetChanged();
1276}
1277
1279{
1280 const QgsRangeList &ranges = mRenderer->ranges();
1281 bool ordered = true;
1282 for ( int i = 1; i < ranges.size(); ++i )
1283 {
1284 if ( ranges[i] < ranges[i - 1] )
1285 {
1286 ordered = false;
1287 break;
1288 }
1289 }
1290 return ordered;
1291}
1292
1294{
1295 //If the checkbox controlling the link between boundaries was unchecked and we check it, we have to link the boundaries
1296 //This is done by updating all lower ranges to the upper value of the range above
1297 if ( linked )
1298 {
1299 if ( ! rowsOrdered() )
1300 {
1301 int result = QMessageBox::warning(
1302 this,
1303 tr( "Link Class Boundaries" ),
1304 tr( "Rows will be reordered before linking boundaries. Continue?" ),
1305 QMessageBox::Ok | QMessageBox::Cancel );
1306 if ( result != QMessageBox::Ok )
1307 {
1308 cbxLinkBoundaries->setChecked( false );
1309 return;
1310 }
1311 mRenderer->sortByValue();
1312 }
1313
1314 // Ok to proceed
1315 for ( int i = 1; i < mRenderer->ranges().size(); ++i )
1316 {
1317 mRenderer->updateRangeLowerValue( i, mRenderer->ranges()[i - 1].upperValue() );
1318 }
1320 }
1321}
1322
1324{
1325 if ( item->column() == 2 )
1326 {
1327 QString label = item->text();
1328 int idx = item->row();
1329 mRenderer->updateRangeLabel( idx, label );
1330 }
1331}
1332
1334{
1335 mRenderer->classificationMethod()->setLabelFormat( txtLegendFormat->text() );
1336 mRenderer->classificationMethod()->setLabelPrecision( spinPrecision->value() );
1337 mRenderer->classificationMethod()->setLabelTrimTrailingZeroes( cbxTrimTrailingZeroes->isChecked() );
1338 mRenderer->updateRangeLabels();
1339 mModel->updateLabels();
1340}
1341
1342
1344{
1345 QList<QgsSymbol *> selectedSymbols;
1346
1347 QItemSelectionModel *m = viewGraduated->selectionModel();
1348 QModelIndexList selectedIndexes = m->selectedRows( 1 );
1349 if ( !selectedIndexes.isEmpty() )
1350 {
1351 const QgsRangeList &ranges = mRenderer->ranges();
1352 QModelIndexList::const_iterator indexIt = selectedIndexes.constBegin();
1353 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
1354 {
1355 QStringList list = m->model()->data( *indexIt ).toString().split( ' ' );
1356 if ( list.size() < 3 )
1357 {
1358 continue;
1359 }
1360 // Not strictly necessary because the range should have been sanitized already
1361 // after user input, but being permissive never hurts
1362 bool ok = false;
1363 double lowerBound = qgsPermissiveToDouble( list.at( 0 ), ok );
1364 if ( ! ok )
1365 lowerBound = 0.0;
1366 double upperBound = qgsPermissiveToDouble( list.at( 2 ), ok );
1367 if ( ! ok )
1368 upperBound = 0.0;
1369 QgsSymbol *s = findSymbolForRange( lowerBound, upperBound, ranges );
1370 if ( s )
1371 {
1372 selectedSymbols.append( s );
1373 }
1374 }
1375 }
1376 return selectedSymbols;
1377}
1378
1379QgsSymbol *QgsGraduatedSymbolRendererWidget::findSymbolForRange( double lowerBound, double upperBound, const QgsRangeList &ranges ) const
1380{
1381 int decimalPlaces = mRenderer->classificationMethod()->labelPrecision() + 2;
1382 if ( decimalPlaces < 0 )
1383 decimalPlaces = 0;
1384 double precision = 1.0 / std::pow( 10, decimalPlaces );
1385
1386 for ( QgsRangeList::const_iterator it = ranges.begin(); it != ranges.end(); ++it )
1387 {
1388 if ( qgsDoubleNear( lowerBound, it->lowerValue(), precision ) && qgsDoubleNear( upperBound, it->upperValue(), precision ) )
1389 {
1390 return it->symbol();
1391 }
1392 }
1393 return nullptr;
1394}
1395
1397{
1398 if ( mModel )
1399 {
1400 mModel->updateSymbology();
1401 }
1402 mHistogramWidget->refresh();
1403 emit widgetChanged();
1404}
1405
1410
1412{
1413 viewGraduated->selectionModel()->clear();
1414 if ( ! rowsOrdered() )
1415 {
1416 cbxLinkBoundaries->setChecked( false );
1417 }
1418 emit widgetChanged();
1419}
1420
1425
1427{
1428 if ( !event )
1429 {
1430 return;
1431 }
1432
1433 if ( event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier )
1434 {
1435 mCopyBuffer.clear();
1436 mCopyBuffer = selectedRanges();
1437 }
1438 else if ( event->key() == Qt::Key_V && event->modifiers() == Qt::ControlModifier )
1439 {
1440 QgsRangeList::const_iterator rIt = mCopyBuffer.constBegin();
1441 for ( ; rIt != mCopyBuffer.constEnd(); ++rIt )
1442 {
1443 mModel->addClass( *rIt );
1444 }
1445 emit widgetChanged();
1446 }
1447}
1448
1449void QgsGraduatedSymbolRendererWidget::selectionChanged( const QItemSelection &, const QItemSelection & )
1450{
1451 const QgsRangeList ranges = selectedRanges();
1452 if ( !ranges.isEmpty() )
1453 {
1454 whileBlocking( btnChangeGraduatedSymbol )->setSymbol( ranges.at( 0 ).symbol()->clone() );
1455 }
1456 else if ( mRenderer->sourceSymbol() )
1457 {
1458 whileBlocking( btnChangeGraduatedSymbol )->setSymbol( mRenderer->sourceSymbol()->clone() );
1459 }
1460 btnChangeGraduatedSymbol->setDialogTitle( ranges.size() == 1 ? ranges.at( 0 ).label() : tr( "Symbol Settings" ) );
1461}
1462
1463void QgsGraduatedSymbolRendererWidget::dataDefinedSizeLegend()
1464{
1465 QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mGraduatedSymbol.get() ); // this should be only enabled for marker symbols
1466 QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
1467 if ( panel )
1468 {
1469 connect( panel, &QgsPanelWidget::widgetChanged, this, [ = ]
1470 {
1471 mRenderer->setDataDefinedSizeLegend( panel->dataDefinedSizeLegend() );
1472 emit widgetChanged();
1473 } );
1474 openPanel( panel ); // takes ownership of the panel
1475 }
1476}
1477
1478void QgsGraduatedSymbolRendererWidget::changeGraduatedSymbol()
1479{
1480 mGraduatedSymbol.reset( btnChangeGraduatedSymbol->symbol()->clone() );
1482}
1483
1485{
1486 std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
1487 if ( !tempSymbol )
1488 return;
1489
1490 const QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
1491 for ( const QModelIndex &index : selectedRows )
1492 {
1493 if ( !index.isValid() )
1494 continue;
1495
1496 const int row = index.row();
1497 if ( !mRenderer || mRenderer->ranges().size() <= row )
1498 continue;
1499
1500 if ( mRenderer->ranges().at( row ).symbol()->type() != tempSymbol->type() )
1501 continue;
1502
1503 std::unique_ptr< QgsSymbol > newCatSymbol( tempSymbol->clone() );
1504 if ( selectedRows.count() > 1 )
1505 {
1506 //if updating multiple ranges, retain the existing category colors
1507 newCatSymbol->setColor( mRenderer->ranges().at( row ).symbol()->color() );
1508 }
1509
1510 mRenderer->updateRangeSymbol( row, newCatSymbol.release() );
1511 }
1512 emit widgetChanged();
1513}
@ Size
Alter size of symbols.
@ Color
Alter color of symbols.
@ Marker
Marker symbol.
@ Line
Line symbol.
@ Fill
Fill symbol.
@ Hybrid
Hybrid symbol.
A widget wrapper for Processing parameter value widgets.
QVariant parameterValue() const
Returns the current value of the parameter.
QLabel * createWrappedLabel()
Creates and returns a new label to accompany widgets created by the wrapper.
QWidget * createWrappedWidget(QgsProcessingContext &context)
Creates and return a new wrapped widget which allows customization of the parameter's value.
void widgetValueHasChanged(QgsAbstractProcessingParameterWidgetWrapper *wrapper)
Emitted whenever the parameter value (as defined by the wrapped widget) is changed.
const QgsProcessingParameterDefinition * parameterDefinition() const
Returns the parameter definition associated with this wrapper.
void setParameterValue(const QVariant &value, QgsProcessingContext &context)
Sets the current value for the parameter.
static QgsClassificationMethodRegistry * classificationMethodRegistry()
Returns the application's classification methods registry, used in graduated renderer.
QgsClassificationMethod * method(const QString &id)
Returns a new instance of the method for the given id.
QIcon icon(const QString &id) const
Returns the icon for a given method id.
QMap< QString, QString > methodNames() const
Returns a map <name, id> of all registered methods.
QgsClassificationMethod is an abstract class for implementations of classification methods.
double symmetryPoint() const
Returns the symmetry point for symmetric mode.
int codeComplexity() const
Code complexity as the exponent in Big O notation.
bool symmetricModeEnabled() const
Returns if the symmetric mode is enabled.
int labelPrecision() const
Returns the precision for the formatting of the labels.
virtual QString id() const =0
The id of the method as saved in the project, must be unique in registry.
QVariantMap parameterValues() const
Returns the values of the processing parameters.
void setSymmetricMode(bool enabled, double symmetryPoint=0, bool symmetryAstride=false)
Defines if the symmetric mode is enables and configures its parameters.
bool symmetryAstride() const
Returns if the symmetric mode is astride if true, it will remove the symmetry point break so that the...
QString labelFormat() const
Returns the format of the label for the classes.
void setParameterValues(const QVariantMap &values)
Defines the values of the additional parameters.
bool labelTrimTrailingZeroes() const
Returns if the trailing 0 are trimmed in the label.
@ IgnoresClassCount
The classification method does not compute classes based on a class count (since QGIS 3....
bool symmetricModeAvailable() const
Returns if the method supports symmetric calculation.
QgsClassificationMethod::MethodProperties flags() const
Returns the classification flags.
void colorRampChanged()
Emitted whenever a new color ramp is set for the button.
Abstract base class for color ramps.
Widget for configuration of appearance of legend for marker symbols with data-defined size.
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration as set up in the dialog (may be nullptr). Ownership is passed to the caller.
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
void setTop(double top)
Set top range limit.
void setMaxDecimals(int maxDecimals)
Sets the number of decimals accepted by the validator to maxDecimals.
void setBottom(double bottom)
Set top range limit.
Abstract interface for generating an expression context scope.
Single scope for storing variables and functions for use within a QgsExpressionContext.
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 * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
@ Date
Date or datetime fields.
@ Numeric
All numeric fields.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
void rangesModified(bool rangesAdded)
Emitted when the user modifies the graduated ranges using the histogram widget.
void deleteClasses()
Removes currently selected classes.
QList< QgsSymbol * > selectedSymbols() override
Subclasses may provide the capability of changing multiple symbols at once by implementing the follow...
void disableSymbolLevels() override
Disables symbol level modification on the widget.
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
QgsGraduatedSymbolRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
void refreshRanges(bool reset)
Refreshes the ranges for the renderer.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
void setSymbolLevels(const QgsLegendSymbolList &levels, bool enabled) override
Sets the symbol levels for the renderer defined in the widget.
QgsSymbol * findSymbolForRange(double lowerBound, double upperBound, const QgsRangeList &ranges) const
void deleteAllClasses()
Removes all classes from the classification.
void addClass()
Adds a class manually to the classification.
void toggleBoundariesLink(bool linked)
Toggle the link between classes boundaries.
void applyChangeToSymbol()
Applies current symbol to selected ranges, or to all ranges if none is selected.
QList< int > selectedClasses()
Returns a list of indexes for the classes under selection.
static QgsGraduatedSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
creates a QgsGraduatedSymbolRenderer from an existing renderer.
const QgsRangeList & ranges() const
Returns a list of all ranges used in the classification.
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition qgsgui.cpp:138
void setSourceFieldExp(const QString &fieldOrExp)
Sets the source field or expression to use for values in the histogram.
double upperValueDouble() const
Returns the upper value.
double lowerValueDouble() const
Returns the lower value.
void setLowerValue(const QString &val)
void setUpperValue(const QString &val)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
The QgsMapSettings class contains configuration for rendering of the map.
A marker symbol type, for rendering Point and MultiPoint geometries.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void widgetChanged()
Emitted when the widget state changes.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
Contains information about the context in which a processing algorithm is executed.
QgsAbstractProcessingParameterWidgetWrapper * createParameterWidgetWrapper(const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type)
Creates a new parameter widget wrapper for the given parameter.
@ Standard
Standard algorithm dialog.
Base class for the definition of processing parameters.
QVariant defaultValueForGui() const
Returns the default value to use for the parameter in a GUI.
QString name() const
Returns the name of the parameter.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...
QString label() const
Returns the label used for the range.
QgsSymbol * symbol() const
Returns the symbol used for the range.
bool renderState() const
Returns true if the range should be rendered.
double upperValue() const
Returns the upper bound of the range.
double lowerValue() const
Returns the lower bound of the range.
Base class for renderer settings widgets.
void showSymbolLevelsDialog(QgsFeatureRenderer *r)
Show a dialog with renderer's symbol level settings.
QgsSymbolWidgetContext mContext
Context in which widget is shown.
virtual void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsDataDefinedSizeLegendWidget * createDataDefinedSizeLegendWidget(const QgsMarkerSymbol *symbol, const QgsDataDefinedSizeLegend *ddsLegend)
Creates widget to setup data-defined size legend.
void contextMenuViewCategories(QPoint p)
const QgsVectorLayer * vectorLayer() const
Returns the vector layer associated with the widget.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
QgsVectorLayer * mLayer
void changed()
Emitted when the symbol's settings are changed.
static QgsSymbol * symbolFromMimeData(const QMimeData *data)
Attempts to parse mime data as a symbol.
static QIcon symbolPreviewIcon(const QgsSymbol *symbol, QSize size, int padding=0, QgsLegendPatchShape *shape=nullptr)
Returns an icon preview for a color ramp.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
Symbol selector widget that can be used to select and build a symbol.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the symbol widget is shown, e.g., the associated map canvas and expression ...
QgsSymbol * symbol()
Returns the symbol that is currently active in the widget.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QList< QgsExpressionContextScope > additionalExpressionContextScopes() const
Returns the list of additional expression context scopes to show as available within the layer.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:93
static QgsSymbol * defaultSymbol(QgsWkbTypes::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
void setColor(const QColor &color) const
Sets the color for the symbol.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Definition qgssymbol.h:215
Temporarily sets a cursor override for the QApplication for the lifetime of the object.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
@ RenderPoints
Points (e.g., for font sizes)
@ RenderPixels
Pixels.
@ RenderInches
Inches.
@ RenderMillimeters
Millimeters.
@ RenderMapUnits
Map units.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
void minimumAndMaximumValue(int index, QVariant &minimum, QVariant &maximum) const
Calculates both the minimum and maximum value for an attribute column.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition qgis.cpp:71
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:2527
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:2453
const QgsField & field
Definition qgsfield.h:476
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugMsg(str)
Definition qgslogger.h:38
QList< QgsRendererRange > QgsRangeList
int precision