-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainscene.cpp
94 lines (84 loc) · 2.31 KB
/
mainscene.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "mainscene.hpp"
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
MainScene::MainScene(QObject *parent) :
QGraphicsScene(parent)
{
QGraphicsLineItem *line;
QPen pen(Qt::DashLine);
//draw net
setSceneRect(0,0, WIDTH, HEIGTH);
for (int i = 1; i < WIDTH/EDGE_LENGTH; ++i)
{
line = new QGraphicsLineItem;
line->setPen(pen);
line->setLine(i*EDGE_LENGTH, 0, i*EDGE_LENGTH, HEIGTH);
addItem(line);
}
for (int i = 1; i < HEIGTH/EDGE_LENGTH; ++i)
{
line = new QGraphicsLineItem;
line->setPen(pen);
line->setLine(0, i*EDGE_LENGTH, WIDTH, i*EDGE_LENGTH);
addItem(line);
}
}
MainScene::~MainScene()
{
}
void MainScene::drawPixel(QPoint point, QColor color)
{
if (itemAt(point.x() * EDGE_LENGTH + 2, point.y() * EDGE_LENGTH + 2))
{
qDebug()<<"Pixel is already been drawn";
return;
}
QGraphicsRectItem *item = new QGraphicsRectItem;
QBrush br(color, Qt::SolidPattern);
item->setBrush(br);
item->setRect(point.x()*EDGE_LENGTH, point.y()*EDGE_LENGTH, EDGE_LENGTH, EDGE_LENGTH);
addItem(item);
}
QPoint &MainScene::convertFromRealCoordinate(QPoint realPoint)
{
QPoint point(realPoint.x() / EDGE_LENGTH, realPoint.y() / EDGE_LENGTH);
return point;
}
void MainScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPoint p = event->scenePos().toPoint();
QPoint p1 = convertFromRealCoordinate(p);
if (event->button() == Qt::LeftButton)
{
drawPixel(p1);
}
if (event->button() == Qt::RightButton)
{
QGraphicsRectItem *item = qgraphicsitem_cast<QGraphicsRectItem*>(itemAt(p));
if (item)
removeItem(item);
}
}
void MainScene::createNet()
{
QGraphicsLineItem *line;
QPen pen(Qt::DashLine);
//draw net
setSceneRect(0,0, WIDTH, HEIGTH);
for (int i = 1; i < WIDTH/EDGE_LENGTH; ++i)
{
line = new QGraphicsLineItem;
line->setPen(pen);
line->setLine(i*EDGE_LENGTH, 0, i*EDGE_LENGTH, HEIGTH);
addItem(line);
}
for (int i = 1; i < HEIGTH/EDGE_LENGTH; ++i)
{
line = new QGraphicsLineItem;
line->setPen(pen);
line->setLine(0, i*EDGE_LENGTH, WIDTH, i*EDGE_LENGTH);
addItem(line);
}
}