-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
140 lines (111 loc) · 2.83 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
model(nullptr)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
delete model;
}
void MainWindow::InitModel()
{
if (nullptr != model)
{
delete model;
}
QRect rect = ui->graphicsView->rect();
ui->dsbX->setRange(0, rect.width());
ui->dsbY->setRange(0, rect.height());
model = new Model(this, rect.width(), rect.height());
model->Scene()->setSceneRect(rect);
ui->graphicsView->setScene(model->Scene());
model->Init();
model->Display(true);
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
QRect rect = ui->graphicsView->rect();
ui->dsbX->setRange(0, rect.width());
ui->dsbY->setRange(0, rect.height());
if (model)
{
model->Scene()->setSceneRect(rect);
model->SetWidth(rect.width());
model->SetHeight(rect.height());
model->Display(true);
}
}
void MainWindow::on_btnGenerate_clicked()
{
model->SetAnimationParameter(model->ModelToDisplayY(0));
model->SetAnimationOngoing(false);
model->SetAnimationToOngoing(false);
model->Clear(true);
for (int i = 0; i < ui->sbNumOfPoints->value(); i++)
{
model->AddPoint(model->Width() * (double)rand() / (double)RAND_MAX,
model->Height() * (double)rand() / (double)RAND_MAX);
}
model->Init();
model->Display(true);
}
void MainWindow::on_hsAnimationParameter_sliderMoved(int position)
{
// Animation parameter from 0 to 1
double ap = (double)position / ui->hsAnimationParameter->maximum();
model->SetAnimationParameter(model->GetYFromAP(ap),
false /* updateSlider */);
model->Display();
}
void MainWindow::on_btnStart_clicked()
{
if (model->AnimationOngoing())
{
model->SetAnimationOngoing(false);
}
else
{
model->SetAnimationOngoing(true);
}
}
void MainWindow::on_btnPrevious_clicked()
{
model->AnimateToPrevious();
}
void MainWindow::on_btnNext_clicked()
{
model->AnimateToNext();
}
void MainWindow::on_btnAddPoint_clicked()
{
model->AddPoint(ui->dsbX->value(), ui->dsbY->value());
model->Init();
model->Display(true);
}
void MainWindow::on_btnClear_clicked()
{
model->Clear(true);
}
void MainWindow::on_btnLoadFile_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), ".", tr("Image Files (*.txt)"));
std::ifstream in(fileName.toStdString(), std::ifstream::in);
double x, y;
while (in >> x && in >> y)
{
if (x < 0 || y < 0 || x > model->Width() || y > model->Height())
{
continue;
}
model->AddPoint(x, y);
}
model->Init();
model->Display(true);
}