wfplot.cpp
117 lines
| 4.7 KiB
| text/x-c
|
CppLexer
/ rmapplugin / wfplot.cpp
r20 | #include "wfplot.h" | |||
#include <QFontInfo> | ||||
WFPlot::WFPlot(QWidget *parent) : | ||||
QWidget(parent) | ||||
{ | ||||
// Create Fonts | ||||
QFont font; | ||||
font = QFont(this->fontInfo().family(), FONT_SIZE_WAVEFORM_TITLE, QFont::Light); | ||||
customPlot = new QCustomPlot(); | ||||
mainLayout = new QVBoxLayout(); | ||||
customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes | | ||||
QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle); | ||||
customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical); | ||||
customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical); | ||||
customPlot->xAxis->setRange(0, XMAX); | ||||
customPlot->yAxis->setRange(-YMAX, YMAX); | ||||
customPlot->setTitleFont(font); | ||||
//customPlot->setupFullAxesBox(); | ||||
customPlot->addGraph(); | ||||
mainLayout->addWidget(customPlot); | ||||
this->setLayout(mainLayout); | ||||
// connect slot that ties some axis selections together (especially opposite axes): | ||||
connect(customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged())); | ||||
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed: | ||||
connect(customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress())); | ||||
connect(customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel())); | ||||
} | ||||
void WFPlot::selectionChanged() | ||||
{ | ||||
/* | ||||
normally, axis base line, axis tick labels and axis labels are selectable separately, but we want | ||||
the user only to be able to select the axis as a whole, so we tie the selected states of the tick labels | ||||
and the axis base line together. However, the axis label shall be selectable individually. | ||||
The selection state of the left and right axes shall be synchronized as well as the state of the | ||||
bottom and top axes. | ||||
Further, we want to synchronize the selection of the graphs with the selection state of the respective | ||||
legend item belonging to that graph. So the user can select a graph by either clicking on the graph itself | ||||
or on its legend item. | ||||
*/ | ||||
// make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object: | ||||
if (customPlot->xAxis->selected().testFlag(QCPAxis::spAxis) || customPlot->xAxis->selected().testFlag(QCPAxis::spTickLabels) || | ||||
customPlot->xAxis2->selected().testFlag(QCPAxis::spAxis) || customPlot->xAxis2->selected().testFlag(QCPAxis::spTickLabels)) | ||||
{ | ||||
customPlot->xAxis2->setSelected(QCPAxis::spAxis|QCPAxis::spTickLabels); | ||||
customPlot->xAxis->setSelected(QCPAxis::spAxis|QCPAxis::spTickLabels); | ||||
} | ||||
// make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object: | ||||
if (customPlot->yAxis->selected().testFlag(QCPAxis::spAxis) || customPlot->yAxis->selected().testFlag(QCPAxis::spTickLabels) || | ||||
customPlot->yAxis2->selected().testFlag(QCPAxis::spAxis) || customPlot->yAxis2->selected().testFlag(QCPAxis::spTickLabels)) | ||||
{ | ||||
customPlot->yAxis2->setSelected(QCPAxis::spAxis|QCPAxis::spTickLabels); | ||||
customPlot->yAxis->setSelected(QCPAxis::spAxis|QCPAxis::spTickLabels); | ||||
} | ||||
// synchronize selection of graphs with selection of corresponding legend items: | ||||
for (int i=0; i<customPlot->graphCount(); ++i) | ||||
{ | ||||
QCPGraph *graph = customPlot->graph(i); | ||||
QCPPlottableLegendItem *item = customPlot->legend->itemWithPlottable(graph); | ||||
if (item->selected() || graph->selected()) | ||||
{ | ||||
item->setSelected(true); | ||||
graph->setSelected(true); | ||||
} | ||||
} | ||||
} | ||||
void WFPlot::mousePress() | ||||
{ | ||||
// if an axis is selected, only allow the direction of that axis to be dragged | ||||
// if no axis is selected, both directions may be dragged | ||||
if (customPlot->xAxis->selected().testFlag(QCPAxis::spAxis)) | ||||
customPlot->setRangeDrag(customPlot->xAxis->orientation()); | ||||
else if (customPlot->yAxis->selected().testFlag(QCPAxis::spAxis)) | ||||
customPlot->setRangeDrag(customPlot->yAxis->orientation()); | ||||
else | ||||
customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical); | ||||
} | ||||
void WFPlot::mouseWheel() | ||||
{ | ||||
// if an axis is selected, only allow the direction of that axis to be zoomed | ||||
// if no axis is selected, both directions may be zoomed | ||||
if (customPlot->xAxis->selected().testFlag(QCPAxis::spAxis)) | ||||
customPlot->setRangeZoom(customPlot->xAxis->orientation()); | ||||
else if (customPlot->yAxis->selected().testFlag(QCPAxis::spAxis)) | ||||
customPlot->setRangeZoom(customPlot->yAxis->orientation()); | ||||
else | ||||
customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical); | ||||
} | ||||
void WFPlot::displayOnPlot(short *data, unsigned int size) | ||||
{ | ||||
QVector<double> x(size), y(size); | ||||
for (unsigned int i=0; i<size; ++i) | ||||
{ | ||||
x[i] = i; | ||||
y[i] = (double) data[i]; | ||||
} | ||||
customPlot->graph(0)->setData(x, y); | ||||
customPlot->replot(); | ||||
} | ||||