-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
248 lines (205 loc) · 7.14 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString file("/home/ssr/Programming/Qt_project/ChartLab/input/inv.tr0");
qDebug() << "open file" << file;
QString result = readFile(file);
if(!result.isEmpty()){
this->entity = parseData(result);
drawCombineCharts();
drawScatterCharts();
}
// on_combinerAction_triggered();
on_scatterAction_triggered();
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::readFile(QString pathToFile)
{
if(pathToFile.isEmpty()){
QMessageBox::warning(this, QString("Путь до файла пуст"), QString("Ссылка на файл пустая"), QMessageBox::Ok);
}
QFile file(pathToFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QMessageBox::warning(this, QString("Ошибка чтения файла"),
QString("По не ясной мне причине файл прочитать не удалось, уж прости"), QMessageBox::Ok);
}
QTextStream in(&file);
return in.readAll();
}
TransientAnalysisEntity MainWindow::parseData(QString toParse)
{
Parser parser;
TransientAnalysisEntity entity;
try{
TransientAnalysisEntity entityObj = parser.parse(toParse);
entity = entityObj;
} catch(ParseException &wrongString){
QMessageBox::warning(this, QString("Ошибка парсинга"), QString(wrongString.getMessage()), QMessageBox::Ok);
qDebug() << wrongString.getMessage();
}
return entity;
}
QBrush MainWindow::getBackgroundBrush()
{
QBrush brush;
brush.setStyle(Qt::SolidPattern);
//можно изменять масштаб "сетки", если она будет =)
QTransform transform;
transform.scale(2.0,2.0);
brush.setTransform(transform);
return brush;
}
QValueAxis *MainWindow::getAxisX()
{
QValueAxis *axisX = new QValueAxis;
axisX->setLabelFormat("%.3e");
axisX->setTitleText(entity.getSweep());
return axisX;
}
QValueAxis *MainWindow::getAxisY()
{
QValueAxis *axisY = new QValueAxis;
axisY->setLabelFormat("%.3e");
axisY->setTitleText("Values");
return axisY;
}
QChart *MainWindow::getChart()
{
QChart *chart = new QChart();
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
chart->setMargins(QMargins(1,1, 1, 1));
chart->setBackgroundBrush(QBrush(QColor(239,240,241)));
return chart;
}
void MainWindow::drawScatterCharts()
{
if(entity.getHeaders().size() == 0){
return;
}
QVBoxLayout *layout = new QVBoxLayout;
ui->scrollAreaWidgetContents->setLayout(layout);
foreach (QString chartName, entity.getHeaders()) {
QChart *chart = getChart();
QValueAxis *axisX = getAxisX();
chart->addAxis(axisX, Qt::AlignBottom);
QValueAxis *axisY = getAxisY();
chart->addAxis(axisY, Qt::AlignLeft);
float maxValue = std::numeric_limits<float>::min(),
minValue = std::numeric_limits<float>::max();
QList<QPointF> points = entity.getPoints().value(chartName);
qDebug() << chartName << " " << points.size();
QLineSeries *series = new QLineSeries();
foreach (QPointF point, points) {
maxValue = maxValue < point.y() ? point.y() : maxValue;
minValue = minValue > point.y() ? point.y() : minValue;
series->append(point);
}
series->setName(chartName);
series->setColor(entity.getChartColor(chartName));
chart->addSeries(series);
chart->setAxisX(axisX, series);
// ui->page->hide();
chart->setAxisY(axisY, series);
double delta = maxValue - minValue;
if(delta < 0.000001){
delta = 5;
maxValue = 5;
minValue = 0;
}
axisY->setRange(minValue - delta * 0.08, maxValue + delta * 0.08);
chart->setPlotAreaBackgroundBrush(getBackgroundBrush());
chart->setPlotAreaBackgroundVisible(true);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QVBoxLayout *layout2 = new QVBoxLayout;
layout2->addWidget(chartView);
layout2->setMargin(0);
layout2->setSpacing(0);
QWidget *widget = new QWidget();
widget->setLayout(layout2);
widget->setMinimumHeight(800);
layout->addWidget(widget);
}
}
void MainWindow::drawCombineCharts()
{
if(entity.getHeaders().size() == 0){
return;
}
QChart *chart = getChart();
QValueAxis *axisX = getAxisX();
chart->addAxis(axisX, Qt::AlignBottom);
QValueAxis *axisY = getAxisY();
chart->addAxis(axisY, Qt::AlignLeft);
float maxValue = std::numeric_limits<float>::min(),
minValue = std::numeric_limits<float>::max();
foreach (QString chartName, entity.getHeaders()) {
QList<QPointF> points = entity.getPoints().value(chartName);
qDebug() << chartName << " " << points.size();
QLineSeries *series = new QLineSeries();
foreach (QPointF point, points) {
maxValue = maxValue < point.y() ? point.y() : maxValue;
minValue = minValue > point.y() ? point.y() : minValue;
series->append(point);
}
series->setName(chartName);
series->setColor(entity.getChartColor(chartName));
chart->addSeries(series);
chart->setAxisX(axisX, series);
chart->setAxisY(axisY, series);
}
double delta = maxValue - minValue;
axisY->setRange(minValue - delta * 0.08, maxValue + delta * 0.08);
chart->setPlotAreaBackgroundBrush(getBackgroundBrush());
chart->setPlotAreaBackgroundVisible(true);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(chartView);
layout->setMargin(0);
layout->setSpacing(0);
ui->widget->setLayout(layout);
}
void MainWindow::on_openFIleAction_triggered()
{
QString file = QFileDialog::getOpenFileName(this,
tr("Open file"), "/home/ssr", tr("Tr0 Files (*.tr0)"));
qDebug() << "open file" << file;
QString result = readFile(file);
this->entity = parseData(result);
drawCombineCharts();
drawScatterCharts();
on_combinerAction_triggered();
}
void MainWindow::on_combinerAction_triggered()
{
ui->scrollArea->hide();
ui->widget->show();
}
void MainWindow::on_scatterAction_triggered()
{
ui->widget->hide();
ui->scrollArea->show();
}
void MainWindow::on_chartsInfoAction_triggered()
{
if(this->entity.getHeaders().size() == 0){
QMessageBox::warning(this, QString("Нет графика"), QString("Сначала загрузите график"), QMessageBox::Ok);
} else {
DetailDialog *detail = new DetailDialog(this, entity);
detail->show();
}
}
void MainWindow::on_infoAction_triggered()
{
AboutDIalog *aboutDialog = new AboutDIalog(this);
aboutDialog->show();
}