~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to src/Maps/Painting.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Maps/Painting.h"
 
2
#include "Maps/Projection.h"
 
3
#include "Maps/TrackPoint.h"
 
4
#include "Maps/Relation.h"
 
5
#include "Maps/Road.h"
 
6
#include "Utils/LineF.h"
 
7
 
 
8
#include <QtGui/QPainter>
 
9
#include <QtGui/QPainterPath>
 
10
#include <QLineF>
 
11
 
 
12
#include <utility>
 
13
 
 
14
static void buildCubicPath(QPainterPath& Path, const QPointF& P1, const QPointF& P2, const QPointF& P3, const QPointF& P4)
 
15
{
 
16
        LineF L(P1,P4);
 
17
        double D2 = L.distance(P2);
 
18
        double D3 = L.distance(P3);
 
19
        if ( (D2 < 0.5) && (D3<0.5) )
 
20
                Path.lineTo(P4);
 
21
        else
 
22
        {
 
23
                QPointF H = (P2+P3)/2.0;
 
24
                QPointF L2 = (P1+P2)/2.0;
 
25
                QPointF R3 = (P3+P4)/2.0;
 
26
                QPointF L3 = (L2+H)/2.0;
 
27
                QPointF R2 = (H+R3)/2.0;
 
28
                QPointF L4 = (L3+R2)/2.0;
 
29
                buildCubicPath(Path,P1,L2,L3,L4);
 
30
                buildCubicPath(Path,L4,R2,R3,P4);
 
31
 
 
32
        }
 
33
}
 
34
 
 
35
//bool QRectInterstects(const QRect& r, const QLine& l, QPoint& a, QPoint& b)
 
36
//{
 
37
//      QLineF lF = QLineF(l);
 
38
//      QPointF pF;
 
39
//      bool hasP1 = false;
 
40
//      bool hasP2 = false;
 
41
//
 
42
//      if (QLineF(r.topLeft(), r.bottomLeft()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
 
43
//              a = pF.toPoint();
 
44
//              hasP1 = true;
 
45
//      } 
 
46
//      if (QLineF(r.bottomLeft(), r.bottomRight()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
 
47
//              if (hasP1) {
 
48
//                      b = pF.toPoint();
 
49
//                      hasP2 = true;
 
50
//              } else {
 
51
//                      a = pF.toPoint();
 
52
//                      hasP1 = true;
 
53
//              }
 
54
//      } 
 
55
//      if (QLineF(r.bottomRight(), r.topRight()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
 
56
//              if (hasP1) {
 
57
//                      b = pF.toPoint();
 
58
//                      hasP2 = true;
 
59
//              } else {
 
60
//                      a = pF.toPoint();
 
61
//                      hasP1 = true;
 
62
//              }
 
63
//      } 
 
64
//      if (QLineF(r.topRight(), r.topLeft()).intersect(lF, &pF) == QLineF::BoundedIntersection) {
 
65
//              if (hasP1) {
 
66
//                      b = pF.toPoint();
 
67
//                      hasP2 = true;
 
68
//              } else {
 
69
//                      a = pF.toPoint();
 
70
//                      hasP1 = true;
 
71
//              }
 
72
//      }
 
73
//
 
74
//      if (hasP1 && hasP2) {
 
75
//              if (QLineF(a,b).angleTo(lF) > 15.0) {
 
76
//                      QPoint t = b;
 
77
//                      b = a;
 
78
//                      a = t;
 
79
//              }
 
80
//      }
 
81
//      if (hasP1)
 
82
//              return true;
 
83
//      else
 
84
//              return false;
 
85
//}
 
86
//
 
87
//void buildPathFromRoad(Road *R, Projection const &theProjection, QPainterPath &Path, const QRect& clipRect)
 
88
//{
 
89
//      int first=0, last=R->size();
 
90
//      //if (!theProjection.viewport().contains(R->boundingBox())) {
 
91
//      //      for (int i=0; i<R->size(); ++i) {
 
92
//      //              if (theProjection.viewport().contains(R->get(i)->boundingBox())) {
 
93
//      //                      if (!first)
 
94
//      //                              first=i;
 
95
//      //                      last=i;
 
96
//      //              }
 
97
//      //      }
 
98
//      //      if (first) first--;
 
99
//      //      last=qMin(last+2, R->size());
 
100
//      //}
 
101
//
 
102
//      bool lastPointVisible = true;
 
103
//      QPoint lastPoint = theProjection.project(R->get(first)->position());
 
104
//      QPoint p = lastPoint;
 
105
//
 
106
//      if (!clipRect.contains(p)) {
 
107
//              p.setX(qMax(clipRect.left(), p.x()));
 
108
//              p.setX(qMin(clipRect.right(), p.x()));
 
109
//              p.setY(qMax(clipRect.top(), p.y()));
 
110
//              p.setY(qMin(clipRect.bottom(), p.y()));
 
111
//              lastPointVisible = false;
 
112
//      }
 
113
//      Path.moveTo(p);
 
114
//      if (R->smoothed().size())
 
115
//      {
 
116
//              for (int i=3; i<R->smoothed().size(); i+=3)
 
117
//                      Path.cubicTo(
 
118
//                              theProjection.project(R->smoothed()[i-2]),
 
119
//                              theProjection.project(R->smoothed()[i-1]),
 
120
//                              theProjection.project(R->smoothed()[i]));
 
121
//      }
 
122
//      else
 
123
//              for (int j=first+1; j<last; ++j) {
 
124
//                      p = theProjection.project(R->get(j)->position());
 
125
//                      if (!clipRect.contains(p)) {
 
126
//                              if (!lastPointVisible) {
 
127
//                                      QPoint a, b;
 
128
//                                      if (QRectInterstects(clipRect, QLine(lastPoint, p), a, b)) {
 
129
//                                              Path.lineTo(a);
 
130
//                                              lastPoint = p;
 
131
//                                              p = b;
 
132
//                                      } else {
 
133
//                                              lastPoint = p;
 
134
//                                              p.setX(qMax(clipRect.left(), p.x()));
 
135
//                                              p.setX(qMin(clipRect.right(), p.x()));
 
136
//                                              p.setY(qMax(clipRect.top(), p.y()));
 
137
//                                              p.setY(qMin(clipRect.bottom(), p.y()));
 
138
//                                      }
 
139
//                              } else {
 
140
//                                      QPoint a, b;
 
141
//                                      QRectInterstects(clipRect, QLine(lastPoint, p), a, b);
 
142
//                                      lastPoint = p;
 
143
//                                      p = a;
 
144
//                              }
 
145
//                              lastPointVisible = false;
 
146
//                      } else {
 
147
//                              if (!lastPointVisible) {
 
148
//                                      QPoint a, b;
 
149
//                                      QRectInterstects(clipRect, QLine(lastPoint, p), a, b);
 
150
//                                      Path.lineTo(a);
 
151
//                              }
 
152
//                              lastPoint = p;
 
153
//                              lastPointVisible = true;
 
154
//                      }
 
155
//                      Path.lineTo(p);
 
156
//              }
 
157
//}
 
158
//
 
159
void buildPolygonFromRoad(Road *R, Projection const &theProjection, QPolygonF &Polygon)
 
160
{
 
161
        for (int i=0; i<R->size(); ++i)
 
162
                Polygon.append(theProjection.project(R->getNode(i)));
 
163
}
 
164
 
 
165
/// draws way with oneway markers
 
166
void draw(QPainter& thePainter, QPen& thePen, MapFeature::TrafficDirectionType TT, const QPointF& FromF, const QPointF& ToF, double theWidth, const Projection&)
 
167
{
 
168
        QPainterPath Path;
 
169
        Path.moveTo(FromF);
 
170
        Path.lineTo(ToF);
 
171
        double DistFromCenter = theWidth*2;
 
172
        if (distance(FromF,ToF) > qMax(40.0,DistFromCenter*2+4))
 
173
        {
 
174
                QPointF H(FromF+ToF);
 
175
                H *= 0.5;
 
176
                double A = angle(FromF-ToF);
 
177
                QPointF T(DistFromCenter*cos(A),DistFromCenter*sin(A));
 
178
                QPointF V1(theWidth*cos(A+M_PI/6),theWidth*sin(A+M_PI/6));
 
179
                QPointF V2(theWidth*cos(A-M_PI/6),theWidth*sin(A-M_PI/6));
 
180
//              MapFeature::TrafficDirectionType TT = W->trafficDirection();
 
181
                if ( (TT == MapFeature::OtherWay) || (TT == MapFeature::BothWays) )
 
182
                {
 
183
                        thePainter.setPen(QColor(0,0,0));
 
184
                        thePainter.drawLine(H+T,H+T-V1);
 
185
                        thePainter.drawLine(H+T,H+T-V2);
 
186
                }
 
187
                if ( (TT == MapFeature::OneWay) || (TT == MapFeature::BothWays) )
 
188
                {
 
189
                        thePainter.setPen(QColor(0,0,0));
 
190
                        thePainter.drawLine(H-T,H-T+V1);
 
191
                        thePainter.drawLine(H-T,H-T+V2);
 
192
                }
 
193
        }
 
194
        thePainter.strokePath(Path,thePen);
 
195
}
 
196
 
 
197
void draw(QPainter& thePainter, QPen& thePen, MapFeature::TrafficDirectionType TT, const Coord& From, const Coord& To, double theWidth, const Projection& theProjection)
 
198
{
 
199
        QPointF FromF(theProjection.project(From));
 
200
        QPointF ToF(theProjection.project(To));
 
201
        draw(thePainter,thePen,TT,FromF,ToF,theWidth,theProjection);
 
202
}
 
203
 
 
204
 
 
205
/* void draw(QPainter& thePainter, QPen& thePen, Way* W, const Projection& theProjection)
 
206
{
 
207
        QPainterPath Path;
 
208
        QPointF FromF(theProjection.project(W->from()));
 
209
        QPointF ToF(theProjection.project(W->to()));
 
210
        Path.moveTo(FromF);
 
211
        Path.lineTo(ToF);
 
212
        thePainter.strokePath(Path,thePen);
 
213
} */
 
214
 
 
215
 
 
216