-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing_scene.cpp
116 lines (93 loc) · 2.75 KB
/
drawing_scene.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "drawing_scene.h"
#include <QPainter>
#include <qmath.h>
Drawing_Scene::Drawing_Scene(QWidget *parent, int Size) : QWidget(parent)
{
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
SizeField = Size;
}
void Drawing_Scene::RecieveInit(AREA **_area)
{
Field = _area;
update();
}
void Drawing_Scene::paintEvent(QPaintEvent *)
{
QPainter p(this);
DrawGrid(p);
InitField(p);
}
void Drawing_Scene::ChangeFieldSize(const int &size)
{
SizeField = size;
update();
}
void Drawing_Scene::InitField(QPainter &p)
{
for(int i=0; i<SizeField; i++)
for(int j=0; j<SizeField; j++)
{
double cellWidth = (double)width()/SizeField;
double cellHeight = (double)height()/SizeField;
if (Field[i][j].who == 0)
continue;
if (Field[i][j].who == 2)
p.setBrush(Qt::blue);
else if (Field[i][j].who == 1)
p.setBrush(Qt::red);
else
p.setBrush(Qt::black);
p.drawEllipse (i*cellWidth ,j*cellHeight, (qreal)cellWidth,(qreal)cellHeight);
}
}
void Drawing_Scene::DrawGrid(QPainter &p)
{
QRect background(0, 0, width()-1, height()-1);
QRect borders(0, 0, width()-1, height()-1);
p.setBrush(QColor("#faf9fe"));
p.drawRect(background);
p.setBrush(Qt::NoBrush);
QColor gridColor = Qt::blue;
gridColor.setAlpha(20);
p.setPen(gridColor);
double cellWidth = (double)width()/SizeField;
double cellHeight = (double)height()/SizeField;
for(double i = cellWidth; i <= width(); i += cellWidth)
p.drawLine(i, 0, i, height());
for(double i = cellHeight; i <= height(); i += cellHeight)
p.drawLine(0, i, width(), i);
p.drawRect(borders);
}
void Drawing_Scene::mousePressEvent(QMouseEvent *e)
{
double cellWidth = (double)width()/SizeField;
double cellHeight = (double)height()/SizeField;
int k = floor(e->y()/cellHeight);
int j = floor(e->x()/cellWidth);
if (k >= 0 && k<SizeField && j >= 0 && j<SizeField)
{
if (e->button() == Qt::RightButton)
emit SendCoordMous(0, j, k);
else if (e->button() == Qt::LeftButton)
emit SendCoordMous(1, j, k);
else emit SendCoordMous(2, j, k);
}
}
void Drawing_Scene::mouseMoveEvent(QMouseEvent *e)
{
double cellWidth = (double)width()/SizeField;
double cellHeight = (double)height()/SizeField;
int k = floor(e->y()/cellHeight);
int j = floor(e->x()/cellWidth);
if (k >= 0 && k<SizeField && j >= 0 && j<SizeField)
{
if (e->button() == Qt::RightButton)
emit SendCoordMous(0, j, k);
else
emit SendCoordMous(1, j, k);
}
}
Drawing_Scene::~Drawing_Scene()
{
}