-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapwidget.cpp
More file actions
63 lines (53 loc) · 1.48 KB
/
mapwidget.cpp
File metadata and controls
63 lines (53 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "mapwidget.h"
#include <QKeyEvent>
#include <QPointF>
MapWidget::MapWidget(QWidget *parent) : QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex);
scene->setSceneRect(0, 0, 640, 480);
setScene(scene);
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse);
scale(qreal(0.8), qreal(0.8));
setMinimumSize(640, 480);
setDragMode(QGraphicsView::ScrollHandDrag);
setWindowTitle(tr("Elastic Nodes"));
lines.append(new RoadLine(QPointF(50, 100), QPointF(250, 100)));
lines.append(new RoadLine(QPointF(150, 100), QPointF(150, 300)));
lines.append(new RoadLine(QPointF(50, 300), QPointF(250, 300)));
for (auto line: lines)
{
scene->addItem(line);
}
}
void MapWidget::scaleView(qreal scaleFactor)
{
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
#if QT_CONFIG(wheelevent)
void MapWidget::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, -event->delta() / 240.0));
}
#endif
void MapWidget::zoomIn()
{
scaleView(qreal(1.2));
}
void MapWidget::zoomOut()
{
scaleView(1 / qreal(1.2));
}
void MapWidget::setTime(int time)
{
for (auto line: lines)
{
line->setTime(time);
}
}