28
28
#include " qgsnetworkcontentfetcher.h"
29
29
#include " qgssymbollayerv2utils.h"
30
30
#include " qgssvgcache.h"
31
+ #include " qgslogger.h"
32
+ #include " qgsbearingutils.h"
33
+ #include " qgsmapsettings.h"
34
+
31
35
#include < QDomDocument>
32
36
#include < QDomElement>
33
37
#include < QFileInfo>
@@ -44,6 +48,8 @@ QgsComposerPicture::QgsComposerPicture( QgsComposition *composition )
44
48
, mMode( Unknown )
45
49
, mPictureRotation( 0 )
46
50
, mRotationMap( nullptr )
51
+ , mNorthMode( GridNorth )
52
+ , mNorthOffset( 0.0 )
47
53
, mResizeMode( QgsComposerPicture::Zoom )
48
54
, mPictureAnchor( UpperLeft )
49
55
, mSvgFillColor( QColor( 255 , 255 , 255 ) )
@@ -61,6 +67,8 @@ QgsComposerPicture::QgsComposerPicture()
61
67
, mMode( Unknown )
62
68
, mPictureRotation( 0 )
63
69
, mRotationMap( nullptr )
70
+ , mNorthMode( GridNorth )
71
+ , mNorthOffset( 0.0 )
64
72
, mResizeMode( QgsComposerPicture::Zoom )
65
73
, mPictureAnchor( UpperLeft )
66
74
, mSvgFillColor( QColor( 255 , 255 , 255 ) )
@@ -419,6 +427,43 @@ void QgsComposerPicture::remotePictureLoaded()
419
427
mLoaded = true ;
420
428
}
421
429
430
+ void QgsComposerPicture::updateMapRotation ()
431
+ {
432
+ if ( !mRotationMap )
433
+ return ;
434
+
435
+ // take map rotation
436
+ double rotation = mRotationMap ->mapRotation ();
437
+
438
+ // handle true north
439
+ switch ( mNorthMode )
440
+ {
441
+ case GridNorth:
442
+ break ; // nothing to do
443
+
444
+ case TrueNorth:
445
+ {
446
+ QgsPoint center = mRotationMap ->currentMapExtent ()->center ();
447
+ QgsCoordinateReferenceSystem crs = mComposition ->mapSettings ().destinationCrs ();
448
+
449
+ try
450
+ {
451
+ double bearing = QgsBearingUtils::bearingTrueNorth ( crs, center );
452
+ rotation += bearing;
453
+ }
454
+ catch ( QgsException& e )
455
+ {
456
+ Q_UNUSED ( e );
457
+ QgsDebugMsg ( QString ( " Caught exception %1" ).arg ( e.what () ) );
458
+ }
459
+ break ;
460
+ }
461
+ }
462
+
463
+ rotation += mNorthOffset ;
464
+ setPictureRotation ( rotation );
465
+ }
466
+
422
467
void QgsComposerPicture::loadPicture ( const QString &path )
423
468
{
424
469
if ( path.startsWith ( " http" ) )
@@ -650,7 +695,8 @@ void QgsComposerPicture::setRotationMap( int composerMapId )
650
695
651
696
if ( composerMapId == -1 ) // disable rotation from map
652
697
{
653
- QObject::disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( setPictureRotation ( double ) ) );
698
+ disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( updateMapRotation () ) );
699
+ disconnect ( mRotationMap , SIGNAL ( extentChanged () ), this , SLOT ( updateMapRotation () ) );
654
700
mRotationMap = nullptr ;
655
701
}
656
702
@@ -661,12 +707,14 @@ void QgsComposerPicture::setRotationMap( int composerMapId )
661
707
}
662
708
if ( mRotationMap )
663
709
{
664
- QObject::disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( setPictureRotation ( double ) ) );
710
+ disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( updateMapRotation () ) );
711
+ disconnect ( mRotationMap , SIGNAL ( extentChanged () ), this , SLOT ( updateMapRotation () ) );
665
712
}
666
713
mPictureRotation = map->mapRotation ();
667
- QObject::connect ( map, SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( setPictureRotation ( double ) ) );
714
+ connect ( map, SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( updateMapRotation () ) );
715
+ connect ( map, SIGNAL ( extentChanged () ), this , SLOT ( updateMapRotation () ) );
668
716
mRotationMap = map;
669
- update ();
717
+ updateMapRotation ();
670
718
emit pictureRotationChanged ( mPictureRotation );
671
719
}
672
720
@@ -761,6 +809,8 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
761
809
{
762
810
composerPictureElem.setAttribute ( " mapId" , mRotationMap ->id () );
763
811
}
812
+ composerPictureElem.setAttribute ( " northMode" , mNorthMode );
813
+ composerPictureElem.setAttribute ( " northOffset" , mNorthOffset );
764
814
765
815
_writeXML ( composerPictureElem, doc );
766
816
elem.appendChild ( composerPictureElem );
@@ -827,6 +877,9 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
827
877
}
828
878
829
879
// rotation map
880
+ mNorthMode = static_cast < NorthMode >( itemElem.attribute ( " northMode" , " 0" ).toInt () );
881
+ mNorthOffset = itemElem.attribute ( " northOffset" , " 0" ).toDouble ();
882
+
830
883
int rotationMapId = itemElem.attribute ( " mapId" , " -1" ).toInt ();
831
884
if ( rotationMapId == -1 )
832
885
{
@@ -837,10 +890,12 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
837
890
838
891
if ( mRotationMap )
839
892
{
840
- QObject::disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( setRotation ( double ) ) );
893
+ disconnect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( updateMapRotation () ) );
894
+ disconnect ( mRotationMap , SIGNAL ( extentChanged () ), this , SLOT ( updateMapRotation () ) );
841
895
}
842
896
mRotationMap = mComposition ->getComposerMapById ( rotationMapId );
843
- QObject::connect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( setRotation ( double ) ) );
897
+ connect ( mRotationMap , SIGNAL ( mapRotationChanged ( double ) ), this , SLOT ( updateMapRotation () ) );
898
+ connect ( mRotationMap , SIGNAL ( extentChanged () ), this , SLOT ( updateMapRotation () ) );
844
899
}
845
900
846
901
refreshPicture ();
@@ -861,6 +916,18 @@ int QgsComposerPicture::rotationMap() const
861
916
}
862
917
}
863
918
919
+ void QgsComposerPicture::setNorthMode ( QgsComposerPicture::NorthMode mode )
920
+ {
921
+ mNorthMode = mode;
922
+ updateMapRotation ();
923
+ }
924
+
925
+ void QgsComposerPicture::setNorthOffset ( double offset )
926
+ {
927
+ mNorthOffset = offset;
928
+ updateMapRotation ();
929
+ }
930
+
864
931
void QgsComposerPicture::setPictureAnchor ( QgsComposerItem::ItemPositionMode anchor )
865
932
{
866
933
mPictureAnchor = anchor;
0 commit comments