-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdecorationgrid.cpp
865 lines (772 loc) · 29.9 KB
/
qgsdecorationgrid.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/***************************************************************************
qgsdecorationgrid.h
----------------------
begin : May 10, 2012
copyright : (C) 2012 by Etienne Tourigny
email : etourigny.dev at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsdecorationgrid.h"
#include "qgsdecorationgriddialog.h"
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsrasterlayer.h"
#include "qgsmaptopixel.h"
#include "qgspoint.h"
#include "qgsproject.h"
#include "qgssymbollayerutils.h" //for pointOnLineWithDistance
#include "qgssymbol.h" //for symbology
#include "qgsmarkersymbollayer.h"
#include "qgsrendercontext.h"
#include "qgsmapcanvas.h"
#include <QPainter>
#include <QAction>
#include <QPen>
#include <QPolygon>
#include <QString>
#include <QFontMetrics>
#include <QFont>
#include <QColor>
#include <QMenu>
#include <QFile>
#include <QLocale>
#include <QDomDocument>
#include <QMessageBox>
//non qt includes
#include <cmath>
#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
QgsDecorationGrid::QgsDecorationGrid( QObject *parent )
: QgsDecorationItem( parent )
{
setName( "Grid" );
mLineSymbol = nullptr;
mMarkerSymbol = nullptr;
projectRead();
connect( QgisApp::instance()->mapCanvas(), &QgsMapCanvas::destinationCrsChanged,
this, &QgsDecorationGrid::checkMapUnitsChanged );
}
QgsDecorationGrid::~QgsDecorationGrid()
{
if ( mLineSymbol )
delete mLineSymbol;
if ( mMarkerSymbol )
delete mMarkerSymbol;
}
void QgsDecorationGrid::setLineSymbol( QgsLineSymbol *symbol )
{
if ( mLineSymbol )
delete mLineSymbol;
mLineSymbol = symbol;
}
void QgsDecorationGrid::setMarkerSymbol( QgsMarkerSymbol *symbol )
{
if ( mMarkerSymbol )
delete mMarkerSymbol;
mMarkerSymbol = symbol;
}
void QgsDecorationGrid::projectRead()
{
QgsDecorationItem::projectRead();
mEnabled = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Enabled" ), false );
mMapUnits = static_cast< QgsUnitTypes::DistanceUnit >( QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MapUnits" ),
QgsUnitTypes::DistanceUnknownUnit ) );
mGridStyle = static_cast< GridStyle >( QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/Style" ),
QgsDecorationGrid::Line ) );
mGridIntervalX = QgsProject::instance()->readDoubleEntry( mNameConfig, QStringLiteral( "/IntervalX" ), 10 );
mGridIntervalY = QgsProject::instance()->readDoubleEntry( mNameConfig, QStringLiteral( "/IntervalY" ), 10 );
mGridOffsetX = QgsProject::instance()->readDoubleEntry( mNameConfig, QStringLiteral( "/OffsetX" ), 0 );
mGridOffsetY = QgsProject::instance()->readDoubleEntry( mNameConfig, QStringLiteral( "/OffsetY" ), 0 );
// mCrossLength = QgsProject::instance()->readDoubleEntry( mNameConfig, "/CrossLength", 3 );
mShowGridAnnotation = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/ShowAnnotation" ), false );
// mGridAnnotationPosition = ( GridAnnotationPosition ) QgsProject::instance()->readNumEntry( mNameConfig,
// "/AnnotationPosition", 0 );
mGridAnnotationPosition = InsideMapFrame; // don't allow outside frame, doesn't make sense
mGridAnnotationDirection = static_cast< GridAnnotationDirection >( QgsProject::instance()->readNumEntry( mNameConfig,
QStringLiteral( "/AnnotationDirection" ), 0 ) );
QString fontStr = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/AnnotationFont" ), QLatin1String( "" ) );
if ( fontStr != QLatin1String( "" ) )
{
mGridAnnotationFont.fromString( fontStr );
}
else
{
mGridAnnotationFont = QFont();
// TODO fix font scaling problem - put a slightly large font for now
mGridAnnotationFont.setPointSize( 16 );
}
mAnnotationFrameDistance = QgsProject::instance()->readDoubleEntry( mNameConfig, QStringLiteral( "/AnnotationFrameDistance" ), 0 );
mGridAnnotationPrecision = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/AnnotationPrecision" ), 0 );
// read symbol info from xml
QDomDocument doc;
QDomElement elem;
QString xml;
if ( mLineSymbol )
setLineSymbol( nullptr );
xml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/LineSymbol" ) );
if ( xml != QLatin1String( "" ) )
{
doc.setContent( xml );
elem = doc.documentElement();
mLineSymbol = QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( elem );
}
if ( ! mLineSymbol )
mLineSymbol = new QgsLineSymbol();
if ( mMarkerSymbol )
setMarkerSymbol( nullptr );
xml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/MarkerSymbol" ) );
if ( xml != QLatin1String( "" ) )
{
doc.setContent( xml );
elem = doc.documentElement();
mMarkerSymbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( elem );
}
if ( ! mMarkerSymbol )
{
// set default symbol : cross with width=3
QgsSymbolLayerList symbolList;
symbolList << new QgsSimpleMarkerSymbolLayer( QgsSimpleMarkerSymbolLayerBase::Cross, 3, 0 );
mMarkerSymbol = new QgsMarkerSymbol( symbolList );
// mMarkerSymbol = new QgsMarkerSymbol();
}
}
void QgsDecorationGrid::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Enabled" ), mEnabled );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MapUnits" ), static_cast< int >( mMapUnits ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Style" ), static_cast< int >( mGridStyle ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/IntervalX" ), mGridIntervalX );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/IntervalY" ), mGridIntervalY );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/OffsetX" ), mGridOffsetX );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/OffsetY" ), mGridOffsetY );
// QgsProject::instance()->writeEntry( mNameConfig, "/CrossLength", mCrossLength );
// missing mGridPen, but should use styles anyway
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/ShowAnnotation" ), mShowGridAnnotation );
// QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationPosition", ( int ) mGridAnnotationPosition );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/AnnotationDirection" ), static_cast< int >( mGridAnnotationDirection ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/AnnotationFont" ), mGridAnnotationFont.toString() );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/AnnotationFrameDistance" ), mAnnotationFrameDistance );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/AnnotationPrecision" ), mGridAnnotationPrecision );
// write symbol info to xml
QDomDocument doc;
QDomElement elem;
if ( mLineSymbol )
{
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "line symbol" ), mLineSymbol, doc );
doc.appendChild( elem );
// FIXME this works, but XML will not be valid as < is replaced by <
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/LineSymbol" ), doc.toString() );
}
if ( mMarkerSymbol )
{
doc.setContent( QString() );
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "marker symbol" ), mMarkerSymbol, doc );
doc.appendChild( elem );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarkerSymbol" ), doc.toString() );
}
}
void QgsDecorationGrid::run()
{
QgsDecorationGridDialog dlg( *this );
if ( dlg.exec() )
{
update();
}
}
void QgsDecorationGrid::render( QPainter *p )
{
if ( ! mEnabled )
return;
// p->setPen( mGridPen );
QList< QPair< qreal, QLineF > > verticalLines;
yGridLines( verticalLines );
QList< QPair< qreal, QLineF > > horizontalLines;
xGridLines( horizontalLines );
//QgsDebugMsg( QString("grid has %1 vertical and %2 horizontal lines").arg( verticalLines.size() ).arg( horizontalLines.size() ) );
QList< QPair< qreal, QLineF > >::const_iterator vIt = verticalLines.constBegin();
QList< QPair< qreal, QLineF > >::const_iterator hIt = horizontalLines.constBegin();
//simpler approach: draw vertical lines first, then horizontal ones
if ( mGridStyle == QgsDecorationGrid::Line )
{
if ( ! mLineSymbol )
return;
QgsRenderContext context = QgsRenderContext::fromMapSettings( QgisApp::instance()->mapCanvas()->mapSettings() );
context.setPainter( p );
mLineSymbol->startRender( context );
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
// p->drawLine( vIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << vIt->second.p1() << vIt->second.p2();
mLineSymbol->renderPolyline( QPolygonF( poly ), nullptr, context );
}
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
// p->drawLine( hIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << hIt->second.p1() << hIt->second.p2();
mLineSymbol->renderPolyline( QPolygonF( poly ), nullptr, context );
}
mLineSymbol->stopRender( context );
}
#if 0
else if ( mGridStyle == QgsDecorationGrid::Cross )
{
QPointF intersectionPoint, crossEnd1, crossEnd2;
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
//start mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( vIt->second.p1(), vIt->second.p2(), mCrossLength );
p->drawLine( vIt->second.p1(), crossEnd1 );
//test for intersection with every horizontal line
hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
if ( hIt->second.intersect( vIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
{
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, vIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, vIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
}
}
//end mark
QPointF crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( vIt->second.p2(), vIt->second.p1(), mCrossLength );
p->drawLine( vIt->second.p2(), crossEnd2 );
}
hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
//start mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( hIt->second.p1(), hIt->second.p2(), mCrossLength );
p->drawLine( hIt->second.p1(), crossEnd1 );
vIt = verticalLines.constBegin();
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
if ( vIt->second.intersect( hIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
{
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, hIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, hIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
}
}
//end mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
p->drawLine( hIt->second.p2(), crossEnd1 );
}
}
#endif
else //marker
{
if ( ! mMarkerSymbol )
return;
QgsRenderContext context = QgsRenderContext::fromMapSettings( QgisApp::instance()->mapCanvas()->mapSettings() );
context.setPainter( p );
mMarkerSymbol->startRender( context );
QPointF intersectionPoint;
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
//test for intersection with every horizontal line
hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
if ( hIt->second.intersect( vIt->second, &intersectionPoint ) == QLineF::BoundedIntersection )
{
mMarkerSymbol->renderPoint( intersectionPoint, nullptr, context );
}
}
}
mMarkerSymbol->stopRender( context );
}
// p->setClipRect( thisPaintRect, Qt::NoClip );
if ( mShowGridAnnotation )
{
drawCoordinateAnnotations( p, horizontalLines, verticalLines );
}
}
void QgsDecorationGrid::drawCoordinateAnnotations( QPainter *p, const QList< QPair< qreal, QLineF > > &hLines, const QList< QPair< qreal, QLineF > > &vLines )
{
if ( !p )
{
return;
}
QString currentAnnotationString;
QList< QPair< qreal, QLineF > >::const_iterator it = hLines.constBegin();
for ( ; it != hLines.constEnd(); ++it )
{
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
}
it = vLines.constBegin();
for ( ; it != vLines.constEnd(); ++it )
{
currentAnnotationString = QString::number( it->first, 'f', mGridAnnotationPrecision );
drawCoordinateAnnotation( p, it->second.p1(), currentAnnotationString );
drawCoordinateAnnotation( p, it->second.p2(), currentAnnotationString );
}
}
void QgsDecorationGrid::drawCoordinateAnnotation( QPainter *p, QPointF pos, const QString &annotationString )
{
Border frameBorder = borderForLineCoord( pos, p );
double textWidth = textWidthMillimeters( mGridAnnotationFont, annotationString );
//relevant for annotations is the height of digits
double textHeight = fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
double xpos = pos.x();
double ypos = pos.y();
int rotation = 0;
if ( frameBorder == Left )
{
if ( mGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
{
xpos += textHeight + mAnnotationFrameDistance;
ypos += textWidth / 2.0;
rotation = 270;
}
else
{
xpos += mAnnotationFrameDistance;
ypos += textHeight / 2.0;
}
}
else //Outside map frame
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
{
xpos -= mAnnotationFrameDistance;
ypos += textWidth / 2.0;
rotation = 270;
}
else
{
xpos -= textWidth + mAnnotationFrameDistance;
ypos += textHeight / 2.0;
}
}
}
else if ( frameBorder == Right )
{
if ( mGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
{
xpos -= mAnnotationFrameDistance;
ypos += textWidth / 2.0;
rotation = 270;
}
else //Horizontal
{
xpos -= textWidth + mAnnotationFrameDistance;
ypos += textHeight / 2.0;
}
}
else //OutsideMapFrame
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
{
xpos += textHeight + mAnnotationFrameDistance;
ypos += textWidth / 2.0;
rotation = 270;
}
else //Horizontal
{
xpos += mAnnotationFrameDistance;
ypos += textHeight / 2.0;
}
}
}
else if ( frameBorder == Bottom )
{
if ( mGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
{
ypos -= mAnnotationFrameDistance;
xpos -= textWidth / 2.0;
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos -= mAnnotationFrameDistance;
rotation = 270;
}
}
else //OutsideMapFrame
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
{
ypos += mAnnotationFrameDistance + textHeight;
xpos -= textWidth / 2.0;
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos += textWidth + mAnnotationFrameDistance;
rotation = 270;
}
}
}
else //Top
{
if ( mGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
{
xpos -= textWidth / 2.0;
ypos += textHeight + mAnnotationFrameDistance;
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos += textWidth + mAnnotationFrameDistance;
rotation = 270;
}
}
else //OutsideMapFrame
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
{
xpos -= textWidth / 2.0;
ypos -= mAnnotationFrameDistance;
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos -= mAnnotationFrameDistance;
rotation = 270;
}
}
}
drawAnnotation( p, QPointF( xpos, ypos ), rotation, annotationString );
}
void QgsDecorationGrid::drawAnnotation( QPainter *p, QPointF pos, int rotation, const QString &annotationText )
{
p->save();
p->translate( pos );
p->rotate( rotation );
p->setPen( QColor( 0, 0, 0 ) );
drawText( p, 0, 0, annotationText, mGridAnnotationFont );
p->restore();
}
const QgsMapCanvas &canvas()
{
return *QgisApp::instance()->mapCanvas();
}
QPolygonF canvasPolygon()
{
QPolygonF poly;
const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
return mapSettings.visiblePolygon();
}
bool clipByRect( QLineF &line, const QPolygonF &rect )
{
QVector<QLineF> borderLines;
borderLines << QLineF( rect.at( 0 ), rect.at( 1 ) );
borderLines << QLineF( rect.at( 1 ), rect.at( 2 ) );
borderLines << QLineF( rect.at( 2 ), rect.at( 3 ) );
borderLines << QLineF( rect.at( 3 ), rect.at( 0 ) );
QVector<QPointF> intersectionList;
QVector<QLineF>::const_iterator it = borderLines.constBegin();
for ( ; it != borderLines.constEnd(); ++it )
{
QPointF intersectionPoint;
if ( it->intersect( line, &intersectionPoint ) == QLineF::BoundedIntersection )
{
intersectionList.push_back( intersectionPoint );
if ( intersectionList.size() >= 2 )
{
break; //we already have two intersections, skip further tests
}
}
}
if ( intersectionList.size() < 2 ) return false; // no intersection
line = QLineF( intersectionList.at( 0 ), intersectionList.at( 1 ) );
return true;
}
QPolygonF canvasExtent()
{
QPolygonF poly;
QgsRectangle extent = canvas().extent();
poly << QPointF( extent.xMinimum(), extent.yMaximum() );
poly << QPointF( extent.xMaximum(), extent.yMaximum() );
poly << QPointF( extent.xMaximum(), extent.yMinimum() );
poly << QPointF( extent.xMinimum(), extent.yMinimum() );
return poly;
}
int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > > &lines ) const
{
// prepare horizontal lines
lines.clear();
if ( mGridIntervalY <= 0.0 )
{
return 1;
}
const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
const QgsMapToPixel &m2p = mapSettings.mapToPixel();
// draw nothing if the distance between grid lines would be less than 1px
// otherwise the grid lines would completely cover the whole map
//if ( mapBoundingRect.height() / mGridIntervalY >= p->device()->height() )
if ( mGridIntervalY / mapSettings.mapUnitsPerPixel() < 1 )
return 1;
const QPolygonF &canvasPoly = canvasPolygon();
const QPolygonF &mapPolygon = canvasExtent();
const QRectF &mapBoundingRect = mapPolygon.boundingRect();
QLineF lineEast( mapPolygon[2], mapPolygon[1] );
QLineF lineWest( mapPolygon[3], mapPolygon[0] );
double len = lineEast.length();
Q_ASSERT( fabs( len - lineWest.length() ) < 1e-6 ); // no shear
double roundCorrection = mapBoundingRect.top() > 0 ? 1.0 : 0.0;
double dist = static_cast< int >( ( mapBoundingRect.top() - mGridOffsetY ) / mGridIntervalY + roundCorrection ) * mGridIntervalY + mGridOffsetY;
dist = dist - mapBoundingRect.top();
while ( dist < len )
{
double t = dist / len;
QPointF p0 = lineWest.pointAt( t );
QPointF p1 = lineEast.pointAt( t );
QLineF line( p0, p1 );
clipByRect( line, canvasPoly );
line = QLineF( m2p.transform( QgsPoint( line.pointAt( 0 ) ) ).toQPointF(), m2p.transform( QgsPoint( line.pointAt( 1 ) ) ).toQPointF() );
lines.push_back( qMakePair( p0.y(), line ) );
dist += mGridIntervalY;
}
return 0;
}
int QgsDecorationGrid::yGridLines( QList< QPair< qreal, QLineF > > &lines ) const
{
// prepare vertical lines
lines.clear();
if ( mGridIntervalX <= 0.0 )
{
return 1;
}
const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
const QgsMapToPixel &m2p = mapSettings.mapToPixel();
// draw nothing if the distance between grid lines would be less than 1px
// otherwise the grid lines would completely cover the whole map
//if ( mapBoundingRect.height() / mGridIntervalY >= p->device()->height() )
if ( mGridIntervalX / mapSettings.mapUnitsPerPixel() < 1 )
return 1;
const QPolygonF &canvasPoly = canvasPolygon();
const QPolygonF &mapPolygon = canvasExtent();
QLineF lineSouth( mapPolygon[3], mapPolygon[2] );
QLineF lineNorth( mapPolygon[0], mapPolygon[1] );
double len = lineSouth.length();
Q_ASSERT( fabs( len - lineNorth.length() ) < 1e-6 ); // no shear
const QRectF &mapBoundingRect = mapPolygon.boundingRect();
double roundCorrection = mapBoundingRect.left() > 0 ? 1.0 : 0.0;
double dist = static_cast< int >( ( mapBoundingRect.left() - mGridOffsetX ) / mGridIntervalX + roundCorrection ) * mGridIntervalX + mGridOffsetX;
dist = dist - mapBoundingRect.left();
while ( dist < len )
{
double t = dist / len;
QPointF p0( lineNorth.pointAt( t ) );
QPointF p1( lineSouth.pointAt( t ) );
QLineF line( p0, p1 );
clipByRect( line, canvasPoly );
line = QLineF( m2p.transform( QgsPoint( line.pointAt( 0 ) ) ).toQPointF(), m2p.transform( QgsPoint( line.pointAt( 1 ) ) ).toQPointF() );
lines.push_back( qMakePair( p0.x(), line ) );
dist += mGridIntervalX;
}
return 0;
}
QgsDecorationGrid::Border QgsDecorationGrid::borderForLineCoord( QPointF point, QPainter *p ) const
{
if ( point.x() <= mGridPen.widthF() )
{
return Left;
}
else if ( point.x() >= ( p->device()->width() - mGridPen.widthF() ) )
{
return Right;
}
else if ( point.y() <= mGridPen.widthF() )
{
return Top;
}
else
{
return Bottom;
}
}
void QgsDecorationGrid::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
{
QFont textFont = scaledFontPixelSize( font );
p->save();
p->setFont( textFont );
p->setPen( QColor( 0, 0, 0 ) ); //draw text always in black
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
p->restore();
}
void QgsDecorationGrid::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment ) const
{
QFont textFont = scaledFontPixelSize( font );
QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
p->save();
p->setFont( textFont );
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( scaledRect, halignment | valignment | Qt::TextWordWrap, text );
p->restore();
}
double QgsDecorationGrid::textWidthMillimeters( const QFont &font, const QString &text ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetrics fontMetrics( metricsFont );
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
}
double QgsDecorationGrid::fontHeightCharacterMM( const QFont &font, QChar c ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
}
double QgsDecorationGrid::fontAscentMillimeters( const QFont &font ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
}
double QgsDecorationGrid::pixelFontSize( double pointSize ) const
{
// return ( pointSize * 0.3527 );
// TODO fix font scaling problem - this seems to help, but text seems still a bit too small (about 5/6)
return pointSize;
}
QFont QgsDecorationGrid::scaledFontPixelSize( const QFont &font ) const
{
QFont scaledFont = font;
double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
scaledFont.setPixelSize( pixelSize );
return scaledFont;
}
void QgsDecorationGrid::checkMapUnitsChanged()
{
// disable grid if units changed and grid is enabled, and update the canvas
// this is to avoid problems when CRS changes to/from geographic and projected
// a better solution would be to change the grid interval, but this is a little tricky
// note: we could be less picky (e.g. from degrees to DMS)
QgsUnitTypes::DistanceUnit mapUnits = QgisApp::instance()->mapCanvas()->mapSettings().mapUnits();
if ( mEnabled && ( mMapUnits != mapUnits ) )
{
mEnabled = false;
mMapUnits = QgsUnitTypes::DistanceUnknownUnit; // make sure isDirty() returns true
if ( ! QgisApp::instance()->mapCanvas()->isFrozen() )
{
update();
}
}
}
bool QgsDecorationGrid::isDirty()
{
// checks if stored map units is undefined or different from canvas map units
// or if interval is 0
if ( mMapUnits == QgsUnitTypes::DistanceUnknownUnit ||
mMapUnits != QgisApp::instance()->mapCanvas()->mapSettings().mapUnits() ||
qgsDoubleNear( mGridIntervalX, 0.0 ) || qgsDoubleNear( mGridIntervalY, 0.0 ) )
return true;
return false;
}
void QgsDecorationGrid::setDirty( bool dirty )
{
if ( dirty )
{
mMapUnits = QgsUnitTypes::DistanceUnknownUnit;
}
else
{
mMapUnits = QgisApp::instance()->mapCanvas()->mapSettings().mapUnits();
}
}
bool QgsDecorationGrid::getIntervalFromExtent( double *values, bool useXAxis )
{
// get default interval from current extents
// calculate a default interval that is approx (extent width)/5, adjusted so that it is a rounded number
// e.g. 12.7 -> 10 66556 -> 70000
double interval = 0;
QgsRectangle extent = QgisApp::instance()->mapCanvas()->extent();
if ( useXAxis )
interval = ( extent.xMaximum() - extent.xMinimum() ) / 5;
else
interval = ( extent.yMaximum() - extent.yMinimum() ) / 5;
QgsDebugMsg( QString( "interval: %1" ).arg( interval ) );
if ( !qgsDoubleNear( interval, 0.0 ) )
{
double interval2 = 0;
int factor = pow( 10, floor( log10( interval ) ) );
if ( factor != 0 )
{
interval2 = qRound( interval / factor ) * factor;
QgsDebugMsg( QString( "interval2: %1" ).arg( interval2 ) );
if ( !qgsDoubleNear( interval2, 0.0 ) )
interval = interval2;
}
}
values[0] = values[1] = interval;
values[2] = values[3] = 0;
return true;
}
bool QgsDecorationGrid::getIntervalFromCurrentLayer( double *values )
{
// get current layer and make sure it is a raster layer and CRSs match
QgsMapLayer *layer = QgisApp::instance()->mapCanvas()->currentLayer();
if ( ! layer )
{
QMessageBox::warning( nullptr, tr( "Error" ), tr( "No active layer" ) );
return false;
}
if ( layer->type() != QgsMapLayer::RasterLayer )
{
QMessageBox::warning( nullptr, tr( "Error" ), tr( "Please select a raster layer" ) );
return false;
}
QgsRasterLayer *rlayer = dynamic_cast<QgsRasterLayer *>( layer );
if ( !rlayer || rlayer->width() == 0 || rlayer->height() == 0 )
{
QMessageBox::warning( nullptr, tr( "Error" ), tr( "Invalid raster layer" ) );
return false;
}
QgsCoordinateReferenceSystem layerCRS = layer->crs();
QgsCoordinateReferenceSystem mapCRS =
QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs();
// is this the best way to compare CRS? should we also make sure map has OTF enabled?
// TODO calculate transformed values if necessary
if ( layerCRS != mapCRS )
{
QMessageBox::warning( nullptr, tr( "Error" ), tr( "Layer CRS must be equal to project CRS" ) );
return false;
}
// calculate interval
// TODO add a function in QgsRasterLayer to get x/y resolution from provider,
// because this might not be 100% accurate
QgsRectangle extent = rlayer->extent();
values[0] = fabs( extent.xMaximum() - extent.xMinimum() ) / rlayer->width();
values[1] = fabs( extent.yMaximum() - extent.yMinimum() ) / rlayer->height();
// calculate offset - when using very high resolution rasters in geographic CRS
// there seems to be a small shift, but this may be due to rendering issues and depends on zoom
double ratio = extent.xMinimum() / values[0];
values[2] = ( ratio - floor( ratio ) ) * values[0];
ratio = extent.yMinimum() / values[1];
values[3] = ( ratio - floor( ratio ) ) * values[1];
QgsDebugMsg( QString( "xmax: %1 xmin: %2 width: %3 xInterval: %4 xOffset: %5" ).arg(
extent.xMaximum() ).arg( extent.xMinimum() ).arg( rlayer->width() ).arg( values[0] ).arg( values[2] ) );
QgsDebugMsg( QString( "ymax: %1 ymin: %2 height: %3 yInterval: %4 yOffset: %5" ).arg(
extent.yMaximum() ).arg( extent.yMinimum() ).arg( rlayer->height() ).arg( values[1] ).arg( values[3] ) );
return true;
}