1
#include "Interaction/CreateAreaInteraction.h"
2
#include "Command/DocumentCommands.h"
3
#include "Command/RelationCommands.h"
4
#include "Command/RoadCommands.h"
5
#include "Command/TrackPointCommands.h"
6
#include "Map/Painting.h"
7
#include "Map/Relation.h"
9
#include "Map/TrackPoint.h"
10
#include "Utils/LineF.h"
11
#include "MainWindow.h"
12
#include "PropertiesDock.h"
14
#include <QtGui/QDockWidget>
15
#include <QtGui/QMessageBox>
16
#include <QtGui/QPainter>
18
CreateAreaInteraction::CreateAreaInteraction(MainWindow* aMain, MapView* aView)
19
: GenericFeatureSnapInteraction<MapFeature>(aView), Main(aMain),
20
theRelation(0), theRoad(0), LastRoad(0), FirstPoint(0,0),
21
FirstNode(0), HaveFirst(false), EndNow(false)
25
CreateAreaInteraction::~CreateAreaInteraction()
29
void CreateAreaInteraction::paintEvent(QPaintEvent* anEvent, QPainter& thePainter)
31
if (theRoad && (!theRoad->layer() || theRoad->isDeleted())) { // The road was begon and then undoed. Restarting....
38
QPointF PreviousPoint;
39
if (theRoad && theRoad->size())
40
PreviousPoint = view()->projection().project(CAST_NODE(theRoad->get(theRoad->size()-1))->position());
42
PreviousPoint = view()->projection().project(FirstPoint);
43
QBrush SomeBrush(QColor(0xff,0x77,0x11,128));
44
QPen TP(SomeBrush,projection().pixelPerM()*4);
45
::draw(thePainter,TP,MapFeature::UnknownDirection, PreviousPoint,LastCursor ,4 ,view()->projection());
47
GenericFeatureSnapInteraction<MapFeature>::paintEvent(anEvent,thePainter);
50
void CreateAreaInteraction::snapMouseMoveEvent(QMouseEvent* ev, MapFeature* aFeature)
52
if (TrackPoint* Pt = dynamic_cast<TrackPoint*>(aFeature))
53
LastCursor = view()->projection().project(Pt);
54
else if (Road* R = dynamic_cast<Road*>(aFeature))
56
Coord P(projection().inverse(ev->pos()));
57
findSnapPointIndex(R, P);
58
LastCursor = projection().project(P);
61
LastCursor = ev->pos();
65
void CreateAreaInteraction::startNewRoad(QMouseEvent* anEvent, MapFeature* aFeature)
67
if (TrackPoint* Pt = dynamic_cast<TrackPoint*>(aFeature))
69
else if (Road* aRoad = dynamic_cast<Road*>(aFeature))
71
Coord P(projection().inverse(anEvent->pos()));
72
CommandList* theList = new CommandList(MainWindow::tr("Create Area %1").arg(aRoad->description()), aRoad);
73
unsigned int SnapIdx = findSnapPointIndex(aRoad, P);
74
TrackPoint* N = new TrackPoint(P);
75
if (M_PREFS->apiVersionNum() < 0.6)
76
N->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
77
theList->add(new AddFeatureCommand(main()->document()->getDirtyOrOriginLayer(),N,true));
78
theList->add(new RoadAddTrackPointCommand(aRoad,N,SnapIdx));
79
document()->addHistory(theList);
80
view()->invalidate(true, false);
85
void CreateAreaInteraction::createNewRoad(CommandList* L)
89
if (M_PREFS->apiVersionNum() < 0.6)
90
theRoad->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
95
if (!From->isDirty() && !From->hasOSMId() && From->isUploadable())
96
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),From,true));
100
From = new TrackPoint(FirstPoint);
101
if (M_PREFS->apiVersionNum() < 0.6)
102
From->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
103
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),From,true));
105
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),theRoad,true));
106
L->add(new RoadAddTrackPointCommand(theRoad,From));
107
L->setDescription(MainWindow::tr("Area: Create Road %1").arg(theRoad->description()));
108
L->setFeature(theRoad);
111
void CreateAreaInteraction::finishRoad(CommandList* L)
114
L->add(new RelationAddFeatureCommand(theRelation,"inner",theRoad));
117
theRelation = new Relation;
118
if (M_PREFS->apiVersionNum() < 0.6)
119
theRelation->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
120
theRelation->setTag("type","multipolygon");
121
theRelation->add("outer",LastRoad);
122
theRelation->add("inner",theRoad);
123
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),theRelation,true));
132
if (QMessageBox::question(Main, tr("Add a hole?"),
133
tr("Do you want to add a(nother) hole to this area?"),
134
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
138
L->setDescription(MainWindow::tr("Area: Finish Road %1").arg(LastRoad->description()));
139
L->setFeature(LastRoad);
142
void CreateAreaInteraction::addToRoad(QMouseEvent* anEvent, MapFeature* Snap, CommandList* L)
144
TrackPoint* Pt = dynamic_cast<TrackPoint*>(Snap);
148
else if (Road* aRoad = dynamic_cast<Road*>(Snap))
150
Coord P(projection().inverse(anEvent->pos()));
151
unsigned int SnapIdx = findSnapPointIndex(aRoad, P);
152
TrackPoint* N = new TrackPoint(P);
153
if (M_PREFS->apiVersionNum() < 0.6)
154
N->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
155
CommandList* theList = new CommandList(MainWindow::tr("Area: Add node %1 to Road %2").arg(N->description()).arg(theRoad->description()), N);
156
theList->add(new AddFeatureCommand(main()->document()->getDirtyOrOriginLayer(),N,true));
157
theList->add(new RoadAddTrackPointCommand(aRoad,N,SnapIdx));
158
document()->addHistory(theList);
159
view()->invalidate(true, false);
164
To = new TrackPoint(view()->projection().inverse(anEvent->pos()));
165
if (M_PREFS->apiVersionNum() < 0.6)
166
To->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
167
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),To,true));
168
L->setDescription(MainWindow::tr("Area: Add node %1 to Road %2").arg(To->description().arg(theRoad->description())));
171
if (!To->isDirty() && !To->hasOSMId() && To->isUploadable())
172
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),To,true));
174
L->add(new RoadAddTrackPointCommand(theRoad,To));
175
if (To == theRoad->get(0))
179
void CreateAreaInteraction::snapMousePressEvent(QMouseEvent* anEvent, MapFeature* aFeature)
181
if (anEvent->buttons() & Qt::LeftButton)
186
startNewRoad(anEvent, aFeature);
190
CommandList* L = new CommandList();
193
addToRoad(anEvent, aFeature, L);
194
document()->addHistory(L);
195
view()->invalidate(true, false);
197
Main->properties()->setSelection(theRelation);
199
Main->properties()->setSelection(theRoad);
201
FirstPoint = view()->projection().inverse(anEvent->pos());
204
Interaction::mousePressEvent(anEvent);
209
QCursor CreateAreaInteraction::cursor() const
211
return QCursor(Qt::CrossCursor);