#include "qplotter.h"
{
create_actions();
create_file_test("d:\\my_projects\\1.txt");
read_file("d:\\my_projects\\1.txt");
create_menus();
plot_item();
//connect(loadFileAction, SIGNAL(triggered()), this, SLOT(repaint()));
}
void QPlotter::repaint()
{
funPlot->resize(this->size());
}
void QPlotter::plot_item()
{
temp_curve
->setRenderHint
(QwtPlotItem::RenderAntialiased);
temp_curve->setPen(pen);
temp_curve->setData(y_points, x_points);
temp_curve->setVisible(true);
temp_curve->setTitle("Channel 1, blue");
funPlot->setTitle("Channel 1");
temp_curve->attach(funPlot);
funPlot->resize(800, 600);
funPlot
->insertLegend
(legend,
QwtPlot::RightLegend,
-1.0);
this->resize(funPlot->size());
this->repaint();
}
void QPlotter::create_actions()
{
loadFileAction
= new QAction(tr
("Load file..."),
this);
saveCurveAsImageAction
= new QAction(tr
("Save as image..."),
this);
}
void QPlotter::create_menus()
{
this->menuBar()->addMenu(fileMenu);
fileMenu->setTitle("File");
fileMenu->addAction(loadFileAction);
fileMenu->addAction(saveCurveAsImageAction);
}
void QPlotter::create_file_test(char* name)
{
FILE* in_file;
in_file = fopen(name, "w");
//create and fill vectors with test values
std::vector<float> a, b;
for (int i = 0; i < 10000; i+=50) a.push_back(i);
for (int i = 0; i < 200; i++) b.push_back(i);
for (int i = 0; i < 200; i++) fprintf(in_file, "%f %f\n", b[i], a[i]);
fclose(in_file);
}
void QPlotter::read_file(char* name)
{
FILE* in_file;
in_file = fopen(name, "r");
rewind(in_file);
float* temp1 = (float*)calloc(1, sizeof(float));
float* temp2 = (float*)calloc(1, sizeof(float));
while (!feof(in_file))
{
fscanf(in_file, "%f %f", temp1, temp2);
x_points.push_back(*temp1);
y_points.push_back(*temp2);
}
int k = 0;
free(temp1);//free allocated memory
free(temp2);//free allocated memory
}
#include "qplotter.h"
QPlotter::QPlotter(QMainWindow *parent) :
QMainWindow(parent)
{
create_actions();
create_file_test("d:\\my_projects\\1.txt");
read_file("d:\\my_projects\\1.txt");
create_menus();
plot_item();
//connect(loadFileAction, SIGNAL(triggered()), this, SLOT(repaint()));
}
void QPlotter::repaint()
{
funPlot->resize(this->size());
}
void QPlotter::plot_item()
{
QwtPlotCurve* temp_curve = new QwtPlotCurve();
QPen pen = QPen(Qt::blue);
temp_curve->setRenderHint(QwtPlotItem::RenderAntialiased);
temp_curve->setPen(pen);
temp_curve->setData(y_points, x_points);
temp_curve->setVisible(true);
temp_curve->setTitle("Channel 1, blue");
funPlot = new QwtPlot(this);
funPlot->setTitle("Channel 1");
temp_curve->attach(funPlot);
funPlot->resize(800, 600);
QwtLegend* legend = new QwtLegend(this);
funPlot->insertLegend(legend, QwtPlot::RightLegend, -1.0);
this->resize(funPlot->size());
this->repaint();
}
void QPlotter::create_actions()
{
loadFileAction = new QAction(tr("Load file..."),this);
saveCurveAsImageAction = new QAction(tr("Save as image..."), this);
}
void QPlotter::create_menus()
{
fileMenu = new QMenu();
this->menuBar()->addMenu(fileMenu);
fileMenu->setTitle("File");
fileMenu->addAction(loadFileAction);
fileMenu->addAction(saveCurveAsImageAction);
}
void QPlotter::create_file_test(char* name)
{
FILE* in_file;
in_file = fopen(name, "w");
//create and fill vectors with test values
std::vector<float> a, b;
for (int i = 0; i < 10000; i+=50) a.push_back(i);
for (int i = 0; i < 200; i++) b.push_back(i);
for (int i = 0; i < 200; i++) fprintf(in_file, "%f %f\n", b[i], a[i]);
fclose(in_file);
}
void QPlotter::read_file(char* name)
{
FILE* in_file;
in_file = fopen(name, "r");
rewind(in_file);
float* temp1 = (float*)calloc(1, sizeof(float));
float* temp2 = (float*)calloc(1, sizeof(float));
while (!feof(in_file))
{
fscanf(in_file, "%f %f", temp1, temp2);
x_points.push_back(*temp1);
y_points.push_back(*temp2);
}
int k = 0;
free(temp1);//free allocated memory
free(temp2);//free allocated memory
}
To copy to clipboard, switch view to plain text mode
Bookmarks