-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxplot.cpp
154 lines (128 loc) · 4.01 KB
/
pxplot.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
#include <QtGui>
#include <QMessageBox>
#include <qwt.h>
#include <qwt_legend.h>
#include <qwt_plot_layout.h>
#include "pxplot.h"
#include "curve.h"
#include "fileformat.h"
#include "xye.h"
static Qt::GlobalColor colorlist[10] = { Qt::red,
Qt::green, Qt::blue, Qt::cyan,
Qt::magenta, Qt::darkRed, Qt::darkGreen,
Qt::darkBlue, Qt::darkCyan,
Qt::darkMagenta
};
PxPlot::PxPlot(QWidget *parent): QwtPlot(parent)
{
setAutoReplot(false);
setTitle(tr(" ")); // creates a wider top frame
setCanvasBackground(QColor(Qt::white));
plotLayout()->setLegendRatio(0.25);
QwtLegend *legend = new QwtLegend;
legend->setFrameStyle(QFrame::Box|QFrame::Sunken);
legend->setDefaultItemMode(QwtLegendData::Checkable);
insertLegend(legend, QwtPlot::RightLegend);
connect(legend,SIGNAL(checked(QVariant,bool,int)),this,
SLOT(labelClick(QVariant,bool,int)));
setAxisTitle(QwtPlot::xBottom, QString::fromUtf8("2Θ / °"));
setAxisTitle(QwtPlot::yLeft, tr("Intensity"));
curves.clear();
setAutoReplot(true);
}
bool PxPlot::setData(const QString& name, const QString & filter)
{
bool svReplot = autoReplot();
setAutoReplot(false);
int baselen = name.length()-name.lastIndexOf("/")-1;
Qt::GlobalColor col = colorlist[curves.size() % 10];
Curve* crv1;
crv1 = new Curve(name.right(baselen));
crv1->setRenderHint(QwtPlotItem::RenderAntialiased);
crv1->setPen(col,2.0);
// crv1->setYAxis(QwtPlot::yLeft);
crv1->attach(this);
curves.append(crv1);
int arraySize = 0;
QVector<double> dx;
QVector<double> dy;
dx.reserve(50000); // size hint: enough for most lab data
dy.reserve(50000);
FileFormat* ff = new FileFormat();
FileFormatIDX fid = ff->guess(name, filter);
delete ff;
if (fid==PXV_XY_FILE)
arraySize = readXYE(name,' ',dx,dy,0);
else if (fid==PXV_ARL_FILE)
arraySize = readXYE(name,' ',dx,dy,38); // skip 38 lines of metadata
else if (fid==PXV_TSV_FILE)
arraySize = readXYE(name,' ',dx,dy,1); // possible header in first line
else if (fid==PXV_CSV_FILE)
arraySize = readXYE(name,',',dx,dy,1); // possible header in first line
if (arraySize > 0)
{
// setSamples makes a copy, unlike setRawSamples
crv1->setSamples(dx.data(),dy.data(),arraySize);
} else
{
removeCurve(crv1);
QMessageBox::warning(this,tr("File error"),
tr("Reading data from the selected file failed.\n"));
}
setAutoReplot(svReplot);
replot();
return(arraySize > 0);
}
QRectF PxPlot::boundingRect()
{
QRectF bounds = QRectF(0,0,-1,-1);
if (curves.isEmpty()) {
return bounds;
}
bounds = curves[0]->boundingRect();
for (int i = 1; i < curves.size(); ++i)
{
if (bounds.left() > curves[i]->minXValue())
{
bounds.setLeft(curves[i]->minXValue());
}
if (bounds.right() < curves[i]->maxXValue())
{
bounds.setRight(curves[i]->maxXValue());
}
if (bounds.top() > curves[i]->minYValue())
{
bounds.setTop(curves[i]->minYValue());
}
if (bounds.bottom() < curves[i]->maxYValue())
{
bounds.setBottom(curves[i]->maxYValue());
}
}
return bounds;
}
void PxPlot::labelClick(QVariant qv, bool on, int index)
{
QwtPlotItem *plotitem;
if ( ! qv.canConvert<QwtPlotItem *>() ) return;
plotitem = qvariant_cast<QwtPlotItem *>( qv );
for (int i = 0; i < curves.size(); ++i)
{
if (curves[i] == plotitem)
{
if (on) plotitem->hide(); else plotitem->show();
}
}
}
QList<Curve*> PxPlot::curveList()
{
return curves;
}
bool PxPlot::removeCurve(Curve *c)
{
bool ok;
c->detach();
ok = curves.removeOne(c);
if (ok) delete c;
return(ok);
}