qwt_plot_item.cpp 11.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_text.h"
#include "qwt_plot.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_plot_item.h"

class QwtPlotItem::PrivateData
{
public:
    PrivateData():
        plot(NULL),
        isVisible(true),
        attributes(0),
#if QT_VERSION >= 0x040000
        renderHints(0),
#endif
        z(0.0),
        xAxis(QwtPlot::xBottom),
28
        yAxis(QwtPlot::yLeft) {
pixhawk's avatar
pixhawk committed
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
    }

    mutable QwtPlot *plot;

    bool isVisible;
    int attributes;
#if QT_VERSION >= 0x040000
    int renderHints;
#endif
    double z;

    int xAxis;
    int yAxis;

    QwtText title;
};

//! Constructor
QwtPlotItem::QwtPlotItem(const QwtText &title)
{
    d_data = new PrivateData;
    d_data->title = title;
}

//! Destroy the QwtPlotItem
QwtPlotItem::~QwtPlotItem()
{
    attach(NULL);
    delete d_data;
}

60
/*!
pixhawk's avatar
pixhawk committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  \brief Attach the item to a plot.

  This method will attach a QwtPlotItem to the QwtPlot argument. It will first
  detach the QwtPlotItem from any plot from a previous call to attach (if
  necessary). If a NULL argument is passed, it will detach from any QwtPlot it
  was attached to.

  \sa QwtPlotItem::detach()
*/
void QwtPlotItem::attach(QwtPlot *plot)
{
    if ( plot == d_data->plot )
        return;

    // remove the item from the previous plot

77 78
    if ( d_data->plot ) {
        if ( d_data->plot->legend() ) {
pixhawk's avatar
pixhawk committed
79 80
            QWidget *legendItem = d_data->plot->legend()->find(this);
            if ( legendItem )
81
                delete legendItem;
pixhawk's avatar
pixhawk committed
82 83 84 85 86 87 88 89 90 91
        }

        d_data->plot->attachItem(this, false);

        if ( d_data->plot->autoReplot() )
            d_data->plot->update();
    }

    d_data->plot = plot;

92
    if ( d_data->plot ) {
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99
        // insert the item into the current plot

        d_data->plot->attachItem(this, true);
        itemChanged();
    }
}

100
/*!
pixhawk's avatar
pixhawk committed
101 102 103 104
   Return rtti for the specific class represented. QwtPlotItem is simply
   a virtual interface class, and base classes will implement this method
   with specific rtti values so a user can differentiate them.

105
   The rtti value is useful for environments, where the
pixhawk's avatar
pixhawk committed
106 107
   runtime type information is disabled and it is not possible
   to do a dynamic_cast<...>.
108

pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116 117
   \return rtti value
   \sa RttiValues
*/
int QwtPlotItem::rtti() const
{
    return Rtti_PlotItem;
}

//! Return attached plot
118 119 120
QwtPlot *QwtPlotItem::plot() const
{
    return d_data->plot;
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127
}

/*!
   Plot items are painted in increasing z-order.

   \return setZ(), QwtPlotDict::itemList()
*/
128 129 130
double QwtPlotItem::z() const
{
    return d_data->z;
pixhawk's avatar
pixhawk committed
131 132 133 134 135 136 137 138 139 140
}

/*!
   \brief Set the z value

   Plot items are painted in increasing z-order.

   \param z Z-value
   \sa z(), QwtPlotDict::itemList()
*/
141 142 143 144 145
void QwtPlotItem::setZ(double z)
{
    if ( d_data->z != z ) {
        d_data->z = z;
        if ( d_data->plot ) {
pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152 153
            // update the z order
            d_data->plot->attachItem(this, false);
            d_data->plot->attachItem(this, true);
        }
        itemChanged();
    }
}

154
/*!
pixhawk's avatar
pixhawk committed
155 156 157
   Set a new title

   \param title Title
158 159
   \sa title()
*/
pixhawk's avatar
pixhawk committed
160 161 162 163 164
void QwtPlotItem::setTitle(const QString &title)
{
    setTitle(QwtText(title));
}

165
/*!
pixhawk's avatar
pixhawk committed
166 167 168
   Set a new title

   \param title Title
169 170
   \sa title()
*/
pixhawk's avatar
pixhawk committed
171 172
void QwtPlotItem::setTitle(const QwtText &title)
{
173 174
    if ( d_data->title != title ) {
        d_data->title = title;
pixhawk's avatar
pixhawk committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        itemChanged();
    }
}

/*!
   \return Title of the item
   \sa setTitle()
*/
const QwtText &QwtPlotItem::title() const
{
    return d_data->title;
}

/*!
   Toggle an item attribute
190

pixhawk's avatar
pixhawk committed
191 192 193 194 195 196 197
   \param attribute Attribute type
   \param on true/false

   \sa testItemAttribute(), ItemAttribute
*/
void QwtPlotItem::setItemAttribute(ItemAttribute attribute, bool on)
{
198
    if ( bool(d_data->attributes & attribute) != on ) {
pixhawk's avatar
pixhawk committed
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
        if ( on )
            d_data->attributes |= attribute;
        else
            d_data->attributes &= ~attribute;

        itemChanged();
    }
}

/*!
   Test an item attribute

   \param ItemAttribute Attribute type
   \return true/false
   \sa setItemAttribute(), ItemAttribute
*/
bool QwtPlotItem::testItemAttribute(ItemAttribute attribute) const
{
    return d_data->attributes & attribute;
}

#if QT_VERSION >= 0x040000

/*!
   Toggle an render hint
224

pixhawk's avatar
pixhawk committed
225 226 227 228 229 230 231
   \param hint Render hint
   \param on true/false

   \sa testRenderHint(), RenderHint
*/
void QwtPlotItem::setRenderHint(RenderHint hint, bool on)
{
232
    if ( ((d_data->renderHints & hint) != 0) != on ) {
pixhawk's avatar
pixhawk committed
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
        if ( on )
            d_data->renderHints |= hint;
        else
            d_data->renderHints &= ~hint;

        itemChanged();
    }
}

/*!
   Test a render hint

   \param hint Render hint
   \return true/false
   \sa setRenderHint(), RenderHint
*/
bool QwtPlotItem::testRenderHint(RenderHint hint) const
{
    return (d_data->renderHints & hint);
}

#endif

//! Show the item
void QwtPlotItem::show()
{
    setVisible(true);
}

//! Hide the item
void QwtPlotItem::hide()
{
    setVisible(false);
}

268
/*!
pixhawk's avatar
pixhawk committed
269 270 271 272 273
    Show/Hide the item

    \param on Show if true, otherwise hide
    \sa isVisible(), show(), hide()
*/
274 275 276 277 278
void QwtPlotItem::setVisible(bool on)
{
    if ( on != d_data->isVisible ) {
        d_data->isVisible = on;
        itemChanged();
pixhawk's avatar
pixhawk committed
279 280 281
    }
}

282
/*!
pixhawk's avatar
pixhawk committed
283 284 285 286
    \return true if visible
    \sa setVisible(), show(), hide()
*/
bool QwtPlotItem::isVisible() const
287 288
{
    return d_data->isVisible;
pixhawk's avatar
pixhawk committed
289 290
}

291 292
/*!
   Update the legend and call QwtPlot::autoRefresh for the
pixhawk's avatar
pixhawk committed
293 294 295 296 297 298
   parent plot.

   \sa updateLegend()
*/
void QwtPlotItem::itemChanged()
{
299
    if ( d_data->plot ) {
pixhawk's avatar
pixhawk committed
300 301 302 303 304 305 306
        if ( d_data->plot->legend() )
            updateLegend(d_data->plot->legend());

        d_data->plot->autoRefresh();
    }
}

307
/*!
pixhawk's avatar
pixhawk committed
308 309 310 311 312 313 314 315 316 317 318 319
   Set X and Y axis

   The item will painted according to the coordinates its Axes.

   \param xAxis X Axis
   \param yAxis Y Axis

   \sa setXAxis(), setYAxis(), xAxis(), yAxis()
*/
void QwtPlotItem::setAxis(int xAxis, int yAxis)
{
    if (xAxis == QwtPlot::xBottom || xAxis == QwtPlot::xTop )
320
        d_data->xAxis = xAxis;
pixhawk's avatar
pixhawk committed
321 322

    if (yAxis == QwtPlot::yLeft || yAxis == QwtPlot::yRight )
323
        d_data->yAxis = yAxis;
pixhawk's avatar
pixhawk committed
324

325
    itemChanged();
pixhawk's avatar
pixhawk committed
326 327
}

328
/*!
pixhawk's avatar
pixhawk committed
329 330 331 332 333 334 335 336 337
   Set the X axis

   The item will painted according to the coordinates its Axes.

   \param axis X Axis
   \sa setAxis(), setYAxis(), xAxis()
*/
void QwtPlotItem::setXAxis(int axis)
{
338 339 340
    if (axis == QwtPlot::xBottom || axis == QwtPlot::xTop ) {
        d_data->xAxis = axis;
        itemChanged();
pixhawk's avatar
pixhawk committed
341 342 343
    }
}

344
/*!
pixhawk's avatar
pixhawk committed
345 346 347 348 349 350 351 352 353
   Set the Y axis

   The item will painted according to the coordinates its Axes.

   \param axis Y Axis
   \sa setAxis(), setXAxis(), yAxis()
*/
void QwtPlotItem::setYAxis(int axis)
{
354 355 356
    if (axis == QwtPlot::yLeft || axis == QwtPlot::yRight ) {
        d_data->yAxis = axis;
        itemChanged();
pixhawk's avatar
pixhawk committed
357 358 359 360
    }
}

//! Return xAxis
361 362 363
int QwtPlotItem::xAxis() const
{
    return d_data->xAxis;
pixhawk's avatar
pixhawk committed
364 365 366
}

//! Return yAxis
367 368 369
int QwtPlotItem::yAxis() const
{
    return d_data->yAxis;
pixhawk's avatar
pixhawk committed
370 371 372 373 374 375 376 377 378 379 380 381 382
}

/*!
   \return An invalid bounding rect: QwtDoubleRect(1.0, 1.0, -2.0, -2.0)
*/
QwtDoubleRect QwtPlotItem::boundingRect() const
{
    return QwtDoubleRect(1.0, 1.0, -2.0, -2.0); // invalid
}

/*!
   \brief Allocate the widget that represents the item on the legend

383
   The default implementation is made for QwtPlotCurve and returns a
pixhawk's avatar
pixhawk committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
   QwtLegendItem(), but an item could be represented by any type of widget,
   by overloading legendItem() and updateLegend().

   \return QwtLegendItem()
   \sa updateLegend() QwtLegend()
*/
QWidget *QwtPlotItem::legendItem() const
{
    return new QwtLegendItem;
}

/*!
   \brief Update the widget that represents the item on the legend

   updateLegend() is called from itemChanged() to adopt the widget
   representing the item on the legend to its new configuration.
400 401

   The default implementation is made for QwtPlotCurve and updates a
pixhawk's avatar
pixhawk committed
402 403 404 405 406 407 408 409 410 411 412
   QwtLegendItem(), but an item could be represented by any type of widget,
   by overloading legendItem() and updateLegend().

   \sa legendItem(), itemChanged(), QwtLegend()
*/
void QwtPlotItem::updateLegend(QwtLegend *legend) const
{
    if ( !legend )
        return;

    QWidget *lgdItem = legend->find(this);
413 414
    if ( testItemAttribute(QwtPlotItem::Legend) ) {
        if ( lgdItem == NULL ) {
pixhawk's avatar
pixhawk committed
415
            lgdItem = legendItem();
416 417
            if ( lgdItem ) {
                if ( lgdItem->inherits("QwtLegendItem") ) {
pixhawk's avatar
pixhawk committed
418 419 420
                    QwtLegendItem *label = (QwtLegendItem *)lgdItem;
                    label->setItemMode(legend->itemMode());

421 422 423 424 425
                    if ( d_data->plot ) {
                        QObject::connect(label, SIGNAL(clicked()),
                                         d_data->plot, SLOT(legendItemClicked()));
                        QObject::connect(label, SIGNAL(checked(bool)),
                                         d_data->plot, SLOT(legendItemChecked(bool)));
pixhawk's avatar
pixhawk committed
426 427 428 429 430
                    }
                }
                legend->insert(this, lgdItem);
            }
        }
431
        if ( lgdItem && lgdItem->inherits("QwtLegendItem") ) {
pixhawk's avatar
pixhawk committed
432 433 434 435
            QwtLegendItem* label = (QwtLegendItem*)lgdItem;
            if ( label )
                label->setText(d_data->title);
        }
436
    } else {
pixhawk's avatar
pixhawk committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
        delete lgdItem;
    }
}

/*!
   \brief Update the item to changes of the axes scale division

   Update the item, when the axes of plot have changed.
   The default implementation does nothing, but items that depend
   on the scale division (like QwtPlotGrid()) have to reimplement
   updateScaleDiv()

   \param xScaleDiv Scale division of the x-axis
   \param yScaleDiv Scale division of the y-axis

   \sa QwtPlot::updateAxes()
*/
void QwtPlotItem::updateScaleDiv(const QwtScaleDiv &,
455 456
                                 const QwtScaleDiv &)
{
pixhawk's avatar
pixhawk committed
457 458 459 460 461 462 463 464 465 466
}

/*!
   \brief Calculate the bounding scale rect of 2 maps

   \param xMap X map
   \param yMap X map

   \return Bounding rect of the scale maps
*/
467 468
QwtDoubleRect QwtPlotItem::scaleRect(const QwtScaleMap &xMap,
                                     const QwtScaleMap &yMap) const
pixhawk's avatar
pixhawk committed
469
{
470 471
    return QwtDoubleRect(xMap.s1(), yMap.s1(),
                         xMap.sDist(), yMap.sDist() );
pixhawk's avatar
pixhawk committed
472 473 474 475 476 477 478 479 480 481
}

/*!
   \brief Calculate the bounding paint rect of 2 maps

   \param xMap X map
   \param yMap X map

   \return Bounding rect of the scale maps
*/
482 483
QRect QwtPlotItem::paintRect(const QwtScaleMap &xMap,
                             const QwtScaleMap &yMap) const
pixhawk's avatar
pixhawk committed
484 485
{
    const QRect rect( qRound(xMap.p1()), qRound(yMap.p1()),
486
                      qRound(xMap.pDist()), qRound(yMap.pDist()) );
pixhawk's avatar
pixhawk committed
487 488 489 490 491 492 493 494 495 496 497

    return rect;
}

/*!
   Transform a rectangle

   \param xMap X map
   \param yMap Y map
   \param rect Rectangle in scale coordinates
   \return Rectangle in paint coordinates
498

pixhawk's avatar
pixhawk committed
499 500
   \sa invTransform()
*/
501 502
QRect QwtPlotItem::transform(const QwtScaleMap &xMap,
                             const QwtScaleMap &yMap, const QwtDoubleRect& rect) const
pixhawk's avatar
pixhawk committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
    int x1 = qRound(xMap.transform(rect.left()));
    int x2 = qRound(xMap.transform(rect.right()));
    int y1 = qRound(yMap.transform(rect.top()));
    int y2 = qRound(yMap.transform(rect.bottom()));

    if ( x2 < x1 )
        qSwap(x1, x2);
    if ( y2 < y1 )
        qSwap(y1, y2);

    return QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
}

/*!
   Transform a rectangle from paint to scale coordinates

   \param xMap X map
   \param yMap Y map
   \param rect Rectangle in paint coordinates
   \return Rectangle in scale coordinates
   \sa transform()
*/
526 527
QwtDoubleRect QwtPlotItem::invTransform(const QwtScaleMap &xMap,
                                        const QwtScaleMap &yMap, const QRect& rect) const
pixhawk's avatar
pixhawk committed
528 529 530 531 532
{
    const double x1 = xMap.invTransform(rect.left());
    const double x2 = xMap.invTransform(rect.right());
    const double y1 = yMap.invTransform(rect.top());
    const double y2 = yMap.invTransform(rect.bottom());
533

pixhawk's avatar
pixhawk committed
534 535 536 537
    const QwtDoubleRect r(x1, y1, x2 - x1, y2 - y1);

    return r.normalized();
}