Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2974 from nirvn/diagram_line_unit
[diagram] add unit type setting for outlines (fixes #14614)
  • Loading branch information
nyalldawson committed Apr 5, 2016
2 parents 410a438 + dc5d7ab commit fe54a78
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 62 deletions.
9 changes: 8 additions & 1 deletion python/core/qgsdiagramrendererv2.sip
Expand Up @@ -289,7 +289,14 @@ class QgsDiagramSettings
//! @note added in 2.10
QList< QString > categoryLabels;
QSizeF size; //size
SizeType sizeType; //mm or map units
//! Diagram size unit index (mm, map units, or pixels)
QgsSymbolV2::OutputUnit sizeType;
//! Diagram size unit scale
QgsMapUnitScale sizeScale;
//! Line unit index (mm, map units, or pixels)
QgsSymbolV2::OutputUnit lineSizeType;
//! Line unit scale
QgsMapUnitScale lineSizeScale;
QColor backgroundColor;
QColor penColor;
double penWidth;
Expand Down
24 changes: 12 additions & 12 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -84,8 +84,8 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer, QWidget* pare

mMaxValueSpinBox->setShowClearButton( false );

mDiagramUnitComboBox->insertItem( 0, tr( "mm" ), QgsDiagramSettings::MM );
mDiagramUnitComboBox->insertItem( 1, tr( "Map units" ), QgsDiagramSettings::MapUnits );
mDiagramUnitComboBox->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::MapUnit << QgsSymbolV2::Pixel );
mDiagramLineUnitComboBox->setUnits( QgsSymbolV2::OutputUnitList() << QgsSymbolV2::MM << QgsSymbolV2::MapUnit << QgsSymbolV2::Pixel );

QGis::GeometryType layerType = layer->geometryType();
if ( layerType == QGis::UnknownGeometry || layerType == QGis::NoGeometry )
Expand Down Expand Up @@ -192,7 +192,8 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer, QWidget* pare
mDiagramTypeFrame->setEnabled( false );
mDiagramFrame->setEnabled( false );
mFixedSizeRadio->setChecked( true );
mDiagramUnitComboBox->setCurrentIndex( mDiagramUnitComboBox->findText( tr( "mm" ) ) );
mDiagramUnitComboBox->setUnit( QgsSymbolV2::MM );
mDiagramLineUnitComboBox->setUnit( QgsSymbolV2::MM );
mLabelPlacementComboBox->setCurrentIndex( mLabelPlacementComboBox->findText( tr( "x-height" ) ) );
mDiagramSizeSpinBox->setEnabled( true );
mDiagramSizeSpinBox->setValue( 15 );
Expand Down Expand Up @@ -263,14 +264,10 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer* layer, QWidget* pare
mScaleRangeWidget->setScaleRange( 1.0 / ( settingList.at( 0 ).maxScaleDenominator > 0 ? settingList.at( 0 ).maxScaleDenominator : layer->maximumScale() ),
1.0 / ( settingList.at( 0 ).minScaleDenominator > 0 ? settingList.at( 0 ).minScaleDenominator : layer->minimumScale() ) );
mScaleVisibilityGroupBox->setChecked( settingList.at( 0 ).scaleBasedVisibility );
if ( settingList.at( 0 ).sizeType == QgsDiagramSettings::MM )
{
mDiagramUnitComboBox->setCurrentIndex( 0 );
}
else
{
mDiagramUnitComboBox->setCurrentIndex( 1 );
}
mDiagramUnitComboBox->setUnit( settingList.at( 0 ).sizeType );
mDiagramUnitComboBox->setMapUnitScale( settingList.at( 0 ).sizeScale );
mDiagramLineUnitComboBox->setUnit( settingList.at( 0 ).lineSizeType );
mDiagramLineUnitComboBox->setMapUnitScale( settingList.at( 0 ).lineSizeScale );

if ( settingList.at( 0 ).labelPlacementMethod == QgsDiagramSettings::Height )
{
Expand Down Expand Up @@ -700,7 +697,10 @@ void QgsDiagramProperties::apply()
ds.categoryAttributes = categoryAttributes;
ds.categoryLabels = categoryLabels;
ds.size = QSizeF( mDiagramSizeSpinBox->value(), mDiagramSizeSpinBox->value() );
ds.sizeType = static_cast<QgsDiagramSettings::SizeType>( mDiagramUnitComboBox->itemData( mDiagramUnitComboBox->currentIndex() ).toInt() );
ds.sizeType = mDiagramUnitComboBox->unit();
ds.sizeScale = mDiagramUnitComboBox->getMapUnitScale();
ds.lineSizeType = mDiagramLineUnitComboBox->unit();
ds.lineSizeScale = mDiagramLineUnitComboBox->getMapUnitScale();
ds.labelPlacementMethod = static_cast<QgsDiagramSettings::LabelPlacementMethod>( mLabelPlacementComboBox->itemData( mLabelPlacementComboBox->currentIndex() ).toInt() );
ds.scaleByArea = mScaleDependencyComboBox->itemData( mScaleDependencyComboBox->currentIndex() ).toBool();

Expand Down
33 changes: 6 additions & 27 deletions src/core/diagram/qgsdiagram.cpp
Expand Up @@ -70,51 +70,30 @@ QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsEx

void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
pen.setWidthF( s.penWidth * c.scaleFactor() );
}
else
{
pen.setWidthF( s.penWidth / c.mapToPixel().mapUnitsPerPixel() );
}
pen.setWidthF( QgsSymbolLayerV2Utils::convertToPainterUnits( c, s.penWidth, s.lineSizeType, s.lineSizeScale ) );
}


QSizeF QgsDiagram::sizePainterUnits( QSizeF size, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
return QSizeF( size.width() * c.scaleFactor(), size.height() * c.scaleFactor() );
}
else
{
return QSizeF( size.width() / c.mapToPixel().mapUnitsPerPixel(), size.height() / c.mapToPixel().mapUnitsPerPixel() );
}
return QSizeF( QgsSymbolLayerV2Utils::convertToPainterUnits( c, size.width(), s.sizeType, s.sizeScale ), QgsSymbolLayerV2Utils::convertToPainterUnits( c, size.height(), s.sizeType, s.sizeScale ) );
}

float QgsDiagram::sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
return l * c.scaleFactor();
}
else
{
return l / c.mapToPixel().mapUnitsPerPixel();
}
return QgsSymbolLayerV2Utils::convertToPainterUnits( c, l, s.sizeType, s.sizeScale );
}

QFont QgsDiagram::scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c )
{
QFont f = s.font;
if ( s.sizeType == QgsDiagramSettings::MM )
if ( s.sizeType == QgsSymbolV2::MapUnit )
{
f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
f.setPixelSize( s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel() );
}
else
{
f.setPixelSize( s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel() );
f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
}

return f;
Expand Down
45 changes: 30 additions & 15 deletions src/core/qgsdiagramrendererv2.cpp
Expand Up @@ -181,15 +181,21 @@ void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer*
scaleBasedVisibility = minScaleDenominator >= 0 && maxScaleDenominator >= 0;
}

//mm vs map units
if ( elem.attribute( "sizeType" ) == "MM" )
//diagram size unit type and scale
if ( elem.attribute( "sizeType" ) == "MapUnits" )
{
sizeType = MM;
//compatibility with pre-2.16 project files
sizeType = QgsSymbolV2::MapUnit;
}
else
{
sizeType = MapUnits;
sizeType = QgsSymbolLayerV2Utils::decodeOutputUnit( elem.attribute( "sizeType" ) );
}
sizeScale = QgsSymbolLayerV2Utils::decodeMapUnitScale( elem.attribute( "sizeScale" ) );

//line width unit type and scale
lineSizeType = QgsSymbolLayerV2Utils::decodeOutputUnit( elem.attribute( "lineSizeType" ) );
lineSizeScale = QgsSymbolLayerV2Utils::decodeMapUnitScale( elem.attribute( "lineSizeScale" ) );

//label placement method
if ( elem.attribute( "labelPlacementMethod" ) == "Height" )
Expand Down Expand Up @@ -299,15 +305,13 @@ void QgsDiagramSettings::writeXML( QDomElement& rendererElem, QDomDocument& doc,
categoryElem.setAttribute( "maxScaleDenominator", QString::number( maxScaleDenominator ) );
categoryElem.setAttribute( "transparency", QString::number( transparency ) );

// site type (mm vs. map units)
if ( sizeType == MM )
{
categoryElem.setAttribute( "sizeType", "MM" );
}
else
{
categoryElem.setAttribute( "sizeType", "MapUnits" );
}
//diagram size unit type and scale
categoryElem.setAttribute( "sizeType", QgsSymbolLayerV2Utils::encodeOutputUnit( sizeType ) );
categoryElem.setAttribute( "sizeScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( sizeScale ) );

//line width unit type and scale
categoryElem.setAttribute( "lineSizeType", QgsSymbolLayerV2Utils::encodeOutputUnit( lineSizeType ) );
categoryElem.setAttribute( "lineSizeScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( lineSizeScale ) );

// label placement method (text diagram)
if ( labelPlacementMethod == Height )
Expand Down Expand Up @@ -417,9 +421,20 @@ QSizeF QgsDiagramRendererV2::sizeMapUnits( const QgsFeature& feature, const QgsR
}

QSizeF size = diagramSize( feature, c );
if ( s.sizeType == QgsDiagramSettings::MM )
if ( size.isValid() )
{
convertSizeToMapUnits( size, c );
if ( s.sizeType == QgsSymbolV2::MM )
{
double pixelToMap = c.scaleFactor() * c.mapToPixel().mapUnitsPerPixel();
size.rwidth() *= pixelToMap;
size.rheight() *= pixelToMap;
}
else if ( s.sizeType == QgsSymbolV2::Pixel )
{
double pixelToMap = c.mapToPixel().mapUnitsPerPixel();
size.rwidth() *= pixelToMap;
size.rheight() *= pixelToMap;
}
}
return size;
}
Expand Down
21 changes: 19 additions & 2 deletions src/core/qgsdiagramrendererv2.h
Expand Up @@ -24,6 +24,7 @@

#include "qgsfeature.h"
#include "qgsexpressioncontext.h"
#include "qgssymbollayerv2.h"

class QgsDiagram;
class QgsDiagramRendererV2;
Expand Down Expand Up @@ -320,7 +321,8 @@ class CORE_EXPORT QgsDiagramSettings

QgsDiagramSettings()
: enabled( true )
, sizeType( MM )
, sizeType( QgsSymbolV2::MM )
, lineSizeType( QgsSymbolV2::MM )
, penWidth( 0.0 )
, labelPlacementMethod( QgsDiagramSettings::Height )
, diagramOrientation( QgsDiagramSettings::Up )
Expand All @@ -340,7 +342,22 @@ class CORE_EXPORT QgsDiagramSettings
//! @note added in 2.10
QList< QString > categoryLabels;
QSizeF size; //size
SizeType sizeType; //mm or map units
/** Diagram size unit index (mm, map units, or pixels)
* @note added in 2.16
*/
QgsSymbolV2::OutputUnit sizeType;
/** Diagram size unit scale
* @note added in 2.16
*/
QgsMapUnitScale sizeScale;
/** Line unit index (mm, map units, or pixels)
* @note added in 2.16
*/
QgsSymbolV2::OutputUnit lineSizeType;
/** Line unit scale
* @note added in 2.16
*/
QgsMapUnitScale lineSizeScale;
QColor backgroundColor;
QColor penColor;
double penWidth;
Expand Down
46 changes: 43 additions & 3 deletions src/ui/qgsdiagrampropertiesbase.ui
Expand Up @@ -702,7 +702,10 @@
<item row="1" column="1">
<widget class="QgsDoubleSpinBox" name="mBarWidthSpinBox">
<property name="minimum">
<double>0.010000000000000</double>
<double>0.00000000000000</double>
</property>
<property name="maximum">
<double>99999999.99000000000</double>
</property>
<property name="value">
<double>5.000000000000000</double>
Expand Down Expand Up @@ -763,13 +766,33 @@
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="mPenWidthLabel">
<property name="text">
<string>Line Unit</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QgsUnitSelectionWidget" name="mDiagramLineUnitComboBox" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mAngleOffsetLabel">
<property name="text">
<string>Start angle</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QComboBox" name="mAngleOffsetComboBox"/>
</item>
<item row="3" column="1">
Expand Down Expand Up @@ -986,7 +1009,17 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="mDiagramUnitComboBox"/>
<widget class="QgsUnitSelectionWidget" name="mDiagramUnitComboBox" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<spacer name="verticalSpacer_3">
Expand Down Expand Up @@ -1839,6 +1872,12 @@
<extends>QWidget</extends>
<header>qgsscalerangewidget.h</header>
</customwidget>
<customwidget>
<class>QgsUnitSelectionWidget</class>
<extends>QWidget</extends>
<header>qgsunitselectionwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mEnableDiagramsCheckBox</tabstop>
Expand All @@ -1858,6 +1897,7 @@
<tabstop>mBackgroundColorButton</tabstop>
<tabstop>mDiagramPenColorButton</tabstop>
<tabstop>mPenWidthSpinBox</tabstop>
<tabstop>mDiagramLineUnitComboBox</tabstop>
<tabstop>mAngleOffsetComboBox</tabstop>
<tabstop>mDiagramFontButton</tabstop>
<tabstop>mShowAllCheckBox</tabstop>
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsdiagram.cpp
Expand Up @@ -144,7 +144,7 @@ class TestQgsDiagram : public QObject
ds.penColor = Qt::green;
ds.penWidth = .5;
ds.scaleByArea = true;
ds.sizeType = QgsDiagramSettings::MM;
ds.sizeType = QgsSymbolV2::MM;
ds.size = QSizeF( 5, 5 );
ds.angleOffset = 0;

Expand Down Expand Up @@ -181,7 +181,7 @@ class TestQgsDiagram : public QObject
ds.penColor = Qt::green;
ds.penWidth = .5;
ds.scaleByArea = true;
ds.sizeType = QgsDiagramSettings::MM;
ds.sizeType = QgsSymbolV2::MM;
ds.size = QSizeF( 5, 5 );
ds.angleOffset = 0;

Expand Down

0 comments on commit fe54a78

Please sign in to comment.