-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace2dwidget.cpp
More file actions
53 lines (44 loc) · 1.58 KB
/
workspace2dwidget.cpp
File metadata and controls
53 lines (44 loc) · 1.58 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
#include "workspace2dwidget.h"
Workspace2DWidget::Workspace2DWidget(QWidget* parent)
: QGraphicsView(parent), gridSize(20)
{
scene = new QGraphicsScene(this);
setScene(scene);
setRenderHint(QPainter::Antialiasing);
setDragMode(QGraphicsView::NoDrag);
setMouseTracking(true);
drawGrid();
}
void Workspace2DWidget::wheelEvent(QWheelEvent* event)
{
double scaleFactor = 1.15;
if (event->angleDelta().y() > 0) {
scale(scaleFactor, scaleFactor);
} else {
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
}
void Workspace2DWidget::mouseMoveEvent(QMouseEvent* event)
{
lastMousePos = mapToScene(event->pos());
viewport()->update();
QGraphicsView::mouseMoveEvent(event);
}
void Workspace2DWidget::drawBackground(QPainter* painter, const QRectF& rect)
{
QGraphicsView::drawBackground(painter, rect);
painter->setPen(QPen(Qt::gray, 0.5));
qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
for (qreal x = left; x < rect.right(); x += gridSize)
painter->drawLine(QLineF(x, rect.top(), x, rect.bottom()));
for (qreal y = top; y < rect.bottom(); y += gridSize)
painter->drawLine(QLineF(rect.left(), y, rect.right(), y));
painter->setPen(QPen(Qt::red, 0.8));
painter->drawLine(QLineF(lastMousePos.x(), rect.top(), lastMousePos.x(), rect.bottom()));
painter->drawLine(QLineF(rect.left(), lastMousePos.y(), rect.right(), lastMousePos.y()));
}
void Workspace2DWidget::drawGrid()
{
scene->setSceneRect(-1000, -1000, 2000, 2000);
}