source: subversion/applications/editors/osm-editor/qt3/SegSplitter.cpp @ 16590

Last change on this file since 16590 was 1158, checked in by nick, 13 years ago

moved current version to qt3 directory in preparation for qt4

File size: 4.7 KB
Line 
1// SegSplitter
2// This class represents the process of splitting a segment.
3// Because of the many different asynchronous HTTP requests going off, it
4// makes sense to encapsulate the whole process in its own class.
5
6
7#include "SegSplitter.h"
8
9namespace OpenStreetMap
10{
11
12// splitSeg() is the method which kicks it off
13// Parameters: a pointer to the segment to be split, and the EarthPoint to
14// split it at
15//
16void SegSplitter::splitSeg(Segment *seg,const EarthPoint& p,int limit)
17{
18        bool liveUpdate = true;
19
20        Way *w;
21        // Get the node nearest the split point
22        Node *n = components->getNearestNode(p.y,p.x,limit);
23
24        // If there isn't an existing node there, add one
25        if(!n)
26                n=components->addNewNode(p.y,p.x,"","node");
27
28        // If the segment is in a way, get hold of the way and remove the
29        // segment from it. Store the index of the segment in the way; the
30        // split segments will need to be added at that position later.
31        if(wayID=seg->getWayID())
32        {
33                w = components->getWayByID(wayID);
34                wayIndex = w->removeSegment(seg);
35        }
36
37        // Actually break the segment; this returns a pair of the two new segments
38        segments = components->breakSegment(seg,n);
39        QString url = "/api/0.3/segment/0";
40
41        // Upload the node to the server
42        // This will call nodeAdded() when the response comes back
43        if(n->getOSMID()<=0 && liveUpdate)
44        {
45                osmhttp->scheduleCommand("PUT","/api/0.3/node/0",n->toOSM(),
46                                                this,
47                                                SLOT(nodeAdded(const QByteArray&,void*)),
48                                                n,SLOT(handleError(const QString&)));
49        }
50
51        // Delete the old segment
52        if(liveUpdate)
53        {
54                url.sprintf("/api/0.3/segment/%d",seg->getOSMID());
55                osmhttp->scheduleCommand("DELETE",url,this,NULL,NULL,
56                                                                        SLOT(handleError(const QString&)));
57        }       
58        // If not in live update the only thing we need to do is add the
59        // segments to the way
60        else
61        {
62                if(wayID)
63                {
64                        w->addSegmentAt(wayIndex,segments->second);
65                        w->addSegmentAt(wayIndex,segments->first);
66                }
67        }
68}
69
70// nodeAdded()
71// called when the node at the break point has been added on the server
72void SegSplitter::nodeAdded(const QByteArray& resp, void *node)
73{
74        Node *n = (Node*)node;
75    QString str = resp;
76    QStringList ids;
77
78    if(!str.isNull())cerr<<"STR=" << str << endl;
79    ids = QStringList::split("\n", str);
80
81        // Set the node ID to the ID returned
82    if(n)
83    {
84        n->setOSMID(atoi(ids[0].ascii()));
85
86                // Call addSplitSegs() to add the two new segments
87                addSplitSegs();
88    }
89}
90
91// This method simply uploads the two new segments.
92// The callback on receiving the response from the server is
93// splitSegAdded(), see below
94
95void SegSplitter::addSplitSegs()
96{
97
98        QString a = segments->first->toOSM();
99        QString b = segments->second->toOSM();
100
101
102        osmhttp->scheduleCommand("PUT","/api/0.3/segment/0",
103                                        segments->first->toOSM(), this,
104                                                        SLOT(splitSegAdded(const QByteArray&,void*)),
105                                                segments->first,
106                                                SLOT(handleError(const QString&)));
107
108        osmhttp->scheduleCommand("PUT","/api/0.3/segment/0",
109                                                                segments->second->toOSM(), this,
110                                                                SLOT(splitSegAdded(const QByteArray&,void*)),
111                                                segments->second,
112                                                SLOT(handleError(const QString&)));
113}
114
115// splitSegAdded()
116// Callback function from uploading the two new segments to the server
117void SegSplitter::splitSegAdded(const QByteArray& resp, void *segment)
118{
119        // Set the ID
120        Segment *seg = (Segment*) segment;
121    QString str = resp;
122    QStringList ids;
123    ids = QStringList::split("\n", str);
124    if(seg)
125    {
126        seg->setOSMID(atoi(ids[0].ascii()));
127    }
128
129        cerr << "segments->first->getOSMID():" << segments->first->getOSMID()<<endl;
130        cerr << "segments->2nd->getOSMID():" << segments->second->getOSMID()<<endl;
131        cerr << "wayID: " << wayID << endl;
132        // This if will only be true after both segments have been uploaded
133        if(segments->first->getOSMID() && segments->second->getOSMID())
134        {
135                // If the original segment belonged in a way...
136                if(wayID>0)
137                {
138                        // get the way
139                        Way *w=components->getWayByID(wayID);
140
141                        // Add the two new segments to the way at the same place
142                        w->addSegmentAt(wayIndex,segments->second);
143                        w->addSegmentAt(wayIndex,segments->first);
144
145                        // Upload the altered way
146                        QString url;
147                        url.sprintf("/api/0.3/way/%d",w->getOSMID());
148                        osmhttp->scheduleCommand("PUT",url, w->toOSM(),this, 
149                                                        SLOT(finished(const QByteArray&,void*)),
150                                                        NULL,
151                                                        SLOT(handleError(const QString&)));
152                }
153                else
154                        emit done();
155        }
156}       
157
158// We're done (at last!!!)
159void SegSplitter::finished(const QByteArray&, void *)
160{
161        emit done();
162}
163
164SegSplitter::~SegSplitter()
165{
166        if(segments)
167                delete segments;
168}
169
170void SegSplitter::handleHttpError(int i,const QString& e)
171{
172        QString eout;
173        cerr << "HTTP error: " << i << " " << e << endl;
174        eout.sprintf("HTTP error %d %s", i, e.ascii());
175    emit error(eout);   
176}
177
178void SegSplitter::handleError(const QString& e)
179{
180        cerr << "Error: " << e << endl;
181    emit error(e);     
182}
183}
Note: See TracBrowser for help on using the repository browser.