Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[diagram] make use of unit widget for diagram size
  • Loading branch information
nirvn committed Apr 5, 2016
1 parent 31d7be4 commit dc5d7ab
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 61 deletions.
13 changes: 6 additions & 7 deletions python/core/qgsdiagramrendererv2.sip
Expand Up @@ -289,14 +289,13 @@ class QgsDiagramSettings
//! @note added in 2.10
QList< QString > categoryLabels;
QSizeF size; //size
SizeType sizeType; //mm or map units
/** Line unit index (mm, map units, or pixels)
* @note added in 2.16
*/
//! 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
* @note added in 2.16
*/
//! Line unit scale
QgsMapUnitScale lineSizeScale;
QColor backgroundColor;
QColor penColor;
Expand Down
18 changes: 6 additions & 12 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -84,8 +84,7 @@ 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();
Expand Down Expand Up @@ -193,7 +192,7 @@ 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 );
Expand Down Expand Up @@ -265,14 +264,8 @@ 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 );

Expand Down Expand Up @@ -704,7 +697,8 @@ 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() );
Expand Down
24 changes: 5 additions & 19 deletions src/core/diagram/qgsdiagram.cpp
Expand Up @@ -76,38 +76,24 @@ void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsR

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
41 changes: 24 additions & 17 deletions src/core/qgsdiagramrendererv2.cpp
Expand Up @@ -181,17 +181,19 @@ void QgsDiagramSettings::readXML( const QDomElement& elem, const QgsVectorLayer*
scaleBasedVisibility = minScaleDenominator >= 0 && maxScaleDenominator >= 0;
}

//mm vs map units for diagram
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" ) );

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

Expand Down Expand Up @@ -303,17 +305,11 @@ 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) for diagram
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 ) );

// site type (mm vs. map units) for line
//line width unit type and scale
categoryElem.setAttribute( "lineSizeType", QgsSymbolLayerV2Utils::encodeOutputUnit( lineSizeType ) );
categoryElem.setAttribute( "lineSizeScale", QgsSymbolLayerV2Utils::encodeMapUnitScale( lineSizeScale ) );

Expand Down Expand Up @@ -425,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
11 changes: 9 additions & 2 deletions src/core/qgsdiagramrendererv2.h
Expand Up @@ -321,7 +321,7 @@ class CORE_EXPORT QgsDiagramSettings

QgsDiagramSettings()
: enabled( true )
, sizeType( MM )
, sizeType( QgsSymbolV2::MM )
, lineSizeType( QgsSymbolV2::MM )
, penWidth( 0.0 )
, labelPlacementMethod( QgsDiagramSettings::Height )
Expand All @@ -342,7 +342,14 @@ 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
*/
Expand Down
17 changes: 15 additions & 2 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 @@ -1006,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
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 dc5d7ab

Please sign in to comment.