55
55
#include " qgslegend.h"
56
56
#include " qgsvertexmarker.h"
57
57
#include " qgsrubberband.h"
58
+ #include " qgsproject.h"
58
59
59
60
extern " C" {
60
61
#include < grass/gis.h>
@@ -120,6 +121,8 @@ QgsGrassEdit::QgsGrassEdit ( QgisApp *qgisApp, QgisIface *iface,
120
121
mIface = iface;
121
122
mNewMap = false ;
122
123
124
+ mProjectionEnabled = (QgsProject::instance ()->readNumEntry (" SpatialRefSys" ," /ProjectionsEnabled" ,0 )!=0 );
125
+
123
126
mCanvas = mIface ->getMapCanvas ();
124
127
125
128
// TODO QGIS: crash if canvas is empty
@@ -130,10 +133,10 @@ QgsGrassEdit::QgsGrassEdit ( QgisApp *qgisApp, QgisIface *iface,
130
133
if ( !isEditable (layer) ) return ;
131
134
132
135
// TODO dynamic_cast ?
133
- QgsVectorLayer *vector = (QgsVectorLayer*)layer;
136
+ mLayer = (QgsVectorLayer*)layer;
134
137
135
138
// TODO dynamic_cast ?
136
- mProvider = (QgsGrassProvider *) vector ->getDataProvider ();
139
+ mProvider = (QgsGrassProvider *) mLayer ->getDataProvider ();
137
140
138
141
init ();
139
142
@@ -1097,11 +1100,20 @@ double QgsGrassEdit::threshold ( void )
1097
1100
int snapPixels = mSnapPixels ->text ().toInt ();
1098
1101
1099
1102
// Convert to map units (not nice)
1100
- mTransform = mCanvas -> getCoordinateTransform () ;
1101
- double x1 = mTransform ->toMapCoordinates ( 0 , 0 ). x ();
1102
- double x2 = mTransform ->toMapCoordinates ( snapPixels, 0 ). x ();
1103
+ QgsPoint p1, p2 ;
1104
+ p1 = mTransform ->toMapCoordinates (0 , 0 );
1105
+ p2 = mTransform ->toMapCoordinates (snapPixels, 0 );
1103
1106
1104
- return ( x2 - x1 );
1107
+ if ( mProjectionEnabled )
1108
+ {
1109
+ p1 = mLayer ->coordinateTransform ()->transform (p1, QgsCoordinateTransform::INVERSE );
1110
+ p2 = mLayer ->coordinateTransform ()->transform (p2, QgsCoordinateTransform::INVERSE );
1111
+ }
1112
+
1113
+ double dx = p2.x () - p1.x ();
1114
+ double dy = p2.y () - p1.y ();
1115
+ double thresh = sqrt ( dx*dx + dy*dy );
1116
+ return thresh;
1105
1117
}
1106
1118
1107
1119
void QgsGrassEdit::snap ( double *x, double *y )
@@ -1461,6 +1473,7 @@ void QgsGrassEdit::displayUpdated (void)
1461
1473
#endif
1462
1474
1463
1475
mTransform = mCanvas ->getCoordinateTransform ();
1476
+ mProjectionEnabled = (QgsProject::instance ()->readNumEntry (" SpatialRefSys" ," /ProjectionsEnabled" ,0 )!=0 );
1464
1477
1465
1478
QPainter *painter = new QPainter ();
1466
1479
painter->begin (mPixmap );
@@ -1518,7 +1531,7 @@ void QgsGrassEdit::displayElement ( int line, const QPen & pen, int size, QPaint
1518
1531
for ( int i = 0 ; i < mPoints ->n_points ; i++ ) {
1519
1532
point.setX (mPoints ->x [i]);
1520
1533
point.setY (mPoints ->y [i]);
1521
- mTransform -> transform (& point);
1534
+ point = transformLayerToCanvas ( point );
1522
1535
pointArray.setPoint ( i, static_cast <int >(round (point.x ())),
1523
1536
static_cast <int >(round (point.y ())) );
1524
1537
}
@@ -1586,7 +1599,10 @@ void QgsGrassEdit::displayDynamic ( struct line_pnts *Points, double x, double y
1586
1599
#endif
1587
1600
QgsPoint point;
1588
1601
1589
- mTransform = mCanvas ->getCoordinateTransform ();
1602
+ // mTransform = mCanvas->getCoordinateTransform();
1603
+
1604
+ // Dynamic points are in layer coordinate system, we have to
1605
+ // reproject them to current coordinate system if necessary
1590
1606
1591
1607
mRubberBandLine ->reset ();
1592
1608
@@ -1596,13 +1612,15 @@ void QgsGrassEdit::displayDynamic ( struct line_pnts *Points, double x, double y
1596
1612
{
1597
1613
point.setX (Points->x [i]);
1598
1614
point.setY (Points->y [i]);
1599
- mRubberBandLine ->addPoint (point);
1615
+ point = transformLayerToMap ( point );
1616
+ mRubberBandLine ->addPoint (point);
1600
1617
}
1601
1618
}
1602
1619
1603
1620
mRubberBandIcon ->setIconType (type);
1604
1621
mRubberBandIcon ->setIconSize (size);
1605
- mRubberBandIcon ->setCenter (QgsPoint (x,y));
1622
+ point = transformLayerToMap (QgsPoint (x,y) );
1623
+ mRubberBandIcon ->setCenter (point);
1606
1624
}
1607
1625
1608
1626
void QgsGrassEdit::displayNode ( int node, const QPen & pen, int size, QPainter *painter )
@@ -1620,6 +1638,41 @@ void QgsGrassEdit::displayNode ( int node, const QPen & pen, int size, QPainter
1620
1638
displayIcon ( x, y, pen, QgsVertexMarker::ICON_X, size, painter );
1621
1639
}
1622
1640
1641
+ QgsPoint QgsGrassEdit::transformLayerToCanvas ( QgsPoint point)
1642
+ {
1643
+ if ( mProjectionEnabled && mLayer ->coordinateTransform () )
1644
+ {
1645
+ try
1646
+ {
1647
+ point = mLayer ->coordinateTransform ()->transform (point);
1648
+ }
1649
+ catch (QgsCsException &cse)
1650
+ {
1651
+ std::cout << " cannot transform point" << std::endl;
1652
+ }
1653
+
1654
+ }
1655
+ mTransform ->transform (&point);
1656
+ return point;
1657
+ }
1658
+
1659
+ QgsPoint QgsGrassEdit::transformLayerToMap ( QgsPoint point)
1660
+ {
1661
+ if ( mProjectionEnabled && mLayer ->coordinateTransform () )
1662
+ {
1663
+ try
1664
+ {
1665
+ point = mLayer ->coordinateTransform ()->transform (point);
1666
+ }
1667
+ catch (QgsCsException &cse)
1668
+ {
1669
+ std::cout << " cannot transform point" << std::endl;
1670
+ }
1671
+
1672
+ }
1673
+ return point;
1674
+ }
1675
+
1623
1676
void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,
1624
1677
int type, int size, QPainter *painter )
1625
1678
{
@@ -1632,7 +1685,8 @@ void QgsGrassEdit::displayIcon ( double x, double y, const QPen & pen,
1632
1685
1633
1686
point.setX (x);
1634
1687
point.setY (y);
1635
- mTransform ->transform (&point);
1688
+
1689
+ point = transformLayerToCanvas ( point );
1636
1690
1637
1691
int px = static_cast <int >(round (point.x ()));
1638
1692
int py = static_cast <int >(round (point.y ()));
0 commit comments