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

« back to all changes in this revision

Viewing changes to Interaction/CreateAreaInteraction.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 "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"
8
 
#include "Map/Road.h"
9
 
#include "Map/TrackPoint.h"
10
 
#include "Utils/LineF.h"
11
 
#include "MainWindow.h"
12
 
#include "PropertiesDock.h"
13
 
 
14
 
#include <QtGui/QDockWidget>
15
 
#include <QtGui/QMessageBox>
16
 
#include <QtGui/QPainter>
17
 
 
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)
22
 
{
23
 
}
24
 
 
25
 
CreateAreaInteraction::~CreateAreaInteraction()
26
 
{
27
 
}
28
 
 
29
 
void CreateAreaInteraction::paintEvent(QPaintEvent* anEvent, QPainter& thePainter)
30
 
{
31
 
        if (theRoad && (!theRoad->layer() || theRoad->isDeleted())) { // The road was begon and then undoed. Restarting....
32
 
                HaveFirst = false;
33
 
                theRoad = NULL;
34
 
        }
35
 
 
36
 
        if (HaveFirst)
37
 
        {
38
 
                QPointF PreviousPoint;
39
 
                if (theRoad && theRoad->size())
40
 
                        PreviousPoint = view()->projection().project(CAST_NODE(theRoad->get(theRoad->size()-1))->position());
41
 
                else
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());
46
 
        }
47
 
        GenericFeatureSnapInteraction<MapFeature>::paintEvent(anEvent,thePainter);
48
 
}
49
 
 
50
 
void CreateAreaInteraction::snapMouseMoveEvent(QMouseEvent* ev, MapFeature* aFeature)
51
 
{
52
 
        if (TrackPoint* Pt = dynamic_cast<TrackPoint*>(aFeature))
53
 
                LastCursor = view()->projection().project(Pt);
54
 
        else if (Road* R = dynamic_cast<Road*>(aFeature))
55
 
        {
56
 
                Coord P(projection().inverse(ev->pos()));
57
 
                findSnapPointIndex(R, P);
58
 
                LastCursor = projection().project(P);
59
 
        }
60
 
        else
61
 
                LastCursor = ev->pos();
62
 
        view()->update();
63
 
}
64
 
 
65
 
void CreateAreaInteraction::startNewRoad(QMouseEvent* anEvent, MapFeature* aFeature)
66
 
{
67
 
        if (TrackPoint* Pt = dynamic_cast<TrackPoint*>(aFeature))
68
 
                FirstNode = Pt;
69
 
        else if (Road* aRoad = dynamic_cast<Road*>(aFeature))
70
 
        {
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);
81
 
                FirstNode = N;
82
 
        }
83
 
}
84
 
 
85
 
void CreateAreaInteraction::createNewRoad(CommandList* L)
86
 
{
87
 
        TrackPoint* From = 0;
88
 
        theRoad = new Road;
89
 
        if (M_PREFS->apiVersionNum() < 0.6)
90
 
                theRoad->setTag("created_by", QString("Merkaartor %1").arg(VERSION));
91
 
        if (FirstNode)
92
 
        {
93
 
                From = FirstNode;
94
 
                FirstNode = 0;
95
 
                if (!From->isDirty() && !From->hasOSMId() && From->isUploadable())
96
 
                        L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),From,true));
97
 
        }
98
 
        else
99
 
        {
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));
104
 
        }
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);
109
 
}
110
 
 
111
 
void CreateAreaInteraction::finishRoad(CommandList* L)
112
 
{
113
 
        if (theRelation)
114
 
                L->add(new RelationAddFeatureCommand(theRelation,"inner",theRoad));
115
 
        else if (LastRoad)
116
 
        {
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));
124
 
                LastRoad = 0;
125
 
        }
126
 
        else
127
 
                LastRoad = theRoad;
128
 
        HaveFirst = false;
129
 
        LastRoad = theRoad;
130
 
        theRoad = 0;
131
 
 
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)
135
 
        {
136
 
                EndNow = true;
137
 
        }
138
 
        L->setDescription(MainWindow::tr("Area: Finish Road %1").arg(LastRoad->description()));
139
 
        L->setFeature(LastRoad);
140
 
}
141
 
 
142
 
void CreateAreaInteraction::addToRoad(QMouseEvent* anEvent, MapFeature* Snap, CommandList* L)
143
 
{
144
 
        TrackPoint* Pt = dynamic_cast<TrackPoint*>(Snap);
145
 
        TrackPoint* To = 0;
146
 
        if (Pt)
147
 
                To = Pt;
148
 
        else if (Road* aRoad = dynamic_cast<Road*>(Snap))
149
 
        {
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);
160
 
                To = N;
161
 
        }
162
 
        if (!To)
163
 
        {
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())));
169
 
                L->setFeature(To);
170
 
        } else {
171
 
                if (!To->isDirty() && !To->hasOSMId() && To->isUploadable())
172
 
                        L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),To,true));
173
 
        }
174
 
        L->add(new RoadAddTrackPointCommand(theRoad,To));
175
 
        if (To == theRoad->get(0))
176
 
                finishRoad(L);
177
 
}
178
 
 
179
 
void CreateAreaInteraction::snapMousePressEvent(QMouseEvent* anEvent, MapFeature* aFeature)
180
 
{
181
 
        if (anEvent->buttons() & Qt::LeftButton)
182
 
        {
183
 
                if (!HaveFirst)
184
 
                {
185
 
                        HaveFirst = true;
186
 
                        startNewRoad(anEvent, aFeature);
187
 
                }
188
 
                else
189
 
                {
190
 
                        CommandList* L  = new CommandList();
191
 
                        if (!theRoad)
192
 
                                createNewRoad(L);
193
 
                        addToRoad(anEvent, aFeature, L);
194
 
                        document()->addHistory(L);
195
 
                        view()->invalidate(true, false);
196
 
                        if (theRelation)
197
 
                                Main->properties()->setSelection(theRelation);
198
 
                        else
199
 
                                Main->properties()->setSelection(theRoad);
200
 
                }
201
 
                FirstPoint = view()->projection().inverse(anEvent->pos());
202
 
        }
203
 
        else
204
 
                Interaction::mousePressEvent(anEvent);
205
 
        if (EndNow)
206
 
                view()->launch(0);
207
 
}
208
 
 
209
 
QCursor CreateAreaInteraction::cursor() const
210
 
{
211
 
        return QCursor(Qt::CrossCursor);
212
 
}