Skip to content

Commit

Permalink
[gui] Implement apply() for QgsStackedDiagramProperties, delegating p…
Browse files Browse the repository at this point in the history
…arts to QgsDiagramProperties for subdiagrams
  • Loading branch information
gacarrillor committed Aug 27, 2024
1 parent 0a70fbd commit 99e0969
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 94 deletions.
157 changes: 88 additions & 69 deletions src/gui/vector/qgsdiagramproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,40 +775,12 @@ void QgsDiagramProperties::mEngineSettingsButton_clicked()
}
}

void QgsDiagramProperties::apply()
std::unique_ptr<QgsDiagramSettings> QgsDiagramProperties::createDiagramSettings()
{
const int index = mDiagramTypeComboBox->currentIndex();
const bool diagramsEnabled = ( index != 0 );

std::unique_ptr< QgsDiagram > diagram;

if ( diagramsEnabled && 0 == mDiagramAttributesTreeWidget->topLevelItemCount() )
{
QMessageBox::warning( this, tr( "Diagrams: No attributes added." ),
tr( "You did not add any attributes to this diagram layer. Please specify the attributes to visualize on the diagrams or disable diagrams." ) );
}

if ( mDiagramType == DIAGRAM_NAME_TEXT )
{
diagram = std::make_unique< QgsTextDiagram >();
}
else if ( mDiagramType == DIAGRAM_NAME_PIE )
{
diagram = std::make_unique< QgsPieDiagram >();
}
else if ( mDiagramType == DIAGRAM_NAME_STACKED_BAR )
{
diagram = std::make_unique< QgsStackedBarDiagram >();
}
else // if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
{
diagram = std::make_unique< QgsHistogramDiagram >();
}

QgsDiagramSettings ds;
ds.enabled = ( mDiagramTypeComboBox->currentIndex() != 0 );
ds.font = mDiagramFontButton->currentFont();
ds.opacity = mOpacityWidget->opacity();
std::unique_ptr< QgsDiagramSettings > ds = std::make_unique< QgsDiagramSettings>();
ds->enabled = ( mDiagramTypeComboBox->currentIndex() != 0 );
ds->font = mDiagramFontButton->currentFont();
ds->opacity = mOpacityWidget->opacity();

QList<QColor> categoryColors;
QList<QString> categoryAttributes;
Expand All @@ -823,64 +795,69 @@ void QgsDiagramProperties::apply()
categoryAttributes.append( mDiagramAttributesTreeWidget->topLevelItem( i )->data( 0, RoleAttributeExpression ).toString() );
categoryLabels.append( mDiagramAttributesTreeWidget->topLevelItem( i )->text( 2 ) );
}
ds.categoryColors = categoryColors;
ds.categoryAttributes = categoryAttributes;
ds.categoryLabels = categoryLabels;
ds.size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
ds.sizeType = mDiagramUnitComboBox->unit();
ds.sizeScale = mDiagramUnitComboBox->getMapUnitScale();
ds.lineSizeUnit = mDiagramLineUnitComboBox->unit();
ds.lineSizeScale = mDiagramLineUnitComboBox->getMapUnitScale();
ds.labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->currentData().toInt() );
ds.scaleByArea = ( mDiagramType == DIAGRAM_NAME_STACKED_BAR ) ? false : mScaleDependencyComboBox->currentData().toBool();
ds->categoryColors = categoryColors;
ds->categoryAttributes = categoryAttributes;
ds->categoryLabels = categoryLabels;
ds->size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
ds->sizeType = mDiagramUnitComboBox->unit();
ds->sizeScale = mDiagramUnitComboBox->getMapUnitScale();
ds->lineSizeUnit = mDiagramLineUnitComboBox->unit();
ds->lineSizeScale = mDiagramLineUnitComboBox->getMapUnitScale();
ds->labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->currentData().toInt() );
ds->scaleByArea = ( mDiagramType == DIAGRAM_NAME_STACKED_BAR ) ? false : mScaleDependencyComboBox->currentData().toBool();

if ( mIncreaseSmallDiagramsCheck->isChecked() )
{
ds.minimumSize = mIncreaseMinimumSizeSpinBox->value();
ds->minimumSize = mIncreaseMinimumSizeSpinBox->value();
}
else
{
ds.minimumSize = 0;
ds->minimumSize = 0;
}

ds.backgroundColor = mBackgroundColorButton->color();
ds.penColor = mDiagramPenColorButton->color();
ds.penWidth = mPenWidthSpinBox->value();
ds.minimumScale = mScaleRangeWidget->minimumScale();
ds.maximumScale = mScaleRangeWidget->maximumScale();
ds.scaleBasedVisibility = mScaleVisibilityGroupBox->isChecked();
ds->backgroundColor = mBackgroundColorButton->color();
ds->penColor = mDiagramPenColorButton->color();
ds->penWidth = mPenWidthSpinBox->value();
ds->minimumScale = mScaleRangeWidget->minimumScale();
ds->maximumScale = mScaleRangeWidget->maximumScale();
ds->scaleBasedVisibility = mScaleVisibilityGroupBox->isChecked();

// Diagram angle offset (pie)
ds.rotationOffset = mAngleOffsetComboBox->currentData().toInt();
ds.setDirection( static_cast< QgsDiagramSettings::Direction>( mAngleDirectionComboBox->currentData().toInt() ) );
ds->rotationOffset = mAngleOffsetComboBox->currentData().toInt();
ds->setDirection( static_cast< QgsDiagramSettings::Direction>( mAngleDirectionComboBox->currentData().toInt() ) );

// Diagram orientation (histogram)
ds.diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( mOrientationButtonGroup->checkedButton()->property( "direction" ).toInt() );
ds->diagramOrientation = static_cast<QgsDiagramSettings::DiagramOrientation>( mOrientationButtonGroup->checkedButton()->property( "direction" ).toInt() );

ds.barWidth = mBarWidthSpinBox->value();
ds->barWidth = mBarWidthSpinBox->value();

ds.setAxisLineSymbol( mAxisLineStyleButton->clonedSymbol< QgsLineSymbol >() );
ds.setShowAxis( mShowAxisGroupBox->isChecked() );
ds->setAxisLineSymbol( mAxisLineStyleButton->clonedSymbol< QgsLineSymbol >() );
ds->setShowAxis( mShowAxisGroupBox->isChecked() );

ds.setSpacing( mBarSpacingSpinBox->value() );
ds.setSpacingUnit( mBarSpacingUnitComboBox->unit() );
ds.setSpacingMapUnitScale( mBarSpacingUnitComboBox->getMapUnitScale() );
ds->setSpacing( mBarSpacingSpinBox->value() );
ds->setSpacingUnit( mBarSpacingUnitComboBox->unit() );
ds->setSpacingMapUnitScale( mBarSpacingUnitComboBox->getMapUnitScale() );

if ( mPaintEffect && ( !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect.get() ) || mPaintEffect->enabled() ) )
ds.setPaintEffect( mPaintEffect->clone() );
ds->setPaintEffect( mPaintEffect->clone() );
else
ds.setPaintEffect( nullptr );
ds->setPaintEffect( nullptr );

return ds;
}

QgsDiagramRenderer *renderer = nullptr;
std::unique_ptr<QgsDiagramRenderer> QgsDiagramProperties::createRendererBaseInfo( const QgsDiagramSettings &ds )
{
std::unique_ptr< QgsDiagramRenderer > renderer;
if ( mFixedSizeRadio->isChecked() )
{
QgsSingleCategoryDiagramRenderer *dr = new QgsSingleCategoryDiagramRenderer();
std::unique_ptr< QgsSingleCategoryDiagramRenderer > dr = std::make_unique< QgsSingleCategoryDiagramRenderer >();
dr->setDiagramSettings( ds );
renderer = dr;
renderer = std::move( dr );
}
else
{
QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer();
std::unique_ptr< QgsLinearlyInterpolatedDiagramRenderer > dr = std::make_unique< QgsLinearlyInterpolatedDiagramRenderer >();
dr->setLowerValue( 0.0 );
dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
dr->setUpperValue( mMaxValueSpinBox->value() );
Expand All @@ -901,12 +878,15 @@ void QgsDiagramProperties::apply()

dr->setDataDefinedSizeLegend( mSizeLegend ? new QgsDataDefinedSizeLegend( *mSizeLegend ) : nullptr );

renderer = dr;
renderer = std::move( dr );
}
renderer->setDiagram( diagram.release() );

renderer->setAttributeLegend( mCheckBoxAttributeLegend->isChecked() );
mLayer->setDiagramRenderer( renderer );
return renderer;
}

QgsDiagramLayerSettings QgsDiagramProperties::createDiagramLayerSettings()
{
QgsDiagramLayerSettings dls;
dls.setDataDefinedProperties( mDataDefinedProperties );
dls.setDistance( mDiagramDistanceSpinBox->value() );
Expand Down Expand Up @@ -951,6 +931,45 @@ void QgsDiagramProperties::apply()
flags |= QgsDiagramLayerSettings::MapOrientation;
dls.setLinePlacementFlags( flags );

return dls;
}

void QgsDiagramProperties::apply()
{
const int index = mDiagramTypeComboBox->currentIndex();
const bool diagramsEnabled = ( index != 0 );

std::unique_ptr< QgsDiagram > diagram;

if ( diagramsEnabled && 0 == mDiagramAttributesTreeWidget->topLevelItemCount() )
{
QMessageBox::warning( this, tr( "Diagrams: No attributes added." ),
tr( "You did not add any attributes to this diagram layer. Please specify the attributes to visualize on the diagrams or disable diagrams." ) );
}

if ( mDiagramType == DIAGRAM_NAME_TEXT )
{
diagram = std::make_unique< QgsTextDiagram >();
}
else if ( mDiagramType == DIAGRAM_NAME_PIE )
{
diagram = std::make_unique< QgsPieDiagram >();
}
else if ( mDiagramType == DIAGRAM_NAME_STACKED_BAR )
{
diagram = std::make_unique< QgsStackedBarDiagram >();
}
else // if ( diagramType == DIAGRAM_NAME_HISTOGRAM )
{
diagram = std::make_unique< QgsHistogramDiagram >();
}

std::unique_ptr< QgsDiagramSettings > ds = createDiagramSettings();
std::unique_ptr< QgsDiagramRenderer > renderer = createRendererBaseInfo( *ds );
renderer->setDiagram( diagram.release() );
mLayer->setDiagramRenderer( renderer.release() );

QgsDiagramLayerSettings dls = createDiagramLayerSettings();
mLayer->setDiagramLayerSettings( dls );

// refresh
Expand Down
23 changes: 23 additions & 0 deletions src/gui/vector/qgsdiagramproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ class GUI_EXPORT QgsDiagramProperties : public QWidget, private Ui::QgsDiagramPr
QgsExpressionContext createExpressionContext() const override;

void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsDiagramLayerSettings::Property key );

/**
* Creates a QgsDiagramSettings object from the GUI settings.
*
* \since QGIS 3.40
*/
std::unique_ptr<QgsDiagramSettings> createDiagramSettings();

/**
* Creates a QgsDiagramRenderer object from the GUI settings.
*
* \since QGIS 3.40
*/
std::unique_ptr<QgsDiagramRenderer> createRendererBaseInfo( const QgsDiagramSettings &ds );

/**
* Creates a QgsDiagramLayerSettings object from the GUI settings.
*
* \since QGIS 3.40
*/
QgsDiagramLayerSettings createDiagramLayerSettings();

friend class QgsStackedDiagramProperties;
};


Expand Down
89 changes: 68 additions & 21 deletions src/gui/vector/qgsstackeddiagramproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
* *
***************************************************************************/

#include "qgsstackeddiagramproperties.h"
#include "qgsdiagramproperties.h"
#include "diagram/qgshistogramdiagram.h"
#include "diagram/qgspiediagram.h"
#include "diagram/qgstextdiagram.h"
#include "diagram/qgsstackedbardiagram.h"

#include "qgsdiagramproperties.h"
#include "qgsproject.h"
#include "qgsstackeddiagram.h"
#include "qgsstackeddiagramproperties.h"
#include "qgsvectorlayer.h"

QgsStackedDiagramProperties::QgsStackedDiagramProperties( QgsVectorLayer *layer, QWidget *parent, QgsMapCanvas *canvas )
: QWidget{parent}
Expand Down Expand Up @@ -55,22 +62,11 @@ QgsStackedDiagramProperties::QgsStackedDiagramProperties( QgsVectorLayer *layer,

void QgsStackedDiagramProperties::addSubDiagram()
{
if ( mSubDiagramsTabWidget->count() == 0 )
{
defaultDiagram = new QgsDiagramProperties( mLayer, this, mMapCanvas );
defaultDiagram->layout()->setContentsMargins( 6, 6, 6, 6 );
connect( defaultDiagram, &QgsDiagramProperties::auxiliaryFieldCreated, this, &QgsStackedDiagramProperties::auxiliaryFieldCreated );
QgsDiagramProperties *gui = new QgsDiagramProperties( mLayer, this, mMapCanvas );
gui->layout()->setContentsMargins( 6, 6, 6, 6 );
connect( gui, &QgsDiagramProperties::auxiliaryFieldCreated, this, &QgsStackedDiagramProperties::auxiliaryFieldCreated );

mSubDiagramsTabWidget->addTab( defaultDiagram, tr( "Diagram 1" ) );
}
else
{
QgsDiagramProperties *gui = new QgsDiagramProperties( mLayer, this, mMapCanvas );
gui->layout()->setContentsMargins( 6, 6, 6, 6 );
connect( gui, &QgsDiagramProperties::auxiliaryFieldCreated, this, &QgsStackedDiagramProperties::auxiliaryFieldCreated );

mSubDiagramsTabWidget->addTab( gui, tr( "Diagram %1" ).arg( mSubDiagramsTabWidget->count() + 1 ) );
}
mSubDiagramsTabWidget->addTab( gui, tr( "Diagram %1" ).arg( mSubDiagramsTabWidget->count() + 1 ) );

mRemoveSubDiagramButton->setEnabled( mSubDiagramsTabWidget->count() > 2 );
}
Expand All @@ -95,23 +91,74 @@ void QgsStackedDiagramProperties::apply()
{
if ( mDiagramTypeComboBox->currentData( Qt::UserRole ) == QgsDiagramLayerSettings::Single )
{
defaultDiagram->apply();
static_cast<QgsDiagramProperties *>( mSubDiagramsTabWidget->widget( 0 ) )->apply();
}
else
else // Stacked diagram
{
// Get DiagramSettings from each subdiagram
// TODO: Validate that we have at least 2 diagrams

// Create DiagramSetings for the StackedDiagram
std::unique_ptr< QgsDiagramSettings> ds = std::make_unique<QgsDiagramSettings>();
ds->stackedDiagramMode = static_cast<QgsDiagramSettings::StackedDiagramMode>( mStackedDiagramModeComboBox->currentData().toInt() );
ds->setStackedDiagramSpacingUnit( mStackedDiagramSpacingUnitComboBox->unit() );
ds->setStackedDiagramSpacing( mStackedDiagramSpacingSpinBox->value() );

// Add subdiagrams with their DiagramSettings to StackedDiagram
std::unique_ptr< QgsStackedDiagram > stackedDiagram = std::make_unique< QgsStackedDiagram >();

// Get DiagramSettings from each subdiagram
for ( int i = 0; i < mSubDiagramsTabWidget->count(); i++ )
{
QgsDiagramProperties *diagramProperties = static_cast<QgsDiagramProperties *>( mSubDiagramsTabWidget->widget( i ) );
std::unique_ptr< QgsDiagramSettings > ds1 = diagramProperties->createDiagramSettings();
ds->categoryAttributes += ds1->categoryAttributes;
ds->categoryLabels += ds1->categoryLabels;
ds->categoryColors += ds1->categoryColors;

std::unique_ptr< QgsDiagram > diagram;

if ( diagramProperties->mDiagramType == DIAGRAM_NAME_TEXT )
{
diagram = std::make_unique< QgsTextDiagram >();
}
else if ( diagramProperties->mDiagramType == DIAGRAM_NAME_PIE )
{
diagram = std::make_unique< QgsPieDiagram >();
}
else if ( diagramProperties->mDiagramType == DIAGRAM_NAME_STACKED_BAR )
{
diagram = std::make_unique< QgsStackedBarDiagram >();
}
else // if ( diagramProperties->mDiagramType == DIAGRAM_NAME_HISTOGRAM )
{
diagram = std::make_unique< QgsHistogramDiagram >();
}
stackedDiagram->addSubDiagram( diagram.release(), ds1.release() );
}

// Get first diagram to configure some stacked diagram settings from it
QgsDiagramProperties *firstDiagramProperties = static_cast<QgsDiagramProperties *>( mSubDiagramsTabWidget->widget( 0 ) );

// Create DiagramRenderer using info from first diagram and setting Stacked Diagram and the stacked diagram's DiagramSettings
std::unique_ptr< QgsDiagramRenderer > renderer = firstDiagramProperties->createRendererBaseInfo( *ds );
renderer->setDiagram( stackedDiagram.release() );
mLayer->setDiagramRenderer( renderer.release() );

// Create DiagramLayerSettings from first diagram
QgsDiagramLayerSettings dls = firstDiagramProperties->createDiagramLayerSettings();
mLayer->setDiagramLayerSettings( dls );

// refresh
QgsProject::instance()->setDirty( true );
mLayer->triggerRepaint();
}
}

void QgsStackedDiagramProperties::syncToLayer()
{
if ( mDiagramTypeComboBox->currentData( Qt::UserRole ) == QgsDiagramLayerSettings::Single )
{
defaultDiagram->syncToLayer();
static_cast<QgsDiagramProperties *>( mSubDiagramsTabWidget->widget( 0 ) )->syncToLayer();
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/gui/vector/qgsstackeddiagramproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,27 @@ class GUI_EXPORT QgsStackedDiagramProperties : public QWidget, private Ui::QgsSt
void apply();
void mDiagramTypeComboBox_currentIndexChanged( int index );

private:
private slots:

/**
* Adds a diagram tab to the current QgsStackedDiagramProperties.
*
* \since QGIS 3.40
*/
void addSubDiagram();

/**
* Removes a diagram tab from the current QgsStackedDiagramProperties.
* Diagram tabs are removed only if the tab count is greeater than 2.
* Tab texts are adjusted after tab removal, to keep sequential order.
*
* \since QGIS 3.40
*/
void removeSubDiagram();

private:
QgsVectorLayer *mLayer = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
QgsDiagramProperties *defaultDiagram = nullptr;

};

Expand Down
Loading

0 comments on commit 99e0969

Please sign in to comment.