/
qgscomposerscalebar.cpp
369 lines (313 loc) · 10.5 KB
/
qgscomposerscalebar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/***************************************************************************
qgscomposerscalebar.cpp
-------------------
begin : March 2005
copyright : (C) 2005 by Radim Blazek
email : blazek@itc.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscomposerscalebar.h"
#include "qgscomposermap.h"
#include "qgsscalebarstyle.h"
#include "qgsdoubleboxscalebarstyle.h"
#include "qgsnumericscalebarstyle.h"
#include "qgssingleboxscalebarstyle.h"
#include "qgsticksscalebarstyle.h"
#include "qgsrectangle.h"
#include <QDomDocument>
#include <QDomElement>
#include <QFontMetricsF>
#include <QPainter>
#include <cmath>
QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition ): QgsComposerItem( composition ), mComposerMap( 0 ), mStyle( 0 ), mSegmentMillimeters( 0.0 )
{
applyDefaultSettings();
applyDefaultSize();
}
QgsComposerScaleBar::~QgsComposerScaleBar()
{
delete mStyle;
}
void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
if ( !mStyle || !painter )
{
return;
}
drawBackground( painter );
painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); //draw all text black
//x-offset is half of first label width because labels are drawn centered
QString firstLabel = firstLabelString();
double firstLabelWidth = textWidthMillimeters( mFont, firstLabel );
mStyle->draw( painter, firstLabelWidth / 2 );
//draw frame and selection boxes if necessary
drawFrame( painter );
if ( isSelected() )
{
drawSelectionBoxes( painter );
}
}
void QgsComposerScaleBar::setNumUnitsPerSegment( double units )
{
mNumUnitsPerSegment = units;
refreshSegmentMillimeters();
}
void QgsComposerScaleBar::setComposerMap( const QgsComposerMap* map )
{
disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateSegmentSize() ) );
disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
mComposerMap = map;
if ( !map )
{
return;
}
connect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateSegmentSize() ) );
connect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
refreshSegmentMillimeters();
}
void QgsComposerScaleBar::invalidateCurrentMap()
{
disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateSegmentSize() ) );
disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
mComposerMap = 0;
}
void QgsComposerScaleBar::refreshSegmentMillimeters()
{
if ( mComposerMap )
{
//get extent of composer map
QgsRectangle composerMapRect = mComposerMap->extent();
//get mm dimension of composer map
QRectF composerItemRect = mComposerMap->rect();
//calculate size depending on mNumUnitsPerSegment
mSegmentMillimeters = composerItemRect.width() / composerMapRect.width() * mNumUnitsPerSegment;
}
}
void QgsComposerScaleBar::applyDefaultSettings()
{
mNumSegments = 2;
mNumSegmentsLeft = 0;
mNumMapUnitsPerScaleBarUnit = 1.0;
//style
delete mStyle;
mStyle = new QgsSingleBoxScaleBarStyle( this );
mHeight = 5;
mPen = QPen( QColor( 0, 0, 0 ) );
mPen.setWidthF( 1.0 );
mBrush.setColor( QColor( 0, 0, 0 ) );
mBrush.setStyle( Qt::SolidPattern );
mFont.setPointSizeF( 12.0 );
mLabelBarSpace = 3.0;
mBoxContentSpace = 1.0;
}
void QgsComposerScaleBar::applyDefaultSize()
{
if ( mComposerMap )
{
//calculate mNumUnitsPerSegment
QgsRectangle composerMapRect = mComposerMap->extent();
double proposedScaleBarLength = composerMapRect.width() / 4;
int powerOf10 = int ( pow( 10.0, int ( log( proposedScaleBarLength ) / log( 10.0 ) ) ) ); // from scalebar plugin
int nPow10 = proposedScaleBarLength / powerOf10;
mNumSegments = 2;
mNumUnitsPerSegment = ( nPow10 / 2 ) * powerOf10;
}
refreshSegmentMillimeters();
adjustBoxSize();
}
void QgsComposerScaleBar::adjustBoxSize()
{
if ( !mStyle )
{
return;
}
QRectF box = mStyle->calculateBoxSize();
setSceneRect( box );
}
void QgsComposerScaleBar::update()
{
adjustBoxSize();
QgsComposerItem::update();
}
void QgsComposerScaleBar::updateSegmentSize()
{
refreshSegmentMillimeters();
update();
}
void QgsComposerScaleBar::segmentPositions( QList<QPair<double, double> >& posWidthList ) const
{
posWidthList.clear();
double mCurrentXCoord = mPen.widthF() + mBoxContentSpace;
//left segments
for ( int i = 0; i < mNumSegmentsLeft; ++i )
{
posWidthList.push_back( qMakePair( mCurrentXCoord, mSegmentMillimeters / mNumSegmentsLeft ) );
mCurrentXCoord += mSegmentMillimeters / mNumSegmentsLeft;
}
//right segments
for ( int i = 0; i < mNumSegments; ++i )
{
posWidthList.push_back( qMakePair( mCurrentXCoord, mSegmentMillimeters ) );
mCurrentXCoord += mSegmentMillimeters;
}
}
void QgsComposerScaleBar::setStyle( const QString& styleName )
{
delete mStyle;
mStyle = 0;
//switch depending on style name
if ( styleName == "Single Box" )
{
mStyle = new QgsSingleBoxScaleBarStyle( this );
}
else if ( styleName == "Double Box" )
{
mStyle = new QgsDoubleBoxScaleBarStyle( this );
}
else if ( styleName == "Line Ticks Middle" || styleName == "Line Ticks Down" || styleName == "Line Ticks Up" )
{
QgsTicksScaleBarStyle* tickStyle = new QgsTicksScaleBarStyle( this );
if ( styleName == "Line Ticks Middle" )
{
tickStyle->setTickPosition( QgsTicksScaleBarStyle::TicksMiddle );
}
else if ( styleName == "Line Ticks Down" )
{
tickStyle->setTickPosition( QgsTicksScaleBarStyle::TicksDown );
}
else if ( styleName == "Line Ticks Up" )
{
tickStyle->setTickPosition( QgsTicksScaleBarStyle::TicksUp );
}
mStyle = tickStyle;
}
else if ( styleName == "Numeric" )
{
mStyle = new QgsNumericScaleBarStyle( this );
}
}
QString QgsComposerScaleBar::style() const
{
if ( mStyle )
{
return mStyle->name();
}
else
{
return "";
}
}
QString QgsComposerScaleBar::firstLabelString() const
{
if ( mNumSegmentsLeft > 0 )
{
return QString::number( mNumUnitsPerSegment / mNumMapUnitsPerScaleBarUnit );
}
else
{
return "0";
}
}
QFont QgsComposerScaleBar::font() const
{
return mFont;
}
void QgsComposerScaleBar::setFont( const QFont& font )
{
mFont = font;
adjustBoxSize();
update();
}
bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
if ( elem.isNull() )
{
return false;
}
QDomElement composerScaleBarElem = doc.createElement( "ComposerScaleBar" );
composerScaleBarElem.setAttribute( "height", mHeight );
composerScaleBarElem.setAttribute( "labelBarSpace", mLabelBarSpace );
composerScaleBarElem.setAttribute( "boxContentSpace", mBoxContentSpace );
composerScaleBarElem.setAttribute( "numSegments", mNumSegments );
composerScaleBarElem.setAttribute( "numSegmentsLeft", mNumSegmentsLeft );
composerScaleBarElem.setAttribute( "numUnitsPerSegment", mNumUnitsPerSegment );
composerScaleBarElem.setAttribute( "segmentMillimeters", mSegmentMillimeters );
composerScaleBarElem.setAttribute( "numMapUnitsPerScaleBarUnit", mNumMapUnitsPerScaleBarUnit );
composerScaleBarElem.setAttribute( "font", mFont.toString() );
composerScaleBarElem.setAttribute( "outlineWidth", mPen.widthF() );
composerScaleBarElem.setAttribute( "unitLabel", mUnitLabeling );
//style
if ( mStyle )
{
composerScaleBarElem.setAttribute( "style", mStyle->name() );
}
//map id
if ( mComposerMap )
{
composerScaleBarElem.setAttribute( "mapId", mComposerMap->id() );
}
//fill color
QColor brushColor = mBrush.color();
QDomElement colorElem = doc.createElement( "BrushColor" );
colorElem.setAttribute( "red", brushColor.red() );
colorElem.setAttribute( "green", brushColor.green() );
colorElem.setAttribute( "blue", brushColor.blue() );
composerScaleBarElem.appendChild( colorElem );
elem.appendChild( composerScaleBarElem );
return _writeXML( composerScaleBarElem, doc );
}
bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocument& doc )
{
if ( itemElem.isNull() )
{
return false;
}
mHeight = itemElem.attribute( "height", "5.0" ).toDouble();
mLabelBarSpace = itemElem.attribute( "labelBarSpace", "3.0" ).toDouble();
mBoxContentSpace = itemElem.attribute( "boxContentSpace", "1.0" ).toDouble();
mNumSegments = itemElem.attribute( "numSegments", "2" ).toInt();
mNumSegmentsLeft = itemElem.attribute( "numSegmentsLeft", "0" ).toInt();
mNumUnitsPerSegment = itemElem.attribute( "numUnitsPerSegment", "1.0" ).toDouble();
mSegmentMillimeters = itemElem.attribute( "segmentMillimeters", "0.0" ).toDouble();
mNumMapUnitsPerScaleBarUnit = itemElem.attribute( "numMapUnitsPerScaleBarUnit", "1.0" ).toDouble();
mPen.setWidthF( itemElem.attribute( "outlineWidth", "1.0" ).toDouble() );
mUnitLabeling = itemElem.attribute( "unitLabel" );
QString fontString = itemElem.attribute( "font", "" );
if ( !fontString.isEmpty() )
{
mFont.fromString( fontString );
}
//style
delete mStyle;
mStyle = 0;
QString styleString = itemElem.attribute( "style", "" );
setStyle( tr( styleString.toLocal8Bit().data() ) );
//map
int mapId = itemElem.attribute( "mapId", "-1" ).toInt();
if ( mapId >= 0 )
{
const QgsComposerMap* composerMap = mComposition->getComposerMapById( mapId );
mComposerMap = composerMap;
if ( mComposerMap )
{
connect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateSegmentSize() ) );
connect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
}
}
refreshSegmentMillimeters();
//restore general composer item properties
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
if ( composerItemList.size() > 0 )
{
QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
_readXML( composerItemElem, doc );
}
return true;
}