qwt_plot.cpp 21.1 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 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 65 66 67 68 69 70 71 72 73 74 75 76
/* -*- 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 <qpainter.h>
#if QT_VERSION < 0x040000
#include <qguardedptr.h>
#include <qfocusdata.h>
#else
#include <qpointer.h>
#include <qpaintengine.h>
#endif
#include <qapplication.h>
#include <qevent.h>
#include "qwt_plot.h"
#include "qwt_plot_dict.h"
#include "qwt_plot_layout.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_engine.h"
#include "qwt_text_label.h"
#include "qwt_legend.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_plot_canvas.h"
#include "qwt_paint_buffer.h"

class QwtPlot::PrivateData
{
public:
#if QT_VERSION < 0x040000
    QGuardedPtr<QwtTextLabel> lblTitle;
    QGuardedPtr<QwtPlotCanvas> canvas;
    QGuardedPtr<QwtLegend> legend;
#else
    QPointer<QwtTextLabel> lblTitle;
    QPointer<QwtPlotCanvas> canvas;
    QPointer<QwtLegend> legend;
#endif
    QwtPlotLayout *layout;

    bool autoReplot;
};

/*!
  \brief Constructor
  \param parent Parent widget
 */
QwtPlot::QwtPlot(QWidget *parent):
    QFrame(parent)
{
    initPlot(QwtText());
}

/*!
  \brief Constructor
  \param title Title text
  \param parent Parent widget
 */
QwtPlot::QwtPlot(const QwtText &title, QWidget *parent):
    QFrame(parent)
{
    initPlot(title);
}

#if QT_VERSION < 0x040000
/*!
  \brief Constructor
  \param parent Parent widget
  \param name Object name
 */
QwtPlot::QwtPlot(QWidget *parent, const char *name):
    QFrame(parent, name)
77
{
pixhawk's avatar
pixhawk committed
78
    initPlot(QwtText());
79
}
pixhawk's avatar
pixhawk committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#endif


//! Destructor
QwtPlot::~QwtPlot()
{
    detachItems(QwtPlotItem::Rtti_PlotItem, autoDelete());

    delete d_data->layout;
    deleteAxesData();
    delete d_data;
}

/*!
  \brief Initializes a QwtPlot instance
  \param title Title text
 */
void QwtPlot::initPlot(const QwtText &title)
{
    d_data = new PrivateData;

#if QT_VERSION < 0x040000
    setWFlags(Qt::WNoAutoErase);
103
#endif
pixhawk's avatar
pixhawk committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    d_data->layout = new QwtPlotLayout;

    d_data->autoReplot = false;

    d_data->lblTitle = new QwtTextLabel(title, this);
    d_data->lblTitle->setFont(QFont(fontInfo().family(), 14, QFont::Bold));

    QwtText text(title);
    int flags = Qt::AlignCenter;
#if QT_VERSION < 0x040000
    flags |= Qt::WordBreak | Qt::ExpandTabs;
#else
    flags |= Qt::TextWordWrap;
#endif
    text.setRenderFlags(flags);
    d_data->lblTitle->setText(text);

    d_data->legend = NULL;

    initAxesData();

    d_data->canvas = new QwtPlotCanvas(this);
    d_data->canvas->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    d_data->canvas->setLineWidth(2);
    d_data->canvas->setMidLineWidth(0);

    updateTabOrder();

133 134
    setSizePolicy(QSizePolicy::MinimumExpanding,
                  QSizePolicy::MinimumExpanding);
pixhawk's avatar
pixhawk committed
135 136 137 138 139 140 141 142
}

/*!
  \brief Adds handling of layout requests
*/
bool QwtPlot::event(QEvent *e)
{
    bool ok = QFrame::event(e);
143
    switch(e->type()) {
pixhawk's avatar
pixhawk committed
144
#if QT_VERSION < 0x040000
145
    case QEvent::LayoutHint:
pixhawk's avatar
pixhawk committed
146
#else
147
    case QEvent::LayoutRequest:
pixhawk's avatar
pixhawk committed
148
#endif
149 150
        updateLayout();
        break;
pixhawk's avatar
pixhawk committed
151
#if QT_VERSION >= 0x040000
152 153 154
    case QEvent::PolishRequest:
        polish();
        break;
pixhawk's avatar
pixhawk committed
155
#endif
156 157
    default:
        ;
pixhawk's avatar
pixhawk committed
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 191
    }
    return ok;
}

//! Replots the plot if QwtPlot::autoReplot() is \c true.
void QwtPlot::autoRefresh()
{
    if (d_data->autoReplot)
        replot();
}

/*!
  \brief Set or reset the autoReplot option

  If the autoReplot option is set, the plot will be
  updated implicitly by manipulating member functions.
  Since this may be time-consuming, it is recommended
  to leave this option switched off and call replot()
  explicitly if necessary.

  The autoReplot option is set to false by default, which
  means that the user has to call replot() in order to make
  changes visible.
  \param tf \c true or \c false. Defaults to \c true.
  \sa replot()
*/
void QwtPlot::setAutoReplot(bool tf)
{
    d_data->autoReplot = tf;
}

//! \return true if the autoReplot option is set.
bool QwtPlot::autoReplot() const
{
192
    return d_data->autoReplot;
pixhawk's avatar
pixhawk committed
193 194 195 196 197 198 199 200
}

/*!
  Change the plot's title
  \param title New title
*/
void QwtPlot::setTitle(const QString &title)
{
201
    if ( title != d_data->lblTitle->text().text() ) {
pixhawk's avatar
pixhawk committed
202 203 204 205 206 207 208 209 210 211 212
        d_data->lblTitle->setText(title);
        updateLayout();
    }
}

/*!
  Change the plot's title
  \param title New title
*/
void QwtPlot::setTitle(const QwtText &title)
{
213
    if ( title != d_data->lblTitle->text() ) {
pixhawk's avatar
pixhawk committed
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
        d_data->lblTitle->setText(title);
        updateLayout();
    }
}

//! \return the plot's title
QwtText QwtPlot::title() const
{
    return d_data->lblTitle->text();
}

//! \return the plot's title
QwtPlotLayout *QwtPlot::plotLayout()
{
    return d_data->layout;
}

//! \return the plot's titel label.
const QwtPlotLayout *QwtPlot::plotLayout() const
{
    return d_data->layout;
}

//! \return the plot's titel label.
QwtTextLabel *QwtPlot::titleLabel()
{
    return d_data->lblTitle;
}

/*!
  \return the plot's titel label.
*/
const QwtTextLabel *QwtPlot::titleLabel() const
{
    return d_data->lblTitle;
}

/*!
  \return the plot's legend
  \sa insertLegend()
*/
QwtLegend *QwtPlot::legend()
256
{
pixhawk's avatar
pixhawk committed
257
    return d_data->legend;
258
}
pixhawk's avatar
pixhawk committed
259 260 261 262 263 264

/*!
  \return the plot's legend
  \sa insertLegend()
*/
const QwtLegend *QwtPlot::legend() const
265
{
pixhawk's avatar
pixhawk committed
266
    return d_data->legend;
267
}
pixhawk's avatar
pixhawk committed
268 269 270 271 272 273


/*!
  \return the plot's canvas
*/
QwtPlotCanvas *QwtPlot::canvas()
274
{
pixhawk's avatar
pixhawk committed
275
    return d_data->canvas;
276
}
pixhawk's avatar
pixhawk committed
277 278 279 280 281

/*!
  \return the plot's canvas
*/
const QwtPlotCanvas *QwtPlot::canvas() const
282
{
pixhawk's avatar
pixhawk committed
283 284 285 286 287 288 289 290 291 292 293 294 295
    return d_data->canvas;
}

//! Polish
void QwtPlot::polish()
{
    replot();

#if QT_VERSION < 0x040000
    QFrame::polish();
#endif
}

296
/*!
pixhawk's avatar
pixhawk committed
297 298 299 300 301 302 303 304
  Return sizeHint
  \sa minimumSizeHint()
*/

QSize QwtPlot::sizeHint() const
{
    int dw = 0;
    int dh = 0;
305 306
    for ( int axisId = 0; axisId < axisCnt; axisId++ ) {
        if ( axisEnabled(axisId) ) {
pixhawk's avatar
pixhawk committed
307 308 309 310 311
            const int niceDist = 40;
            const QwtScaleWidget *scaleWidget = axisWidget(axisId);
            const QwtScaleDiv &scaleDiv = scaleWidget->scaleDraw()->scaleDiv();
            const int majCnt = scaleDiv.ticks(QwtScaleDiv::MajorTick).count();

312 313 314
            if ( axisId == yLeft || axisId == yRight ) {
                int hDiff = (majCnt - 1) * niceDist
                            - scaleWidget->minimumSizeHint().height();
pixhawk's avatar
pixhawk committed
315 316
                if ( hDiff > dh )
                    dh = hDiff;
317 318 319
            } else {
                int wDiff = (majCnt - 1) * niceDist
                            - scaleWidget->minimumSizeHint().width();
pixhawk's avatar
pixhawk committed
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 375 376 377 378 379 380 381
                if ( wDiff > dw )
                    dw = wDiff;
            }
        }
    }
    return minimumSizeHint() + QSize(dw, dh);
}

/*!
  \brief Return a minimum size hint
*/
QSize QwtPlot::minimumSizeHint() const
{
    QSize hint = d_data->layout->minimumSizeHint(this);
    hint += QSize(2 * frameWidth(), 2 * frameWidth());

    return hint;
}

//! Resize and update internal layout
void QwtPlot::resizeEvent(QResizeEvent *e)
{
    QFrame::resizeEvent(e);
    updateLayout();
}

/*!
  \brief Redraw the plot

  If the autoReplot option is not set (which is the default)
  or if any curves are attached to raw data, the plot has to
  be refreshed explicitly in order to make changes visible.

  \sa setAutoReplot()
  \warning Calls canvas()->repaint, take care of infinite recursions
*/
void QwtPlot::replot()
{
    bool doAutoReplot = autoReplot();
    setAutoReplot(false);

    updateAxes();

    /*
      Maybe the layout needs to be updated, because of changed
      axes labels. We need to process them here before painting
      to avoid that scales and canvas get out of sync.
     */
#if QT_VERSION >= 0x040000
    QApplication::sendPostedEvents(this, QEvent::LayoutRequest);
#else
    QApplication::sendPostedEvents(this, QEvent::LayoutHint);
#endif

    QwtPlotCanvas &canvas = *d_data->canvas;

    canvas.invalidatePaintCache();

    /*
      In case of cached or packed painting the canvas
      is repainted completely and doesn't need to be erased.
     */
382 383
    const bool erase =
        !canvas.testPaintAttribute(QwtPlotCanvas::PaintPacked)
pixhawk's avatar
pixhawk committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        && !canvas.testPaintAttribute(QwtPlotCanvas::PaintCached);

#if QT_VERSION >= 0x040000
    const bool noBackgroundMode = canvas.testAttribute(Qt::WA_NoBackground);
    if ( !erase && !noBackgroundMode )
        canvas.setAttribute(Qt::WA_NoBackground, true);

    canvas.repaint(canvas.contentsRect());

    if ( !erase && !noBackgroundMode )
        canvas.setAttribute(Qt::WA_NoBackground, false);
#else
    canvas.repaint(canvas.contentsRect(), erase);
#endif

    setAutoReplot(doAutoReplot);
}

/*!
  \brief Adjust plot content to its current size.
  \sa resizeEvent()
*/
void QwtPlot::updateLayout()
{
    d_data->layout->activate(this, contentsRect());

    //
    // resize and show the visible widgets
    //
413
    if (!d_data->lblTitle->text().isEmpty()) {
pixhawk's avatar
pixhawk committed
414 415 416
        d_data->lblTitle->setGeometry(d_data->layout->titleRect());
        if (!d_data->lblTitle->isVisible())
            d_data->lblTitle->show();
417
    } else
pixhawk's avatar
pixhawk committed
418 419
        d_data->lblTitle->hide();

420 421
    for (int axisId = 0; axisId < axisCnt; axisId++ ) {
        if (axisEnabled(axisId) ) {
pixhawk's avatar
pixhawk committed
422 423
            axisWidget(axisId)->setGeometry(d_data->layout->scaleRect(axisId));

424
            if ( axisId == xBottom || axisId == xTop ) {
pixhawk's avatar
pixhawk committed
425 426 427 428 429
                QRegion r(d_data->layout->scaleRect(axisId));
                if ( axisEnabled(yLeft) )
                    r = r.subtract(QRegion(d_data->layout->scaleRect(yLeft)));
                if ( axisEnabled(yRight) )
                    r = r.subtract(QRegion(d_data->layout->scaleRect(yRight)));
430 431
                r.translate(-d_data->layout->scaleRect(axisId).x(),
                            -d_data->layout->scaleRect(axisId).y());
pixhawk's avatar
pixhawk committed
432 433 434 435 436

                axisWidget(axisId)->setMask(r);
            }
            if (!axisWidget(axisId)->isVisible())
                axisWidget(axisId)->show();
437
        } else
pixhawk's avatar
pixhawk committed
438 439 440
            axisWidget(axisId)->hide();
    }

441 442 443
    if ( d_data->legend &&
            d_data->layout->legendPosition() != ExternalLegend ) {
        if (d_data->legend->itemCount() > 0) {
pixhawk's avatar
pixhawk committed
444 445
            d_data->legend->setGeometry(d_data->layout->legendRect());
            d_data->legend->show();
446
        } else
pixhawk's avatar
pixhawk committed
447 448 449 450 451 452
            d_data->legend->hide();
    }

    d_data->canvas->setGeometry(d_data->layout->canvasRect());
}

453
/*!
pixhawk's avatar
pixhawk committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
   Update the focus tab order

   The order is changed so that the canvas will be in front of the
   first legend item, or behind the last legend item - depending
   on the position of the legend.
*/

void QwtPlot::updateTabOrder()
{
#if QT_VERSION >= 0x040000
    using namespace Qt; // QWidget::NoFocus/Qt::NoFocus
#else
    if ( d_data->canvas->focusPolicy() == NoFocus )
        return;
#endif
469 470 471
    if ( d_data->legend.isNull()
            || d_data->layout->legendPosition() == ExternalLegend
            || d_data->legend->legendItems().count() == 0 ) {
pixhawk's avatar
pixhawk committed
472 473 474
        return;
    }

475
    // Depending on the position of the legend the
pixhawk's avatar
pixhawk committed
476 477
    // tab order will be changed that the canvas is
    // next to the last legend item, or before
478
    // the first one.
pixhawk's avatar
pixhawk committed
479

480
    const bool canvasFirst =
pixhawk's avatar
pixhawk committed
481 482 483
        d_data->layout->legendPosition() == QwtPlot::BottomLegend ||
        d_data->layout->legendPosition() == QwtPlot::RightLegend;

484
    QWidget *previous = NULL;
pixhawk's avatar
pixhawk committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498

    QWidget *w;
#if QT_VERSION >= 0x040000
    w = d_data->canvas;
    while ( ( w = w->nextInFocusChain() ) != d_data->canvas )
#else
    if ( focusData() == NULL )
        return;

    while ( focusData()->next() != d_data->canvas );
    while ( (w = focusData()->next()) != d_data->canvas )
#endif
    {
        bool isLegendItem = false;
499 500
        if ( w->focusPolicy() != NoFocus
                && w->parent() && w->parent() == d_data->legend->contentsWidget() ) {
pixhawk's avatar
pixhawk committed
501 502 503
            isLegendItem = true;
        }

504
        if ( canvasFirst ) {
pixhawk's avatar
pixhawk committed
505 506 507 508
            if ( isLegendItem )
                break;

            previous = w;
509
        } else {
pixhawk's avatar
pixhawk committed
510 511
            if ( isLegendItem )
                previous = w;
512
            else {
pixhawk's avatar
pixhawk committed
513 514 515 516 517 518 519 520 521 522
                if ( previous )
                    break;
            }
        }
    }

    if ( previous && previous != d_data->canvas)
        setTabOrder(previous, d_data->canvas);
}

523
/*!
pixhawk's avatar
pixhawk committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537
  Redraw the canvas.
  \param painter Painter used for drawing

  \warning drawCanvas calls drawItems what is also used
           for printing. Applications that like to add individual
           plot items better overload drawItems()
  \sa drawItems()
*/
void QwtPlot::drawCanvas(QPainter *painter)
{
    QwtScaleMap maps[axisCnt];
    for ( int axisId = 0; axisId < axisCnt; axisId++ )
        maps[axisId] = canvasMap(axisId);

538 539
    drawItems(painter,
              d_data->canvas->contentsRect(), maps, QwtPlotPrintFilter());
pixhawk's avatar
pixhawk committed
540 541
}

542
/*!
pixhawk's avatar
pixhawk committed
543 544 545 546 547 548 549
  Redraw the canvas items.
  \param painter Painter used for drawing
  \param rect Bounding rectangle where to paint
  \param map QwtPlot::axisCnt maps, mapping between plot and paint device coordinates
  \param pfilter Plot print filter
*/

550 551 552
void QwtPlot::drawItems(QPainter *painter, const QRect &rect,
                        const QwtScaleMap map[axisCnt],
                        const QwtPlotPrintFilter &pfilter) const
pixhawk's avatar
pixhawk committed
553 554 555
{
    const QwtPlotItemList& itmList = itemList();
    for ( QwtPlotItemIterator it = itmList.begin();
556
            it != itmList.end(); ++it ) {
pixhawk's avatar
pixhawk committed
557
        QwtPlotItem *item = *it;
558
        if ( item && item->isVisible() ) {
pixhawk's avatar
pixhawk committed
559
            if ( !(pfilter.options() & QwtPlotPrintFilter::PrintGrid)
560
                    && item->rtti() == QwtPlotItem::Rtti_PlotGrid ) {
pixhawk's avatar
pixhawk committed
561 562 563 564 565 566 567
                continue;
            }

            painter->save();

#if QT_VERSION >= 0x040000
            painter->setRenderHint(QPainter::Antialiasing,
568
                                   item->testRenderHint(QwtPlotItem::RenderAntialiased) );
pixhawk's avatar
pixhawk committed
569 570
#endif

571 572 573
            item->draw(painter,
                       map[item->xAxis()], map[item->yAxis()],
                       rect);
pixhawk's avatar
pixhawk committed
574 575 576 577 578 579 580 581 582 583 584

            painter->restore();
        }
    }
}

/*!
  \param axisId Axis
  \return Map for the axis on the canvas. With this map pixel coordinates can
          translated to plot coordinates and vice versa.
  \sa QwtScaleMap, transform(), invTransform()
585

pixhawk's avatar
pixhawk committed
586 587 588 589 590 591 592 593 594 595 596 597
*/
QwtScaleMap QwtPlot::canvasMap(int axisId) const
{
    QwtScaleMap map;
    if ( !d_data->canvas )
        return map;

    map.setTransformation(axisScaleEngine(axisId)->transformation());

    const QwtScaleDiv *sd = axisScaleDiv(axisId);
    map.setScaleInterval(sd->lBound(), sd->hBound());

598
    if ( axisEnabled(axisId) ) {
pixhawk's avatar
pixhawk committed
599
        const QwtScaleWidget *s = axisWidget(axisId);
600
        if ( axisId == yLeft || axisId == yRight ) {
pixhawk's avatar
pixhawk committed
601 602 603
            int y = s->y() + s->startBorderDist() - d_data->canvas->y();
            int h = s->height() - s->startBorderDist() - s->endBorderDist();
            map.setPaintInterval(y + h, y);
604
        } else {
pixhawk's avatar
pixhawk committed
605 606 607 608
            int x = s->x() + s->startBorderDist() - d_data->canvas->x();
            int w = s->width() - s->startBorderDist() - s->endBorderDist();
            map.setPaintInterval(x, x + w);
        }
609
    } else {
pixhawk's avatar
pixhawk committed
610 611 612
        const int margin = plotLayout()->canvasMargin(axisId);

        const QRect &canvasRect = d_data->canvas->contentsRect();
613 614 615 616 617 618
        if ( axisId == yLeft || axisId == yRight ) {
            map.setPaintInterval(canvasRect.bottom() - margin,
                                 canvasRect.top() + margin);
        } else {
            map.setPaintInterval(canvasRect.left() + margin,
                                 canvasRect.right() - margin);
pixhawk's avatar
pixhawk committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        }
    }
    return map;
}

/*!
  Change the margin of the plot. The margin is the space
  around all components.

  \param margin new margin
  \sa QwtPlotLayout::setMargin(), margin(), plotLayout()
*/
void QwtPlot::setMargin(int margin)
{
    if ( margin < 0 )
        margin = 0;

636
    if ( margin != d_data->layout->margin() ) {
pixhawk's avatar
pixhawk committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
        d_data->layout->setMargin(margin);
        updateLayout();
    }
}

/*!
    \return margin
    \sa setMargin(), QwtPlotLayout::margin(), plotLayout()
*/
int QwtPlot::margin() const
{
    return d_data->layout->margin();
}

/*!
  \brief Change the background of the plotting area
653 654

  Sets c to QColorGroup::Background of all colorgroups of
pixhawk's avatar
pixhawk committed
655 656 657 658 659 660 661 662
  the palette of the canvas. Using canvas()->setPalette()
  is a more powerful way to set these colors.
  \param c new background color
*/
void QwtPlot::setCanvasBackground(const QColor &c)
{
    QPalette p = d_data->canvas->palette();

663
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
664 665 666 667 668 669 670 671 672 673 674 675 676
#if QT_VERSION < 0x040000
        p.setColor((QPalette::ColorGroup)i, QColorGroup::Background, c);
#else
        p.setColor((QPalette::ColorGroup)i, QPalette::Background, c);
#endif
    }

    canvas()->setPalette(p);
}

/*!
  Nothing else than: canvas()->palette().color(
        QPalette::Normal, QColorGroup::Background);
677

pixhawk's avatar
pixhawk committed
678 679 680 681 682 683
  \return the background color of the plotting area.
*/
const QColor & QwtPlot::canvasBackground() const
{
#if QT_VERSION < 0x040000
    return canvas()->palette().color(
684
               QPalette::Normal, QColorGroup::Background);
pixhawk's avatar
pixhawk committed
685 686
#else
    return canvas()->palette().color(
687
               QPalette::Normal, QPalette::Background);
pixhawk's avatar
pixhawk committed
688 689 690 691 692
#endif
}

/*!
  \brief Change the border width of the plotting area
693
  Nothing else than canvas()->setLineWidth(w),
pixhawk's avatar
pixhawk committed
694 695 696 697 698 699 700 701
  left for compatibility only.
  \param w new border width
*/
void QwtPlot::setCanvasLineWidth(int w)
{
    canvas()->setLineWidth(w);
    updateLayout();
}
702 703 704

/*!
  Nothing else than: canvas()->lineWidth(),
pixhawk's avatar
pixhawk committed
705 706 707 708
  left for compatibility only.
  \return the border width of the plotting area
*/
int QwtPlot::canvasLineWidth() const
709
{
pixhawk's avatar
pixhawk committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    return canvas()->lineWidth();
}

/*!
  \return \c true if the specified axis exists, otherwise \c false
  \param axisId axis index
 */
bool QwtPlot::axisValid(int axisId)
{
    return ((axisId >= QwtPlot::yLeft) && (axisId < QwtPlot::axisCnt));
}

/*!
  Called internally when the legend has been clicked on.
  Emits a legendClicked() signal.
*/
void QwtPlot::legendItemClicked()
{
728 729
    if ( d_data->legend && sender()->isWidgetType() ) {
        QwtPlotItem *plotItem =
pixhawk's avatar
pixhawk committed
730 731 732 733 734 735 736 737 738 739 740 741
            (QwtPlotItem*)d_data->legend->find((QWidget *)sender());
        if ( plotItem )
            emit legendClicked(plotItem);
    }
}

/*!
  Called internally when the legend has been checked
  Emits a legendClicked() signal.
*/
void QwtPlot::legendItemChecked(bool on)
{
742 743
    if ( d_data->legend && sender()->isWidgetType() ) {
        QwtPlotItem *plotItem =
pixhawk's avatar
pixhawk committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
            (QwtPlotItem*)d_data->legend->find((QWidget *)sender());
        if ( plotItem )
            emit legendChecked(plotItem, on);
    }
}

//! Remove all curves and markers
void QwtPlot::clear()
{
    detachItems(QwtPlotItem::Rtti_PlotCurve);
    detachItems(QwtPlotItem::Rtti_PlotMarker);
}

/*!
  \brief Insert a legend

  If the position legend is \c QwtPlot::LeftLegend or \c QwtPlot::RightLegend
761 762
  the legend will be organized in one column from top to down.
  Otherwise the legend items will be placed in a table
pixhawk's avatar
pixhawk committed
763 764
  with a best fit number of columns from left to right.

765 766
  If pos != QwtPlot::ExternalLegend the plot widget will become
  parent of the legend. It will be deleted when the plot is deleted,
pixhawk's avatar
pixhawk committed
767
  or another legend is set with insertLegend().
768

pixhawk's avatar
pixhawk committed
769 770 771
  \param legend Legend
  \param pos The legend's position. For top/left position the number
             of colums will be limited to 1, otherwise it will be set to
772
             unlimited.
pixhawk's avatar
pixhawk committed
773 774 775 776 777 778 779 780

  \param ratio Ratio between legend and the bounding rect
               of title, canvas and axes. The legend will be shrinked
               if it would need more space than the given ratio.
               The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0
               it will be reset to the default ratio.
               The default vertical/horizontal ratio is 0.33/0.5.

781
  \sa legend(), QwtPlotLayout::legendPosition(),
pixhawk's avatar
pixhawk committed
782 783
      QwtPlotLayout::setLegendPosition()
*/
784 785
void QwtPlot::insertLegend(QwtLegend *legend,
                           QwtPlot::LegendPosition pos, double ratio)
pixhawk's avatar
pixhawk committed
786 787 788
{
    d_data->layout->setLegendPosition(pos, ratio);

789
    if ( legend != d_data->legend ) {
pixhawk's avatar
pixhawk committed
790 791 792 793 794
        if ( d_data->legend && d_data->legend->parent() == this )
            delete d_data->legend;

        d_data->legend = legend;

795 796 797
        if ( d_data->legend ) {
            if ( pos != ExternalLegend ) {
                if ( d_data->legend->parent() != this ) {
pixhawk's avatar
pixhawk committed
798 799 800 801 802 803 804 805 806 807
#if QT_VERSION < 0x040000
                    d_data->legend->reparent(this, QPoint(0, 0));
#else
                    d_data->legend->setParent(this);
#endif
                }
            }

            const QwtPlotItemList& itmList = itemList();
            for ( QwtPlotItemIterator it = itmList.begin();
808
                    it != itmList.end(); ++it ) {
pixhawk's avatar
pixhawk committed
809 810 811 812
                (*it)->updateLegend(d_data->legend);
            }

            QLayout *l = d_data->legend->contentsWidget()->layout();
813
            if ( l && l->inherits("QwtDynGridLayout") ) {
pixhawk's avatar
pixhawk committed
814
                QwtDynGridLayout *tl = (QwtDynGridLayout *)l;
815 816 817 818 819 820 821 822 823 824 825
                switch(d_data->layout->legendPosition()) {
                case LeftLegend:
                case RightLegend:
                    tl->setMaxCols(1); // 1 column: align vertical
                    break;
                case TopLegend:
                case BottomLegend:
                    tl->setMaxCols(0); // unlimited
                    break;
                case ExternalLegend:
                    break;
pixhawk's avatar
pixhawk committed
826 827 828 829 830 831 832 833
                }
            }
        }
        updateTabOrder();
    }

    updateLayout();
}