QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsrulebasedrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrulebasedrendererwidget.cpp - Settings widget for rule-based renderer
3 ---------------------
4 begin : May 2010
5 copyright : (C) 2010 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
17
19#include "qgsfeatureiterator.h"
20#include "qgssymbollayerutils.h"
21#include "qgssymbol.h"
22#include "qgsvectorlayer.h"
23#include "qgsapplication.h"
24#include "qgsexpression.h"
26#include "qgslogger.h"
27#include "qgsreadwritecontext.h"
28#include "qgspanelwidget.h"
29#include "qgsmapcanvas.h"
30#include "qgssettings.h"
31#include "qgsguiutils.h"
32
33#include <QKeyEvent>
34#include <QMenu>
35#include <QProgressDialog>
36#include <QTreeWidgetItem>
37#include <QVBoxLayout>
38#include <QMessageBox>
39#include <QClipboard>
40#include <QPointer>
41#include <QScreen>
42
43#ifdef ENABLE_MODELTEST
44#include "modeltest.h"
45#endif
46
47
52
54 : QgsRendererWidget( layer, style )
55 , mContextMenu( new QMenu( this ) )
56{
57 mRenderer = nullptr;
58 // try to recognize the previous renderer
59 // (null renderer means "no previous renderer")
60
61
62 if ( renderer )
63 {
65 }
66 if ( !mRenderer )
67 {
68 // some default options
70
71 mRenderer = std::make_unique< QgsRuleBasedRenderer >( symbol );
72 if ( renderer )
74 }
75
76 setupUi( this );
77 this->layout()->setContentsMargins( 0, 0, 0, 0 );
78
79 mModel = new QgsRuleBasedRendererModel( mRenderer.get(), viewRules, screen() );
80#ifdef ENABLE_MODELTEST
81 new ModelTest( mModel, this ); // for model validity checking
82#endif
83 viewRules->setModel( mModel );
84
85 mDeleteAction = new QAction( tr( "Remove Rule" ), this );
86 mDeleteAction->setShortcut( QKeySequence( QKeySequence::Delete ) );
87
88 viewRules->addAction( mCopyAction );
89 viewRules->addAction( mPasteAction );
90 viewRules->addAction( mDeleteAction );
91
92 mRefineMenu = new QMenu( tr( "Refine Current Rule" ), btnRefineRule );
93 mRefineMenu->addAction( tr( "Add Scales to Rule" ), this, SLOT( refineRuleScales() ) );
94 mRefineMenu->addAction( tr( "Add Categories to Rule" ), this, SLOT( refineRuleCategories() ) );
95 mRefineMenu->addAction( tr( "Add Ranges to Rule" ), this, SLOT( refineRuleRanges() ) );
96 btnRefineRule->setMenu( mRefineMenu );
97
98 btnAddRule->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.svg" ) ) );
99 btnEditRule->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.svg" ) ) );
100 btnRemoveRule->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
101
102 connect( viewRules, &QAbstractItemView::doubleClicked, this, static_cast < void ( QgsRuleBasedRendererWidget::* )( const QModelIndex &index ) > ( &QgsRuleBasedRendererWidget::editRule ) );
103
104 // support for context menu (now handled generically)
105 connect( viewRules, &QWidget::customContextMenuRequested, this, &QgsRuleBasedRendererWidget::showContextMenu );
106
107 connect( viewRules->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsRuleBasedRendererWidget::currentRuleChanged );
108 connect( viewRules->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsRuleBasedRendererWidget::selectedRulesChanged );
109
110 connect( btnAddRule, &QAbstractButton::clicked, this, &QgsRuleBasedRendererWidget::addRule );
111 connect( btnEditRule, &QAbstractButton::clicked, this, static_cast < void ( QgsRuleBasedRendererWidget::* )() > ( &QgsRuleBasedRendererWidget::editRule ) );
112 connect( btnRemoveRule, &QAbstractButton::clicked, this, &QgsRuleBasedRendererWidget::removeRule );
113 connect( mDeleteAction, &QAction::triggered, this, &QgsRuleBasedRendererWidget::removeRule );
114 connect( btnCountFeatures, &QAbstractButton::clicked, this, &QgsRuleBasedRendererWidget::countFeatures );
115
116 connect( btnRenderingOrder, &QAbstractButton::clicked, this, &QgsRuleBasedRendererWidget::setRenderingOrder );
117
118 connect( mModel, &QAbstractItemModel::dataChanged, this, &QgsPanelWidget::widgetChanged );
119 connect( mModel, &QAbstractItemModel::rowsInserted, this, &QgsPanelWidget::widgetChanged );
120 connect( mModel, &QAbstractItemModel::rowsRemoved, this, &QgsPanelWidget::widgetChanged );
121
124
125 // store/restore header section widths
126 connect( viewRules->header(), &QHeaderView::sectionResized, this, &QgsRuleBasedRendererWidget::saveSectionWidth );
127
129
130 connect( mContextMenu, &QMenu::aboutToShow, this, [ = ]
131 {
132 std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
133 mPasteSymbolAction->setEnabled( static_cast< bool >( tempSymbol ) );
134 } );
135}
136
141
146
148{
149 if ( dockMode )
150 {
151 // when in dock mode, these shortcuts conflict with the main window shortcuts and cannot be used
152 if ( mDeleteAction )
153 mDeleteAction->setShortcut( QKeySequence() );
154 }
156}
157
159{
162
164 if ( current )
165 {
166 // add after this rule
167 QModelIndex currentIndex = viewRules->selectionModel()->currentIndex();
168 mModel->insertRule( currentIndex.parent(), currentIndex.row() + 1, newrule );
169 QModelIndex newindex = mModel->index( currentIndex.row() + 1, 0, currentIndex.parent() );
170 viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
171 }
172 else
173 {
174 // append to root rule
175 int rows = mModel->rowCount();
176 mModel->insertRule( QModelIndex(), rows, newrule );
177 QModelIndex newindex = mModel->index( rows, 0 );
178 viewRules->selectionModel()->setCurrentIndex( newindex, QItemSelectionModel::ClearAndSelect );
179 }
180 editRule();
181}
182
184{
185 QItemSelectionModel *sel = viewRules->selectionModel();
186 QModelIndex idx = sel->currentIndex();
187 if ( !idx.isValid() )
188 return nullptr;
189 return mModel->ruleForIndex( idx );
190}
191
193{
194 editRule( viewRules->selectionModel()->currentIndex() );
195}
196
197void QgsRuleBasedRendererWidget::editRule( const QModelIndex &index )
198{
199 if ( !index.isValid() )
200 return;
201
204
205 if ( panel && panel->dockMode() )
206 {
207 QgsRendererRulePropsWidget *widget = new QgsRendererRulePropsWidget( rule, mLayer, mStyle, this, mContext );//panel?
208 widget->setPanelTitle( tr( "Edit Rule" ) );
209 connect( widget, &QgsPanelWidget::panelAccepted, this, &QgsRuleBasedRendererWidget::ruleWidgetPanelAccepted );
210 connect( widget, &QgsPanelWidget::widgetChanged, this, &QgsRuleBasedRendererWidget::liveUpdateRuleFromPanel );
211 openPanel( widget );
212 return;
213 }
214
216 if ( dlg.exec() )
217 {
218 mModel->updateRule( index.parent(), index.row() );
220 emit widgetChanged();
221 }
222}
223
225{
226 QItemSelection sel = viewRules->selectionModel()->selection();
227 QgsDebugMsgLevel( QStringLiteral( "REMOVE RULES!!! ranges: %1" ).arg( sel.count() ), 2 );
228 const auto constSel = sel;
229 for ( const QItemSelectionRange &range : constSel )
230 {
231 QgsDebugMsgLevel( QStringLiteral( "RANGE: r %1 - %2" ).arg( range.top() ).arg( range.bottom() ), 2 );
232 if ( range.isValid() )
233 mModel->removeRows( range.top(), range.bottom() - range.top() + 1, range.parent() );
234 }
235 // make sure that the selection is gone
236 viewRules->selectionModel()->clear();
238}
239
240void QgsRuleBasedRendererWidget::currentRuleChanged( const QModelIndex &current, const QModelIndex &previous )
241{
242 Q_UNUSED( previous )
243 btnEditRule->setEnabled( current.isValid() );
244}
245
246
252#include <QDialogButtonBox>
253#include <QInputDialog>
254#include <QClipboard>
255
257{
258 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
259
260 if ( indexlist.isEmpty() )
261 return;
262
263
264 if ( type == 0 ) // categories
266 else if ( type == 1 ) // ranges
268 else // scales
269 refineRuleScalesGui( indexlist );
270
271 // TODO: set initial rule's symbol to NULL (?)
272
273 // show the newly added rules
274 const auto constIndexlist = indexlist;
275 for ( const QModelIndex &index : constIndexlist )
276 viewRules->expand( index );
277}
278
283
288
293
295{
297 w->setPanelTitle( tr( "Add Categories to Rules" ) );
298 connect( w, &QgsPanelWidget::panelAccepted, this, &QgsRuleBasedRendererWidget::refineRuleCategoriesAccepted );
299 w->setContext( mContext );
300 openPanel( w );
301}
302
304{
306 w->setPanelTitle( tr( "Add Ranges to Rules" ) );
307 connect( w, &QgsPanelWidget::panelAccepted, this, &QgsRuleBasedRendererWidget::refineRuleRangesAccepted );
308 w->setContext( mContext );
309 openPanel( w );
310}
311
312void QgsRuleBasedRendererWidget::refineRuleScalesGui( const QModelIndexList &indexList )
313{
314 for ( const QModelIndex &index : indexList )
315 {
316 QgsRuleBasedRenderer::Rule *initialRule = mModel->ruleForIndex( index );
317
318 // If any of the rules don't have a symbol let the user know and exit.
319 if ( !initialRule->symbol() )
320 {
321 QMessageBox::warning( this, tr( "Scale Refinement" ), tr( "Parent rule %1 must have a symbol for this operation." ).arg( initialRule->label() ) );
322 return;
323 }
324 }
325
326 QString txt = QInputDialog::getText( this,
327 tr( "Scale Refinement" ),
328 tr( "Please enter scale denominators at which will split the rule, separate them by commas (e.g. 1000,5000):" ) );
329 if ( txt.isEmpty() )
330 return;
331
332 QList<int> scales;
333 bool ok;
334 const auto constSplit = txt.split( ',' );
335 for ( const QString &item : constSplit )
336 {
337 int scale = item.toInt( &ok );
338 if ( ok )
339 scales.append( scale );
340 else
341 QMessageBox::information( this, tr( "Scale Refinement" ), tr( "\"%1\" is not valid scale denominator, ignoring it." ).arg( item ) );
342 }
343
344 for ( const QModelIndex &index : indexList )
345 {
346 QgsRuleBasedRenderer::Rule *initialRule = mModel->ruleForIndex( index );
347 mModel->willAddRules( index, scales.count() + 1 );
348 QgsRuleBasedRenderer::refineRuleScales( initialRule, scales );
349 }
351}
352
353void QgsRuleBasedRendererWidget::setSymbolLevels( const QList<QgsLegendSymbolItem> &levels, bool )
354{
355 if ( !mRenderer )
356 return;
357
358 for ( const QgsLegendSymbolItem &legendSymbol : std::as_const( levels ) )
359 {
360 QgsSymbol *sym = legendSymbol.symbol();
361 for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
362 {
363 mRenderer->setLegendSymbolItem( legendSymbol.ruleKey(), sym->clone() );
364 }
365 }
366
367 emit widgetChanged();
368}
369
371{
372 QList<QgsSymbol *> symbolList;
373
374 if ( !mRenderer )
375 {
376 return symbolList;
377 }
378
379 QItemSelection sel = viewRules->selectionModel()->selection();
380 const auto constSel = sel;
381 for ( const QItemSelectionRange &range : constSel )
382 {
383 QModelIndex parent = range.parent();
384 QgsRuleBasedRenderer::Rule *parentRule = mModel->ruleForIndex( parent );
385 const QgsRuleBasedRenderer::RuleList &children = parentRule->children();
386 for ( int row = range.top(); row <= range.bottom(); row++ )
387 {
388 symbolList.append( children.at( row )->symbol() );
389 }
390 }
391
392 return symbolList;
393}
394
396{
398 QItemSelection sel = viewRules->selectionModel()->selection();
399 const auto constSel = sel;
400 for ( const QItemSelectionRange &range : constSel )
401 {
402 QModelIndex parent = range.parent();
403 QgsRuleBasedRenderer::Rule *parentRule = mModel->ruleForIndex( parent );
404 const QgsRuleBasedRenderer::RuleList &children = parentRule->children();
405 for ( int row = range.top(); row <= range.bottom(); row++ )
406 {
407 rl.append( children.at( row )->clone() );
408 }
409 }
410 return rl;
411}
412
414{
415 // TODO: model/view
416 /*
417 if ( treeRules )
418 {
419 treeRules->populateRules();
420 }
421 */
422 emit widgetChanged();
423}
424
426{
427 if ( !event )
428 {
429 return;
430 }
431
432 if ( event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier )
433 {
434 qDeleteAll( mCopyBuffer );
435 mCopyBuffer.clear();
437 }
438 else if ( event->key() == Qt::Key_V && event->modifiers() == Qt::ControlModifier )
439 {
440 QgsRuleBasedRenderer::RuleList::const_iterator rIt = mCopyBuffer.constBegin();
441 for ( ; rIt != mCopyBuffer.constEnd(); ++rIt )
442 {
443 int rows = mModel->rowCount();
444 mModel->insertRule( QModelIndex(), rows, ( *rIt )->clone() );
445 }
446 }
447}
448
450
452{
454 if ( panel && panel->dockMode() )
455 {
456 QgsSymbolLevelsWidget *widget = new QgsSymbolLevelsWidget( mRenderer.get(), true, panel );
457 widget->setForceOrderingEnabled( true );
458 widget->setPanelTitle( tr( "Symbol Levels" ) );
459 connect( widget, &QgsPanelWidget::widgetChanged, this, [ = ]()
460 {
461 setSymbolLevels( widget->symbolLevels(), widget->usingLevels() );
462 } );
463 panel->openPanel( widget );
464 }
465 else
466 {
467 QgsSymbolLevelsDialog dlg( mRenderer.get(), true, panel );
468 dlg.setForceOrderingEnabled( true );
469 if ( dlg.exec() )
470 {
472 }
473 }
474}
475
476void QgsRuleBasedRendererWidget::saveSectionWidth( int section, int oldSize, int newSize )
477{
478 Q_UNUSED( oldSize )
479 // skip last section, as it stretches
480 if ( section == 5 )
481 return;
482 QgsSettings settings;
483 QString path = "/Windows/RuleBasedTree/sectionWidth/" + QString::number( section );
484 settings.setValue( path, newSize );
485}
486
488{
489 QgsSettings settings;
490 QString path = QStringLiteral( "/Windows/RuleBasedTree/sectionWidth/" );
491 QHeaderView *head = viewRules->header();
492 head->resizeSection( 0, settings.value( path + QString::number( 0 ), 150 ).toInt() );
493 head->resizeSection( 1, settings.value( path + QString::number( 1 ), 150 ).toInt() );
494 head->resizeSection( 2, settings.value( path + QString::number( 2 ), 80 ).toInt() );
495 head->resizeSection( 3, settings.value( path + QString::number( 3 ), 80 ).toInt() );
496 head->resizeSection( 4, settings.value( path + QString::number( 4 ), 50 ).toInt() );
497 head->resizeSection( 5, settings.value( path + QString::number( 5 ), 50 ).toInt() );
498}
499
501{
502 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
503 QgsDebugMsgLevel( QString::number( indexlist.count() ), 2 );
504
505 if ( indexlist.isEmpty() )
506 return;
507
508 QMimeData *mime = mModel->mimeData( indexlist );
509 QApplication::clipboard()->setMimeData( mime );
510}
511
513{
514 const QMimeData *mime = QApplication::clipboard()->mimeData();
515 QModelIndexList indexlist = viewRules->selectionModel()->selectedRows();
516 QModelIndex index;
517 if ( indexlist.isEmpty() )
518 index = mModel->index( mModel->rowCount(), 0 );
519 else
520 index = indexlist.first();
521 mModel->dropMimeData( mime, Qt::CopyAction, index.row(), index.column(), index.parent() );
522}
523
525{
526 std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
527 if ( !tempSymbol )
528 return;
529
530 const QModelIndexList indexList = viewRules->selectionModel()->selectedRows();
531 for ( const QModelIndex &index : indexList )
532 {
533 if ( QgsRuleBasedRenderer::Rule *rule = mModel->ruleForIndex( index ) )
534 {
535 if ( !rule->symbol() || rule->symbol()->type() != tempSymbol->type() )
536 continue;
537
538 mModel->setSymbol( index, tempSymbol->clone() );
539 }
540 }
541 emit widgetChanged();
542}
543
544void QgsRuleBasedRendererWidget::refineRuleCategoriesAccepted( QgsPanelWidget *panel )
545{
546 QgsCategorizedSymbolRendererWidget *w = qobject_cast<QgsCategorizedSymbolRendererWidget *>( panel );
547
548 // create new rules
550 QModelIndexList indexList = viewRules->selectionModel()->selectedRows();
551 const auto constIndexList = indexList;
552 for ( const QModelIndex &index : constIndexList )
553 {
554 QgsRuleBasedRenderer::Rule *initialRule = mModel->ruleForIndex( index );
555 mModel->willAddRules( index, r->categories().count() );
557 }
559}
560
561void QgsRuleBasedRendererWidget::refineRuleRangesAccepted( QgsPanelWidget *panel )
562{
563 QgsGraduatedSymbolRendererWidget *w = qobject_cast<QgsGraduatedSymbolRendererWidget *>( panel );
564 // create new rules
566 QModelIndexList indexList = viewRules->selectionModel()->selectedRows();
567 const auto constIndexList = indexList;
568 for ( const QModelIndex &index : constIndexList )
569 {
570 QgsRuleBasedRenderer::Rule *initialRule = mModel->ruleForIndex( index );
571 mModel->willAddRules( index, r->ranges().count() );
573 }
575}
576
577void QgsRuleBasedRendererWidget::ruleWidgetPanelAccepted( QgsPanelWidget *panel )
578{
579 QgsRendererRulePropsWidget *widget = qobject_cast<QgsRendererRulePropsWidget *>( panel );
580 if ( !widget )
581 return;
582
583 widget->apply();
584
585 // model should know about the change and emit dataChanged signal for the view
586 QModelIndex index = viewRules->selectionModel()->currentIndex();
587 mModel->updateRule( index.parent(), index.row() );
589}
590
591void QgsRuleBasedRendererWidget::liveUpdateRuleFromPanel()
592{
593 ruleWidgetPanelAccepted( qobject_cast<QgsPanelWidget *>( sender() ) );
594}
595
596void QgsRuleBasedRendererWidget::showContextMenu( QPoint )
597{
598 mContextMenu->clear();
599 mContextMenu->addAction( mCopyAction );
600 mContextMenu->addAction( mPasteAction );
601
602 const QList< QAction * > actions = contextMenu->actions();
603 for ( QAction *act : actions )
604 {
605 mContextMenu->addAction( act );
606 }
607
608 mContextMenu->addMenu( mRefineMenu );
609
610 mContextMenu->exec( QCursor::pos() );
611}
612
613
615{
616 if ( !mLayer || !mRenderer || !mRenderer->rootRule() )
617 {
618 return;
619 }
620 QHash<QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount> countMap;
621
622 QgsRuleBasedRenderer::RuleList ruleList = mRenderer->rootRule()->descendants();
623 // insert all so that we have counts 0
624 const auto constRuleList = ruleList;
625 for ( QgsRuleBasedRenderer::Rule *rule : constRuleList )
626 {
627 countMap[rule].count = 0;
628 countMap[rule].duplicateCount = 0;
629 }
630
631 QgsRenderContext renderContext;
632 renderContext.setRendererScale( 0 ); // ignore scale
633
635
636 // additional scopes
637 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
638 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
639 {
640 context.appendScope( new QgsExpressionContextScope( scope ) );
641 }
642
643 renderContext.setExpressionContext( context );
644
645 mRenderer->startRender( renderContext, mLayer->fields() );
646 // QgsRuleBasedRenderer::filter must be called after startRender
649 req.setSubsetOfAttributes( mRenderer->usedAttributes( renderContext ), mLayer->fields() );
651
652 long long nFeatures = mLayer->featureCount();
653 QProgressDialog p( tr( "Calculating feature count." ), tr( "Abort" ), 0, 100 );
654 p.setWindowModality( Qt::WindowModal );
655 long long featuresCounted = 0;
656
657 QgsFeature f;
658 while ( fit.nextFeature( f ) )
659 {
660 renderContext.expressionContext().setFeature( f );
661 QgsRuleBasedRenderer::RuleList featureRuleList = mRenderer->rootRule()->rulesForFeature( f, &renderContext );
662
663 const auto constFeatureRuleList = featureRuleList;
664 for ( QgsRuleBasedRenderer::Rule *rule : constFeatureRuleList )
665 {
666 countMap[rule].count++;
667 if ( featureRuleList.size() > 1 )
668 {
669 countMap[rule].duplicateCount++;
670 }
671 const auto constFeatureRuleList = featureRuleList;
672 for ( QgsRuleBasedRenderer::Rule *duplicateRule : constFeatureRuleList )
673 {
674 if ( duplicateRule == rule ) continue;
675 countMap[rule].duplicateCountMap[duplicateRule] += 1;
676 }
677 }
678 ++featuresCounted;
679 if ( featuresCounted % 50 == 0 )
680 {
681 if ( featuresCounted > nFeatures ) //sometimes the feature count is not correct
682 {
683 p.setMaximum( 0 );
684 }
685 p.setValue( static_cast< double >( featuresCounted ) / nFeatures * 100.0 );
686 if ( p.wasCanceled() )
687 {
688 return;
689 }
690 }
691 }
692 p.setValue( nFeatures );
693
694 mRenderer->stopRender( renderContext );
695
696#ifdef QGISDEBUG
697 const auto constKeys = countMap.keys();
698 for ( QgsRuleBasedRenderer::Rule *rule : constKeys )
699 {
700 QgsDebugMsgLevel( QStringLiteral( "rule: %1 count %2" ).arg( rule->label() ).arg( countMap[rule].count ), 2 );
701 }
702#endif
703
704 mModel->setFeatureCounts( countMap );
705}
706
708{
709 bool enabled = !viewRules->selectionModel()->selectedIndexes().isEmpty();
710 btnRefineRule->setEnabled( enabled );
711 btnRemoveRule->setEnabled( enabled );
712}
713
715
717 : QgsPanelWidget( parent )
718 , mRule( rule )
719 , mLayer( layer )
720 , mContext( context )
721{
722 setupUi( this );
723 layout()->setContentsMargins( 0, 0, 0, 0 );
724
725 mElseRadio->setChecked( mRule->isElse() );
726 mFilterRadio->setChecked( !mRule->isElse() );
727 editFilter->setText( mRule->filterExpression() );
728 editFilter->setToolTip( mRule->filterExpression() );
729 editLabel->setText( mRule->label() );
730 editDescription->setText( mRule->description() );
731 editDescription->setToolTip( mRule->description() );
732
733 if ( mRule->dependsOnScale() )
734 {
735 groupScale->setChecked( true );
736 mScaleRangeWidget->setScaleRange( std::max( rule->minimumScale(), 0.0 ),
737 std::max( rule->maximumScale(), 0.0 ) );
738 }
739 mScaleRangeWidget->setMapCanvas( mContext.mapCanvas() );
740
741 if ( mRule->symbol() )
742 {
743 groupSymbol->setChecked( true );
744 mSymbol = mRule->symbol()->clone(); // use a clone!
745 }
746 else
747 {
748 groupSymbol->setChecked( false );
750 }
751
756
757 QVBoxLayout *l = new QVBoxLayout;
758 l->addWidget( mSymbolSelector );
759 groupSymbol->setLayout( l );
760
761 connect( btnExpressionBuilder, &QAbstractButton::clicked, this, &QgsRendererRulePropsWidget::buildExpression );
762 connect( btnTestFilter, &QAbstractButton::clicked, this, &QgsRendererRulePropsWidget::testFilter );
763 connect( editFilter, &QLineEdit::textChanged, this, &QgsPanelWidget::widgetChanged );
764 connect( editLabel, &QLineEdit::editingFinished, this, &QgsPanelWidget::widgetChanged );
765 connect( editDescription, &QLineEdit::editingFinished, this, &QgsPanelWidget::widgetChanged );
766 connect( groupSymbol, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
767 connect( groupScale, &QGroupBox::toggled, this, &QgsPanelWidget::widgetChanged );
768 connect( mScaleRangeWidget, &QgsScaleRangeWidget::rangeChanged, this, &QgsPanelWidget::widgetChanged );
769 connect( mFilterRadio, &QRadioButton::toggled, this, [ = ]( bool toggled ) { filterFrame->setEnabled( toggled ) ; } );
770 connect( mElseRadio, &QRadioButton::toggled, this, [ = ]( bool toggled ) { if ( toggled ) editFilter->setText( QStringLiteral( "ELSE" ) );} );
771}
772
773#include "qgsvscrollarea.h"
774
776 : QDialog( parent )
777{
778
779#ifdef Q_OS_MAC
780 setWindowModality( Qt::WindowModal );
781#endif
782
783 QVBoxLayout *layout = new QVBoxLayout( this );
784 QgsVScrollArea *scrollArea = new QgsVScrollArea( this );
785 scrollArea->setFrameShape( QFrame::NoFrame );
786 layout->addWidget( scrollArea );
787
788 buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
789 mPropsWidget = new QgsRendererRulePropsWidget( rule, layer, style, this, context );
790
791 scrollArea->setWidget( mPropsWidget );
792 layout->addWidget( buttonBox );
793 this->setWindowTitle( "Edit Rule" );
795
796 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsRendererRulePropsDialog::accept );
797 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
798 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsRendererRulePropsDialog::showHelp );
799}
800
802{
803 mPropsWidget->testFilter();
804}
805
810
812{
813 mPropsWidget->apply();
814 QDialog::accept();
815}
816
817void QgsRendererRulePropsDialog::showHelp()
818{
819 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#rule-based-rendering" ) );
820}
821
822
824{
826
827 // additional scopes
828 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
829 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
830 {
831 context.appendScope( new QgsExpressionContextScope( scope ) );
832 }
833
834 QgsExpressionBuilderDialog dlg( mLayer, editFilter->text(), this, QStringLiteral( "generic" ), context );
835
836 if ( dlg.exec() )
837 editFilter->setText( dlg.expressionText() );
838}
839
841{
842 if ( !mFilterRadio->isChecked() )
843 return;
844
845 QgsExpression filter( editFilter->text() );
846 if ( filter.hasParserError() )
847 {
848 QMessageBox::critical( this, tr( "Test Filter" ), tr( "Filter expression parsing error:\n" ) + filter.parserErrorString() );
849 return;
850 }
851
853
854 // additional scopes
855 const auto constAdditionalExpressionContextScopes = mContext.additionalExpressionContextScopes();
856 for ( const QgsExpressionContextScope &scope : constAdditionalExpressionContextScopes )
857 {
858 context.appendScope( new QgsExpressionContextScope( scope ) );
859 }
860
861 if ( !filter.prepare( &context ) )
862 {
863 QMessageBox::critical( this, tr( "Test Filter" ), filter.evalErrorString() );
864 return;
865 }
866
867 QApplication::setOverrideCursor( Qt::WaitCursor );
868
871 .setFilterExpression( editFilter->text() )
872 .setExpressionContext( context );
873
875
876 int count = 0;
877 QgsFeature f;
878 while ( fit.nextFeature( f ) )
879 {
880 count++;
881 }
882
883 QApplication::restoreOverrideCursor();
884
885 QMessageBox::information( this, tr( "Test Filter" ), tr( "Filter returned %n feature(s)", "number of filtered features", count ) );
886}
887
889{
890 QString filter = mElseRadio->isChecked() ? QStringLiteral( "ELSE" ) : editFilter->text();
891 mRule->setFilterExpression( filter );
892 mRule->setLabel( editLabel->text() );
893 mRule->setDescription( editDescription->text() );
894 // caution: rule uses scale denom, scale widget uses true scales
895 mRule->setMinimumScale( groupScale->isChecked() ? mScaleRangeWidget->minimumScale() : 0 );
896 mRule->setMaximumScale( groupScale->isChecked() ? mScaleRangeWidget->maximumScale() : 0 );
897 mRule->setSymbol( groupSymbol->isChecked() ? mSymbol->clone() : nullptr );
898}
899
905
907
908/*
909 setDragEnabled(true);
910 viewport()->setAcceptDrops(true);
911 setDropIndicatorShown(true);
912 setDragDropMode(QAbstractItemView::InternalMove);
913*/
914
916
918 : QAbstractItemModel( parent )
919 , mR( renderer )
920 , mScreen( screen )
921{
922}
923
924Qt::ItemFlags QgsRuleBasedRendererModel::flags( const QModelIndex &index ) const
925{
926 if ( !index.isValid() )
927 return Qt::ItemIsDropEnabled;
928
929 // allow drop only at first column
930 Qt::ItemFlag drop = ( index.column() == 0 ? Qt::ItemIsDropEnabled : Qt::NoItemFlags );
931
932 Qt::ItemFlag checkable = ( index.column() == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags );
933
934 return Qt::ItemIsEnabled | Qt::ItemIsSelectable |
935 Qt::ItemIsEditable | checkable |
936 Qt::ItemIsDragEnabled | drop;
937}
938
939QVariant QgsRuleBasedRendererModel::data( const QModelIndex &index, int role ) const
940{
941 if ( !index.isValid() )
942 return QVariant();
943
945
946 if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
947 {
948 switch ( index.column() )
949 {
950 case 0:
951 return rule->label();
952 case 1:
953 if ( rule->isElse() )
954 {
955 return "ELSE";
956 }
957 else
958 {
959 return rule->filterExpression().isEmpty() ? tr( "(no filter)" ) : rule->filterExpression();
960 }
961 case 2:
962 return rule->dependsOnScale() ? QgsScaleComboBox::toString( rule->minimumScale() ) : QVariant();
963 case 3:
964 return rule->dependsOnScale() ? QgsScaleComboBox::toString( rule->maximumScale() ) : QVariant();
965 case 4:
966 if ( mFeatureCountMap.count( rule ) == 1 )
967 {
968 return QVariant( mFeatureCountMap[rule].count );
969 }
970 return QVariant();
971 case 5:
972 if ( mFeatureCountMap.count( rule ) == 1 )
973 {
974 if ( role == Qt::DisplayRole )
975 {
976 return QVariant( mFeatureCountMap[rule].duplicateCount );
977 }
978 else // tooltip - detailed info about duplicates
979 {
980 if ( mFeatureCountMap[rule].duplicateCount > 0 )
981 {
982 QString tip = QStringLiteral( "<p style='margin:0px;'><ul>" );
983 const auto duplicateMap = mFeatureCountMap[rule].duplicateCountMap;
984 for ( auto it = duplicateMap.constBegin(); it != duplicateMap.constEnd(); ++it )
985 {
986 QString label = it.key()->label().replace( '&', QLatin1String( "&amp;" ) ).replace( '>', QLatin1String( "&gt;" ) ).replace( '<', QLatin1String( "&lt;" ) );
987 tip += tr( "<li><nobr>%1 features also in rule %2</nobr></li>" ).arg( it.value() ).arg( label );
988 }
989 tip += QLatin1String( "</ul>" );
990 return tip;
991 }
992 else
993 {
994 return 0;
995 }
996 }
997 }
998 return QVariant();
999 default:
1000 return QVariant();
1001 }
1002 }
1003 else if ( role == Qt::DecorationRole && index.column() == 0 && rule->symbol() )
1004 {
1005 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
1006 return QgsSymbolLayerUtils::symbolPreviewIcon( rule->symbol(), QSize( iconSize, iconSize ), 0, nullptr, QgsScreenProperties( mScreen.data() ) );
1007 }
1008 else if ( role == Qt::TextAlignmentRole )
1009 {
1010 return ( index.column() == 2 || index.column() == 3 ) ? static_cast<Qt::Alignment::Int>( Qt::AlignRight ) : static_cast<Qt::Alignment::Int>( Qt::AlignLeft );
1011 }
1012 else if ( role == Qt::FontRole && index.column() == 1 )
1013 {
1014 if ( rule->isElse() )
1015 {
1016 QFont italicFont;
1017 italicFont.setItalic( true );
1018 return italicFont;
1019 }
1020 return QVariant();
1021 }
1022 else if ( role == Qt::EditRole )
1023 {
1024 switch ( index.column() )
1025 {
1026 case 0:
1027 return rule->label();
1028 case 1:
1029 return rule->filterExpression();
1030 case 2:
1031 return rule->minimumScale();
1032 case 3:
1033 return rule->maximumScale();
1034 default:
1035 return QVariant();
1036 }
1037 }
1038 else if ( role == Qt::CheckStateRole )
1039 {
1040 if ( index.column() != 0 )
1041 return QVariant();
1042 return rule->active() ? Qt::Checked : Qt::Unchecked;
1043 }
1044 else
1045 return QVariant();
1046}
1047
1048QVariant QgsRuleBasedRendererModel::headerData( int section, Qt::Orientation orientation, int role ) const
1049{
1050 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 7 )
1051 {
1052 QStringList lst;
1053 lst << tr( "Label" ) << tr( "Rule" ) << tr( "Min. Scale" ) << tr( "Max. Scale" ) << tr( "Count" ) << tr( "Duplicate Count" );
1054 return lst[section];
1055 }
1056 else if ( orientation == Qt::Horizontal && role == Qt::ToolTipRole )
1057 {
1058 if ( section == 4 ) // Count
1059 {
1060 return tr( "Number of features in this rule." );
1061 }
1062 else if ( section == 5 ) // Duplicate count
1063 {
1064 return tr( "Number of features in this rule which are also present in other rule(s)." );
1065 }
1066 }
1067
1068 return QVariant();
1069}
1070
1071int QgsRuleBasedRendererModel::rowCount( const QModelIndex &parent ) const
1072{
1073 if ( parent.column() > 0 )
1074 return 0;
1075
1077
1078 return parentRule->children().count();
1079}
1080
1081int QgsRuleBasedRendererModel::columnCount( const QModelIndex & ) const
1082{
1083 return 6;
1084}
1085
1086QModelIndex QgsRuleBasedRendererModel::index( int row, int column, const QModelIndex &parent ) const
1087{
1088 if ( hasIndex( row, column, parent ) )
1089 {
1091 QgsRuleBasedRenderer::Rule *childRule = parentRule->children()[row];
1092 return createIndex( row, column, childRule );
1093 }
1094 return QModelIndex();
1095}
1096
1097QModelIndex QgsRuleBasedRendererModel::parent( const QModelIndex &index ) const
1098{
1099 if ( !index.isValid() )
1100 return QModelIndex();
1101
1103 QgsRuleBasedRenderer::Rule *parentRule = childRule->parent();
1104
1105 if ( parentRule == mR->rootRule() )
1106 return QModelIndex();
1107
1108 // this is right: we need to know row number of our parent (in our grandparent)
1109 int row = parentRule->parent()->children().indexOf( parentRule );
1110
1111 return createIndex( row, 0, parentRule );
1112}
1113
1114bool QgsRuleBasedRendererModel::setData( const QModelIndex &index, const QVariant &value, int role )
1115{
1116 if ( !index.isValid() )
1117 return false;
1118
1120
1121 if ( role == Qt::CheckStateRole )
1122 {
1123 rule->setActive( value.toInt() == Qt::Checked );
1124 emit dataChanged( index, index );
1125 return true;
1126 }
1127
1128 if ( role != Qt::EditRole )
1129 return false;
1130
1131 switch ( index.column() )
1132 {
1133 case 0: // label
1134 rule->setLabel( value.toString() );
1135 break;
1136 case 1: // filter
1137 rule->setFilterExpression( value.toString() );
1138 break;
1139 case 2: // scale min
1140 rule->setMinimumScale( value.toDouble() );
1141 break;
1142 case 3: // scale max
1143 rule->setMaximumScale( value.toDouble() );
1144 break;
1145 default:
1146 return false;
1147 }
1148
1149 emit dataChanged( index, index );
1150 return true;
1151}
1152
1154{
1155 return Qt::MoveAction; // | Qt::CopyAction
1156}
1157
1159{
1160 QStringList types;
1161 types << QStringLiteral( "application/vnd.text.list" );
1162 return types;
1163}
1164
1165QMimeData *QgsRuleBasedRendererModel::mimeData( const QModelIndexList &indexes ) const
1166{
1167 QMimeData *mimeData = new QMimeData();
1168 QByteArray encodedData;
1169
1170 QDataStream stream( &encodedData, QIODevice::WriteOnly );
1171
1172 const auto constIndexes = indexes;
1173 for ( const QModelIndex &index : constIndexes )
1174 {
1175 // each item consists of several columns - let's add it with just first one
1176 if ( !index.isValid() || index.column() != 0 )
1177 continue;
1178
1179 // we use a clone of the existing rule because it has a new unique rule key
1180 // non-unique rule keys would confuse other components using them (e.g. legend)
1182 QDomDocument doc;
1183 QgsSymbolMap symbols;
1184
1185 QDomElement rootElem = doc.createElement( QStringLiteral( "rule_mime" ) );
1186 rootElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "renderer" ) ); // for determining whether rules are from renderer or labeling
1187 QDomElement rulesElem = rule->save( doc, symbols );
1188 rootElem.appendChild( rulesElem );
1189 QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( symbols, QStringLiteral( "symbols" ), doc, QgsReadWriteContext() );
1190 rootElem.appendChild( symbolsElem );
1191 doc.appendChild( rootElem );
1192
1193 delete rule;
1194
1195 stream << doc.toString( -1 );
1196 }
1197
1198 mimeData->setData( QStringLiteral( "application/vnd.text.list" ), encodedData );
1199 return mimeData;
1200}
1201
1202
1203// manipulate DOM before dropping it so that rules are more useful
1204void _labeling2rendererRules( QDomElement &ruleElem )
1205{
1206 // labeling rules recognize only "description"
1207 if ( ruleElem.hasAttribute( QStringLiteral( "description" ) ) )
1208 ruleElem.setAttribute( QStringLiteral( "label" ), ruleElem.attribute( QStringLiteral( "description" ) ) );
1209
1210 // run recursively
1211 QDomElement childRuleElem = ruleElem.firstChildElement( QStringLiteral( "rule" ) );
1212 while ( !childRuleElem.isNull() )
1213 {
1214 _labeling2rendererRules( childRuleElem );
1215 childRuleElem = childRuleElem.nextSiblingElement( QStringLiteral( "rule" ) );
1216 }
1217}
1218
1219
1221 Qt::DropAction action, int row, int column, const QModelIndex &parent )
1222{
1223 Q_UNUSED( column )
1224
1225 if ( action == Qt::IgnoreAction )
1226 return true;
1227
1228 if ( !data->hasFormat( QStringLiteral( "application/vnd.text.list" ) ) )
1229 return false;
1230
1231 if ( parent.column() > 0 )
1232 return false;
1233
1234 QByteArray encodedData = data->data( QStringLiteral( "application/vnd.text.list" ) );
1235 QDataStream stream( &encodedData, QIODevice::ReadOnly );
1236 int rows = 0;
1237
1238 if ( row == -1 )
1239 {
1240 // the item was dropped at a parent - we may decide where to put the items - let's append them
1241 row = rowCount( parent );
1242 }
1243
1244 while ( !stream.atEnd() )
1245 {
1246 QString text;
1247 stream >> text;
1248
1249 QDomDocument doc;
1250 if ( !doc.setContent( text ) )
1251 continue;
1252 QDomElement rootElem = doc.documentElement();
1253 if ( rootElem.tagName() != QLatin1String( "rule_mime" ) )
1254 continue;
1255 if ( rootElem.attribute( QStringLiteral( "type" ) ) == QLatin1String( "labeling" ) )
1256 rootElem.appendChild( doc.createElement( QStringLiteral( "symbols" ) ) );
1257 QDomElement symbolsElem = rootElem.firstChildElement( QStringLiteral( "symbols" ) );
1258 if ( symbolsElem.isNull() )
1259 continue;
1261 QDomElement ruleElem = rootElem.firstChildElement( QStringLiteral( "rule" ) );
1262 if ( rootElem.attribute( QStringLiteral( "type" ) ) == QLatin1String( "labeling" ) )
1263 _labeling2rendererRules( ruleElem );
1264 QgsRuleBasedRenderer::Rule *rule = QgsRuleBasedRenderer::Rule::create( ruleElem, symbolMap, false );
1265
1266 insertRule( parent, row + rows, rule );
1267
1268 ++rows;
1269 }
1270 return true;
1271}
1272
1274{
1275 if ( index.isValid() )
1276 return static_cast<QgsRuleBasedRenderer::Rule *>( index.internalPointer() );
1277 return mR->rootRule();
1278}
1279
1280bool QgsRuleBasedRendererModel::removeRows( int row, int count, const QModelIndex &parent )
1281{
1283
1284 if ( row < 0 || row >= parentRule->children().count() )
1285 return false;
1286
1287 QgsDebugMsgLevel( QStringLiteral( "Called: row %1 count %2 parent ~~%3~~" ).arg( row ).arg( count ).arg( parentRule->dump() ), 2 );
1288
1289 beginRemoveRows( parent, row, row + count - 1 );
1290
1291 for ( int i = 0; i < count; i++ )
1292 {
1293 if ( row < parentRule->children().count() )
1294 {
1295 //QgsRuleBasedRenderer::Rule* r = parentRule->children()[row];
1296 parentRule->removeChildAt( row );
1297 //parentRule->takeChildAt( row );
1298 }
1299 else
1300 {
1301 QgsDebugError( QStringLiteral( "trying to remove invalid index - this should not happen!" ) );
1302 }
1303 }
1304
1305 endRemoveRows();
1306
1307 return true;
1308}
1309
1310
1311void QgsRuleBasedRendererModel::insertRule( const QModelIndex &parent, int before, QgsRuleBasedRenderer::Rule *newrule )
1312{
1313 beginInsertRows( parent, before, before );
1314
1315 QgsDebugMsgLevel( QStringLiteral( "insert before %1 rule: %2" ).arg( before ).arg( newrule->dump() ), 2 );
1316
1318 parentRule->insertChild( before, newrule );
1319
1320 endInsertRows();
1321}
1322
1323void QgsRuleBasedRendererModel::updateRule( const QModelIndex &parent, int row )
1324{
1325 emit dataChanged( index( row, 0, parent ),
1326 index( row, columnCount( parent ), parent ) );
1327}
1328
1329void QgsRuleBasedRendererModel::updateRule( const QModelIndex &idx )
1330{
1331 emit dataChanged( index( 0, 0, idx ),
1332 index( rowCount( idx ) - 1, columnCount( idx ) - 1, idx ) );
1333
1334 for ( int i = 0; i < rowCount( idx ); i++ )
1335 {
1336 updateRule( index( i, 0, idx ) );
1337 }
1338}
1339
1340
1341void QgsRuleBasedRendererModel::removeRule( const QModelIndex &index )
1342{
1343 if ( !index.isValid() )
1344 return;
1345
1346 beginRemoveRows( index.parent(), index.row(), index.row() );
1347
1349 rule->parent()->removeChild( rule );
1350
1351 endRemoveRows();
1352}
1353
1354void QgsRuleBasedRendererModel::setSymbol( const QModelIndex &index, QgsSymbol *symbol )
1355{
1357 rule->setSymbol( symbol );
1358 emit dataChanged( index, index );
1359}
1360
1361void QgsRuleBasedRendererModel::willAddRules( const QModelIndex &parent, int count )
1362{
1363 int row = rowCount( parent ); // only consider appending
1364 beginInsertRows( parent, row, row + count - 1 );
1365}
1366
1368{
1369 endInsertRows();
1370}
1371
1372void QgsRuleBasedRendererModel::setFeatureCounts( const QHash<QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount> &countMap )
1373{
1374 mFeatureCountMap = countMap;
1375 updateRule( QModelIndex() );
1376}
1377
1379{
1380 mFeatureCountMap.clear();
1381 updateRule( QModelIndex() );
1382}
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
const QgsCategoryList & categories() const
Returns a list of all categories recognized by the renderer.
A generic dialog for building expression strings.
Single scope for storing variables and functions for use within a QgsExpressionContext.
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 setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QString evalErrorString() const
Returns evaluation error.
QString parserErrorString() const
Returns parser error.
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.
Abstract base class for all 2D vector feature renderers.
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setContext(const QgsSymbolWidgetContext &context) override
Sets the context in which the renderer widget is shown, e.g., the associated map canvas and expressio...
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
A vector feature renderer which uses numeric attributes to classify features into different ranges.
const QgsRangeList & ranges() const
Returns a list of all ranges used in the classification.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:208
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Base class for any widget that can be shown as a inline panel.
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
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.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
bool dockMode()
Returns the dock mode state.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setRendererScale(double scale)
Sets the renderer map scale.
QgsRuleBasedRenderer::Rule * rule()
QgsRendererRulePropsDialog(QgsRuleBasedRenderer::Rule *rule, QgsVectorLayer *layer, QgsStyle *style, QWidget *parent=nullptr, const QgsSymbolWidgetContext &context=QgsSymbolWidgetContext())
Constructor for QgsRendererRulePropsDialog.
void buildExpression()
Open the expression builder widget to check if the.
void setDockMode(bool dockMode) override
Set the widget in dock mode.
QgsRendererRulePropsWidget(QgsRuleBasedRenderer::Rule *rule, QgsVectorLayer *layer, QgsStyle *style, QWidget *parent=nullptr, const QgsSymbolWidgetContext &context=QgsSymbolWidgetContext())
Widget to edit the details of a rule based renderer rule.
QgsSymbolSelectorWidget * mSymbolSelector
void apply()
Apply any changes from the widget to the set rule.
QgsRuleBasedRenderer::Rule * mRule
void testFilter()
Test the filter that is set in the widget.
QgsRuleBasedRenderer::Rule * rule()
Returns the current set rule.
Base class for renderer settings widgets.
QAction * mPasteSymbolAction
Paste symbol action.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
QgsSymbolWidgetContext mContext
Context in which widget is shown.
QgsSymbolWidgetContext context() const
Returns the context in which the renderer widget is shown, e.g., the associated map canvas and expres...
QgsVectorLayer * mLayer
QgsRuleBasedRenderer::Rule * ruleForIndex(const QModelIndex &index) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void updateRule(const QModelIndex &parent, int row)
void willAddRules(const QModelIndex &parent, int count)
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setSymbol(const QModelIndex &index, QgsSymbol *symbol)
Sets the symbol for the rule at the specified index.
QStringList mimeTypes() const override
void removeRule(const QModelIndex &index)
int columnCount(const QModelIndex &=QModelIndex()) const override
Qt::DropActions supportedDropActions() const override
QgsRuleBasedRendererModel(QgsRuleBasedRenderer *renderer, QObject *parent, QScreen *screen=nullptr)
Constructor for QgsRuleBasedRendererModel, for the specified renderer.
void setFeatureCounts(const QHash< QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount > &countMap)
QModelIndex parent(const QModelIndex &index) const override
QHash< QgsRuleBasedRenderer::Rule *, QgsRuleBasedRendererCount > mFeatureCountMap
QMimeData * mimeData(const QModelIndexList &indexes) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void insertRule(const QModelIndex &parent, int before, QgsRuleBasedRenderer::Rule *newrule)
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void refineRuleCategoriesGui()
Opens the dialog for refining a rule using categories.
QgsRuleBasedRenderer::RuleList selectedRules()
void refineRuleScalesGui(const QModelIndexList &index)
void refineRuleRangesGui()
Opens the dialog for refining a rule using ranges.
static QgsRendererWidget * create(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
QList< QgsSymbol * > selectedSymbols() override
Subclasses may provide the capability of changing multiple symbols at once by implementing the follow...
QgsRuleBasedRendererWidget(QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer)
QgsRuleBasedRenderer::RuleList mCopyBuffer
void currentRuleChanged(const QModelIndex &current=QModelIndex(), const QModelIndex &previous=QModelIndex())
QgsRuleBasedRendererModel * mModel
void setSymbolLevels(const QList< QgsLegendSymbolItem > &levels, bool enabled) override
Sets the symbol levels for the renderer defined in the widget.
std::unique_ptr< QgsRuleBasedRenderer > mRenderer
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
QgsFeatureRenderer * renderer() override
Returns pointer to the renderer (no transfer of ownership)
void keyPressEvent(QKeyEvent *event) override
QgsRuleBasedRenderer::Rule * currentRule()
void saveSectionWidth(int section, int oldSize, int newSize)
This class keeps data about a rules for rule-based renderer.
void setSymbol(QgsSymbol *sym)
Sets a new symbol (or nullptr). Deletes old symbol.
void removeChild(QgsRuleBasedRenderer::Rule *rule)
delete child rule
void insertChild(int i, QgsRuleBasedRenderer::Rule *rule)
add child rule, take ownership, sets this as parent
void setDescription(const QString &description)
Set a human readable description for this rule.
const QgsRuleBasedRenderer::RuleList & children() const
Returns all children rules of this rule.
double maximumScale() const
Returns the maximum map scale (i.e.
QgsRuleBasedRenderer::Rule * parent()
The parent rule.
bool isElse() const
Check if this rule is an ELSE rule.
QgsRuleBasedRenderer::Rule * clone() const
clone this rule, return new instance
void removeChildAt(int i)
delete child rule
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
void setActive(bool state)
Sets if this rule is active.
QDomElement save(QDomDocument &doc, QgsSymbolMap &symbolMap) const
double minimumScale() const
Returns the minimum map scale (i.e.
QString description() const
A human readable description for this rule.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
void setLabel(const QString &label)
QString dump(int indent=0) const
Dump for debug purpose.
static QgsRuleBasedRenderer::Rule * create(QDomElement &ruleElem, QgsSymbolMap &symbolMap, bool reuseId=true)
Create a rule from an XML definition.
QString filterExpression() const
A filter that will check if this rule applies.
bool active() const
Returns if this rule is active.
Rule based renderer.
static void refineRuleCategories(QgsRuleBasedRenderer::Rule *initialRule, QgsCategorizedSymbolRenderer *r)
take a rule and create a list of new rules based on the categories from categorized symbol renderer
static void refineRuleRanges(QgsRuleBasedRenderer::Rule *initialRule, QgsGraduatedSymbolRenderer *r)
take a rule and create a list of new rules based on the ranges from graduated symbol renderer
static QgsRuleBasedRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer, QgsVectorLayer *layer=nullptr)
Creates a new QgsRuleBasedRenderer from an existing renderer.
static void refineRuleScales(QgsRuleBasedRenderer::Rule *initialRule, QList< int > scales)
take a rule and create a list of new rules with intervals of scales given by the passed scale denomin...
QList< QgsRuleBasedRenderer::Rule * > RuleList
QgsRuleBasedRenderer::Rule * rootRule()
static QString toString(double scale)
Helper function to convert a scale double to scale string.
void rangeChanged(double min, double max)
Emitted when the scale range set in the widget is changed.
Stores properties relating to a screen.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsSymbol * symbolFromMimeData(const QMimeData *data)
Attempts to parse mime data as a symbol.
static QgsSymbolMap loadSymbols(QDomElement &element, const QgsReadWriteContext &context)
Reads a collection of symbols from XML and returns them in a map. Caller is responsible for deleting ...
static QDomElement saveSymbols(QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a collection of symbols to XML with specified tagName for the top-level element.
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.
A dialog which allows the user to modify the rendering order of symbol layers.
QgsLegendSymbolList symbolLevels() const
Returns the current legend symbols with rendering passes set, as defined in the widget.
bool usingLevels() const
Returns whether the level ordering is enabled.
void setForceOrderingEnabled(bool enabled)
A widget which allows the user to modify the rendering order of symbol layers.
void setForceOrderingEnabled(bool enabled)
Sets whether the level ordering is always forced on and hide the checkbox (used by rule-based rendere...
bool usingLevels() const
Returns whether the level ordering is enabled.
QgsLegendSymbolList symbolLevels() const
Returns the current legend symbols with rendering passes set, as defined in the widget.
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 ...
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.
QList< QgsExpressionContextScope * > globalProjectAtlasMapLayerScopes(const QgsMapLayer *layer) const
Returns list of scopes: global, project, atlas, map, layer.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the widget.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
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:352
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
QgsVScrollArea is a QScrollArea subclass which only displays a vertical scrollbar and fits the width ...
Represents a vector layer which manages a vector based data sets.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition qgsrenderer.h:48
void _labeling2rendererRules(QDomElement &ruleElem)