1
#ifndef MERKATOR_INTERACTION_H_
2
#define MERKATOR_INTERACTION_H_
14
#include "MainWindow.h"
17
#include "PropertiesDock.h"
18
#include "FeaturesDock.h"
19
#include "Maps/MapDocument.h"
20
#include "Maps/MapFeature.h"
21
#include "Maps/Road.h"
22
#include "Maps/TrackPoint.h"
24
#include <QtCore/QObject>
25
#include <QtCore/QTime>
26
#include <QtGui/QApplication>
27
#include <QtGui/QCursor>
28
#include <QtGui/QMouseEvent>
33
#define XY_TO_COORD(x) projection().inverse(transform().inverted().map(QPointF(x)))
34
#define COORD_TO_XY(x) transform().map(projection().project(x)).toPoint()
36
class Interaction : public QObject
40
Interaction(MapView* theView);
41
virtual ~Interaction();
43
virtual void mousePressEvent(QMouseEvent * event);
44
virtual void mouseReleaseEvent(QMouseEvent * event);
45
virtual void mouseMoveEvent(QMouseEvent* event);
46
virtual void paintEvent(QPaintEvent* anEvent, QPainter& thePainter);
47
virtual QString toHtml() = 0;
50
virtual QCursor cursor() const;
53
MapDocument* document();
55
const Projection& projection() const;
56
const QTransform& transform() const;
64
void requestCustomContextMenu(const QPoint & pos);
72
template<class FeatureType>
73
class GenericFeatureSnapInteraction : public Interaction
76
GenericFeatureSnapInteraction(MapView* theView)
77
: Interaction(theView), LastSnap(0), SnapActive(true),
78
NoSelectPoints(false), NoSelectRoads(false)
82
virtual void paintEvent(QPaintEvent* anEvent, QPainter& thePainter)
84
Interaction::paintEvent(anEvent, thePainter);
86
for (int i=0; i<main()->features()->highlightedSize(); ++i)
87
if (document()->exists(main()->features()->highlighted(i)))
88
main()->features()->highlighted(i)->drawHighlight(thePainter, view(), true);
89
for (int i=0; i<main()->properties()->size(); ++i)
90
if (document()->exists(main()->properties()->selection(i)))
91
main()->properties()->selection(i)->drawFocus(thePainter, view());
92
for (int i=0; i<main()->properties()->highlightedSize(); ++i)
93
if (document()->exists(main()->properties()->highlighted(i)))
94
main()->properties()->highlighted(i)->drawHighlight(thePainter, view(), true);
97
if (LastSnap && document()->exists(LastSnap)) {
98
LastSnap->drawHover(thePainter, view());
99
view()->setToolTip(LastSnap->toHtml());
101
view()->setToolTip("");
105
virtual void mousePressEvent(QMouseEvent * event)
108
snapMousePressEvent(event,LastSnap);
109
if (!(M_PREFS->getMouseSingleButton() && LastSnap))
110
Interaction::mousePressEvent(event);
112
virtual void mouseReleaseEvent(QMouseEvent * event)
115
snapMouseReleaseEvent(event,LastSnap);
116
if (!(M_PREFS->getMouseSingleButton() && LastSnap))
117
Interaction::mouseReleaseEvent(event);
119
virtual void mouseMoveEvent(QMouseEvent* event)
122
snapMouseMoveEvent(event, LastSnap);
123
if (!(M_PREFS->getMouseSingleButton() && LastSnap))
124
Interaction::mouseMoveEvent(event);
126
virtual void snapMousePressEvent(QMouseEvent * , FeatureType*)
129
virtual void snapMouseReleaseEvent(QMouseEvent * , FeatureType*)
132
virtual void snapMouseMoveEvent(QMouseEvent* , FeatureType*)
135
void activateSnap(bool b)
139
void addToNoSnap(FeatureType* F)
155
QList<MapFeature*> snapList()
159
void addSnap(MapFeature* aSnap)
161
StackSnap.append(aSnap);
163
void setSnap(QList<MapFeature*> aSnapList)
165
StackSnap = aSnapList;
171
if (curStackSnap > StackSnap.size() -1)
173
view()->properties()->setSelection(StackSnap[curStackSnap]);
179
if (curStackSnap < 0)
180
curStackSnap = StackSnap.size() -1;
181
view()->properties()->setSelection(StackSnap[curStackSnap]);
184
void setDontSelectPoints(bool b)
188
void setDontSelectRoads(bool b)
193
void updateSnap(QMouseEvent* event)
201
( (QApplication::keyboardModifiers() & Qt::AltModifier) && (QApplication::keyboardModifiers() &Qt::ControlModifier) );
202
FeatureType* Prev = LastSnap;
204
if (!SnapActive) return;
205
//QTime Start(QTime::currentTime());
206
CoordBox HotZone(XY_TO_COORD(event->pos()-QPointF(15,15)),XY_TO_COORD(event->pos()+QPointF(15,15)));
208
double BestDistance = 5;
210
//ggl::box < Coord > cb(HotZone.bottomLeft(), HotZone.topRight());
212
for (int j=0; j<document()->layerSize(); ++j) {
213
if (!document()->getLayer(j)->isVisible() || document()->getLayer(j)->isReadonly())
216
std::deque < MapFeaturePtr > ret = document()->getLayer(j)->indexFind(HotZone);
217
for (std::deque < MapFeaturePtr >::const_iterator it = ret.begin(); it < ret.end(); ++it) {
218
FeatureType* Pt = dynamic_cast<FeatureType*>(*it);
221
if (Pt->notEverythingDownloaded())
223
if ( (NoRoads || NoSelectRoads) && dynamic_cast<Road*>(Pt))
225
if (NoSelectPoints && dynamic_cast<TrackPoint*>(Pt))
227
if (std::find(NoSnap.begin(),NoSnap.end(),Pt) != NoSnap.end())
229
double Distance = Pt->pixelDistance(event->pos(), 5.01, projection(), transform());
230
SnapList.push_back(Pt);
231
if (Distance < BestDistance)
233
BestDistance = Distance;
241
for (VisibleFeatureIterator it(document()); !it.isEnd(); ++it)
243
FeatureType* Pt = dynamic_cast<FeatureType*>(it.get());
246
if (Pt->layer()->isReadonly())
248
if (Pt->notEverythingDownloaded())
250
if ( (NoRoads || NoSelectRoads) && dynamic_cast<Road*>(Pt))
252
if (NoSelectPoints && dynamic_cast<TrackPoint*>(Pt))
254
if (std::find(NoSnap.begin(),NoSnap.end(),Pt) != NoSnap.end())
256
if (Pt->boundingBox().disjunctFrom(HotZone))
258
double Distance = Pt->pixelDistance(transform().inverted().map(event->pos()), 5.01, projection());
259
SnapList.push_back(Pt);
260
if (Distance < BestDistance)
262
BestDistance = Distance;
268
if (Prev != LastSnap) {
269
curStackSnap = SnapList.indexOf(LastSnap);
273
if (M_PREFS->getMapTooltip()) {
275
view()->setToolTip(LastSnap->toHtml());
277
view()->setToolTip("");
279
if (M_PREFS->getInfoOnHover() && main()->info()->isVisible()) {
281
main()->info()->setHoverHtml(LastSnap->toHtml());
283
main()->info()->unsetHoverHtml();
287
FeatureType* LastSnap;
288
QList<FeatureType*> NoSnap;
295
QList<MapFeature*> StackSnap;
296
QList<MapFeature*> SnapList;
300
typedef GenericFeatureSnapInteraction<MapFeature> FeatureSnapInteraction;
301
typedef GenericFeatureSnapInteraction<TrackPoint> TrackPointSnapInteraction;
302
typedef GenericFeatureSnapInteraction<Road> RoadSnapInteraction;