-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
181 lines (148 loc) · 4.82 KB
/
mainwindow.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
179
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "abstractshape.h"
#include "graphicssystem.h"
#include "circle.h"
#include "square.h"
#include "pie.h"
#include "cmyobject.h"
using namespace pashazz;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->gr->setMouseTracking(true); //Следить за перемещениями мыши
ui->gr->scale(1, -1); //Установить ось ординат вверх
/* установить связь между GUI и кодом */
connect(ui->action_Quit, &QAction::triggered, qApp, &QApplication::quit);
/* нам нужен сигнал с аргументом double */
/// https://isocpp.org/wiki/faq/pointers-to-members
void (QDoubleSpinBox:: *valueChangedSignal)(double) = &QDoubleSpinBox::valueChanged;
connect(ui->boxRad1, valueChangedSignal, this, &MainWindow::updateFirstRadius);
connect(ui->boxRad2, valueChangedSignal, this, &MainWindow::updateSecondRadius);
connect(ui->boxRad3, valueChangedSignal, this, &MainWindow::updateThirdRadius);
connect(ui->boxAngle, valueChangedSignal, this, &MainWindow::updateAngle);
connect(ui->boxX, valueChangedSignal, this, &MainWindow::setX);
connect(ui->boxY, valueChangedSignal, this, &MainWindow::setY);
connect(ui->boxSquare, valueChangedSignal, this, &MainWindow::updateEdge);
/* status bar */
lCoords = new QLabel(this);
statusBar()->addPermanentWidget(lCoords);
/* создать объект CMyObject */
obj = new CMy2DObjectA2(Point2D(ui->boxX->value(), ui->boxY->value()), ui->boxSquare->value(),
ui->boxRad1->value(), ui->boxRad2->value(), ui->boxRad3->value(), ui->boxAngle->value(), ui->gr->backgroundBrush(), Qt::magenta);
/* отрисовать */
ui->gr->setScene(obj->GetScene());
/* Соединить сцену и форму */
connect(obj->GetScene(), &GraphicsScene::mouseMoveSignal, this, &MainWindow::updateLabel);
connect(obj->GetScene(), &GraphicsScene::mouseClickSignal, this, &MainWindow::showIfInside);
/* оживить кнопку вращения */
rotator.setInterval(100);
connect(&rotator, &QTimer::timeout, this, &MainWindow::rotateBySmallRad);
connect(ui->cmdRotate, &QPushButton::clicked, this, &MainWindow::switchRotation);
}
MainWindow::~MainWindow()
{
delete ui;
delete lCoords;
delete obj;
}
void MainWindow::showStatusMessage()
{
std::string str;
if(obj->CheckIntersection(str))
statusBar()->showMessage(QString(str.c_str()));
else
statusBar()->clearMessage();
}
void MainWindow::updateUserBoxes()
{
ui->boxRad1->setMaximum(obj->GetEdge());
ui->boxRad2->setMaximum(obj->GetEdge());
ui->boxRad3->setMaximum(obj->GetEdge());
}
void MainWindow::updateFirstRadius(double r)
{
#ifndef NDEBUG
std::cout << "Radius of the first circle has been updated to " << r << std::endl;
#endif
obj->SetRadius1(r);
obj->GetScene()->update();
showStatusMessage();
}
void MainWindow::updateSecondRadius(double r)
{
obj->SetRadius2(r);
obj->GetScene()->update();
showStatusMessage();
}
void MainWindow::updateThirdRadius(double r)
{
obj->SetRadius3(r);
obj->GetScene()->update();
showStatusMessage();
}
void MainWindow::updateAngle(double a)
{
obj->SetAngle(a);
obj->GetScene()->update();
}
void MainWindow::updateLabel(double x, double y)
{
Point2D rel = Point2D(ui->boxX->value(), ui->boxY->value()).translate(Point2D(x,y)).rotateAxis(ui->boxAngle->value());
lCoords->setText(tr("abs: (%1, %2) rel: (%3, %4)").arg(x).arg(y).arg(rel.x).arg(rel.y));
}
void MainWindow::updateEdge(double e)
{
obj->SetEdge(e);
obj->GetScene()->update();
showStatusMessage();
updateUserBoxes();
}
void MainWindow::showIfInside(double x, double y)
{
if (obj->IsInside({x,y}))
{
statusBar()->showMessage(tr("Point (%1, %2) внутри объекта").arg(x).arg(y));
}
else
{
statusBar()->showMessage(tr("Point (%1, %2) снаружи объекта").arg(x).arg(y));
}
}
void MainWindow::rotate(double angle)
{
obj->Rotate(angle);
ui->boxAngle->setValue(obj->GetAngle());
obj->GetScene()->update();
}
void MainWindow::setX(double x)
{
obj->SetCentre(Point2D(x, obj->GetCentre().y));
obj->GetScene()->update();
}
void MainWindow::setY(double y)
{
obj->SetCentre(Point2D(obj->GetCentre().x, y));
obj->GetScene()->update();
}
void MainWindow::switchRotation()
{
if (rotate_enabled)
{
rotator.stop();
ui->cmdRotate->setText("Вращать");
rotate_enabled = false;
}
else
{
rotator.start();
ui->cmdRotate->setText("Остановить");
rotate_enabled = true;
}
}
void MainWindow::rotateBySmallRad()
{
rotate(0.01);
}