-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdecorationscalebar.cpp
511 lines (457 loc) · 19.2 KB
/
qgsdecorationscalebar.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/***************************************************************************
plugin.cpp
Plugin to draw scale bar on map
Functions:
-------------------
begin : Jun 1, 2004
copyright : (C) 2004 by Peter Brewer
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsdecorationscalebar.h"
#include "qgsdecorationscalebardialog.h"
#include "qgis.h"
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaptopixel.h"
#include "qgspoint.h"
#include "qgsproject.h"
#include "qgsunittypes.h"
#include "qgssettings.h"
#include <QPainter>
#include <QAction>
#include <QPen>
#include <QPolygon>
#include <QString>
#include <QFontMetrics>
#include <QFont>
#include <QColor>
#include <QMenu>
#include <QFile>
#include <QLocale>
//non qt includes
#include <cmath>
QgsDecorationScaleBar::QgsDecorationScaleBar( QObject *parent )
: QgsDecorationItem( parent )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mPlacement = TopLeft;
mMarginUnit = QgsUnitTypes::RenderMillimeters;
mStyleLabels << tr( "Tick Down" ) << tr( "Tick Up" )
<< tr( "Bar" ) << tr( "Box" );
setName( "Scale Bar" );
projectRead();
}
QgsDecorationScaleBar::~QgsDecorationScaleBar()
{
}
void QgsDecorationScaleBar::projectRead()
{
QgsDecorationItem::projectRead();
mPreferredSize = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/PreferredSize" ), 30 );
mStyleIndex = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/Style" ), 0 );
mSnapping = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Snapping" ), true );
int myRedInt = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/ColorRedPart" ), 0 );
int myGreenInt = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/ColorGreenPart" ), 0 );
int myBlueInt = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/ColorBluePart" ), 0 );
mColor = QColor( myRedInt, myGreenInt, myBlueInt );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
}
void QgsDecorationScaleBar::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/PreferredSize" ), mPreferredSize );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Snapping" ), mSnapping );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Style" ), mStyleIndex );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/ColorRedPart" ), mColor.red() );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/ColorGreenPart" ), mColor.green() );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/ColorBluePart" ), mColor.blue() );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );
}
void QgsDecorationScaleBar::run()
{
QgsDecorationScaleBarDialog dlg( *this, QgisApp::instance()->mapCanvas()->mapUnits(), QgisApp::instance() );
dlg.exec();
}
void QgsDecorationScaleBar::render( QPainter *theQPainter )
{
QgsMapCanvas *canvas = QgisApp::instance()->mapCanvas();
int myBufferSize = 1; //softcode this later
//Get canvas dimensions
int myCanvasHeight = theQPainter->device()->height();
int myCanvasWidth = theQPainter->device()->width();
//Get map units per pixel. This can be negative at times (to do with
//projections) and that just confuses the rest of the code in this
//function, so force to a positive number.
double myMapUnitsPerPixelDouble = qAbs( canvas->mapUnitsPerPixel() );
double myActualSize = mPreferredSize;
// Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze
int myLayerCount = canvas->layerCount();
if ( !myLayerCount || !myCanvasWidth || !myMapUnitsPerPixelDouble )
return;
//Large if statement which determines whether to render the scale bar
if ( enabled() )
{
// Hard coded sizes
int myMajorTickSize = 8;
int myTextOffsetX = 3;
QgsSettings settings;
bool ok = false;
QgsUnitTypes::DistanceUnit myPreferredUnits = QgsUnitTypes::decodeDistanceUnit( settings.value( QStringLiteral( "qgis/measure/displayunits" ) ).toString(), &ok );
if ( !ok )
myPreferredUnits = QgsUnitTypes::DistanceMeters;
QgsUnitTypes::DistanceUnit myMapUnits = canvas->mapUnits();
// Adjust units meter/feet/... or vice versa
myMapUnitsPerPixelDouble *= QgsUnitTypes::fromUnitToUnitFactor( myMapUnits, myPreferredUnits );
myMapUnits = myPreferredUnits;
//Calculate size of scale bar for preferred number of map units
double myScaleBarWidth = mPreferredSize / myMapUnitsPerPixelDouble;
//If scale bar is very small reset to 1/4 of the canvas wide
if ( myScaleBarWidth < 30 )
{
myScaleBarWidth = myCanvasWidth / 4.0; // pixels
myActualSize = myScaleBarWidth * myMapUnitsPerPixelDouble; // map units
}
//if scale bar is more than half the canvas wide keep halving until not
while ( myScaleBarWidth > myCanvasWidth / 3.0 )
{
myScaleBarWidth = myScaleBarWidth / 3;
}
myActualSize = myScaleBarWidth * myMapUnitsPerPixelDouble;
// Work out the exponent for the number - e.g, 1234 will give 3,
// and .001234 will give -3
double myPowerOf10 = floor( log10( myActualSize ) );
// snap to integer < 10 times power of 10
if ( mSnapping )
{
double scaler = pow( 10.0, myPowerOf10 );
myActualSize = qRound( myActualSize / scaler ) * scaler;
myScaleBarWidth = myActualSize / myMapUnitsPerPixelDouble;
}
//Get type of map units and set scale bar unit label text
QString myScaleBarUnitLabel;
switch ( myMapUnits )
{
case QgsUnitTypes::DistanceMeters:
if ( myActualSize > 1000.0 )
{
myScaleBarUnitLabel = tr( " km" );
myActualSize = myActualSize / 1000;
}
else if ( myActualSize < 0.01 )
{
myScaleBarUnitLabel = tr( " mm" );
myActualSize = myActualSize * 1000;
}
else if ( myActualSize < 0.1 )
{
myScaleBarUnitLabel = tr( " cm" );
myActualSize = myActualSize * 100;
}
else
myScaleBarUnitLabel = tr( " m" );
break;
case QgsUnitTypes::DistanceFeet:
if ( myActualSize > 5280.0 ) //5280 feet to the mile
{
myScaleBarUnitLabel = tr( " miles" );
// Adjust scale bar width to get even numbers
myActualSize = myActualSize / 5000;
myScaleBarWidth = ( myScaleBarWidth * 5280 ) / 5000;
}
else if ( myActualSize == 5280.0 ) //5280 feet to the mile
{
myScaleBarUnitLabel = tr( " mile" );
// Adjust scale bar width to get even numbers
myActualSize = myActualSize / 5000;
myScaleBarWidth = ( myScaleBarWidth * 5280 ) / 5000;
}
else if ( myActualSize < 1 )
{
myScaleBarUnitLabel = tr( " inches" );
myActualSize = myActualSize * 10;
myScaleBarWidth = ( myScaleBarWidth * 10 ) / 12;
}
else if ( myActualSize == 1.0 )
{
myScaleBarUnitLabel = tr( " foot" );
}
else
{
myScaleBarUnitLabel = tr( " feet" );
}
break;
case QgsUnitTypes::DistanceDegrees:
if ( myActualSize == 1.0 )
myScaleBarUnitLabel = tr( " degree" );
else
myScaleBarUnitLabel = tr( " degrees" );
break;
case QgsUnitTypes::DistanceUnknownUnit:
myScaleBarUnitLabel = tr( " unknown" );
//intentional fall-through
FALLTHROUGH;
default:
QgsDebugMsg( QString( "Error: not picked up map units - actual value = %1" ).arg( myMapUnits ) );
}
//Set font and calculate width of unit label
int myFontSize = 10; //we use this later for buffering
QFont myFont( QStringLiteral( "helvetica" ), myFontSize );
theQPainter->setFont( myFont );
QFontMetrics myFontMetrics( myFont );
double myFontWidth = myFontMetrics.width( myScaleBarUnitLabel );
double myFontHeight = myFontMetrics.height();
//Set the maximum label
QString myScaleBarMaxLabel = QLocale::system().toString( myActualSize );
//Calculate total width of scale bar and label
double myTotalScaleBarWidth = myScaleBarWidth + myFontWidth;
int myMarginW = 10;
int myMarginH = 20;
int myOriginX = 0;
int myOriginY = 0;
// Set margin according to selected units
switch ( mMarginUnit )
{
case QgsUnitTypes::RenderMillimeters:
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myOriginX = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal;
myOriginY = myPixelsInchY * INCHES_TO_MM * mMarginVertical;
break;
}
case QgsUnitTypes::RenderPixels:
myOriginX = mMarginHorizontal - 5.; // Minus 5 to shift tight into corner
myOriginY = mMarginVertical - 5.;
break;
case QgsUnitTypes::RenderPercentage:
{
float myMarginDoubledW = myMarginW * 2.0;
float myMarginDoubledH = myMarginH * 2.0;
myOriginX = ( ( myCanvasWidth - myMarginDoubledW - myTotalScaleBarWidth ) / 100. ) * mMarginHorizontal;
myOriginY = ( ( myCanvasHeight - myMarginDoubledH ) / 100. ) * mMarginVertical;
break;
}
default: // Use default of top left
break;
}
//Determine the origin of scale bar depending on placement selected
switch ( mPlacement )
{
case BottomLeft:
myOriginX += myMarginW;
myOriginY = myCanvasHeight - myOriginY - myMarginH;
break;
case TopLeft:
myOriginX += myMarginW;
myOriginY += myMarginH;
break;
case TopRight:
myOriginX = myCanvasWidth - myOriginX - myMarginW - ( static_cast< int >( myTotalScaleBarWidth ) );
myOriginY += myMarginH;
break;
case BottomRight:
myOriginX = myCanvasWidth - myOriginX - myMarginW - ( static_cast< int >( myTotalScaleBarWidth ) );
myOriginY = myCanvasHeight - myOriginY - myMarginH;
break;
default:
QgsDebugMsg( "Unable to determine where to put scale bar so defaulting to top left" );
}
//Set pen to draw with
QPen myForegroundPen( mColor, 2 );
QPen myBackgroundPen( Qt::white, 4 );
//Cast myScaleBarWidth to int for drawing
int myScaleBarWidthInt = static_cast< int >( myScaleBarWidth );
//Create array of vertices for scale bar depending on style
switch ( mStyleIndex )
{
case 0: // Tick Down
{
QPolygon myTickDownArray( 4 );
//draw a buffer first so bar shows up on dark images
theQPainter->setPen( myBackgroundPen );
myTickDownArray.putPoints( 0, 4,
myOriginX, myOriginY + myMajorTickSize,
myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize
);
theQPainter->drawPolyline( myTickDownArray );
//now draw the bar itself in user selected color
theQPainter->setPen( myForegroundPen );
myTickDownArray.putPoints( 0, 4,
myOriginX, myOriginY + myMajorTickSize,
myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize
);
theQPainter->drawPolyline( myTickDownArray );
break;
}
case 1: // tick up
{
QPolygon myTickUpArray( 4 );
//draw a buffer first so bar shows up on dark images
theQPainter->setPen( myBackgroundPen );
myTickUpArray.putPoints( 0, 4,
myOriginX, myOriginY,
myOriginX, myOriginY + myMajorTickSize,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize,
myScaleBarWidthInt + myOriginX, myOriginY
);
theQPainter->drawPolyline( myTickUpArray );
//now draw the bar itself in user selected color
theQPainter->setPen( myForegroundPen );
myTickUpArray.putPoints( 0, 4,
myOriginX, myOriginY,
myOriginX, myOriginY + myMajorTickSize,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize,
myScaleBarWidthInt + myOriginX, myOriginY
);
theQPainter->drawPolyline( myTickUpArray );
break;
}
case 2: // Bar
{
QPolygon myBarArray( 2 );
//draw a buffer first so bar shows up on dark images
theQPainter->setPen( myBackgroundPen );
myBarArray.putPoints( 0, 2,
myOriginX, myOriginY + ( myMajorTickSize / 2 ),
myScaleBarWidthInt + myOriginX, myOriginY + ( myMajorTickSize / 2 )
);
theQPainter->drawPolyline( myBarArray );
//now draw the bar itself in user selected color
theQPainter->setPen( myForegroundPen );
myBarArray.putPoints( 0, 2,
myOriginX, myOriginY + ( myMajorTickSize / 2 ),
myScaleBarWidthInt + myOriginX, myOriginY + ( myMajorTickSize / 2 )
);
theQPainter->drawPolyline( myBarArray );
break;
}
case 3: // box
{
// Want square corners for a box
myBackgroundPen.setJoinStyle( Qt::MiterJoin );
myForegroundPen.setJoinStyle( Qt::MiterJoin );
QPolygon myBoxArray( 5 );
//draw a buffer first so bar shows up on dark images
theQPainter->setPen( myBackgroundPen );
myBoxArray.putPoints( 0, 5,
myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize,
myOriginX, myOriginY + myMajorTickSize,
myOriginX, myOriginY
);
theQPainter->drawPolyline( myBoxArray );
//now draw the bar itself in user selected color
theQPainter->setPen( myForegroundPen );
theQPainter->setBrush( QBrush( mColor, Qt::SolidPattern ) );
int midPointX = myScaleBarWidthInt / 2 + myOriginX;
myBoxArray.putPoints( 0, 5,
myOriginX, myOriginY,
midPointX, myOriginY,
midPointX, myOriginY + myMajorTickSize,
myOriginX, myOriginY + myMajorTickSize,
myOriginX, myOriginY
);
theQPainter->drawPolygon( myBoxArray );
theQPainter->setBrush( Qt::NoBrush );
myBoxArray.putPoints( 0, 5,
midPointX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY,
myScaleBarWidthInt + myOriginX, myOriginY + myMajorTickSize,
midPointX, myOriginY + myMajorTickSize,
midPointX, myOriginY
);
theQPainter->drawPolygon( myBoxArray );
break;
}
default:
QgsDebugMsg( "Unknown style" );
}
//Do actual drawing of scale bar
//
//Do drawing of scale bar text
//
QColor myBackColor = Qt::white;
QColor myForeColor = Qt::black;
//Draw the minimum label buffer
theQPainter->setPen( myBackColor );
myFontWidth = myFontMetrics.width( QStringLiteral( "0" ) );
myFontHeight = myFontMetrics.height();
for ( int i = 0 - myBufferSize; i <= myBufferSize; i++ )
{
for ( int j = 0 - myBufferSize; j <= myBufferSize; j++ )
{
theQPainter->drawText( int( i + ( myOriginX - ( myFontWidth / 2 ) ) ),
int( j + ( myOriginY - ( myFontHeight / 4 ) ) ),
QStringLiteral( "0" ) );
}
}
//Draw minimum label
theQPainter->setPen( myForeColor );
theQPainter->drawText(
int( myOriginX - ( myFontWidth / 2 ) ),
int( myOriginY - ( myFontHeight / 4 ) ),
QStringLiteral( "0" )
);
//
//Draw maximum label
//
theQPainter->setPen( myBackColor );
myFontWidth = myFontMetrics.width( myScaleBarMaxLabel );
myFontHeight = myFontMetrics.height();
//first the buffer
for ( int i = 0 - myBufferSize; i <= myBufferSize; i++ )
{
for ( int j = 0 - myBufferSize; j <= myBufferSize; j++ )
{
theQPainter->drawText( int( i + ( myOriginX + myScaleBarWidthInt - ( myFontWidth / 2 ) ) ),
int( j + ( myOriginY - ( myFontHeight / 4 ) ) ),
myScaleBarMaxLabel );
}
}
//then the text itself
theQPainter->setPen( myForeColor );
theQPainter->drawText(
int( myOriginX + myScaleBarWidthInt - ( myFontWidth / 2 ) ),
int( myOriginY - ( myFontHeight / 4 ) ),
myScaleBarMaxLabel
);
//
//Draw unit label
//
theQPainter->setPen( myBackColor );
//first the buffer
for ( int i = 0 - myBufferSize; i <= myBufferSize; i++ )
{
for ( int j = 0 - myBufferSize; j <= myBufferSize; j++ )
{
theQPainter->drawText( i + ( myOriginX + myScaleBarWidthInt + myTextOffsetX ),
j + ( myOriginY + myMajorTickSize ),
myScaleBarUnitLabel );
}
}
//then the text itself
theQPainter->setPen( myForeColor );
theQPainter->drawText(
( myOriginX + myScaleBarWidthInt + myTextOffsetX ), ( myOriginY + myMajorTickSize ),
myScaleBarUnitLabel
);
}
}