Skip to content

Commit 43f150d

Browse files
committed
Allow diagrams to be disabled without losing settings (fix #11288)
1 parent 1340677 commit 43f150d

6 files changed

+157
-151
lines changed

python/core/qgsdiagramrendererv2.sip

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class QgsDiagramSettings
7070
};
7171

7272
QgsDiagramSettings();
73+
bool enabled;
7374
QFont font;
7475
QList< QColor > categoryColors;
7576
QList< QString > categoryAttributes;

src/app/qgsdiagramproperties.cpp

+141-148
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,14 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer, QWidget* pare
183183
}
184184
else // already a diagram renderer present
185185
{
186-
mDisplayDiagramsGroupBox->setChecked( true );
187-
188186
//single category renderer or interpolated one?
189187
mFixedSizeCheckBox->setChecked( dr->rendererName() == "SingleCategory" );
190188

191189
//assume single category or linearly interpolated diagram renderer for now
192190
QList<QgsDiagramSettings> settingList = dr->diagramSettings();
193191
if ( settingList.size() > 0 )
194192
{
193+
mDisplayDiagramsGroupBox->setChecked( settingList.at( 0 ).enabled );
195194
mDiagramFont = settingList.at( 0 ).font;
196195
QSizeF size = settingList.at( 0 ).size;
197196
mBackgroundColorButton->setColor( settingList.at( 0 ).backgroundColor );
@@ -520,184 +519,178 @@ void QgsDiagramProperties::apply()
520519
QSettings().setValue( "/Windows/VectorLayerProperties/diagram/tab",
521520
mDiagramPropertiesTabWidget->currentIndex() );
522521

523-
if ( !mDisplayDiagramsGroupBox->isChecked() )
522+
QgsDiagram* diagram = 0;
523+
int index = mDiagramTypeComboBox->currentIndex();
524+
QString diagramType = mDiagramTypeComboBox->itemData( index ).toString();
525+
526+
if ( 0 == mDiagramAttributesTreeWidget->topLevelItemCount() )
524527
{
525-
mLayer->setDiagramRenderer( 0 );
528+
QgisApp::instance()->messageBar()->pushMessage(
529+
tr( "Diagrams: No attributes added." ),
530+
tr( "You did not add any attributes to this diagram layer. Please specify the attributes to visualize on the diagrams or disable diagrams." ),
531+
QgsMessageBar::WARNING );
526532
}
527-
else
528-
{
529-
QgsDiagram* diagram = 0;
530-
int index = mDiagramTypeComboBox->currentIndex();
531-
QString diagramType = mDiagramTypeComboBox->itemData( index ).toString();
532533

533-
if ( 0 == mDiagramAttributesTreeWidget->topLevelItemCount() )
534-
{
535-
QgisApp::instance()->messageBar()->pushMessage(
536-
tr( "Diagrams: No attributes added." ),
537-
tr( "You did not add any attributes to this diagram layer. Please specify the attributes to visualize on the diagrams or disable diagrams." ),
538-
QgsMessageBar::WARNING );
539-
}
534+
bool scaleAttributeValueOk = false;
535+
// Check if a (usable) scale attribute value is inserted
536+
mValueLineEdit->text().toDouble( &scaleAttributeValueOk );
540537

541-
bool scaleAttributeValueOk = false;
542-
// Check if a (usable) scale attribute value is inserted
543-
mValueLineEdit->text().toDouble( &scaleAttributeValueOk );
538+
if ( !mFixedSizeCheckBox->isChecked() && !scaleAttributeValueOk )
539+
{
540+
double maxVal = DBL_MIN;
541+
QgsVectorDataProvider* provider = mLayer->dataProvider();
544542

545-
if ( !mFixedSizeCheckBox->isChecked() && !scaleAttributeValueOk )
543+
if ( provider )
546544
{
547-
double maxVal = DBL_MIN;
548-
QgsVectorDataProvider* provider = mLayer->dataProvider();
549-
550-
if ( provider )
545+
if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
551546
{
552-
if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
547+
// Find maximum value
548+
for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
553549
{
554-
// Find maximum value
555-
for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
550+
QString fldName = mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString();
551+
if ( fldName.count() >= 2 && fldName.at( 0 ) == '"' && fldName.at( fldName.count() - 1 ) == '"' )
552+
fldName = fldName.mid( 1, fldName.count() - 2 ); // remove enclosing double quotes
553+
int fld = provider->fieldNameIndex( fldName );
554+
if ( fld != -1 )
556555
{
557-
QString fldName = mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString();
558-
if ( fldName.count() >= 2 && fldName.at( 0 ) == '"' && fldName.at( fldName.count() - 1 ) == '"' )
559-
fldName = fldName.mid( 1, fldName.count() - 2 ); // remove enclosing double quotes
560-
int fld = provider->fieldNameIndex( fldName );
561-
if ( fld != -1 )
562-
{
563-
bool ok = false;
564-
double val = provider->maximumValue( fld ).toDouble( &ok );
565-
if ( ok )
566-
maxVal = qMax( maxVal, val );
567-
}
556+
bool ok = false;
557+
double val = provider->maximumValue( fld ).toDouble( &ok );
558+
if ( ok )
559+
maxVal = qMax( maxVal, val );
568560
}
569561
}
570-
else
571-
{
572-
maxVal = provider->maximumValue( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() ).toDouble();
573-
}
574562
}
575-
576-
if ( maxVal != DBL_MIN )
563+
else
577564
{
578-
QgisApp::instance()->messageBar()->pushMessage(
579-
tr( "Interpolation value" ),
580-
tr( "You did not specify an interpolation value. A default value of %1 has been set." ).arg( QString::number( maxVal ) ),
581-
QgsMessageBar::INFO,
582-
5 );
583-
584-
mValueLineEdit->setText( QString::number( maxVal ) );
565+
maxVal = provider->maximumValue( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() ).toDouble();
585566
}
586567
}
587568

588-
if ( diagramType == DIAGRAM_NAME_TEXT )
589-
{
590-
diagram = new QgsTextDiagram();
591-
}
592-
else if ( diagramType == DIAGRAM_NAME_PIE )
593-
{
594-
diagram = new QgsPieDiagram();
595-
}
596-
else // if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
569+
if ( maxVal != DBL_MIN )
597570
{
598-
diagram = new QgsHistogramDiagram();
571+
QgisApp::instance()->messageBar()->pushMessage(
572+
tr( "Interpolation value" ),
573+
tr( "You did not specify an interpolation value. A default value of %1 has been set." ).arg( QString::number( maxVal ) ),
574+
QgsMessageBar::INFO,
575+
5 );
576+
577+
mValueLineEdit->setText( QString::number( maxVal ) );
599578
}
579+
}
600580

601-
QgsDiagramSettings ds;
602-
ds.font = mDiagramFont;
603-
ds.transparency = mTransparencySlider->value();
581+
if ( diagramType == DIAGRAM_NAME_TEXT )
582+
{
583+
diagram = new QgsTextDiagram();
584+
}
585+
else if ( diagramType == DIAGRAM_NAME_PIE )
586+
{
587+
diagram = new QgsPieDiagram();
588+
}
589+
else // if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
590+
{
591+
diagram = new QgsHistogramDiagram();
592+
}
604593

605-
QList<QColor> categoryColors;
606-
QList<QString> categoryAttributes;
607-
for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
608-
{
609-
QColor color = mDiagramAttributesTreeWidget->topLevelItem( i )->background( 1 ).color();
610-
color.setAlpha( 255 - ds.transparency );
611-
categoryColors.append( color );
612-
categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString() );
613-
}
614-
ds.categoryColors = categoryColors;
615-
ds.categoryAttributes = categoryAttributes;
616-
ds.size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
617-
ds.sizeType = static_cast<QgsDiagramSettings::SizeType>( mDiagramUnitComboBox->itemData( mDiagramUnitComboBox->currentIndex() ).toInt() );
618-
ds.labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->itemData( mLabelPlacementComboBox->currentIndex() ).toInt() );
619-
ds.scaleByArea = mScaleDependencyComboBox->itemData( mScaleDependencyComboBox->currentIndex() ).toBool();
620-
621-
if ( mIncreaseSmallDiagramsGroupBox->isChecked() )
622-
{
623-
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
624-
}
625-
else
626-
{
627-
ds.minimumSize = 0;
628-
}
594+
QgsDiagramSettings ds;
595+
ds.enabled = mDisplayDiagramsGroupBox->isChecked();
596+
ds.font = mDiagramFont;
597+
ds.transparency = mTransparencySlider->value();
629598

630-
ds.backgroundColor = mBackgroundColorButton->color();
631-
ds.penColor = mDiagramPenColorButton->color();
632-
ds.penWidth = mPenWidthSpinBox->value();
633-
if ( mVisibilityGroupBox->isChecked() )
634-
{
635-
ds.minScaleDenominator = mMinimumDiagramScaleLineEdit->text().toDouble();
636-
ds.maxScaleDenominator = mMaximumDiagramScaleLineEdit->text().toDouble();
637-
}
638-
else
639-
{
640-
ds.minScaleDenominator = -1;
641-
ds.maxScaleDenominator = -1;
642-
}
599+
QList<QColor> categoryColors;
600+
QList<QString> categoryAttributes;
601+
for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
602+
{
603+
QColor color = mDiagramAttributesTreeWidget->topLevelItem( i )->background( 1 ).color();
604+
color.setAlpha( 255 - ds.transparency );
605+
categoryColors.append( color );
606+
categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, Qt::UserRole ).toString() );
607+
}
608+
ds.categoryColors = categoryColors;
609+
ds.categoryAttributes = categoryAttributes;
610+
ds.size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
611+
ds.sizeType = static_cast<QgsDiagramSettings::SizeType>( mDiagramUnitComboBox->itemData( mDiagramUnitComboBox->currentIndex() ).toInt() );
612+
ds.labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->itemData( mLabelPlacementComboBox->currentIndex() ).toInt() );
613+
ds.scaleByArea = mScaleDependencyComboBox->itemData( mScaleDependencyComboBox->currentIndex() ).toBool();
643614

644-
// Diagram angle offset (pie)
645-
ds.angleOffset = mAngleOffsetComboBox->itemData( mAngleOffsetComboBox->currentIndex() ).toInt();
615+
if ( mIncreaseSmallDiagramsGroupBox->isChecked() )
616+
{
617+
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
618+
}
619+
else
620+
{
621+
ds.minimumSize = 0;
622+
}
646623

647-
// Diagram orientation (histogram)
648-
ds.diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( mOrientationButtonGroup->checkedButton()->property( "direction" ).toInt() );
624+
ds.backgroundColor = mBackgroundColorButton->color();
625+
ds.penColor = mDiagramPenColorButton->color();
626+
ds.penWidth = mPenWidthSpinBox->value();
627+
if ( mVisibilityGroupBox->isChecked() )
628+
{
629+
ds.minScaleDenominator = mMinimumDiagramScaleLineEdit->text().toDouble();
630+
ds.maxScaleDenominator = mMaximumDiagramScaleLineEdit->text().toDouble();
631+
}
632+
else
633+
{
634+
ds.minScaleDenominator = -1;
635+
ds.maxScaleDenominator = -1;
636+
}
649637

650-
ds.barWidth = mBarWidthSpinBox->value();
638+
// Diagram angle offset (pie)
639+
ds.angleOffset = mAngleOffsetComboBox->itemData( mAngleOffsetComboBox->currentIndex() ).toInt();
651640

652-
if ( mFixedSizeCheckBox->isChecked() )
653-
{
654-
QgsSingleCategoryDiagramRenderer* dr = new QgsSingleCategoryDiagramRenderer();
655-
dr->setDiagram( diagram );
656-
dr->setDiagramSettings( ds );
657-
mLayer->setDiagramRenderer( dr );
658-
}
659-
else
660-
{
661-
QgsLinearlyInterpolatedDiagramRenderer* dr = new QgsLinearlyInterpolatedDiagramRenderer();
662-
dr->setLowerValue( 0.0 );
663-
dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
664-
dr->setUpperValue( mValueLineEdit->text().toDouble() );
665-
dr->setUpperSize( QSizeF( mSizeSpinBox->value(), mSizeSpinBox->value() ) );
666-
bool isExpression = mSizeAttributeComboBox->currentIndex() >= mAvailableAttributes;
667-
dr->setClassificationAttributeIsExpression( isExpression );
668-
if ( isExpression )
669-
{
670-
dr->setClassificationAttributeExpression( mSizeAttributeComboBox->currentText() );
671-
}
672-
else
673-
{
674-
dr->setClassificationAttribute( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() );
675-
}
676-
dr->setDiagram( diagram );
677-
dr->setDiagramSettings( ds );
678-
mLayer->setDiagramRenderer( dr );
679-
}
641+
// Diagram orientation (histogram)
642+
ds.diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( mOrientationButtonGroup->checkedButton()->property( "direction" ).toInt() );
643+
644+
ds.barWidth = mBarWidthSpinBox->value();
680645

681-
QgsDiagramLayerSettings dls;
682-
dls.dist = mDiagramDistanceSpinBox->value();
683-
dls.priority = mPrioritySlider->value();
684-
if ( mDataDefinedPositionGroupBox->isChecked() )
646+
if ( mFixedSizeCheckBox->isChecked() )
647+
{
648+
QgsSingleCategoryDiagramRenderer* dr = new QgsSingleCategoryDiagramRenderer();
649+
dr->setDiagram( diagram );
650+
dr->setDiagramSettings( ds );
651+
mLayer->setDiagramRenderer( dr );
652+
}
653+
else
654+
{
655+
QgsLinearlyInterpolatedDiagramRenderer* dr = new QgsLinearlyInterpolatedDiagramRenderer();
656+
dr->setLowerValue( 0.0 );
657+
dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
658+
dr->setUpperValue( mValueLineEdit->text().toDouble() );
659+
dr->setUpperSize( QSizeF( mSizeSpinBox->value(), mSizeSpinBox->value() ) );
660+
bool isExpression = mSizeAttributeComboBox->currentIndex() >= mAvailableAttributes;
661+
dr->setClassificationAttributeIsExpression( isExpression );
662+
if ( isExpression )
685663
{
686-
dls.xPosColumn = mDataDefinedXComboBox->itemData( mDataDefinedXComboBox->currentIndex() ).toInt();
687-
dls.yPosColumn = mDataDefinedYComboBox->itemData( mDataDefinedYComboBox->currentIndex() ).toInt();
664+
dr->setClassificationAttributeExpression( mSizeAttributeComboBox->currentText() );
688665
}
689666
else
690667
{
691-
dls.xPosColumn = -1;
692-
dls.yPosColumn = -1;
668+
dr->setClassificationAttribute( mSizeAttributeComboBox->itemData( mSizeAttributeComboBox->currentIndex() ).toInt() );
693669
}
694-
dls.placement = ( QgsDiagramLayerSettings::Placement )mPlacementComboBox->itemData( mPlacementComboBox->currentIndex() ).toInt();
695-
if ( mLineOptionsComboBox->isEnabled() )
696-
{
697-
dls.placementFlags = static_cast<QgsDiagramLayerSettings::LinePlacementFlags>( mLineOptionsComboBox->itemData( mLineOptionsComboBox->currentIndex() ).toInt() );
698-
}
699-
mLayer->setDiagramLayerSettings( dls );
670+
dr->setDiagram( diagram );
671+
dr->setDiagramSettings( ds );
672+
mLayer->setDiagramRenderer( dr );
673+
}
674+
675+
QgsDiagramLayerSettings dls;
676+
dls.dist = mDiagramDistanceSpinBox->value();
677+
dls.priority = mPrioritySlider->value();
678+
if ( mDataDefinedPositionGroupBox->isChecked() )
679+
{
680+
dls.xPosColumn = mDataDefinedXComboBox->itemData( mDataDefinedXComboBox->currentIndex() ).toInt();
681+
dls.yPosColumn = mDataDefinedYComboBox->itemData( mDataDefinedYComboBox->currentIndex() ).toInt();
682+
}
683+
else
684+
{
685+
dls.xPosColumn = -1;
686+
dls.yPosColumn = -1;
687+
}
688+
dls.placement = ( QgsDiagramLayerSettings::Placement )mPlacementComboBox->itemData( mPlacementComboBox->currentIndex() ).toInt();
689+
if ( mLineOptionsComboBox->isEnabled() )
690+
{
691+
dls.placementFlags = static_cast<QgsDiagramLayerSettings::LinePlacementFlags>( mLineOptionsComboBox->itemData( mLineOptionsComboBox->currentIndex() ).toInt() );
700692
}
693+
mLayer->setDiagramLayerSettings( dls );
701694
}
702695

703696
void QgsDiagramProperties::showSizeAttributeExpressionDialog()

src/app/qgsmaptoollabel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ bool QgsMapToolLabel::dataDefinedShowHide( QgsVectorLayer* vlayer, const QgsFeat
572572
bool QgsMapToolLabel::diagramMoveable( QgsMapLayer* ml, int& xCol, int& yCol ) const
573573
{
574574
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
575-
if ( vlayer && vlayer->diagramRenderer() )
575+
if ( vlayer && vlayer->diagramsEnabled() )
576576
{
577577
const QgsDiagramLayerSettings *dls = vlayer->diagramLayerSettings();
578578
if ( dls && dls->xPosColumn >= 0 && dls->yPosColumn >= 0 )

src/core/qgsdiagramrendererv2.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer*
7474
{
7575
Q_UNUSED( layer );
7676

77+
enabled = ( elem.attribute( "enabled", "1" ) != "0" );
7778
font.fromString( elem.attribute( "font" ) );
7879
backgroundColor.setNamedColor( elem.attribute( "backgroundColor" ) );
7980
backgroundColor.setAlpha( elem.attribute( "backgroundAlpha" ).toInt() );
@@ -186,6 +187,7 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc,
186187
Q_UNUSED( layer );
187188

188189
QDomElement categoryElem = doc.createElement( "DiagramCategory" );
190+
categoryElem.setAttribute( "enabled", enabled );
189191
categoryElem.setAttribute( "font", font.toString() );
190192
categoryElem.setAttribute( "backgroundColor", backgroundColor.name() );
191193
categoryElem.setAttribute( "backgroundAlpha", backgroundColor.alpha() );

src/core/qgsdiagramrendererv2.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ class CORE_EXPORT QgsDiagramSettings
110110
};
111111

112112
QgsDiagramSettings()
113-
: sizeType( MM )
113+
: enabled( true )
114+
, sizeType( MM )
114115
, penWidth( 0.0 )
115116
, labelPlacementMethod( QgsDiagramSettings::Height )
116117
, diagramOrientation( QgsDiagramSettings::Up )
@@ -122,6 +123,7 @@ class CORE_EXPORT QgsDiagramSettings
122123
, maxScaleDenominator( -1 )
123124
, minimumSize( 0.0 )
124125
{}
126+
bool enabled;
125127
QFont font;
126128
QList< QColor > categoryColors;
127129
QList< QString > categoryAttributes;

0 commit comments

Comments
 (0)