-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcoolline.cpp
178 lines (149 loc) · 5.41 KB
/
qcoolline.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "qcoolline.h"
QCoolLine::QCoolLine() {
sceneInit(500, 500);
leeAlg = new LeeAlgorythm();
leeAlg->SetSize(this->sceneX_sz, this->sceneY_sz);
setRects(box_quantity, box_size);
setWidgets();
this->show();
}
void QCoolLine::sceneInit(const size_t &szX, const size_t &szY) {
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, szX, szY + 26);
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(szX, szY + 25);
scene->addLine(0, szY, szX, szY, QPen(Qt::black));
this->mouse_es = set_first_point;
}
void QCoolLine::setRects(const size_t &quantity, const size_t &box_size) {
// Set rects
QPoint point;
qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
for(size_t i = 0; i < quantity; ++i) {
point.setX(qrand() % (sceneX_sz - 1));
point.setY(qrand() % (sceneY_sz - 1));
int w = (qrand() % (box_size + 4) + 4);
int h = (qrand() % (box_size + 4) + 4);
leeAlg->setRect(point, w, h);
scene->addRect(point.x(), point.y(), w, h, QPen(Qt::black), QBrush(Qt::lightGray));
}
grid.resize(leeAlg->grid.size());
for(size_t m = 0; m < leeAlg->grid.size(); ++m)
grid[m].resize(leeAlg->grid[m].size());
grid = leeAlg->grid;
}
void QCoolLine::setWidgets() {
QGraphicsProxyWidget *proxy;
size_t spinbox_width(60);
size_t label_width(30);
// Box Quantity
boxQuantityLabel = new QLabel;
boxQuantityLabel->setText(" Qu:");
boxQuantityLabel->setGeometry(QRect(100, 0, label_width, 25));
proxy = this->scene->addWidget(boxQuantityLabel);
proxy->setPos(0, sceneY_sz);
boxQuantity = new QSpinBox;
boxQuantity->setMaximum(1000);
boxQuantity->setValue(box_quantity);
boxQuantity->setGeometry(QRect(100, 0, spinbox_width, 25));
proxy = this->scene->addWidget(boxQuantity);
proxy->setPos(label_width, sceneY_sz);
// Boxs size
boxSizeLabel = new QLabel;
boxSizeLabel->setText(" Sz:");
boxSizeLabel->setGeometry(QRect(100, 0, label_width, 25));
proxy = this->scene->addWidget(boxSizeLabel);
proxy->setPos(label_width + spinbox_width, sceneY_sz);
boxSize = new QSpinBox;
boxSize->setMaximum(1000);
boxSize->setValue(box_size);
boxSize->setGeometry(QRect(200, 0, spinbox_width, 25));
proxy = this->scene->addWidget(boxSize);
proxy->setPos(label_width*2 + spinbox_width, sceneY_sz);
// Board size (X)
boardSizeXLabel = new QLabel;
boardSizeXLabel->setText(" Bx:");
boardSizeXLabel->setGeometry(QRect(100, 0, label_width, 25));
proxy = this->scene->addWidget(boardSizeXLabel);
proxy->setPos(label_width*2 + spinbox_width*2, sceneY_sz);
boardSizeX = new QSpinBox;
boardSizeX->setMaximum(1000);
boardSizeX->setMinimum(500);
boardSizeX->setValue(sceneX_sz);
boardSizeX->setGeometry(QRect(300, 0, spinbox_width, 25));
proxy = this->scene->addWidget(boardSizeX);
proxy->setPos(label_width*3 + spinbox_width*2, sceneY_sz);
// Board size (Y)
boardSizeYLabel = new QLabel;
boardSizeYLabel->setText(" By:");
boardSizeYLabel->setGeometry(QRect(100, 0, label_width, 25));
proxy = this->scene->addWidget(boardSizeYLabel);
proxy->setPos(label_width*3 + spinbox_width*3, sceneY_sz);
boardSizeY = new QSpinBox;
boardSizeY->setMaximum(1000);
boardSizeY->setMinimum(500);
boardSizeY->setValue(sceneY_sz);
boardSizeY->setGeometry(QRect(300, 0, spinbox_width, 25));
proxy = this->scene->addWidget(boardSizeY);
proxy->setPos(label_width*4 + spinbox_width*3, sceneY_sz);
// Button
btnuser = new QPushButton;
btnuser->setGeometry(QRect(0, 0, sceneX_sz - (label_width*4 + spinbox_width*4), 25));
btnuser->setText("Generate");
proxy = this->scene->addWidget(btnuser);
proxy->setPos(label_width*4 + spinbox_width*4, sceneY_sz);
connect(btnuser, SIGNAL(clicked(bool)), this, SLOT(buttonPressHandle()));
}
void QCoolLine::mousePressEvent(QMouseEvent *mouse_event) {
QPoint p = mouse_event->pos();
if(p.y() < sceneY_sz) {
switch (mouse_es) {
case set_first_point:
point1 = p;
mouse_es = set_second_point;
break;
case set_second_point:
point2 = p;
leeAlg->grid = grid;
if(leeAlg->CustomLee(point1, point2))
for(int i = 0; i < leeAlg->path.size(); i++)
scene->addEllipse(leeAlg->path.at(i).x(), leeAlg->path.at(i).y(), 1, 1, QPen(Qt::red));
mouse_es = set_first_point;
leeAlg->path.clear();
break;
#if 0
case delete_line:
this->mouse_es = set_first_point;
break;
#endif
}
}
}
void QCoolLine::buttonPressHandle() {
leeAlg = new LeeAlgorythm();
this->sceneX_sz = boardSizeX->value();
this->sceneY_sz = boardSizeY->value();
sceneInit(this->sceneX_sz, this->sceneY_sz);
leeAlg->path.clear();
leeAlg->SetSize(this->sceneX_sz, this->sceneY_sz);
box_quantity = boxQuantity->value();
box_size = boxSize->value();
setRects(box_quantity, box_size);
setWidgets();
}
QCoolLine::~QCoolLine()
{
delete btnuser;
delete boxQuantity;
delete boxSize;
delete boardSizeX;
delete boardSizeY;
delete boxQuantityLabel;
delete boxSizeLabel;
delete boardSizeXLabel;
delete boardSizeYLabel;
delete leeAlg;
delete scene;
}