Skip to content

Commit

Permalink
[FEATURE][composer] Allow setting line join and cap style for scalebars
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 14, 2014
1 parent 80b7021 commit 103b3b4
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 20 deletions.
28 changes: 28 additions & 0 deletions python/core/composer/qgscomposerscalebar.sip
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ class QgsComposerScaleBar: QgsComposerItem

/**@note: this method was added in version 1.9*/
void setUnits( ScaleBarUnits u );

/** Returns the join style used for drawing lines in the scalebar
* @returns Join style for lines
* @note introduced in 2.3
* @see setLineJoinStyle
*/
Qt::PenJoinStyle lineJoinStyle() const;
/** Sets join style used when drawing the lines in the scalebar
* @param style Join style for lines
* @returns nothing
* @note introduced in 2.3
* @see lineJoinStyle
*/
void setLineJoinStyle( Qt::PenJoinStyle style );

/** Returns the cap style used for drawing lines in the scalebar
* @returns Cap style for lines
* @note introduced in 2.3
* @see setLineCapStyle
*/
Qt::PenCapStyle lineCapStyle() const;
/** Sets cap style used when drawing the lines in the scalebar
* @param style Cap style for lines
* @returns nothing
* @note introduced in 2.3
* @see lineCapStyle
*/
void setLineCapStyle( Qt::PenCapStyle style );

/**Apply default settings*/
void applyDefaultSettings();
Expand Down
45 changes: 44 additions & 1 deletion src/app/composer/qgscomposerscalebarwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void QgsComposerScaleBarWidget::setGuiElements()
mLabelBarSpaceSpinBox->setValue( mComposerScaleBar->labelBarSpace() );
mBoxSizeSpinBox->setValue( mComposerScaleBar->boxContentSpace() );
mUnitLabelLineEdit->setText( mComposerScaleBar->unitLabeling() );
mLineJoinStyleCombo->setPenJoinStyle( mComposerScaleBar->lineJoinStyle() );
mLineCapStyleCombo->setPenCapStyle( mComposerScaleBar->lineCapStyle() );

//map combo box
if ( mComposerScaleBar->composerMap() )
Expand Down Expand Up @@ -462,16 +464,29 @@ void QgsComposerScaleBarWidget::toggleStyleSpecificControls( const QString& styl
mLineWidthSpinBox->setEnabled( false );
mColorPushButton->setEnabled( false );
mStrokeColorPushButton->setEnabled( false );
mLineJoinStyleCombo->setEnabled( false );
mLineCapStyleCombo->setEnabled( false );
}
else
{
//Enable all controls
//Enable controls
mGroupBoxUnits->setEnabled( true );
mGroupBoxSegments->setEnabled( true );
mLabelBarSpaceSpinBox->setEnabled( true );
mLineWidthSpinBox->setEnabled( true );
mColorPushButton->setEnabled( true );
mStrokeColorPushButton->setEnabled( true );
if ( style == "Single Box" || style == "Double Box" )
{
mLineJoinStyleCombo->setEnabled( true );
mLineCapStyleCombo->setEnabled( false );
}
else
{
mLineJoinStyleCombo->setEnabled( false );
mLineCapStyleCombo->setEnabled( true );
}

}
}

Expand Down Expand Up @@ -583,6 +598,8 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mBoxSizeSpinBox->blockSignals( block );
mAlignmentComboBox->blockSignals( block );
mUnitsComboBox->blockSignals( block );
mLineJoinStyleCombo->blockSignals( block );
mLineCapStyleCombo->blockSignals( block );
}

void QgsComposerScaleBarWidget::connectUpdateSignal()
Expand All @@ -600,3 +617,29 @@ void QgsComposerScaleBarWidget::disconnectUpdateSignal()
QObject::disconnect( mComposerScaleBar, SIGNAL( itemChanged() ), this, SLOT( setGuiElements() ) );
}
}

void QgsComposerScaleBarWidget::on_mLineJoinStyleCombo_currentIndexChanged( int index )
{
Q_UNUSED( index );
if ( !mComposerScaleBar )
{
return;
}

mComposerScaleBar->beginCommand( tr( "Scalebar line join style" ) );
mComposerScaleBar->setLineJoinStyle( mLineJoinStyleCombo->penJoinStyle() );
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::on_mLineCapStyleCombo_currentIndexChanged( int index )
{
Q_UNUSED( index );
if ( !mComposerScaleBar )
{
return;
}

mComposerScaleBar->beginCommand( tr( "Scalebar line cap style" ) );
mComposerScaleBar->setLineCapStyle( mLineCapStyleCombo->penCapStyle() );
mComposerScaleBar->endCommand();
}
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposerscalebarwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
void on_mBoxSizeSpinBox_valueChanged( double d );
void on_mAlignmentComboBox_currentIndexChanged( int index );
void on_mUnitsComboBox_currentIndexChanged( int index );
void on_mLineJoinStyleCombo_currentIndexChanged( int index );
void on_mLineCapStyleCombo_currentIndexChanged( int index );

private slots:
void setGuiElements();
Expand Down
39 changes: 37 additions & 2 deletions src/core/composer/qgscomposerscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgsticksscalebarstyle.h"
#include "qgsrectangle.h"
#include "qgsproject.h"
#include "qgssymbollayerv2utils.h"
#include <QDomDocument>
#include <QDomElement>
#include <QFontMetricsF>
Expand All @@ -42,6 +43,8 @@ QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition )
, mSegmentMillimeters( 0.0 )
, mAlignment( Left )
, mUnits( MapUnits )
, mLineJoinStyle( Qt::MiterJoin )
, mLineCapStyle( Qt::SquareCap )
{
applyDefaultSettings();
applyDefaultSize();
Expand Down Expand Up @@ -228,6 +231,32 @@ void QgsComposerScaleBar::setUnits( ScaleBarUnits u )
emit itemChanged();
}

void QgsComposerScaleBar::setLineJoinStyle( Qt::PenJoinStyle style )
{
if ( mLineJoinStyle == style )
{
//no change
return;
}
mLineJoinStyle = style;
mPen.setJoinStyle( mLineJoinStyle );
update();
emit itemChanged();
}

void QgsComposerScaleBar::setLineCapStyle( Qt::PenCapStyle style )
{
if ( mLineCapStyle == style )
{
//no change
return;
}
mLineCapStyle = style;
mPen.setCapStyle( mLineCapStyle );
update();
emit itemChanged();
}

void QgsComposerScaleBar::applyDefaultSettings()
{
mNumSegments = 2;
Expand All @@ -242,7 +271,8 @@ void QgsComposerScaleBar::applyDefaultSettings()
mHeight = 3;

mPen = QPen( QColor( 0, 0, 0 ) );
mPen.setJoinStyle( Qt::MiterJoin );
mPen.setJoinStyle( mLineJoinStyle );
mPen.setCapStyle( mLineCapStyle );
mPen.setWidthF( 1.0 );

mBrush.setColor( QColor( 0, 0, 0 ) );
Expand Down Expand Up @@ -487,6 +517,8 @@ bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) cons
composerScaleBarElem.setAttribute( "outlineWidth", QString::number( mPen.widthF() ) );
composerScaleBarElem.setAttribute( "unitLabel", mUnitLabeling );
composerScaleBarElem.setAttribute( "units", mUnits );
composerScaleBarElem.setAttribute( "lineJoinStyle", QgsSymbolLayerV2Utils::encodePenJoinStyle( mLineJoinStyle ) );
composerScaleBarElem.setAttribute( "lineCapStyle", QgsSymbolLayerV2Utils::encodePenCapStyle( mLineCapStyle ) );

//style
if ( mStyle )
Expand Down Expand Up @@ -529,6 +561,10 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
mNumMapUnitsPerScaleBarUnit = itemElem.attribute( "numMapUnitsPerScaleBarUnit", "1.0" ).toDouble();
mPen.setWidthF( itemElem.attribute( "outlineWidth", "1.0" ).toDouble() );
mUnitLabeling = itemElem.attribute( "unitLabel" );
mLineJoinStyle = QgsSymbolLayerV2Utils::decodePenJoinStyle( itemElem.attribute( "lineJoinStyle", "miter" ) );
mPen.setJoinStyle( mLineJoinStyle );
mLineCapStyle = QgsSymbolLayerV2Utils::decodePenCapStyle( itemElem.attribute( "lineCapStyle", "square" ) );
mPen.setCapStyle( mLineCapStyle );
QString fontString = itemElem.attribute( "font", "" );
if ( !fontString.isEmpty() )
{
Expand Down Expand Up @@ -594,4 +630,3 @@ void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthA
}
}


31 changes: 31 additions & 0 deletions src/core/composer/qgscomposerscalebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
/**@note: this method was added in version 1.9*/
void setUnits( ScaleBarUnits u );

/** Returns the join style used for drawing lines in the scalebar
* @returns Join style for lines
* @note introduced in 2.3
* @see setLineJoinStyle
*/
Qt::PenJoinStyle lineJoinStyle() const { return mLineJoinStyle; }
/** Sets join style used when drawing the lines in the scalebar
* @param style Join style for lines
* @returns nothing
* @note introduced in 2.3
* @see lineJoinStyle
*/
void setLineJoinStyle( Qt::PenJoinStyle style );

/** Returns the cap style used for drawing lines in the scalebar
* @returns Cap style for lines
* @note introduced in 2.3
* @see setLineCapStyle
*/
Qt::PenCapStyle lineCapStyle() const { return mLineCapStyle; }
/** Sets cap style used when drawing the lines in the scalebar
* @param style Cap style for lines
* @returns nothing
* @note introduced in 2.3
* @see lineCapStyle
*/
void setLineCapStyle( Qt::PenCapStyle style );

/**Apply default settings*/
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
Expand Down Expand Up @@ -203,6 +231,9 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem

ScaleBarUnits mUnits;

Qt::PenJoinStyle mLineJoinStyle;
Qt::PenCapStyle mLineCapStyle;

/**Calculates with of a segment in mm and stores it in mSegmentMillimeters*/
void refreshSegmentMillimeters();

Expand Down
58 changes: 41 additions & 17 deletions src/ui/qgscomposerscalebarwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>337</width>
<height>757</height>
<width>358</width>
<height>429</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -51,9 +51,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<height>792</height>
<y>-406</y>
<width>340</width>
<height>888</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -290,12 +290,6 @@
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_4">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
Expand All @@ -313,14 +307,14 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Labels margin</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="mLabelBarSpaceSpinBox">
<property name="prefix">
<string/>
Expand All @@ -330,14 +324,14 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Line width</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="4" column="1">
<widget class="QDoubleSpinBox" name="mLineWidthSpinBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
Expand All @@ -362,14 +356,34 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Join style</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QgsPenJoinStyleComboBox" name="mLineJoinStyleCombo"/>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Cap style</string>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QgsPenCapStyleComboBox" name="mLineCapStyleCombo"/>
</item>
<item row="11" column="0">
<widget class="QLabel" name="mAlignmentLabel">
<property name="text">
<string>Alignment</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="11" column="1">
<widget class="QComboBox" name="mAlignmentComboBox"/>
</item>
</layout>
Expand Down Expand Up @@ -432,12 +446,22 @@
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsPenJoinStyleComboBox</class>
<extends>QComboBox</extends>
<header>qgspenstylecombobox.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
<header location="global">qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsPenCapStyleComboBox</class>
<extends>QComboBox</extends>
<header>qgspenstylecombobox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down

0 comments on commit 103b3b4

Please sign in to comment.