Skip to content
Permalink
Browse files
Merge branch 'master' of github.com:qgis/Quantum-GIS
  • Loading branch information
timlinux committed Nov 29, 2011
2 parents 2bd2d84 + 4b86c71 commit ad8ae1bda8d2d0a956bede26aa7253b8f49bb729
@@ -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();

@@ -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)
@@ -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 );
}
@@ -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
@@ -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 );
@@ -384,4 +406,5 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mLineWidthSpinBox->blockSignals( block );
mLabelBarSpaceSpinBox->blockSignals( block );
mBoxSizeSpinBox->blockSignals( block );
mAlignmentComboBox->blockSignals( block );
}
@@ -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:
@@ -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()
@@ -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();
@@ -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 )
@@ -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();
}

@@ -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 );
}
@@ -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 )
@@ -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 );
}
}


@@ -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();

@@ -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 );
@@ -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)
@@ -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*/
@@ -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();
};
@@ -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 )
{

}
@@ -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
@@ -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;
}
@@ -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.