IncrementalPlot.cc 8.61 KB
Newer Older
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
#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_grid.h>
#include <qwt_scale_engine.h>
#include "IncrementalPlot.h"
#include <Scrollbar.h>
#include <ScrollZoomer.h>
#include <float.h>
#if QT_VERSION >= 0x040000
#include <qpaintengine.h>
#endif

CurveData::CurveData():
        d_count(0)
{
}

void CurveData::append(double *x, double *y, int count)
{
    int newSize = ( (d_count + count) / 1000 + 1 ) * 1000;
    if ( newSize > size() )
    {
        d_x.resize(newSize);
        d_y.resize(newSize);
    }

    for ( register int i = 0; i < count; i++ )
    {
        d_x[d_count + i] = x[i];
        d_y[d_count + i] = y[i];
    }
    d_count += count;
}

int CurveData::count() const
{
    return d_count;
}

int CurveData::size() const
{
    return d_x.size();
}

const double *CurveData::x() const
{
    return d_x.data();
}

const double *CurveData::y() const
{
    return d_y.data();
}

IncrementalPlot::IncrementalPlot(QWidget *parent):
        QwtPlot(parent)
{
    setAutoReplot(false);

    setFrameStyle(QFrame::NoFrame);
    setLineWidth(0);
65
    setStyleText("solid crosses");
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    setCanvasLineWidth(2);

    plotLayout()->setAlignCanvasToScales(true);

    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));
    grid->attach(this);

    QwtLinearScaleEngine* yScaleEngine = new QwtLinearScaleEngine();
    setAxisScaleEngine(QwtPlot::yLeft, yScaleEngine);

    setAxisAutoScale(xBottom);
    setAxisAutoScale(yLeft);

    resetScaling();

    // enable zooming

    zoomer = new ScrollZoomer(canvas());
    zoomer->setRubberBandPen(QPen(Qt::red, 2, Qt::DotLine));
    zoomer->setTrackerPen(QPen(Qt::red));
    //zoomer->setZoomBase(QwtDoubleRect());
88
    legend = NULL;
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

    colors = QList<QColor>();
    nextColor = 0;

    ///> Color map for plots, includes 20 colors
    ///> Map will start from beginning when the first 20 colors are exceeded
    colors.append(QColor(242,255,128));
    colors.append(QColor(70,80,242));
    colors.append(QColor(232,33,47));
    colors.append(QColor(116,251,110));
    colors.append(QColor(81,183,244));
    colors.append(QColor(234,38,107));
    colors.append(QColor(92,247,217));
    colors.append(QColor(151,59,239));
    colors.append(QColor(231,72,28));
    colors.append(QColor(236,48,221));
    colors.append(QColor(75,133,243));
    colors.append(QColor(203,254,121));
    colors.append(QColor(104,64,240));
    colors.append(QColor(200,54,238));
    colors.append(QColor(104,250,138));
    colors.append(QColor(235,43,165));
    colors.append(QColor(98,248,176));
    colors.append(QColor(161,252,116));
    colors.append(QColor(87,231,246));
    colors.append(QColor(230,126,23));
}

IncrementalPlot::~IncrementalPlot()
{

}

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
void IncrementalPlot::showLegend(bool show)
{
    if (show)
    {
        if (legend == NULL)
        {
            legend = new QwtLegend;
            legend->setFrameStyle(QFrame::Box);
        }
        insertLegend(legend, QwtPlot::RightLegend);
    }
    else
    {
        delete legend;
        legend = NULL;
    }
    replot();
}

/**
 * Set datapoint and line style. This interface is intented
 * to be directly connected to the UI and allows to parse
 * human-readable, textual descriptions into plot specs.
 *
 * Data points: Either "circles", "crosses" or the default "dots"
 * Lines: Either "dotted", ("solid"/"line") or no lines if not used
 *
 * No special formatting is needed, as long as the keywords are contained
 * in the string. Lower/uppercase is ignored as well.
 *
 * @param style Formatting string for line/data point style
 */
void IncrementalPlot::setStyleText(QString style)
{
    foreach (QwtPlotCurve* curve, d_curve)
    {
        // Style of datapoints
        if (style.toLower().contains("circles"))
        {
            curve->setSymbol(QwtSymbol(QwtSymbol::Ellipse,
                                       QBrush(curve->pen().color()), curve->pen(), QSize(5, 5)) );
        }
        else if (style.toLower().contains("crosses"))
        {
            curve->setSymbol(QwtSymbol(QwtSymbol::XCross,
                                       QBrush(curve->pen().color()), curve->pen(), QSize(5, 5)) );
        }
        else // Always show dots (style.toLower().contains("dots"))
        {
            curve->setSymbol(QwtSymbol(QwtSymbol::Rect,
                                       QBrush(curve->pen().color()), curve->pen(), QSize(1, 1)) );
        }

        // Style of lines
        if (style.toLower().contains("dotted"))
        {
            curve->setStyle(QwtPlotCurve::Dots);
        }
        else if (style.toLower().contains("line") || style.toLower().contains("solid"))
        {
            curve->setStyle(QwtPlotCurve::Lines);
        }
        else
        {
            curve->setStyle(QwtPlotCurve::NoCurve);
        }
    }
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
void IncrementalPlot::resetScaling()
{
    xmin = 0;
    xmax = 500;
    ymin = 0;
    ymax = 500;

    setAxisScale(xBottom, xmin+xmin*0.05, xmax+xmax*0.05);
    setAxisScale(yLeft, ymin+ymin*0.05, ymax+ymax*0.05);

    replot();

    // Make sure the first data access hits these
    xmin = DBL_MAX;
    xmax = DBL_MIN;
    ymin = DBL_MAX;
    ymax = DBL_MIN;
}

void IncrementalPlot::appendData(QString key, double x, double y)
{
    appendData(key, &x, &y, 1);
}

void IncrementalPlot::appendData(QString key, double *x, double *y, int size)
{
    CurveData* data;
    QwtPlotCurve* curve;
    if (!d_data.contains(key))
    {
        data = new CurveData;
        d_data.insert(key, data);
    }
    else
    {
        data = d_data.value(key);
    }

    if (!d_curve.contains(key))
    {
        curve = new QwtPlotCurve(key);
        d_curve.insert(key, curve);
        curve->setStyle(QwtPlotCurve::NoCurve);
        curve->setPaintAttribute(QwtPlotCurve::PaintFiltered);

        const QColor &c = getNextColor();
        curve->setSymbol(QwtSymbol(QwtSymbol::XCross,
                                   QBrush(c), QPen(c), QSize(5, 5)) );

        curve->attach(this);
    }
    else
    {
        curve = d_curve.value(key);
    }

    data->append(x, y, size);
    curve->setRawData(data->x(), data->y(), data->count());

    bool scaleChanged = false;

    // Update scales
    for (int i = 0; i<size; i++)
    {
        if (x[i] < xmin)
        {
            xmin = x[i];
            scaleChanged = true;
        }
        if (x[i] > xmax)
        {
            xmax = x[i];
            scaleChanged = true;
        }

        if (y[i] < ymin)
        {
            ymin = y[i];
            scaleChanged = true;
        }
        if (y[i] > ymax)
        {
            ymax = y[i];
            scaleChanged = true;
        }
    }
    //    setAxisScale(xBottom, xmin+xmin*0.05, xmax+xmax*0.05);
    //    setAxisScale(yLeft, ymin+ymin*0.05, ymax+ymax*0.05);

    //#ifdef __GNUC__
    //#warning better use QwtData
    //#endif

    //replot();

    if(scaleChanged)
    {
        setAxisScale(xBottom, xmin+xmin*0.05, xmax+xmax*0.05);
        setAxisScale(yLeft, ymin+ymin*0.05, ymax+ymax*0.05);
        zoomer->setZoomBase(true);
    }
    else
    {

        const bool cacheMode =
                canvas()->testPaintAttribute(QwtPlotCanvas::PaintCached);

#if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
        // Even if not recommended by TrollTech, Qt::WA_PaintOutsidePaintEvent
        // works on X11. This has an tremendous effect on the performance..

        canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
#endif

        canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
        // FIXME Check if here all curves should be drawn
307 308 309 310 311
        //        QwtPlotCurve* plotCurve;
        //        foreach(plotCurve, d_curve)
        //        {
        //            plotCurve->draw(0, curve->dataSize()-1);
        //        }
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

        curve->draw(curve->dataSize() - size, curve->dataSize() - 1);
        canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, cacheMode);

#if QT_VERSION >= 0x040000 && defined(Q_WS_X11)
        canvas()->setAttribute(Qt::WA_PaintOutsidePaintEvent, false);
#endif

    }

}

QList<QColor> IncrementalPlot::getColorMap()
{
    return colors;
}

QColor IncrementalPlot::getNextColor()
{
    /* Return current color and increment counter for next round */
    nextColor++;
    if(nextColor >= colors.size()) nextColor = 0;
    return colors[nextColor++];
}

QColor IncrementalPlot::getColorForCurve(QString id)
{
    return d_curve.value(id)->pen().color();
}

void IncrementalPlot::removeData()
{
pixhawk's avatar
pixhawk committed
344 345 346 347
    foreach (QwtPlotCurve* curve, d_curve)
    {
        delete curve;
    }
348
    d_curve.clear();
pixhawk's avatar
pixhawk committed
349 350 351 352 353

    foreach (CurveData* data, d_data)
    {
        delete data;
    }
354 355 356 357
    d_data.clear();
    resetScaling();
    replot();
}