@@ -1615,11 +1615,15 @@ int QgsComposerMapGrid::xGridLinesCRSTransform( const QgsRectangle& bbox, const
1615
1615
}
1616
1616
crossed180 = false ;
1617
1617
1618
- gridLine = trimLineToMap ( gridLine, QgsRectangle ( mComposerMap ->rect () ) );
1619
- if ( gridLine.size () > 0 )
1618
+ QList<QPolygonF> lineSegments = trimLinesToMap ( gridLine, QgsRectangle ( mComposerMap ->rect () ) );
1619
+ QList<QPolygonF>::const_iterator lineIt = lineSegments.constBegin ();
1620
+ for ( ; lineIt != lineSegments.constEnd (); lineIt ++ )
1620
1621
{
1621
- lines.append ( qMakePair ( currentLevel, gridLine ) );
1622
- gridLineCount++;
1622
+ if (( *lineIt ).size () > 0 )
1623
+ {
1624
+ lines.append ( qMakePair ( currentLevel, *lineIt ) );
1625
+ gridLineCount++;
1626
+ }
1623
1627
}
1624
1628
currentLevel -= mGridIntervalY ;
1625
1629
}
@@ -1677,11 +1681,15 @@ int QgsComposerMapGrid::yGridLinesCRSTransform( const QgsRectangle& bbox, const
1677
1681
currentY += step;
1678
1682
}
1679
1683
// clip grid line to map polygon
1680
- gridLine = trimLineToMap ( gridLine, QgsRectangle ( mComposerMap ->rect () ) );
1681
- if ( gridLine.size () > 0 )
1684
+ QList<QPolygonF> lineSegments = trimLinesToMap ( gridLine, QgsRectangle ( mComposerMap ->rect () ) );
1685
+ QList<QPolygonF>::const_iterator lineIt = lineSegments.constBegin ();
1686
+ for ( ; lineIt != lineSegments.constEnd (); lineIt ++ )
1682
1687
{
1683
- lines.append ( qMakePair ( currentLevel, gridLine ) );
1684
- gridLineCount++;
1688
+ if (( *lineIt ).size () > 0 )
1689
+ {
1690
+ lines.append ( qMakePair ( currentLevel, *lineIt ) );
1691
+ gridLineCount++;
1692
+ }
1685
1693
}
1686
1694
currentLevel += mGridIntervalX ;
1687
1695
if ( crosses180 && currentLevel > 180.0 )
@@ -1775,26 +1783,31 @@ bool QgsComposerMapGrid::shouldShowDivisionForDisplayMode( const QgsComposerMapG
1775
1783
|| ( mode == QgsComposerMapGrid::LongitudeOnly && coordinate == QgsComposerMapGrid::Longitude );
1776
1784
}
1777
1785
1786
+ bool sortByDistance ( const QPair<double , QgsComposerMapGrid::BorderSide>& a, const QPair<double , QgsComposerMapGrid::BorderSide>& b )
1787
+ {
1788
+ return a.first < b.first ;
1789
+ }
1790
+
1778
1791
QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord ( const QPointF& p, const AnnotationCoordinate coordinateType ) const
1779
1792
{
1780
1793
if ( !mComposerMap )
1781
1794
{
1782
1795
return QgsComposerMapGrid::Left;
1783
1796
}
1784
1797
1785
- double framePenWidth = mComposerMap ->hasFrame () ? mComposerMap ->pen ().widthF () : 0.000000001 ;
1798
+ double tolerance = qMax ( mComposerMap ->hasFrame () ? mComposerMap ->pen ().widthF () : 0.0 , 1.0 ) ;
1786
1799
1787
1800
// check for corner coordinates
1788
- if (( p.y () <= framePenWidth && p.x () <= framePenWidth ) // top left
1789
- || ( p.y () <= framePenWidth && p.x () >= ( mComposerMap ->rect ().width () - framePenWidth ) ) // top right
1790
- || ( p.y () >= ( mComposerMap ->rect ().height () - framePenWidth ) && p.x () <= framePenWidth ) // bottom left
1791
- || ( p.y () >= ( mComposerMap ->rect ().height () - framePenWidth ) && p.x () >= ( mComposerMap ->rect ().width () - framePenWidth ) ) // bottom right
1801
+ if (( p.y () <= tolerance && p.x () <= tolerance ) // top left
1802
+ || ( p.y () <= tolerance && p.x () >= ( mComposerMap ->rect ().width () - tolerance ) ) // top right
1803
+ || ( p.y () >= ( mComposerMap ->rect ().height () - tolerance ) && p.x () <= tolerance ) // bottom left
1804
+ || ( p.y () >= ( mComposerMap ->rect ().height () - tolerance ) && p.x () >= ( mComposerMap ->rect ().width () - tolerance ) ) // bottom right
1792
1805
)
1793
1806
{
1794
1807
// coordinate is in corner - fall back to preferred side for coordinate type
1795
1808
if ( coordinateType == QgsComposerMapGrid::Latitude )
1796
1809
{
1797
- if ( p.x () <= framePenWidth )
1810
+ if ( p.x () <= tolerance )
1798
1811
{
1799
1812
return QgsComposerMapGrid::Left;
1800
1813
}
@@ -1805,7 +1818,7 @@ QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord( const QPo
1805
1818
}
1806
1819
else
1807
1820
{
1808
- if ( p.y () <= framePenWidth )
1821
+ if ( p.y () <= tolerance )
1809
1822
{
1810
1823
return QgsComposerMapGrid::Top;
1811
1824
}
@@ -1816,23 +1829,15 @@ QgsComposerMapGrid::BorderSide QgsComposerMapGrid::borderForLineCoord( const QPo
1816
1829
}
1817
1830
}
1818
1831
1819
- // otherwise, guess side based on point
1820
- if ( p.y () <= framePenWidth )
1821
- {
1822
- return QgsComposerMapGrid::Top;
1823
- }
1824
- else if ( p.x () <= framePenWidth )
1825
- {
1826
- return QgsComposerMapGrid::Left;
1827
- }
1828
- else if ( p.x () >= ( mComposerMap ->rect ().width () - framePenWidth ) )
1829
- {
1830
- return QgsComposerMapGrid::Right;
1831
- }
1832
- else
1833
- {
1834
- return QgsComposerMapGrid::Bottom;
1835
- }
1832
+ // otherwise, guess side based on closest map side to point
1833
+ QList< QPair<double , QgsComposerMapGrid::BorderSide > > distanceToSide;
1834
+ distanceToSide << qMakePair ( p.x (), QgsComposerMapGrid::Left );
1835
+ distanceToSide << qMakePair ( mComposerMap ->rect ().width () - p.x (), QgsComposerMapGrid::Right );
1836
+ distanceToSide << qMakePair ( p.y (), QgsComposerMapGrid::Top );
1837
+ distanceToSide << qMakePair ( mComposerMap ->rect ().height () - p.y (), QgsComposerMapGrid::Bottom );
1838
+
1839
+ qSort ( distanceToSide.begin (), distanceToSide.end (), sortByDistance );
1840
+ return distanceToSide.at ( 0 ).second ;
1836
1841
}
1837
1842
1838
1843
void QgsComposerMapGrid::setLineSymbol ( QgsLineSymbolV2* symbol )
@@ -2268,21 +2273,25 @@ int QgsComposerMapGrid::crsGridParams( QgsRectangle& crsRect, QgsCoordinateTrans
2268
2273
return 0 ;
2269
2274
}
2270
2275
2271
- QPolygonF QgsComposerMapGrid::trimLineToMap ( const QPolygonF& line, const QgsRectangle& rect )
2276
+ QList< QPolygonF> QgsComposerMapGrid::trimLinesToMap ( const QPolygonF& line, const QgsRectangle& rect )
2272
2277
{
2273
- QgsPolyline polyLine;
2274
- QPolygonF::const_iterator lineIt = line.constBegin ();
2275
- for ( ; lineIt != line.constEnd (); ++lineIt )
2278
+ QgsGeometry* lineGeom = QgsGeometry::fromQPolygonF ( line );
2279
+ QgsGeometry* rectGeom = QgsGeometry::fromRect ( rect );
2280
+
2281
+ QgsGeometry* intersected = lineGeom->intersection ( rectGeom );
2282
+ QList<QgsGeometry*> intersectedParts = intersected->asGeometryCollection ();
2283
+
2284
+ QList<QPolygonF> trimmedLines;
2285
+ QList<QgsGeometry*>::const_iterator geomIt = intersectedParts.constBegin ();
2286
+ for ( ; geomIt != intersectedParts.constEnd (); geomIt++ )
2276
2287
{
2277
- polyLine. append ( QgsPoint ( lineIt-> x (), lineIt-> y () ) );
2288
+ trimmedLines << ( *geomIt )-> asQPolygonF ( );
2278
2289
}
2279
2290
2280
- QgsGeometry* geom = QgsGeometry::fromPolyline ( polyLine );
2281
-
2282
- QPolygonF clippedLine ;
2283
- QgsClipper::clippedLineWKB ( geom-> asWkb (), rect, clippedLine ) ;
2284
- delete geom ;
2285
- return clippedLine ;
2291
+ qDeleteAll ( intersectedParts );
2292
+ intersectedParts. clear ();
2293
+ delete intersected ;
2294
+ delete lineGeom ;
2295
+ delete rectGeom ;
2296
+ return trimmedLines ;
2286
2297
}
2287
-
2288
-
0 commit comments