Skip to content

Commit ad8ae1b

Browse files
committedNov 29, 2011
Merge branch 'master' of github.com:qgis/Quantum-GIS
2 parents 2bd2d84 + 4b86c71 commit ad8ae1b

9 files changed

+178
-20
lines changed
 

‎python/core/qgscomposerscalebar.sip

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ class QgsComposerScaleBar: QgsComposerItem
99
%End
1010
public:
1111

12+
/**Added in version 1.8*/
13+
enum Alignment
14+
{
15+
Left = 0,
16+
Middle,
17+
Right
18+
};
19+
1220
QgsComposerScaleBar( QgsComposition* composition /TransferThis/);
1321
~QgsComposerScaleBar();
1422

@@ -58,6 +66,13 @@ class QgsComposerScaleBar: QgsComposerItem
5866

5967
double segmentMillimeters() const;
6068

69+
/**Left / Middle/ Right
70+
@note: this method was added in version 1.8*/
71+
Alignment alignment() const;
72+
73+
/**@note: this method was added in version 1.8*/
74+
void setAlignment( Alignment a );
75+
6176
/**Apply default settings*/
6277
void applyDefaultSettings();
6378
/**Apply default size (scale bar 1/5 of map item width)

‎src/app/composer/qgscomposerscalebarwidget.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
3131
toolBox->addItem( itemPropertiesWidget, tr( "General options" ) );
3232

3333
blockMemberSignals( true );
34+
35+
//style combo box
3436
mStyleComboBox->insertItem( 0, tr( "Single Box" ) );
3537
mStyleComboBox->insertItem( 1, tr( "Double Box" ) );
3638
mStyleComboBox->insertItem( 2, tr( "Line Ticks Middle" ) );
3739
mStyleComboBox->insertItem( 3, tr( "Line Ticks Down" ) );
3840
mStyleComboBox->insertItem( 4, tr( "Line Ticks Up" ) );
3941
mStyleComboBox->insertItem( 5, tr( "Numeric" ) );
42+
43+
mAlignmentComboBox->insertItem( 0, tr( "Left" ) );
44+
mAlignmentComboBox->insertItem( 1, tr( "Middle" ) );
45+
mAlignmentComboBox->insertItem( 2, tr( "Right" ) );
46+
4047
setGuiElements(); //set the GUI elements to the state of scaleBar
4148
blockMemberSignals( false );
4249
}
@@ -161,6 +168,9 @@ void QgsComposerScaleBarWidget::setGuiElements()
161168
//style...
162169
QString style = mComposerScaleBar->style();
163170
mStyleComboBox->setCurrentIndex( mStyleComboBox->findText( tr( style.toLocal8Bit().data() ) ) );
171+
172+
//alignment
173+
mAlignmentComboBox->setCurrentIndex(( int )( mComposerScaleBar->alignment() ) );
164174
}
165175

166176
//slots
@@ -371,6 +381,18 @@ void QgsComposerScaleBarWidget::on_mBoxSizeSpinBox_valueChanged( double d )
371381
mComposerScaleBar->endCommand();
372382
}
373383

384+
void QgsComposerScaleBarWidget::on_mAlignmentComboBox_currentIndexChanged( int index )
385+
{
386+
if ( !mComposerScaleBar )
387+
{
388+
return;
389+
}
390+
391+
mComposerScaleBar->beginCommand( tr( "Scalebar alignment" ) );
392+
mComposerScaleBar->setAlignment(( QgsComposerScaleBar::Alignment ) index );
393+
mComposerScaleBar->endCommand();
394+
}
395+
374396
void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
375397
{
376398
mSegmentSizeSpinBox->blockSignals( block );
@@ -384,4 +406,5 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
384406
mLineWidthSpinBox->blockSignals( block );
385407
mLabelBarSpaceSpinBox->blockSignals( block );
386408
mBoxSizeSpinBox->blockSignals( block );
409+
mAlignmentComboBox->blockSignals( block );
387410
}

‎src/app/composer/qgscomposerscalebarwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
4646
void on_mStyleComboBox_currentIndexChanged( const QString& text );
4747
void on_mLabelBarSpaceSpinBox_valueChanged( double d );
4848
void on_mBoxSizeSpinBox_valueChanged( double d );
49+
void on_mAlignmentComboBox_currentIndexChanged( int index );
4950

5051

5152
protected:

‎src/app/composer/qgscomposershapewidget.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ QgsComposerShapeWidget::QgsComposerShapeWidget( QgsComposerShape* composerShape
3939

4040
blockAllSignals( false );
4141

42-
connect( mShapeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setGuiElementValues() ) );
42+
if ( mComposerShape )
43+
{
44+
connect( mComposerShape, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
45+
}
4346
}
4447

4548
QgsComposerShapeWidget::~QgsComposerShapeWidget()

‎src/core/composer/qgscomposerscalebar.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#include <QPainter>
2929
#include <cmath>
3030

31-
QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition ): QgsComposerItem( composition ), mComposerMap( 0 ), mStyle( 0 ), mSegmentMillimeters( 0.0 )
31+
QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition ): QgsComposerItem( composition ), mComposerMap( 0 ), mStyle( 0 ),
32+
mSegmentMillimeters( 0.0 ), mAlignment( Left )
3233
{
3334
applyDefaultSettings();
3435
applyDefaultSize();
@@ -65,10 +66,57 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
6566
}
6667
}
6768

69+
void QgsComposerScaleBar::setNumSegments( int nSegments )
70+
{
71+
if ( !mStyle )
72+
{
73+
mNumSegments = nSegments;
74+
return;
75+
}
76+
double width = mStyle->calculateBoxSize().width();
77+
mNumSegments = nSegments;
78+
double widthAfter = mStyle->calculateBoxSize().width();
79+
correctXPositionAlignment( width, widthAfter );
80+
}
81+
6882
void QgsComposerScaleBar::setNumUnitsPerSegment( double units )
6983
{
84+
if ( !mStyle )
85+
{
86+
mNumUnitsPerSegment = units;
87+
return;
88+
}
89+
double width = mStyle->calculateBoxSize().width();
7090
mNumUnitsPerSegment = units;
7191
refreshSegmentMillimeters();
92+
double widthAfter = mStyle->calculateBoxSize().width();
93+
correctXPositionAlignment( width, widthAfter );
94+
}
95+
96+
void QgsComposerScaleBar::setNumSegmentsLeft( int nSegmentsLeft )
97+
{
98+
if ( !mStyle )
99+
{
100+
mNumSegmentsLeft = nSegmentsLeft;
101+
return;
102+
}
103+
double width = mStyle->calculateBoxSize().width();
104+
mNumSegmentsLeft = nSegmentsLeft;
105+
double widthAfter = mStyle->calculateBoxSize().width();
106+
correctXPositionAlignment( width, widthAfter );
107+
}
108+
109+
void QgsComposerScaleBar::setBoxContentSpace( double space )
110+
{
111+
if ( !mStyle )
112+
{
113+
mBoxContentSpace = space;
114+
return;
115+
}
116+
double width = mStyle->calculateBoxSize().width();
117+
mBoxContentSpace = space;
118+
double widthAfter = mStyle->calculateBoxSize().width();
119+
correctXPositionAlignment( width, widthAfter );
72120
}
73121

74122
void QgsComposerScaleBar::setComposerMap( const QgsComposerMap* map )
@@ -172,7 +220,14 @@ void QgsComposerScaleBar::update()
172220

173221
void QgsComposerScaleBar::updateSegmentSize()
174222
{
223+
if ( !mStyle )
224+
{
225+
return;
226+
}
227+
double width = mStyle->calculateBoxSize().width();
175228
refreshSegmentMillimeters();
229+
double widthAfter = mStyle->calculateBoxSize().width();
230+
correctXPositionAlignment( width, widthAfter );
176231
update();
177232
}
178233

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

367+
//alignment
368+
composerScaleBarElem.setAttribute( "alignment", QString::number(( int ) mAlignment ) );
369+
312370
elem.appendChild( composerScaleBarElem );
313371
return _writeXML( composerScaleBarElem, doc );
314372
}
@@ -357,6 +415,9 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
357415

358416
refreshSegmentMillimeters();
359417

418+
//alignment
419+
mAlignment = ( Alignment )( itemElem.attribute( "alignment", "0" ).toInt() );
420+
360421
//restore general composer item properties
361422
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
362423
if ( composerItemList.size() > 0 )
@@ -368,4 +429,16 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
368429
return true;
369430
}
370431

432+
void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
433+
{
434+
if ( mAlignment == Middle )
435+
{
436+
move( -( widthAfter - width ) / 2.0, 0 );
437+
}
438+
else if ( mAlignment == Right )
439+
{
440+
move( -( widthAfter - width ), 0 );
441+
}
442+
}
443+
371444

‎src/core/composer/qgscomposerscalebar.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
3232

3333
public:
3434

35+
/**Added in version 1.8*/
36+
enum Alignment
37+
{
38+
Left = 0,
39+
Middle,
40+
Right
41+
};
42+
3543
QgsComposerScaleBar( QgsComposition* composition );
3644
~QgsComposerScaleBar();
3745

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

4452
//getters and setters
4553
int numSegments() const {return mNumSegments;}
46-
void setNumSegments( int nSegments ) {mNumSegments = nSegments;}
54+
void setNumSegments( int nSegments );
4755

4856
int numSegmentsLeft() const {return mNumSegmentsLeft;}
49-
void setNumSegmentsLeft( int nSegmentsLeft ) {mNumSegmentsLeft = nSegmentsLeft;}
57+
void setNumSegmentsLeft( int nSegmentsLeft );
5058

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

7987
double boxContentSpace() const {return mBoxContentSpace;}
80-
void setBoxContentSpace( double space ) {mBoxContentSpace = space;}
88+
void setBoxContentSpace( double space );
8189

8290
double segmentMillimeters() const {return mSegmentMillimeters;}
8391

92+
/**Left / Middle/ Right
93+
@note: this method was added in version 1.8*/
94+
Alignment alignment() const { return mAlignment; }
95+
96+
/**@note: this method was added in version 1.8*/
97+
void setAlignment( Alignment a ) { mAlignment = a; }
98+
8499
/**Apply default settings*/
85100
void applyDefaultSettings();
86101
/**Apply default size (scale bar 1/5 of map item width)
@@ -119,6 +134,9 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
119134
*/
120135
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
121136

137+
/**Moves scalebar position to the left / right depending on alignment and change in item width*/
138+
void correctXPositionAlignment( double width, double widthAfter );
139+
122140
public slots:
123141
void updateSegmentSize();
124142
/**Sets mCompositionMap to 0 if the map is deleted*/
@@ -159,6 +177,8 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
159177
/**Width of a segment (in mm)*/
160178
double mSegmentMillimeters;
161179

180+
Alignment mAlignment;
181+
162182
/**Calculates with of a segment in mm and stores it in mSegmentMillimeters*/
163183
void refreshSegmentMillimeters();
164184
};

‎src/core/composer/qgsnumericscalebarstyle.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#include <QList>
2121
#include <QPainter>
2222

23-
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar )
23+
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar ), mLastScaleBarWidth( 0 )
2424
{
2525

2626
}
2727

28-
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 )
28+
QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 ), mLastScaleBarWidth( 0 )
2929
{
3030

3131
}
@@ -67,9 +67,17 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
6767
double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
6868
double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );
6969

70-
return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
70+
rect = QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
7171
+ 2 * mScaleBar->pen().width() + textWidth,
7272
textHeight + 2 * mScaleBar->boxContentSpace() );
73+
74+
if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 )
75+
{
76+
//hack to move scale bar the the left / right in order to keep the bar alignment
77+
const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
78+
}
79+
mLastScaleBarWidth = rect.width();
80+
return rect;
7381
}
7482

7583
QString QgsNumericScaleBarStyle::scaleText() const
@@ -78,12 +86,14 @@ QString QgsNumericScaleBarStyle::scaleText() const
7886
if ( mScaleBar )
7987
{
8088
//find out scale
89+
double scaleDenominator = 1;
8190
const QgsComposerMap* composerMap = mScaleBar->composerMap();
8291
if ( composerMap )
8392
{
84-
double scaleDenominator = composerMap->scale();
93+
scaleDenominator = composerMap->scale();
8594
scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
8695
}
96+
scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
8797
}
8898
return scaleBarText;
8999
}

‎src/core/composer/qgsnumericscalebarstyle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class CORE_EXPORT QgsNumericScaleBarStyle: public QgsScaleBarStyle
3939
QgsNumericScaleBarStyle(); //forbidden
4040
/**Returns the text for the scale bar or an empty string in case of error*/
4141
QString scaleText() const;
42+
43+
/**Store last width (in mm) to keep alignement to left/middle/right side*/
44+
mutable double mLastScaleBarWidth;
4245
};
4346

4447
#endif

‎src/ui/qgscomposerscalebarwidgetbase.ui

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<rect>
3434
<x>0</x>
3535
<y>0</y>
36-
<width>307</width>
37-
<height>616</height>
36+
<width>298</width>
37+
<height>545</height>
3838
</rect>
3939
</property>
4040
<attribute name="label">
@@ -143,7 +143,7 @@
143143
</property>
144144
</widget>
145145
</item>
146-
<item row="10" column="0">
146+
<item row="12" column="0">
147147
<widget class="QSpinBox" name="mHeightSpinBox">
148148
<property name="suffix">
149149
<string> mm</string>
@@ -153,7 +153,7 @@
153153
</property>
154154
</widget>
155155
</item>
156-
<item row="11" column="0">
156+
<item row="13" column="0">
157157
<widget class="QDoubleSpinBox" name="mLineWidthSpinBox">
158158
<property name="sizePolicy">
159159
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -178,7 +178,7 @@
178178
</property>
179179
</widget>
180180
</item>
181-
<item row="12" column="0">
181+
<item row="14" column="0">
182182
<widget class="QDoubleSpinBox" name="mLabelBarSpaceSpinBox">
183183
<property name="prefix">
184184
<string>Label space </string>
@@ -188,7 +188,7 @@
188188
</property>
189189
</widget>
190190
</item>
191-
<item row="13" column="0">
191+
<item row="15" column="0">
192192
<widget class="QDoubleSpinBox" name="mBoxSizeSpinBox">
193193
<property name="prefix">
194194
<string>Box space </string>
@@ -198,7 +198,7 @@
198198
</property>
199199
</widget>
200200
</item>
201-
<item row="14" column="0">
201+
<item row="16" column="0">
202202
<widget class="QLabel" name="mUnitLabelLabel">
203203
<property name="text">
204204
<string>Unit label</string>
@@ -211,10 +211,10 @@
211211
</property>
212212
</widget>
213213
</item>
214-
<item row="15" column="0">
214+
<item row="17" column="0">
215215
<widget class="QLineEdit" name="mUnitLabelLineEdit"/>
216216
</item>
217-
<item row="16" column="0">
217+
<item row="18" column="0">
218218
<widget class="QPushButton" name="mFontButton">
219219
<property name="sizePolicy">
220220
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -227,14 +227,14 @@
227227
</property>
228228
</widget>
229229
</item>
230-
<item row="17" column="0">
230+
<item row="19" column="0">
231231
<widget class="QPushButton" name="mColorPushButton">
232232
<property name="text">
233233
<string>Color...</string>
234234
</property>
235235
</widget>
236236
</item>
237-
<item row="18" column="0">
237+
<item row="20" column="0">
238238
<spacer name="verticalSpacer">
239239
<property name="orientation">
240240
<enum>Qt::Vertical</enum>
@@ -247,6 +247,16 @@
247247
</property>
248248
</spacer>
249249
</item>
250+
<item row="11" column="0">
251+
<widget class="QComboBox" name="mAlignmentComboBox"/>
252+
</item>
253+
<item row="10" column="0">
254+
<widget class="QLabel" name="mAlignmentLabel">
255+
<property name="text">
256+
<string>Alignment</string>
257+
</property>
258+
</widget>
259+
</item>
250260
</layout>
251261
</widget>
252262
</widget>

0 commit comments

Comments
 (0)
Please sign in to comment.