-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtgraph.cpp
More file actions
147 lines (122 loc) · 3.62 KB
/
qtgraph.cpp
File metadata and controls
147 lines (122 loc) · 3.62 KB
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
#include "qtgraph.h"
#include "ui_qtgraph.h"
#include "bnf.h"
#include "plotter.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <cmath>
QtGraph::QtGraph(QWidget *parent) :
QWidget(parent),
ui(new Ui::QtGraph)
{
ui->setupUi(this);
//Plotter config
plotter.setProgressBar(ui->progressBar);
plotter.coordPen = QPen(QBrush(Qt::black), 2);
plotter.gridPen = QPen(QBrush(Qt::lightGray), 0);
plotter.funcPen = QPen(QBrush(Qt::blue), 1.5, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin);
plotter.backgroundColor = Qt::white;
//Set up color pickers
ui->cpPlot->setColor(Qt::blue);
ui->cpGrid->setColor(Qt::lightGray);
ui->cpAxes->setColor(Qt::black);
ui->cpBackground->setColor(Qt::white);
//Set scene
ui->graphView->setScene(&scene);
//set state
ui->progressBar->setVisible(false);
working = false;
}
QtGraph::~QtGraph()
{
delete ui;
}
void QtGraph::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void QtGraph::dbgMsgHandler(QtMsgType type, const char *msg)
{
if (type==QtDebugMsg) {
ui->teLog->append(msg);
} else {
fprintf(stderr, "%s\n", msg);
}
}
void QtGraph::setPlotterSettings(tree *func) {
plotter.setFunc(func);
plotter.setSize(ui->graphView->width(), ui->graphView->height());
plotter.setXRange(ui->limitsX->limitFrom(), ui->limitsX->limitTo());
plotter.setAutoYRange();
if (ui->limitsY->isChecked())
plotter.setYRange(ui->limitsY->limitFrom(), ui->limitsY->limitTo());
plotter.setGrid(ui->sbGridX->value(), ui->sbGridY->value());
plotter.setFindZeros(ui->cbFindZeros->isChecked());
//colors
plotter.gridPen.setColor(ui->cpGrid->color());
plotter.coordPen.setColor(ui->cpAxes->color());
plotter.funcPen.setColor(ui->cpPlot->color());
plotter.backgroundColor = ui->cpBackground->color();
}
void QtGraph::setWorking(bool b) {
if (b==working) return;
if (b) {
working = true;
ui->pbBuild->setText("Abort");
ui->tabView->setEnabled(false);
ui->edFunc->hide();
ui->progressBar->show();
} else {
working = false;
ui->pbBuild->setText("Build");
ui->tabView->setEnabled(true);
ui->progressBar->hide();
ui->edFunc->show();
}
}
void QtGraph::build() {
ui->teLog->clear();
const QString str = ui->edFunc->text();
try {
if (!ui->limitsX->isValid()) throw QString("Bad X boundaries");
if (ui->limitsY->isChecked() && !ui->limitsY->isValid()) throw QString("Bad Y boundaries");
func = build_parse_tree(str);
setPlotterSettings(func);
setWorking(true);
plotter.doPlot();
buildEnd();
} catch (QString e) {
QMessageBox::about(NULL, "Error", e);
}
}
void QtGraph::buildEnd() {
scene.clear();
scene.setSceneRect(0, 0, plotter.imgPlot.width(), plotter.imgPlot.height());
scene.addPixmap(QPixmap::fromImage(plotter.imgPlot));
destroy_tree(func);
setWorking(false);
}
void QtGraph::on_pbBuild_clicked()
{
if (!working) {
build();
} else {
plotter.abort();
}
}
void QtGraph::on_pbSave_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save plot"), "",
tr("JPEG (*.jpeg, *.jpg);;PNG (*.png);;BMP (*.bmp);;All Files (*)"));
if (!fileName.isEmpty())
if (!plotter.imgPlot.save(fileName))
QMessageBox::warning(this, "Error", "Can`t save file \"" + fileName +"\"");
}