Skip to content

Commit

Permalink
Merge branch 'master' of github.com:qgis/Quantum-GIS
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Nov 29, 2011
2 parents 2bd2d84 + 4b86c71 commit ad8ae1b
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 20 deletions.
15 changes: 15 additions & 0 deletions python/core/qgscomposerscalebar.sip
Expand Up @@ -9,6 +9,14 @@ class QgsComposerScaleBar: QgsComposerItem
%End
public:

/**Added in version 1.8*/
enum Alignment
{
Left = 0,
Middle,
Right
};

QgsComposerScaleBar( QgsComposition* composition /TransferThis/);
~QgsComposerScaleBar();

Expand Down Expand Up @@ -58,6 +66,13 @@ class QgsComposerScaleBar: QgsComposerItem

double segmentMillimeters() const;

/**Left / Middle/ Right
@note: this method was added in version 1.8*/
Alignment alignment() const;

/**@note: this method was added in version 1.8*/
void setAlignment( Alignment a );

/**Apply default settings*/
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
Expand Down
23 changes: 23 additions & 0 deletions src/app/composer/qgscomposerscalebarwidget.cpp
Expand Up @@ -31,12 +31,19 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
toolBox->addItem( itemPropertiesWidget, tr( "General options" ) );

blockMemberSignals( true );

//style combo box
mStyleComboBox->insertItem( 0, tr( "Single Box" ) );
mStyleComboBox->insertItem( 1, tr( "Double Box" ) );
mStyleComboBox->insertItem( 2, tr( "Line Ticks Middle" ) );
mStyleComboBox->insertItem( 3, tr( "Line Ticks Down" ) );
mStyleComboBox->insertItem( 4, tr( "Line Ticks Up" ) );
mStyleComboBox->insertItem( 5, tr( "Numeric" ) );

mAlignmentComboBox->insertItem( 0, tr( "Left" ) );
mAlignmentComboBox->insertItem( 1, tr( "Middle" ) );
mAlignmentComboBox->insertItem( 2, tr( "Right" ) );

setGuiElements(); //set the GUI elements to the state of scaleBar
blockMemberSignals( false );
}
Expand Down Expand Up @@ -161,6 +168,9 @@ void QgsComposerScaleBarWidget::setGuiElements()
//style...
QString style = mComposerScaleBar->style();
mStyleComboBox->setCurrentIndex( mStyleComboBox->findText( tr( style.toLocal8Bit().data() ) ) );

//alignment
mAlignmentComboBox->setCurrentIndex(( int )( mComposerScaleBar->alignment() ) );
}

//slots
Expand Down Expand Up @@ -371,6 +381,18 @@ void QgsComposerScaleBarWidget::on_mBoxSizeSpinBox_valueChanged( double d )
mComposerScaleBar->endCommand();
}

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

mComposerScaleBar->beginCommand( tr( "Scalebar alignment" ) );
mComposerScaleBar->setAlignment(( QgsComposerScaleBar::Alignment ) index );
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
{
mSegmentSizeSpinBox->blockSignals( block );
Expand All @@ -384,4 +406,5 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mLineWidthSpinBox->blockSignals( block );
mLabelBarSpaceSpinBox->blockSignals( block );
mBoxSizeSpinBox->blockSignals( block );
mAlignmentComboBox->blockSignals( block );
}
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerscalebarwidget.h
Expand Up @@ -46,6 +46,7 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
void on_mStyleComboBox_currentIndexChanged( const QString& text );
void on_mLabelBarSpaceSpinBox_valueChanged( double d );
void on_mBoxSizeSpinBox_valueChanged( double d );
void on_mAlignmentComboBox_currentIndexChanged( int index );


protected:
Expand Down
5 changes: 4 additions & 1 deletion src/app/composer/qgscomposershapewidget.cpp
Expand Up @@ -39,7 +39,10 @@ QgsComposerShapeWidget::QgsComposerShapeWidget( QgsComposerShape* composerShape

blockAllSignals( false );

connect( mShapeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGuiElementValues() ) );
if ( mComposerShape )
{
connect( mComposerShape, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
}
}

QgsComposerShapeWidget::~QgsComposerShapeWidget()
Expand Down
75 changes: 74 additions & 1 deletion src/core/composer/qgscomposerscalebar.cpp
Expand Up @@ -28,7 +28,8 @@
#include <QPainter>
#include <cmath>

QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition ): QgsComposerItem( composition ), mComposerMap( 0 ), mStyle( 0 ), mSegmentMillimeters( 0.0 )
QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition ): QgsComposerItem( composition ), mComposerMap( 0 ), mStyle( 0 ),
mSegmentMillimeters( 0.0 ), mAlignment( Left )
{
applyDefaultSettings();
applyDefaultSize();
Expand Down Expand Up @@ -65,10 +66,57 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
}
}

void QgsComposerScaleBar::setNumSegments( int nSegments )
{
if ( !mStyle )
{
mNumSegments = nSegments;
return;
}
double width = mStyle->calculateBoxSize().width();
mNumSegments = nSegments;
double widthAfter = mStyle->calculateBoxSize().width();
correctXPositionAlignment( width, widthAfter );
}

void QgsComposerScaleBar::setNumUnitsPerSegment( double units )
{
if ( !mStyle )
{
mNumUnitsPerSegment = units;
return;
}
double width = mStyle->calculateBoxSize().width();
mNumUnitsPerSegment = units;
refreshSegmentMillimeters();
double widthAfter = mStyle->calculateBoxSize().width();
correctXPositionAlignment( width, widthAfter );
}

void QgsComposerScaleBar::setNumSegmentsLeft( int nSegmentsLeft )
{
if ( !mStyle )
{
mNumSegmentsLeft = nSegmentsLeft;
return;
}
double width = mStyle->calculateBoxSize().width();
mNumSegmentsLeft = nSegmentsLeft;
double widthAfter = mStyle->calculateBoxSize().width();
correctXPositionAlignment( width, widthAfter );
}

void QgsComposerScaleBar::setBoxContentSpace( double space )
{
if ( !mStyle )
{
mBoxContentSpace = space;
return;
}
double width = mStyle->calculateBoxSize().width();
mBoxContentSpace = space;
double widthAfter = mStyle->calculateBoxSize().width();
correctXPositionAlignment( width, widthAfter );
}

void QgsComposerScaleBar::setComposerMap( const QgsComposerMap* map )
Expand Down Expand Up @@ -172,7 +220,14 @@ void QgsComposerScaleBar::update()

void QgsComposerScaleBar::updateSegmentSize()
{
if ( !mStyle )
{
return;
}
double width = mStyle->calculateBoxSize().width();
refreshSegmentMillimeters();
double widthAfter = mStyle->calculateBoxSize().width();
correctXPositionAlignment( width, widthAfter );
update();
}

Expand Down Expand Up @@ -309,6 +364,9 @@ bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) cons
colorElem.setAttribute( "blue", brushColor.blue() );
composerScaleBarElem.appendChild( colorElem );

//alignment
composerScaleBarElem.setAttribute( "alignment", QString::number(( int ) mAlignment ) );

elem.appendChild( composerScaleBarElem );
return _writeXML( composerScaleBarElem, doc );
}
Expand Down Expand Up @@ -357,6 +415,9 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume

refreshSegmentMillimeters();

//alignment
mAlignment = ( Alignment )( itemElem.attribute( "alignment", "0" ).toInt() );

//restore general composer item properties
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
if ( composerItemList.size() > 0 )
Expand All @@ -368,4 +429,16 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
return true;
}

void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
{
if ( mAlignment == Middle )
{
move( -( widthAfter - width ) / 2.0, 0 );
}
else if ( mAlignment == Right )
{
move( -( widthAfter - width ), 0 );
}
}


26 changes: 23 additions & 3 deletions src/core/composer/qgscomposerscalebar.h
Expand Up @@ -32,6 +32,14 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem

public:

/**Added in version 1.8*/
enum Alignment
{
Left = 0,
Middle,
Right
};

QgsComposerScaleBar( QgsComposition* composition );
~QgsComposerScaleBar();

Expand All @@ -43,10 +51,10 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem

//getters and setters
int numSegments() const {return mNumSegments;}
void setNumSegments( int nSegments ) {mNumSegments = nSegments;}
void setNumSegments( int nSegments );

int numSegmentsLeft() const {return mNumSegmentsLeft;}
void setNumSegmentsLeft( int nSegmentsLeft ) {mNumSegmentsLeft = nSegmentsLeft;}
void setNumSegmentsLeft( int nSegmentsLeft );

double numUnitsPerSegment() const {return mNumUnitsPerSegment;}
void setNumUnitsPerSegment( double units );
Expand Down Expand Up @@ -77,10 +85,17 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
void setLabelBarSpace( double space ) {mLabelBarSpace = space;}

double boxContentSpace() const {return mBoxContentSpace;}
void setBoxContentSpace( double space ) {mBoxContentSpace = space;}
void setBoxContentSpace( double space );

double segmentMillimeters() const {return mSegmentMillimeters;}

/**Left / Middle/ Right
@note: this method was added in version 1.8*/
Alignment alignment() const { return mAlignment; }

/**@note: this method was added in version 1.8*/
void setAlignment( Alignment a ) { mAlignment = a; }

/**Apply default settings*/
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
Expand Down Expand Up @@ -119,6 +134,9 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

/**Moves scalebar position to the left / right depending on alignment and change in item width*/
void correctXPositionAlignment( double width, double widthAfter );

public slots:
void updateSegmentSize();
/**Sets mCompositionMap to 0 if the map is deleted*/
Expand Down Expand Up @@ -159,6 +177,8 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
/**Width of a segment (in mm)*/
double mSegmentMillimeters;

Alignment mAlignment;

/**Calculates with of a segment in mm and stores it in mSegmentMillimeters*/
void refreshSegmentMillimeters();
};
Expand Down
18 changes: 14 additions & 4 deletions src/core/composer/qgsnumericscalebarstyle.cpp
Expand Up @@ -20,12 +20,12 @@
#include <QList>
#include <QPainter>

QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar )
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar ), mLastScaleBarWidth( 0 )
{

}

QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 )
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 ), mLastScaleBarWidth( 0 )
{

}
Expand Down Expand Up @@ -67,9 +67,17 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );

return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
rect = QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
+ 2 * mScaleBar->pen().width() + textWidth,
textHeight + 2 * mScaleBar->boxContentSpace() );

if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 )
{
//hack to move scale bar the the left / right in order to keep the bar alignment
const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
}
mLastScaleBarWidth = rect.width();
return rect;
}

QString QgsNumericScaleBarStyle::scaleText() const
Expand All @@ -78,12 +86,14 @@ QString QgsNumericScaleBarStyle::scaleText() const
if ( mScaleBar )
{
//find out scale
double scaleDenominator = 1;
const QgsComposerMap* composerMap = mScaleBar->composerMap();
if ( composerMap )
{
double scaleDenominator = composerMap->scale();
scaleDenominator = composerMap->scale();
scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
}
scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
}
return scaleBarText;
}
3 changes: 3 additions & 0 deletions src/core/composer/qgsnumericscalebarstyle.h
Expand Up @@ -39,6 +39,9 @@ class CORE_EXPORT QgsNumericScaleBarStyle: public QgsScaleBarStyle
QgsNumericScaleBarStyle(); //forbidden
/**Returns the text for the scale bar or an empty string in case of error*/
QString scaleText() const;

/**Store last width (in mm) to keep alignement to left/middle/right side*/
mutable double mLastScaleBarWidth;
};

#endif

0 comments on commit ad8ae1b

Please sign in to comment.