48
48
#include < QSettings>
49
49
#include < QDir>
50
50
51
+ #include < limits>
51
52
52
53
QgsComposition::QgsComposition ( QgsMapRenderer* mapRenderer )
53
54
: QGraphicsScene( 0 )
@@ -717,6 +718,35 @@ bool QgsComposition::loadFromTemplate( const QDomDocument& doc, QMap<QString, QS
717
718
return true ;
718
719
}
719
720
721
+ QPointF QgsComposition::minPointFromXml ( const QDomElement& elem ) const
722
+ {
723
+ double minX = std::numeric_limits<double >::max ();
724
+ double minY = std::numeric_limits<double >::max ();
725
+ QDomNodeList composerItemList = elem.elementsByTagName ( " ComposerItem" );
726
+ for ( int i = 0 ; i < composerItemList.size (); ++i )
727
+ {
728
+ QDomElement currentComposerItemElem = composerItemList.at ( i ).toElement ();
729
+ double x, y;
730
+ bool xOk, yOk;
731
+ x = currentComposerItemElem.attribute ( " x" ).toDouble ( &xOk );
732
+ y = currentComposerItemElem.attribute ( " y" ).toDouble ( &yOk );
733
+ if ( !xOk || !yOk )
734
+ {
735
+ continue ;
736
+ }
737
+ minX = qMin ( minX, x );
738
+ minY = qMin ( minY, y );
739
+ }
740
+ if ( minX < std::numeric_limits<double >::max () )
741
+ {
742
+ return QPointF ( minX, minY );
743
+ }
744
+ else
745
+ {
746
+ return QPointF ( 0 , 0 );
747
+ }
748
+ }
749
+
720
750
void QgsComposition::addItemsFromXML ( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore,
721
751
bool addUndoCommands, QPointF* pos, bool pasteInPlace )
722
752
{
@@ -727,6 +757,18 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
727
757
// so, calculate an offset which needs to be added to the zValue of created items
728
758
int zOrderOffset = mItemZList .size ();
729
759
760
+ QPointF pasteShiftPos;
761
+ if ( pos )
762
+ {
763
+ // If we are placing items relative to a certain point, then calculate how much we need
764
+ // to shift the items by so that they are placed at this point
765
+ // First, calculate the minimum position from the xml
766
+ QPointF minItemPos = minPointFromXml ( elem );
767
+ // next, calculate how much each item needs to be shifted from its original position
768
+ // so that it's placed at the correct relative position
769
+ pasteShiftPos = *pos - minItemPos;
770
+ }
771
+
730
772
if ( pasteInPlace )
731
773
{
732
774
pasteInPlacePt = new QPointF ( 0 , pageNumberAt ( *pos ) * ( mPageHeight + mSpaceBetweenPages ) );
@@ -746,7 +788,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
746
788
}
747
789
else
748
790
{
749
- newLabel->setItemPosition ( pos-> x (), pos-> y () );
791
+ newLabel->move ( pasteShiftPos. x (), pasteShiftPos. y () );
750
792
}
751
793
}
752
794
addComposerLabel ( newLabel );
@@ -781,7 +823,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
781
823
}
782
824
else
783
825
{
784
- newMap->setItemPosition ( pos-> x (), pos-> y () );
826
+ newMap->move ( pasteShiftPos. x (), pasteShiftPos. y () );
785
827
}
786
828
}
787
829
@@ -821,7 +863,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
821
863
}
822
864
else
823
865
{
824
- newArrow->setItemPosition ( pos-> x (), pos-> y () );
866
+ newArrow->move ( pasteShiftPos. x (), pasteShiftPos. y () );
825
867
}
826
868
}
827
869
addComposerArrow ( newArrow );
@@ -847,7 +889,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
847
889
}
848
890
else
849
891
{
850
- newScaleBar->setItemPosition ( pos-> x (), pos-> y () );
892
+ newScaleBar->move ( pasteShiftPos. x (), pasteShiftPos. y () );
851
893
}
852
894
}
853
895
addComposerScaleBar ( newScaleBar );
@@ -875,7 +917,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
875
917
}
876
918
else
877
919
{
878
- newShape->setItemPosition ( pos-> x (), pos-> y () );
920
+ newShape->move ( pasteShiftPos. x (), pasteShiftPos. y () );
879
921
}
880
922
}
881
923
addComposerShape ( newShape );
@@ -901,7 +943,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
901
943
}
902
944
else
903
945
{
904
- newPicture->setItemPosition ( pos-> x (), pos-> y () );
946
+ newPicture->move ( pasteShiftPos. x (), pasteShiftPos. y () );
905
947
}
906
948
}
907
949
addComposerPicture ( newPicture );
@@ -927,7 +969,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
927
969
}
928
970
else
929
971
{
930
- newLegend->setItemPosition ( pos-> x (), pos-> y () );
972
+ newLegend->move ( pasteShiftPos. x (), pasteShiftPos. y () );
931
973
}
932
974
}
933
975
addComposerLegend ( newLegend );
@@ -953,7 +995,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
953
995
}
954
996
else
955
997
{
956
- newTable->setItemPosition ( pos-> x (), pos-> y () );
998
+ newTable->move ( pasteShiftPos. x (), pasteShiftPos. y () );
957
999
}
958
1000
}
959
1001
addComposerTable ( newTable );
@@ -1000,6 +1042,9 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
1000
1042
// Make sure z order list matches the actual order of items in the scene.
1001
1043
refreshZList ();
1002
1044
1045
+ delete pasteInPlacePt;
1046
+ pasteInPlacePt = 0 ;
1047
+
1003
1048
}
1004
1049
1005
1050
void QgsComposition::addItemToZList ( QgsComposerItem* item )
0 commit comments