1
#include "Interaction/CreatePolygonInteraction.h"
2
#include "Command/DocumentCommands.h"
3
#include "Command/RoadCommands.h"
4
#include "Command/TrackPointCommands.h"
5
#include "Maps/Painting.h"
7
#include "Maps/TrackPoint.h"
8
#include "Utils/LineF.h"
9
#include "PropertiesDock.h"
10
#include "Preferences/MerkaartorPreferences.h"
12
#include <QtGui/QPainter>
13
#include <QInputDialog>
17
CreatePolygonInteraction::CreatePolygonInteraction(MainWindow* aMain, MapView* aView, int sides)
18
: Interaction(aView), Main(aMain), Origin(0,0), Sides(sides), HaveOrigin(false), bAngle(0.0), bScale(QPointF(1., 1.))
22
CreatePolygonInteraction::~CreatePolygonInteraction()
27
QString CreatePolygonInteraction::toHtml()
30
help = (MainWindow::tr("LEFT-CLICK to start;DRAG to scale;SHIFT-DRAG to rotate;LEFT-CLICK to end"));
32
QStringList helpList = help.split(";");
35
desc = QString("<big><b>%1</b></big>").arg(MainWindow::tr("Create Polygon Interaction"));
39
"<small><i>" + QString(metaObject()->className()) + "</i></small><br/>"
42
S += "<ul style=\"margin-left: 0px; padding-left: 0px;\">";
43
for (int i=0; i<helpList.size(); ++i) {
44
S+= "<li>" + helpList[i] + "</li>";
47
S += "</body></html>";
53
void CreatePolygonInteraction::testIntersections(CommandList* L, Road* Left, int FromIdx, Road* Right, int RightIndex)
55
LineF L1(COORD_TO_XY(Right->getNode(RightIndex-1)),
56
COORD_TO_XY(Right->getNode(RightIndex)));
57
for (int i=FromIdx; i<Left->size(); ++i)
59
LineF L2(COORD_TO_XY(Left->getNode(i-1)),
60
COORD_TO_XY(Left->getNode(i)));
61
QPointF Intersection(L1.intersectionWith(L2));
62
if (L1.segmentContains(Intersection) && L2.segmentContains(Intersection))
64
TrackPoint* Pt = new TrackPoint(XY_TO_COORD(Intersection));
65
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),Pt,true));
66
L->add(new RoadAddTrackPointCommand(Left,Pt,i));
67
L->add(new RoadAddTrackPointCommand(Right,Pt,RightIndex));
68
testIntersections(L,Left,i+2,Right,RightIndex);
69
testIntersections(L,Left,i+2,Right,RightIndex+1);
75
void CreatePolygonInteraction::mousePressEvent(QMouseEvent * event)
77
if (event->buttons() & Qt::LeftButton)
82
Origin = XY_TO_COORD(event->pos());
83
OriginF = event->pos();
85
bScale = QPointF(1., 1.);
89
QPointF CenterF(0.5, 0.5);
93
double Angle = 2*M_PI/Sides;
94
QBrush SomeBrush(QColor(0xff,0x77,0x11,128));
95
QPen TP(SomeBrush,view()->pixelPerM()*4);
98
m.translate(OriginF.x(), OriginF.y());
100
m.scale(bScale.x(), bScale.y());
102
QPointF Prev(CenterF.x()+cos(-Angle/2)*Radius,CenterF.y()+sin(-Angle/2)*Radius);
103
TrackPoint* First = new TrackPoint(XY_TO_COORD(m.map(Prev)));
106
if (M_PREFS->apiVersionNum() < 0.6)
107
R->setTag("created_by", QString("Merkaartor v%1%2").arg(STRINGIFY(VERSION)).arg(STRINGIFY(REVISION)));
108
CommandList* L = new CommandList(MainWindow::tr("Create Polygon %1").arg(R->id()), R);
109
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),First,true));
110
for (double a = 2*M_PI - Angle*3/2; a>0; a-=Angle)
112
QPointF Next(CenterF.x()+cos(a)*Radius,CenterF.y()+sin(a)*Radius);
113
TrackPoint* New = new TrackPoint(XY_TO_COORD(m.map(Next)));
114
if (M_PREFS->apiVersionNum() < 0.6)
115
New->setTag("created_by", QString("Merkaartor v%1%2").arg(STRINGIFY(VERSION)).arg(STRINGIFY(REVISION)));
116
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),New,true));
120
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),R,true));
121
for (FeatureIterator it(document()); !it.isEnd(); ++it)
123
Road* W1 = dynamic_cast<Road*>(it.get());
125
for (int i=1; i<W1->size(); ++i)
127
int Before = W1->size();
128
testIntersections(L,R,1,W1,i);
129
int After = W1->size();
133
Main->properties()->setSelection(R);
134
document()->addHistory(L);
135
view()->invalidate(true, false);
140
Interaction::mousePressEvent(event);
143
void CreatePolygonInteraction::paintEvent(QPaintEvent* , QPainter& thePainter)
147
QPointF CenterF(0.5, 0.5);
150
Radius = sqrt(2.)/2.;
153
m.translate(OriginF.x(), OriginF.y());
155
m.scale(bScale.x(), bScale.y());
156
QPolygonF thePoly = m.map(QRectF(QPointF(0.0, 0.0), QPointF(1.0, 1.0)));
158
thePainter.setPen(QPen(QColor(0,0,255),1,Qt::DotLine));
159
thePainter.drawPolygon(thePoly);
161
double Angle = 2*M_PI/Sides;
162
QBrush SomeBrush(QColor(0xff,0x77,0x11,128));
163
QPen TP(SomeBrush,view()->pixelPerM()*4);
164
QPointF Prev(CenterF.x()+cos(-Angle/2)*Radius,CenterF.y()+sin(-Angle/2)*Radius);
165
for (double a = 2*M_PI - Angle*3/2; a>0; a-=Angle)
167
QPointF Next(CenterF.x()+cos(a)*Radius,CenterF.y()+sin(a)*Radius);
168
::draw(thePainter,TP,MapFeature::UnknownDirection, m.map(Prev),m.map(Next),4,view()->projection());
171
QPointF Next(CenterF.x()+cos(-Angle/2)*Radius,CenterF.y()+sin(-Angle/2)*Radius);
172
::draw(thePainter,TP,MapFeature::UnknownDirection, m.map(Prev),m.map(Next),4,view()->projection());
176
void CreatePolygonInteraction::mouseMoveEvent(QMouseEvent* event)
180
m.translate(OriginF.x(), OriginF.y());
183
if (event->modifiers() & Qt::ShiftModifier) {
184
bAngle += radToAng(angle(m.inverted().map(LastCursor), m.inverted().map(event->pos())));
187
m2.translate(OriginF.x(), OriginF.y());
189
bScale = m2.inverted().map(event->pos());
191
bScale = m.inverted().map(event->pos());
196
LastCursor = event->pos();
197
Interaction::mouseMoveEvent(event);
202
QCursor CreatePolygonInteraction::cursor() const
204
return QCursor(Qt::CrossCursor);