qwt_plot_spectrogram.cpp 16.5 KB
Newer Older
pixhawk's avatar
pixhawk committed
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
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qimage.h>
#include <qpen.h>
#include <qpainter.h>
#include "qwt_painter.h"
#include "qwt_double_interval.h"
#include "qwt_scale_map.h"
#include "qwt_color_map.h"
#include "qwt_plot_spectrogram.h"

#if QT_VERSION < 0x040000
typedef QValueVector<QRgb> QwtColorTable;
#else
typedef QVector<QRgb> QwtColorTable;
#endif

class QwtPlotSpectrogramImage: public QImage
{
27
    // This class hides some Qt3/Qt4 API differences
pixhawk's avatar
pixhawk committed
28 29 30 31 32 33
public:
    QwtPlotSpectrogramImage(const QSize &size, QwtColorMap::Format format):
#if QT_VERSION < 0x040000
        QImage(size, format == QwtColorMap::RGB ? 32 : 8)
#else
        QImage(size, format == QwtColorMap::RGB
34
               ? QImage::Format_ARGB32 : QImage::Format_Indexed8 )
pixhawk's avatar
pixhawk committed
35 36 37 38 39
#endif
    {
    }

    QwtPlotSpectrogramImage(const QImage &other):
40
        QImage(other) {
pixhawk's avatar
pixhawk committed
41 42
    }

43
    void initColorTable(const QImage& other) {
pixhawk's avatar
pixhawk committed
44 45 46 47 48 49 50 51 52 53 54 55 56
#if QT_VERSION < 0x040000
        const unsigned int numColors = other.numColors();

        setNumColors(numColors);
        for ( unsigned int i = 0; i < numColors; i++ )
            setColor(i, other.color(i));
#else
        setColorTable(other.colorTable());
#endif
    }

#if QT_VERSION < 0x040000

57
    void setColorTable(const QwtColorTable &colorTable) {
pixhawk's avatar
pixhawk committed
58 59 60 61 62
        setNumColors(colorTable.size());
        for ( unsigned int i = 0; i < colorTable.size(); i++ )
            setColor(i, colorTable[i]);
    }

63
    QwtColorTable colorTable() const {
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        QwtColorTable table(numColors());
        for ( int i = 0; i < numColors(); i++ )
            table[i] = color(i);

        return table;
    }
#endif
};

class QwtPlotSpectrogram::PrivateData
{
public:
    class DummyData: public QwtRasterData
    {
    public:
79
        virtual QwtRasterData *copy() const {
pixhawk's avatar
pixhawk committed
80 81 82
            return new DummyData();
        }

83
        virtual double value(double, double) const {
pixhawk's avatar
pixhawk committed
84 85 86
            return 0.0;
        }

87
        virtual QwtDoubleInterval range() const {
pixhawk's avatar
pixhawk committed
88 89 90 91
            return QwtDoubleInterval(0.0, 1.0);
        }
    };

92
    PrivateData() {
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99
        data = new DummyData();
        colorMap = new QwtLinearColorMap();
        displayMode = ImageMode;

        conrecAttributes = QwtRasterData::IgnoreAllVerticesOnLevel;
        conrecAttributes |= QwtRasterData::IgnoreOutOfRange;
    }
100
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        delete data;
        delete colorMap;
    }

    QwtRasterData *data;
    QwtColorMap *colorMap;
    int displayMode;

    QwtValueList contourLevels;
    QPen defaultContourPen;
    int conrecAttributes;
};

/*!
   Sets the following item attributes:
   - QwtPlotItem::AutoScale: true
   - QwtPlotItem::Legend:    false

   The z value is initialized by 8.0.
120

pixhawk's avatar
pixhawk committed
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 155 156 157 158 159
   \param title Title

   \sa QwtPlotItem::setItemAttribute(), QwtPlotItem::setZ()
*/
QwtPlotSpectrogram::QwtPlotSpectrogram(const QString &title):
    QwtPlotRasterItem(title)
{
    d_data = new PrivateData();

    setItemAttribute(QwtPlotItem::AutoScale, true);
    setItemAttribute(QwtPlotItem::Legend, false);

    setZ(8.0);
}

//! Destructor
QwtPlotSpectrogram::~QwtPlotSpectrogram()
{
    delete d_data;
}

//! \return QwtPlotItem::Rtti_PlotSpectrogram
int QwtPlotSpectrogram::rtti() const
{
    return QwtPlotItem::Rtti_PlotSpectrogram;
}

/*!
   The display mode controls how the raster data will be represented.

   \param mode Display mode
   \param on On/Off

   The default setting enables ImageMode.

   \sa DisplayMode, displayMode()
*/
void QwtPlotSpectrogram::setDisplayMode(DisplayMode mode, bool on)
{
160
    if ( on != bool(mode & d_data->displayMode) ) {
pixhawk's avatar
pixhawk committed
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        if ( on )
            d_data->displayMode |= mode;
        else
            d_data->displayMode &= ~mode;
    }

    itemChanged();
}

/*!
   The display mode controls how the raster data will be represented.

   \param mode Display mode
   \return true if mode is enabled
*/
bool QwtPlotSpectrogram::testDisplayMode(DisplayMode mode) const
{
    return (d_data->displayMode & mode);
}

/*!
  Change the color map

  Often it is useful to display the mapping between intensities and
  colors as an additional plot axis, showing a color bar.

  \param colorMap Color Map

  \sa colorMap(), QwtScaleWidget::setColorBarEnabled(),
      QwtScaleWidget::setColorMap()
*/
void QwtPlotSpectrogram::setColorMap(const QwtColorMap &colorMap)
{
    delete d_data->colorMap;
    d_data->colorMap = colorMap.copy();

    invalidateCache();
    itemChanged();
}

/*!
   \return Color Map used for mapping the intensity values to colors
   \sa setColorMap()
*/
const QwtColorMap &QwtPlotSpectrogram::colorMap() const
{
    return *d_data->colorMap;
}

/*!
   \brief Set the default pen for the contour lines

213
   If the spectrogram has a valid default contour pen
pixhawk's avatar
pixhawk committed
214 215 216 217 218 219 220 221
   a contour line is painted using the default contour pen.
   Otherwise (pen.style() == Qt::NoPen) the pen is calculated
   for each contour level using contourPen().

   \sa defaultContourPen, contourPen
*/
void QwtPlotSpectrogram::setDefaultContourPen(const QPen &pen)
{
222
    if ( pen != d_data->defaultContourPen ) {
pixhawk's avatar
pixhawk committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        d_data->defaultContourPen = pen;
        itemChanged();
    }
}

/*!
   \return Default contour pen
   \sa setDefaultContourPen
*/
QPen QwtPlotSpectrogram::defaultContourPen() const
{
    return d_data->defaultContourPen;
}

/*!
   \brief Calculate the pen for a contour line
239

pixhawk's avatar
pixhawk committed
240
   The color of the pen is the color for level calculated by the color map
241

pixhawk's avatar
pixhawk committed
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
   \param level Contour level
   \return Pen for the contour line
   \note contourPen is only used if defaultContourPen().style() == Qt::NoPen

   \sa setDefaultContourPen, setColorMap, setContourLevels
*/
QPen QwtPlotSpectrogram::contourPen(double level) const
{
    const QwtDoubleInterval intensityRange = d_data->data->range();
    const QColor c(d_data->colorMap->rgb(intensityRange, level));

    return QPen(c);
}

/*!
   Modify an attribute of the CONREC algorithm, used to calculate
   the contour lines.

   \param attribute CONREC attribute
   \param on On/Off

   \sa testConrecAttribute, renderContourLines, QwtRasterData::contourLines
*/
void QwtPlotSpectrogram::setConrecAttribute(
    QwtRasterData::ConrecAttribute attribute, bool on)
{
    if ( bool(d_data->conrecAttributes & attribute) == on )
        return;

    if ( on )
        d_data->conrecAttributes |= attribute;
    else
        d_data->conrecAttributes &= ~attribute;

    itemChanged();
}

/*!
   Test an attribute of the CONREC algorithm, used to calculate
   the contour lines.

   \param attribute CONREC attribute
   \return true, is enabled

   \sa setConrecAttribute, renderContourLines, QwtRasterData::contourLines
*/
bool QwtPlotSpectrogram::testConrecAttribute(
    QwtRasterData::ConrecAttribute attribute) const
290
{
pixhawk's avatar
pixhawk committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    return d_data->conrecAttributes & attribute;
}

/*!
   Set the levels of the contour lines

   \param levels Values of the contour levels
   \sa contourLevels, renderContourLines, QwtRasterData::contourLines

   \note contourLevels returns the same levels but sorted.
*/
void QwtPlotSpectrogram::setContourLevels(const QwtValueList &levels)
{
    d_data->contourLevels = levels;
#if QT_VERSION >= 0x040000
    qSort(d_data->contourLevels);
#else
    qHeapSort(d_data->contourLevels);
#endif
    itemChanged();
}

/*!
314
   \brief Return the levels of the contour lines.
pixhawk's avatar
pixhawk committed
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

   The levels are sorted in increasing order.

   \sa contourLevels, renderContourLines, QwtRasterData::contourLines
*/
QwtValueList QwtPlotSpectrogram::contourLevels() const
{
    return d_data->contourLevels;
}

/*!
  Set the data to be displayed

  \param data Spectrogram Data
  \sa data()
*/
void QwtPlotSpectrogram::setData(const QwtRasterData &data)
{
    delete d_data->data;
    d_data->data = data.copy();

    invalidateCache();
    itemChanged();
}

/*!
  \return Spectrogram data
  \sa setData()
*/
const QwtRasterData &QwtPlotSpectrogram::data() const
{
    return *d_data->data;
}

/*!
   \return Bounding rect of the data
   \sa QwtRasterData::boundingRect
*/
QwtDoubleRect QwtPlotSpectrogram::boundingRect() const
{
    return d_data->data->boundingRect();
}

/*!
   \brief Returns the recommended raster for a given rect.

   F.e the raster hint is used to limit the resolution of
   the image that is rendered.

   \param rect Rect for the raster hint
   \return data().rasterHint(rect)
*/
QSize QwtPlotSpectrogram::rasterHint(const QwtDoubleRect &rect) const
{
    return d_data->data->rasterHint(rect);
}

/*!
   \brief Render an image from the data and color map.

375
   The area is translated into a rect of the paint device.
pixhawk's avatar
pixhawk committed
376 377 378 379 380 381 382
   For each pixel of this rect the intensity is mapped
   into a color.

  \param xMap X-Scale Map
  \param yMap Y-Scale Map
  \param area Area that should be rendered in scale coordinates.

383
   \return A QImage::Format_Indexed8 or QImage::Format_ARGB32 depending
pixhawk's avatar
pixhawk committed
384 385 386 387 388 389
           on the color map.

   \sa QwtRasterData::intensity(), QwtColorMap::rgb(),
       QwtColorMap::colorIndex()
*/
QImage QwtPlotSpectrogram::renderImage(
390
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
pixhawk's avatar
pixhawk committed
391 392 393 394 395 396 397 398 399 400 401
    const QwtDoubleRect &area) const
{
    if ( area.isEmpty() )
        return QImage();

    QRect rect = transform(xMap, yMap, area);

    QwtScaleMap xxMap = xMap;
    QwtScaleMap yyMap = yMap;

    const QSize res = d_data->data->rasterHint(area);
402
    if ( res.isValid() ) {
pixhawk's avatar
pixhawk committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
        /*
          It is useless to render an image with a higher resolution
          than the data offers. Of course someone will have to
          scale this image later into the size of the given rect, but f.e.
          in case of postscript this will done on the printer.
         */
        rect.setSize(rect.size().boundedTo(res));

        int px1 = rect.x();
        int px2 = rect.x() + rect.width();
        if ( xMap.p1() > xMap.p2() )
            qSwap(px1, px2);

        double sx1 = area.x();
        double sx2 = area.x() + area.width();
        if ( xMap.s1() > xMap.s2() )
            qSwap(sx1, sx2);

        int py1 = rect.y();
        int py2 = rect.y() + rect.height();
        if ( yMap.p1() > yMap.p2() )
            qSwap(py1, py2);

        double sy1 = area.y();
        double sy2 = area.y() + area.height();
        if ( yMap.s1() > yMap.s2() )
            qSwap(sy1, sy2);

        xxMap.setPaintInterval(px1, px2);
        xxMap.setScaleInterval(sx1, sx2);
        yyMap.setPaintInterval(py1, py2);
434
        yyMap.setScaleInterval(sy1, sy2);
pixhawk's avatar
pixhawk committed
435 436 437 438 439 440 441 442 443 444
    }

    QwtPlotSpectrogramImage image(rect.size(), d_data->colorMap->format());

    const QwtDoubleInterval intensityRange = d_data->data->range();
    if ( !intensityRange.isValid() )
        return image;

    d_data->data->initRaster(area, rect.size());

445 446
    if ( d_data->colorMap->format() == QwtColorMap::RGB ) {
        for ( int y = rect.top(); y <= rect.bottom(); y++ ) {
pixhawk's avatar
pixhawk committed
447 448 449
            const double ty = yyMap.invTransform(y);

            QRgb *line = (QRgb *)image.scanLine(y - rect.top());
450
            for ( int x = rect.left(); x <= rect.right(); x++ ) {
pixhawk's avatar
pixhawk committed
451 452 453
                const double tx = xxMap.invTransform(x);

                *line++ = d_data->colorMap->rgb(intensityRange,
454
                                                d_data->data->value(tx, ty));
pixhawk's avatar
pixhawk committed
455 456
            }
        }
457
    } else if ( d_data->colorMap->format() == QwtColorMap::Indexed ) {
pixhawk's avatar
pixhawk committed
458 459
        image.setColorTable(d_data->colorMap->colorTable(intensityRange));

460
        for ( int y = rect.top(); y <= rect.bottom(); y++ ) {
pixhawk's avatar
pixhawk committed
461 462 463
            const double ty = yyMap.invTransform(y);

            unsigned char *line = image.scanLine(y - rect.top());
464
            for ( int x = rect.left(); x <= rect.right(); x++ ) {
pixhawk's avatar
pixhawk committed
465 466 467
                const double tx = xxMap.invTransform(x);

                *line++ = d_data->colorMap->colorIndex(intensityRange,
468
                                                       d_data->data->value(tx, ty));
pixhawk's avatar
pixhawk committed
469 470 471 472 473 474 475 476 477 478
            }
        }
    }

    d_data->data->discardRaster();

    // Mirror the image in case of inverted maps

    const bool hInvert = xxMap.p1() > xxMap.p2();
    const bool vInvert = yyMap.p1() < yyMap.p2();
479
    if ( hInvert || vInvert ) {
pixhawk's avatar
pixhawk committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#ifdef __GNUC__
#endif
#if QT_VERSION < 0x040000
        image = image.mirror(hInvert, vInvert);
#else
        image = image.mirrored(hInvert, vInvert);
#endif
    }

    return image;
}

/*!
   \brief Return the raster to be used by the CONREC contour algorithm.

   A larger size will improve the precisision of the CONREC algorithm,
   but will slow down the time that is needed to calculate the lines.

   The default implementation returns rect.size() / 2 bounded to
   data().rasterHint().

   \param area Rect, where to calculate the contour lines
   \param rect Rect in pixel coordinates, where to paint the contour lines
   \return Raster to be used by the CONREC contour algorithm.

   \note The size will be bounded to rect.size().
506

pixhawk's avatar
pixhawk committed
507 508 509
   \sa drawContourLines, QwtRasterData::contourLines
*/
QSize QwtPlotSpectrogram::contourRasterSize(const QwtDoubleRect &area,
510
        const QRect &rect) const
pixhawk's avatar
pixhawk committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
{
    QSize raster = rect.size() / 2;

    const QSize rasterHint = d_data->data->rasterHint(area);
    if ( rasterHint.isValid() )
        raster = raster.boundedTo(rasterHint);

    return raster;
}

/*!
   Calculate contour lines

   \param rect Rectangle, where to calculate the contour lines
   \param raster Raster, used by the CONREC algorithm

   \sa contourLevels, setConrecAttribute, QwtRasterData::contourLines
*/
QwtRasterData::ContourLines QwtPlotSpectrogram::renderContourLines(
    const QwtDoubleRect &rect, const QSize &raster) const
{
    return d_data->data->contourLines(rect, raster,
533
                                      d_data->contourLevels, d_data->conrecAttributes );
pixhawk's avatar
pixhawk committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
}

/*!
   Paint the contour lines

   \param painter Painter
   \param xMap Maps x-values into pixel coordinates.
   \param yMap Maps y-values into pixel coordinates.
   \param contourLines Contour lines

   \sa renderContourLines, defaultContourPen, contourPen
*/
void QwtPlotSpectrogram::drawContourLines(QPainter *painter,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QwtRasterData::ContourLines &contourLines) const
{
    const QwtDoubleInterval intensityRange = d_data->data->range();

    const int numLevels = (int)d_data->contourLevels.size();
553
    for (int l = 0; l < numLevels; l++) {
pixhawk's avatar
pixhawk committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        const double level = d_data->contourLevels[l];

        QPen pen = defaultContourPen();
        if ( pen.style() == Qt::NoPen )
            pen = contourPen(level);

        if ( pen.style() == Qt::NoPen )
            continue;

        painter->setPen(pen);

#if QT_VERSION >= 0x040000
        const QPolygonF &lines = contourLines[level];
#else
        const QwtArray<QwtDoublePoint> &lines = contourLines[level];
#endif
570
        for ( int i = 0; i < (int)lines.size(); i += 2 ) {
pixhawk's avatar
pixhawk committed
571
            const QPoint p1( xMap.transform(lines[i].x()),
572
                             yMap.transform(lines[i].y()) );
pixhawk's avatar
pixhawk committed
573
            const QPoint p2( xMap.transform(lines[i+1].x()),
574
                             yMap.transform(lines[i+1].y()) );
pixhawk's avatar
pixhawk committed
575 576 577 578 579 580 581 582 583 584 585 586

            QwtPainter::drawLine(painter, p1, p2);
        }
    }
}

/*!
  \brief Draw the spectrogram

  \param painter Painter
  \param xMap Maps x-values into pixel coordinates.
  \param yMap Maps y-values into pixel coordinates.
587
  \param canvasRect Contents rect of the canvas in painter coordinates
pixhawk's avatar
pixhawk committed
588

589
  \sa setDisplayMode, renderImage,
pixhawk's avatar
pixhawk committed
590 591 592 593
      QwtPlotRasterItem::draw, drawContourLines
*/

void QwtPlotSpectrogram::draw(QPainter *painter,
594 595
                              const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                              const QRect &canvasRect) const
pixhawk's avatar
pixhawk committed
596 597 598 599
{
    if ( d_data->displayMode & ImageMode )
        QwtPlotRasterItem::draw(painter, xMap, yMap, canvasRect);

600 601
    if ( d_data->displayMode & ContourMode ) {
        // Add some pixels at the borders, so that
pixhawk's avatar
pixhawk committed
602 603
        const int margin = 2;
        QRect rasterRect(canvasRect.x() - margin, canvasRect.y() - margin,
604
                         canvasRect.width() + 2 * margin, canvasRect.height() + 2 * margin);
pixhawk's avatar
pixhawk committed
605 606 607 608

        QwtDoubleRect area = invTransform(xMap, yMap, rasterRect);

        const QwtDoubleRect br = boundingRect();
609
        if ( br.isValid() ) {
pixhawk's avatar
pixhawk committed
610 611 612 613 614 615 616 617 618
            area &= br;
            if ( area.isEmpty() )
                return;

            rasterRect = transform(xMap, yMap, area);
        }

        QSize raster = contourRasterSize(area, rasterRect);
        raster = raster.boundedTo(rasterRect.size());
619
        if ( raster.isValid() ) {
pixhawk's avatar
pixhawk committed
620 621 622 623 624 625 626 627
            const QwtRasterData::ContourLines lines =
                renderContourLines(area, raster);

            drawContourLines(painter, xMap, yMap, lines);
        }
    }
}