Skip to content
Permalink
Browse files

Manual configuration of classes in legend with data-defined sizes

  • Loading branch information
wonder-sk committed Jun 20, 2017
1 parent c9c4216 commit ea0b6899d1a7099d19b92a4f8e7729b3a47f9fb1
@@ -11,6 +11,7 @@




class QgsDataDefinedSizeLegendDialog : QDialog
{
%Docstring
@@ -69,6 +69,9 @@ void QgsDataDefinedSizeLegend::updateFromSymbolAndProperty( const QgsMarkerSymbo

mTitleLabel = ddSize.propertyType() == QgsProperty::ExpressionBasedProperty ? ddSize.expressionString() : ddSize.field();

if ( !mSizeClasses.isEmpty() )
return; // manually generated classes

// automatically generated classes
if ( const QgsSizeScaleTransformer *sizeTransformer = dynamic_cast< const QgsSizeScaleTransformer * >( ddSize.transformer() ) )
{
@@ -779,16 +779,20 @@ QList< QgsLayerTreeModelLegendNode * > QgsLinearlyInterpolatedDiagramRenderer::l
legendSymbol->setSizeUnit( mSettings.sizeType );
legendSymbol->setSizeMapUnitScale( mSettings.sizeScale );

QList<QgsDataDefinedSizeLegend::SizeClass> sizeClasses;
Q_FOREACH ( double v, QgsSymbolLayerUtils::prettyBreaks( mInterpolationSettings.lowerValue, mInterpolationSettings.upperValue, 4 ) )
{
double size = mDiagram->legendSize( v, mSettings, mInterpolationSettings );
sizeClasses << QgsDataDefinedSizeLegend::SizeClass( size, QString::number( v ) );
}

QgsDataDefinedSizeLegend ddSizeLegend( *mDataDefinedSizeLegend );
ddSizeLegend.setSymbol( legendSymbol ); // transfers ownership
ddSizeLegend.setClasses( sizeClasses );

if ( ddSizeLegend.classes().isEmpty() )
{
// automatic class creation if the classes are not defined manually
QList<QgsDataDefinedSizeLegend::SizeClass> sizeClasses;
Q_FOREACH ( double v, QgsSymbolLayerUtils::prettyBreaks( mInterpolationSettings.lowerValue, mInterpolationSettings.upperValue, 4 ) )
{
double size = mDiagram->legendSize( v, mSettings, mInterpolationSettings );
sizeClasses << QgsDataDefinedSizeLegend::SizeClass( size, QString::number( v ) );
}
ddSizeLegend.setClasses( sizeClasses );
}

Q_FOREACH ( const QgsLegendSymbolItem &si, ddSizeLegend.legendSymbolList() )
{
@@ -15,6 +15,8 @@

#include "qgsdatadefinedsizelegenddialog.h"

#include <QInputDialog>

#include "qgsdatadefinedsizelegend.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
@@ -51,7 +53,7 @@ QgsDataDefinedSizeLegendDialog::QgsDataDefinedSizeLegendDialog( const QgsDataDef
else
cboAlignSymbols->setCurrentIndex( 1 );

symbol = ddsLegend->symbol(); // may be null (undefined)
symbol = ddsLegend->symbol() ? ddsLegend->symbol()->clone() : nullptr; // may be null (undefined)
}

if ( overrideSymbol )
@@ -71,6 +73,28 @@ QgsDataDefinedSizeLegendDialog::QgsDataDefinedSizeLegendDialog( const QgsDataDef
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( mSourceSymbol.get(), btnChangeSymbol->iconSize() );
btnChangeSymbol->setIcon( icon );

mSizeClassesModel = new QStandardItemModel( viewSizeClasses );
mSizeClassesModel->setHorizontalHeaderLabels( QStringList() << tr( "Value" ) );
mSizeClassesModel->setSortRole( Qt::UserRole + 1 );
if ( ddsLegend )
{
groupManualSizeClasses->setChecked( !ddsLegend->classes().isEmpty() );
Q_FOREACH ( const QgsDataDefinedSizeLegend::SizeClass &sc, ddsLegend->classes() )
{
QStandardItem *item = new QStandardItem( sc.label );
item->setEditable( false );
item->setData( sc.label.toInt() );
mSizeClassesModel->appendRow( item );
}
mSizeClassesModel->sort( 0 );
}

connect( groupManualSizeClasses, &QGroupBox::clicked, this, &QgsDataDefinedSizeLegendDialog::updatePreview );
connect( btnAddClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendDialog::addSizeClass );
connect( btnRemoveClass, &QToolButton::clicked, this, &QgsDataDefinedSizeLegendDialog::removeSizeClass );

viewSizeClasses->setModel( mSizeClassesModel );

// prepare layer and model to preview legend
mPreviewLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "Preview", "memory" );
mPreviewTree = new QgsLayerTree;
@@ -107,6 +131,18 @@ QgsDataDefinedSizeLegend *QgsDataDefinedSizeLegendDialog::dataDefinedSizeLegend(
{
ddsLegend->setSymbol( mSourceSymbol->clone() );
}
if ( groupManualSizeClasses->isChecked() )
{
const QgsSizeScaleTransformer *transformer = dynamic_cast<const QgsSizeScaleTransformer *>( mSizeProperty.transformer() );
QList<QgsDataDefinedSizeLegend::SizeClass> classes;
for ( int i = 0; i < mSizeClassesModel->rowCount(); ++i )
{
double value = mSizeClassesModel->data( mSizeClassesModel->index( i, 0 ), Qt::UserRole + 1 ).toDouble();
double size = transformer ? transformer->size( value ) : value;
classes << QgsDataDefinedSizeLegend::SizeClass( size, QString::number( value ) );
}
ddsLegend->setClasses( classes );
}
return ddsLegend;
}

@@ -150,3 +186,29 @@ void QgsDataDefinedSizeLegendDialog::changeSymbol()

updatePreview();
}

void QgsDataDefinedSizeLegendDialog::addSizeClass()
{
bool ok;
double v = QInputDialog::getDouble( this, tr( "Add Size Class" ), tr( "Enter value for a new class" ),
0, -2147483647, 2147483647, 6, &ok );
if ( !ok )
return;

QStandardItem *item = new QStandardItem( QString::number( v ) );
item->setEditable( false );
item->setData( v );
mSizeClassesModel->appendRow( item );
mSizeClassesModel->sort( 0 );
updatePreview();
}

void QgsDataDefinedSizeLegendDialog::removeSizeClass()
{
QModelIndex idx = viewSizeClasses->currentIndex();
if ( !idx.isValid() )
return;

mSizeClassesModel->removeRow( idx.row() );
updatePreview();
}
@@ -25,6 +25,8 @@

#include "qgsproperty.h"

class QStandardItemModel;

class QgsDataDefinedSizeLegend;
class QgsLayerTree;
class QgsLayerTreeLayer;
@@ -58,6 +60,8 @@ class GUI_EXPORT QgsDataDefinedSizeLegendDialog : public QDialog, private Ui::Qg
private slots:
void updatePreview();
void changeSymbol();
void addSizeClass();
void removeSizeClass();

private:
std::unique_ptr<QgsMarkerSymbol> mSourceSymbol; //!< Source symbol (without data-defined size set)
@@ -68,6 +72,7 @@ class GUI_EXPORT QgsDataDefinedSizeLegendDialog : public QDialog, private Ui::Qg
QgsLayerTreeLayer *mPreviewLayerNode;
QgsVectorLayer *mPreviewLayer;
QgsMapCanvas *mMapCanvas = nullptr;
QStandardItemModel *mSizeClassesModel;
};

#endif // QGSDATADEFINEDSIZELEGENDDIALOG_H
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>674</width>
<height>492</height>
<width>718</width>
<height>749</height>
</rect>
</property>
<property name="windowTitle">
@@ -47,6 +47,59 @@
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupManualSizeClasses">
<property name="title">
<string>Manual size classes</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QToolButton" name="btnAddClass">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnRemoveClass">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QTreeView" name="viewSizeClasses">
<property name="rootIsDecorated">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
@@ -79,14 +132,14 @@
</item>
</layout>
</item>
<item row="0" column="1">
<item row="0" column="2">
<widget class="QTreeView" name="viewLayerTree">
<property name="headerHidden">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -102,6 +155,11 @@
<tabstop>radDisabled</tabstop>
<tabstop>radSeparated</tabstop>
<tabstop>radCollapsed</tabstop>
<tabstop>btnChangeSymbol</tabstop>
<tabstop>groupManualSizeClasses</tabstop>
<tabstop>btnAddClass</tabstop>
<tabstop>btnRemoveClass</tabstop>
<tabstop>viewSizeClasses</tabstop>
<tabstop>cboAlignSymbols</tabstop>
<tabstop>viewLayerTree</tabstop>
</tabstops>

0 comments on commit ea0b689

Please sign in to comment.
You can’t perform that action at this time.