#include <PlotCurve.h>


Public Slots | |
| void | itemChanged () |
| void | setColor (QColor color) |
| void | setAxis (QwtPlot::Axis axis) |
Signals | |
| void | signal_curveAttached (PlotCurve *curve) |
Public Member Functions | |
| PlotCurve () | |
| PlotCurve (const QwtText &title) | |
| PlotCurve (const QString &title) | |
| ~PlotCurve () | |
| QWidget * | legendItem () const |
| void | updateLegend (QwtLegend *legend) const |
| void | attach (Plot *plot) |
| int | closestPoint (const QPoint &pos, double *dist) const |
| void | setData (const CachedPlotCurveDataModel &dataModel) |
| PlotCurve::PlotCurve | ( | ) |
{
//qDebug( "PlotCurve::PlotCurve" );
}
| PlotCurve::PlotCurve | ( | const QwtText & | title | ) |
: QwtPlotCurve( title ) { //qDebug( "PlotCurve::PlotCurve( QwtText )" ); }
| PlotCurve::PlotCurve | ( | const QString & | title | ) |
: QwtPlotCurve( title ) { //qDebug( "PlotCurve::PlotCurve( QString )" ); }
| PlotCurve::~PlotCurve | ( | ) |
{
//qDebug( "PlotCurve::~PlotCurve" );
}
| void PlotCurve::attach | ( | Plot * | plot | ) |
Referenced by Grapher::addCurve().
{
qDebug( "PlotCurve::drawCurve" );
QwtPlotCurve::attach( plot );
if ( plot != NULL )
{
plot->showCurve( this, true );
// Connect the context request of the legend item of this curve
// to a slot on the plot
QwtLegend* legend = plot->legend();
if ( legend == NULL ) return;
QWidget* legendWidget = legend->find( this );
if ( legendWidget == NULL ) return;
legendWidget->setContextMenuPolicy( Qt::CustomContextMenu );
plot->connect( legendWidget, SIGNAL( customContextMenuRequested( const QPoint& ) ),
plot, SLOT( curveContextMenuRequested( const QPoint& ) ) );
}
connect(this, SIGNAL(signal_curveAttached(PlotCurve*)),
plot, SIGNAL(signal_curveAttached(PlotCurve*)));
emit signal_curveAttached(this);
}
| int PlotCurve::closestPoint | ( | const QPoint & | pos, | |
| double * | dist | |||
| ) | const |
Reimplemented from QwtPlotCurve.
{
//qDebug( "PlotCurve::closestPoint" );
if ( plot() == NULL || dataSize() <= 0 )
return -1;
const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
int index = -1;
double dmin = 1.0e10;
const double x0 = pos.x();
const double y0 = pos.y();
for ( int i = 0; i < dataSize() - 1; i++ )
{
const double x1 = xMap.xTransform( x( i ) );
const double x2 = xMap.xTransform( x( i + 1 ) );
if ( x1 == x2 ) continue;
const double y1 = yMap.xTransform( y( i ) );
const double y2 = yMap.xTransform( y( i + 1 ) );
const double y2miny1 = y2 - y1;
const double x2minx1 = x2 - x1;
const double m = y2miny1 / x2minx1;
const double b = y1 - m * x1;
// These are the coordinates of the projected point (x0,y0) on
// the line segment between (x1,y1) and (x2,y2)
const double mmplus1 = m * m + 1;
const double xp = ( m * y0 + x0 - m * b ) / ( mmplus1 );
const double yp = ( m * m * y0 + m * x0 + b ) / ( mmplus1 );
//qDebug() << i << "[" << x0 << y0 << "][" << x1 << y1 << "][" << x2 << y2 << "][" << xp << yp << "]";
// The x value of projected point should be between x1 and x2 or the
// y value of the projected point between y1 and y2 (y2 and y1 are swapped
// because of the widget coordinate system)
// If it is outside, we are not interested in the distance to this line segment
if ( !( ( xp > x1 && xp < x2 ) || ( yp > y2 && yp < y1 ) ) ) continue;
// Distance to the line segment
const double f = fabs( ( x2minx1 ) * ( y1 - y0 ) - ( x1 - x0 ) * ( y2miny1 ) )
/ sqrt( qwtSqr( x2minx1 ) + qwtSqr( y2miny1 ) );
//qDebug() << i << "[" << x0 << y0 << "][" << x1 << y1 << "][" << x2 << y2 << "][" << xp << yp << "]" << f;
if ( f < dmin )
{
index = i;
dmin = f;
}
}
if ( dist )
*dist = dmin;
return index;
}
| void PlotCurve::itemChanged | ( | ) | [inline, slot] |
| QWidget* PlotCurve::legendItem | ( | ) | const [inline] |
| void PlotCurve::setAxis | ( | QwtPlot::Axis | axis | ) | [inline, slot] |
Referenced by updateLegend().
| void PlotCurve::setColor | ( | QColor | color | ) | [inline, slot] |
Referenced by updateLegend().
| void PlotCurve::setData | ( | const CachedPlotCurveDataModel & | dataModel | ) | [inline] |
{
qDebug() << "Setting data to plot: " << this;
QwtPlotCurve::setData(dataModel);
CachedPlotCurveDataModel *model = dynamic_cast<CachedPlotCurveDataModel*>(&data());
Q_ASSERT(model);
connect(model, SIGNAL(signal_dataChanged()), this, SLOT(itemChanged()));
}
| void PlotCurve::updateLegend | ( | QwtLegend * | legend | ) | const [inline] |
Reimplemented from QwtPlotCurve.
{
if (!legend) return;
Q_ASSERT(testItemAttribute(QwtPlotItem::Legend));
LegendItem *lgdItem = dynamic_cast<LegendItem*>(legend->find(this));
//qDebug() << "Found: " << lgdItem << this << legend->legendItems();
//als er geen LegendItem in de Legend steekt, maken we er nu een, met alle connecties etc.
if (!lgdItem){
lgdItem = dynamic_cast<LegendItem*>(legendItem());
Q_ASSERT(lgdItem);
// Setup connections
// Qt::QueuedConnection is nodig, want indien het niey Queued is,
// dan wordt deze functie onmiddelijk geëxecuteerd na lgdItem->setCurrentColor,
// wat resulteert in dubbele legenditem omdat de vorige nog niet geinsert was.
connect(lgdItem, SIGNAL(colorChanged(QColor)),
this, SLOT(setColor(QColor)), Qt::QueuedConnection);
connect(lgdItem, SIGNAL(axisChanged(QwtPlot::Axis)),
this, SLOT(setAxis(QwtPlot::Axis)), Qt::QueuedConnection);
//setup LegendItem color
QList<QColor> usedColors;
foreach(QWidget *wItem, legend->legendItems()){
LegendItem *item = dynamic_cast<LegendItem*>(wItem);
Q_ASSERT(item);
usedColors << item->currentColor();
}
Q_ASSERT(lgdItem->color(0).isValid());
lgdItem->setCurrentColor(lgdItem->color(0));
int i = 0;
while(lgdItem->color(i).isValid()){
if (usedColors.contains(lgdItem->color(i))) {
i++;
continue;
} else {
lgdItem->setCurrentColor(lgdItem->color(i));
break;
}
} //by now, a color should be set, or the default used.
// insert into Legend
legend->insert(this, lgdItem);
//qDebug() << "Inserted: " << lgdItem << this << legend->legendItems();
}
Q_ASSERT(lgdItem);
Q_ASSERT(lgdItem->updatesEnabled());
lgdItem->setText(title().text());
}
1.7.1