qwt_legend.cpp 15.5 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
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

// vim: expandtab

12 13
#include <qapplication.h>
#include <qmap.h>
pixhawk's avatar
pixhawk committed
14
#if QT_VERSION >= 0x040000
15
#include <qscrollbar.h>
pixhawk's avatar
pixhawk committed
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
#endif
#include "qwt_math.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_legend_itemmanager.h"
#include "qwt_legend_item.h"
#include "qwt_legend.h"

class QwtLegend::PrivateData
{
public:
    class LegendMap
    {
    public:
        void insert(const QwtLegendItemManager *, QWidget *);

        void remove(const QwtLegendItemManager *);
        void remove(QWidget *);

        void clear();

        uint count() const;

        inline const QWidget *find(const QwtLegendItemManager *) const;
        inline QWidget *find(const QwtLegendItemManager *);

        inline const QwtLegendItemManager *find(const QWidget *) const;
        inline QwtLegendItemManager *find(const QWidget *);

        const QMap<QWidget *, const QwtLegendItemManager *> &widgetMap() const;
        QMap<QWidget *, const QwtLegendItemManager *> &widgetMap();

    private:
        QMap<QWidget *, const QwtLegendItemManager *> d_widgetMap;
        QMap<const QwtLegendItemManager *, QWidget *> d_itemMap;
    };

    QwtLegend::LegendItemMode itemMode;
    QwtLegend::LegendDisplayPolicy displayPolicy;
    int identifierMode;

    LegendMap map;

    class LegendView;
    LegendView *view;
};

#if QT_VERSION < 0x040000
#include <qscrollview.h>

class QwtLegend::PrivateData::LegendView: public QScrollView
{
public:
    LegendView(QWidget *parent):
69
        QScrollView(parent) {
pixhawk's avatar
pixhawk committed
70 71 72 73 74 75 76 77 78
        setResizePolicy(Manual);

        viewport()->setBackgroundMode(Qt::NoBackground); // Avoid flicker

        contentsWidget = new QWidget(viewport());

        addChild(contentsWidget);
    }

79
    void viewportResizeEvent(QResizeEvent *e) {
pixhawk's avatar
pixhawk committed
80 81 82 83 84 85
        QScrollView::viewportResizeEvent(e);

        // It's not safe to update the layout now, because
        // we are in an internal update of the scrollview framework.
        // So we delay the update by posting a LayoutHint.

86 87
        QApplication::postEvent(contentsWidget,
                                new QEvent(QEvent::LayoutHint));
pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94 95 96 97 98 99 100
    }

    QWidget *contentsWidget;
};

#else // QT_VERSION >= 0x040000

#include <qscrollarea.h>

class QwtLegend::PrivateData::LegendView: public QScrollArea
{
public:
    LegendView(QWidget *parent):
101
        QScrollArea(parent) {
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108
        contentsWidget = new QWidget(this);

        setWidget(contentsWidget);
        setWidgetResizable(false);
        setFocusPolicy(Qt::NoFocus);
    }

109
    virtual bool viewportEvent(QEvent *e) {
pixhawk's avatar
pixhawk committed
110 111
        bool ok = QScrollArea::viewportEvent(e);

112
        if ( e->type() == QEvent::Resize ) {
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118
            QEvent event(QEvent::LayoutRequest);
            QApplication::sendEvent(contentsWidget, &event);
        }
        return ok;
    }

119
    QSize viewportSize(int w, int h) const {
pixhawk's avatar
pixhawk committed
120 121
        const int sbHeight = horizontalScrollBar()->sizeHint().height();
        const int sbWidth = verticalScrollBar()->sizeHint().width();
122

pixhawk's avatar
pixhawk committed
123 124 125 126 127 128 129 130 131
        const int cw = contentsRect().width();
        const int ch = contentsRect().height();

        int vw = cw;
        int vh = ch;

        if ( w > vw )
            vh -= sbHeight;

132
        if ( h > vh ) {
pixhawk's avatar
pixhawk committed
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
            vw -= sbWidth;
            if ( w > vw && vh == ch )
                vh -= sbHeight;
        }
        return QSize(vw, vh);
    }

    QWidget *contentsWidget;
};

#endif


void QwtLegend::PrivateData::LegendMap::insert(
    const QwtLegendItemManager *item, QWidget *widget)
{
    d_itemMap.insert(item, widget);
    d_widgetMap.insert(widget, item);
}

void QwtLegend::PrivateData::LegendMap::remove(const QwtLegendItemManager *item)
{
    QWidget *widget = d_itemMap[item];
    d_itemMap.remove(item);
    d_widgetMap.remove(widget);
}

void QwtLegend::PrivateData::LegendMap::remove(QWidget *widget)
{
    const QwtLegendItemManager *item = d_widgetMap[widget];
    d_itemMap.remove(item);
    d_widgetMap.remove(widget);
}

void QwtLegend::PrivateData::LegendMap::clear()
{
169

pixhawk's avatar
pixhawk committed
170 171 172 173 174 175 176 177 178 179
    /*
       We can't delete the widgets in the following loop, because
       we would get ChildRemoved events, changing d_itemMap, while
       we are iterating.
     */

#if QT_VERSION < 0x040000
    QValueList<QWidget *> widgets;

    QMap<const QwtLegendItemManager *, QWidget *>::const_iterator it;
180
    for ( it = d_itemMap.begin(); it != d_itemMap.end(); ++it )
pixhawk's avatar
pixhawk committed
181 182 183 184 185
        widgets.append(it.data());
#else
    QList<QWidget *> widgets;

    QMap<const QwtLegendItemManager *, QWidget *>::const_iterator it;
186
    for ( it = d_itemMap.begin(); it != d_itemMap.end(); ++it )
pixhawk's avatar
pixhawk committed
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        widgets.append(it.value());
#endif

    d_itemMap.clear();
    d_widgetMap.clear();

    for ( int i = 0; i < (int)widgets.size(); i++ )
        delete widgets[i];
}

uint QwtLegend::PrivateData::LegendMap::count() const
{
    return d_itemMap.count();
}

inline const QWidget *QwtLegend::PrivateData::LegendMap::find(const QwtLegendItemManager *item) const
{
    if ( !d_itemMap.contains((QwtLegendItemManager *)item) )
        return NULL;

    return d_itemMap[(QwtLegendItemManager *)item];
}

inline QWidget *QwtLegend::PrivateData::LegendMap::find(const QwtLegendItemManager *item)
{
    if ( !d_itemMap.contains((QwtLegendItemManager *)item) )
        return NULL;

    return d_itemMap[(QwtLegendItemManager *)item];
}

inline const QwtLegendItemManager *QwtLegend::PrivateData::LegendMap::find(
    const QWidget *widget) const
{
    if ( !d_widgetMap.contains((QWidget *)widget) )
        return NULL;

    return d_widgetMap[(QWidget *)widget];
}

inline QwtLegendItemManager *QwtLegend::PrivateData::LegendMap::find(
    const QWidget *widget)
{
    if ( !d_widgetMap.contains((QWidget *)widget) )
        return NULL;

    return (QwtLegendItemManager *)d_widgetMap[(QWidget *)widget];
}

inline const QMap<QWidget *, const QwtLegendItemManager *> &
237
QwtLegend::PrivateData::LegendMap::widgetMap() const
pixhawk's avatar
pixhawk committed
238 239
{
    return d_widgetMap;
240
}
pixhawk's avatar
pixhawk committed
241 242

inline QMap<QWidget *, const QwtLegendItemManager *> &
243
QwtLegend::PrivateData::LegendMap::widgetMap()
pixhawk's avatar
pixhawk committed
244 245
{
    return d_widgetMap;
246
}
pixhawk's avatar
pixhawk committed
247 248 249 250

/*!
  \param parent Parent widget
*/
251
QwtLegend::QwtLegend(QWidget *parent):
pixhawk's avatar
pixhawk committed
252 253 254 255 256 257 258
    QFrame(parent)
{
    setFrameStyle(NoFrame);

    d_data = new QwtLegend::PrivateData;
    d_data->itemMode = QwtLegend::ReadOnlyItem;
    d_data->displayPolicy = QwtLegend::AutoIdentifier;
259 260
    d_data->identifierMode = QwtLegendItem::ShowLine |
                             QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText;
pixhawk's avatar
pixhawk committed
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

    d_data->view = new QwtLegend::PrivateData::LegendView(this);
    d_data->view->setFrameStyle(NoFrame);

    QwtDynGridLayout *layout = new QwtDynGridLayout(
        d_data->view->contentsWidget);
#if QT_VERSION < 0x040000
    layout->setAutoAdd(true);
#endif
    layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    d_data->view->contentsWidget->installEventFilter(this);
}

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

/*!
  Set the legend display policy to:

  \param policy Legend display policy
  \param mode Identifier mode (or'd ShowLine, ShowSymbol, ShowText)

  \sa displayPolicy, LegendDisplayPolicy
*/
void QwtLegend::setDisplayPolicy(LegendDisplayPolicy policy, int mode)
{
    d_data->displayPolicy = policy;
    if (-1 != mode)
293
        d_data->identifierMode = mode;
pixhawk's avatar
pixhawk committed
294

295
    QMap<QWidget *, const QwtLegendItemManager *> &map =
pixhawk's avatar
pixhawk committed
296 297 298
        d_data->map.widgetMap();

    QMap<QWidget *, const QwtLegendItemManager *>::iterator it;
299
    for ( it = map.begin(); it != map.end(); ++it ) {
pixhawk's avatar
pixhawk committed
300 301 302 303 304 305 306 307 308 309
#if QT_VERSION < 0x040000
        QwtLegendItemManager *item = (QwtLegendItemManager *)it.data();
#else
        QwtLegendItemManager *item = (QwtLegendItemManager *)it.value();
#endif
        if ( item )
            item->updateLegend(this);
    }
}

310
/*!
pixhawk's avatar
pixhawk committed
311 312 313
  \return the legend display policy.
  Default is LegendDisplayPolicy::Auto.
  \sa setDisplayPolicy, LegendDisplayPolicy
314
*/
pixhawk's avatar
pixhawk committed
315

316 317 318
QwtLegend::LegendDisplayPolicy QwtLegend::displayPolicy() const
{
    return d_data->displayPolicy;
pixhawk's avatar
pixhawk committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}

void QwtLegend::setItemMode(LegendItemMode mode)
{
    d_data->itemMode = mode;
}

QwtLegend::LegendItemMode QwtLegend::itemMode() const
{
    return d_data->itemMode;
}

/*!
  \return the IdentifierMode to be used in combination with
  LegendDisplayPolicy::Fixed.

  Default is ShowLine | ShowSymbol | ShowText.
*/

int QwtLegend::identifierMode() const
{
    return d_data->identifierMode;
}

343
/*!
pixhawk's avatar
pixhawk committed
344 345 346
  The contents widget is the only child of the viewport() and
  the parent widget of all legend items.
*/
347 348 349
QWidget *QwtLegend::contentsWidget()
{
    return d_data->view->contentsWidget;
pixhawk's avatar
pixhawk committed
350 351 352 353 354 355 356 357 358 359 360 361
}

QScrollBar *QwtLegend::horizontalScrollBar() const
{
    return d_data->view->horizontalScrollBar();
}

QScrollBar *QwtLegend::verticalScrollBar() const
{
    return d_data->view->verticalScrollBar();
}

362
/*!
pixhawk's avatar
pixhawk committed
363 364 365 366
  The contents widget is the only child of the viewport() and
  the parent widget of all legend items.
*/

367 368 369
const QWidget *QwtLegend::contentsWidget() const
{
    return d_data->view->contentsWidget;
pixhawk's avatar
pixhawk committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

/*!
  Insert a new item for a plot item
  \param plotItem Plot item
  \param legendItem New legend item
  \note The parent of item will be changed to QwtLegend::contentsWidget()
*/
void QwtLegend::insert(const QwtLegendItemManager *plotItem, QWidget *legendItem)
{
    if ( legendItem == NULL || plotItem == NULL )
        return;

    QWidget *contentsWidget = d_data->view->contentsWidget;

385
    if ( legendItem->parent() != contentsWidget ) {
pixhawk's avatar
pixhawk committed
386 387 388 389 390 391 392 393 394 395 396 397 398
#if QT_VERSION >= 0x040000
        legendItem->setParent(contentsWidget);
#else
        legendItem->reparent(contentsWidget, QPoint(0, 0));
#endif
    }

    legendItem->show();

    d_data->map.insert(plotItem, legendItem);

    layoutContents();

399
    if ( contentsWidget->layout() ) {
pixhawk's avatar
pixhawk committed
400 401 402 403 404 405 406 407 408
#if QT_VERSION >= 0x040000
        contentsWidget->layout()->addWidget(legendItem);
#endif

        // set tab focus chain

        QWidget *w = NULL;

#if QT_VERSION < 0x040000
409
        QLayoutIterator layoutIterator =
pixhawk's avatar
pixhawk committed
410 411
            contentsWidget->layout()->iterator();
        for ( QLayoutItem *item = layoutIterator.current();
412
                item != 0; item = ++layoutIterator) {
pixhawk's avatar
pixhawk committed
413
#else
414
        for (int i = 0; i < contentsWidget->layout()->count(); i++) {
pixhawk's avatar
pixhawk committed
415 416
            QLayoutItem *item = contentsWidget->layout()->itemAt(i);
#endif
417
            if ( w && item->widget() ) {
pixhawk's avatar
pixhawk committed
418 419 420 421 422
                QWidget::setTabOrder(w, item->widget());
                w = item->widget();
            }
        }
    }
423 424 425 426 427 428 429
    if ( parentWidget() && parentWidget()->layout() == NULL ) {
        /*
           updateGeometry() doesn't post LayoutRequest in certain
           situations, like when we are hidden. But we want the
           parent widget notified, so it can show/hide the legend
           depending on its items.
         */
pixhawk's avatar
pixhawk committed
430 431
#if QT_VERSION < 0x040000
        QApplication::postEvent(parentWidget(),
432
                                new QEvent(QEvent::LayoutHint));
pixhawk's avatar
pixhawk committed
433 434
#else
        QApplication::postEvent(parentWidget(),
435
                                new QEvent(QEvent::LayoutRequest));
pixhawk's avatar
pixhawk committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
#endif
    }
}

/*!
  Find the widget that represents a plot item

  \param plotItem Plot item
  \return Widget on the legend, or NULL
*/
QWidget *QwtLegend::find(const QwtLegendItemManager *plotItem) const
{
    return d_data->map.find(plotItem);
}

/*!
  Find the widget that represents a plot item

  \param plotItem Plot item
  \return Widget on the legend, or NULL
*/
QwtLegendItemManager *QwtLegend::find(const QWidget *legendItem) const
{
    return d_data->map.find(legendItem);
}

462 463
/*!
   Find the corresponding item for a plotItem and remove it
pixhawk's avatar
pixhawk committed
464 465 466 467 468
   from the item list.

   \param plotItem Plot item
*/
void QwtLegend::remove(const QwtLegendItemManager *plotItem)
469
{
pixhawk's avatar
pixhawk committed
470
    QWidget *legendItem = d_data->map.find(plotItem);
471
    d_data->map.remove(legendItem);
pixhawk's avatar
pixhawk committed
472 473 474 475 476 477 478 479 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 506 507 508 509 510
    delete legendItem;
}

//! Remove all items.
void QwtLegend::clear()
{
#if QT_VERSION < 0x040000
    bool doUpdate = isUpdatesEnabled();
#else
    bool doUpdate = updatesEnabled();
#endif
    setUpdatesEnabled(false);

    d_data->map.clear();

    setUpdatesEnabled(doUpdate);
    update();
}

//! Return a size hint.
QSize QwtLegend::sizeHint() const
{
    QSize hint = d_data->view->contentsWidget->sizeHint();
    hint += QSize(2 * frameWidth(), 2 * frameWidth());

    return hint;
}

/*!
  \return The preferred height, for the width w.
  \param width Width
*/
int QwtLegend::heightForWidth(int width) const
{
    width -= 2 * frameWidth();

    int h = d_data->view->contentsWidget->heightForWidth(width);
#if QT_VERSION < 0x040000

511
    // Asking the layout is the default implementation in Qt4
pixhawk's avatar
pixhawk committed
512

513
    if ( h <= 0 ) {
pixhawk's avatar
pixhawk committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        QLayout *l = d_data->view->contentsWidget->layout();
        if ( l && l->hasHeightForWidth() )
            h = l->heightForWidth(width);
    }
#endif
    if ( h >= 0 )
        h += 2 * frameWidth();

    return h;
}

/*!
  Adjust contents widget and item layout to the size of the viewport().
*/
void QwtLegend::layoutContents()
{
    const QSize visibleSize = d_data->view->viewport()->size();

    const QLayout *l = d_data->view->contentsWidget->layout();
533
    if ( l && l->inherits("QwtDynGridLayout") ) {
pixhawk's avatar
pixhawk committed
534 535 536 537 538 539 540 541
        const QwtDynGridLayout *tl = (const QwtDynGridLayout *)l;

        const int minW = int(tl->maxItemWidth()) + 2 * tl->margin();

        int w = qwtMax(visibleSize.width(), minW);
        int h = qwtMax(tl->heightForWidth(w), visibleSize.height());

        const int vpWidth = d_data->view->viewportSize(w, h).width();
542
        if ( w > vpWidth ) {
pixhawk's avatar
pixhawk committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
            w = qwtMax(vpWidth, minW);
            h = qwtMax(tl->heightForWidth(w), visibleSize.height());
        }

        d_data->view->contentsWidget->resize(w, h);
#if QT_VERSION < 0x040000
        d_data->view->resizeContents(w, h);
#endif
    }
}

/*
  Filter layout related events of QwtLegend::contentsWidget().

  \param o Object to be filtered
  \param e Event
*/

bool QwtLegend::eventFilter(QObject *o, QEvent *e)
{
563 564 565 566 567 568 569 570
    if ( o == d_data->view->contentsWidget ) {
        switch(e->type()) {
        case QEvent::ChildRemoved: {
            const QChildEvent *ce = (const QChildEvent *)e;
            if ( ce->child()->isWidgetType() )
                d_data->map.remove((QWidget *)ce->child());
            break;
        }
pixhawk's avatar
pixhawk committed
571
#if QT_VERSION < 0x040000
572
        case QEvent::LayoutHint:
pixhawk's avatar
pixhawk committed
573
#else
574
        case QEvent::LayoutRequest:
pixhawk's avatar
pixhawk committed
575 576 577 578 579 580
#endif
            {
                layoutContents();
                break;
            }
#if QT_VERSION < 0x040000
581 582 583 584
        case QEvent::Resize: {
            updateGeometry();
            break;
        }
pixhawk's avatar
pixhawk committed
585
#endif
586 587
        default:
            break;
pixhawk's avatar
pixhawk committed
588 589
        }
    }
590

pixhawk's avatar
pixhawk committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    return QFrame::eventFilter(o, e);
}


//! Return true, if there are no legend items.
bool QwtLegend::isEmpty() const
{
    return d_data->map.count() == 0;
}

//! Return the number of legend items.
uint QwtLegend::itemCount() const
{
    return d_data->map.count();
}

#if QT_VERSION < 0x040000
QValueList<QWidget *> QwtLegend::legendItems() const
#else
QList<QWidget *> QwtLegend::legendItems() const
#endif
{
613
    const QMap<QWidget *, const QwtLegendItemManager *> &map =
pixhawk's avatar
pixhawk committed
614 615 616 617 618 619 620 621 622
        d_data->map.widgetMap();

#if QT_VERSION < 0x040000
    QValueList<QWidget *> list;
#else
    QList<QWidget *> list;
#endif

    QMap<QWidget *, const QwtLegendItemManager *>::const_iterator it;
623
    for ( it = map.begin(); it != map.end(); ++it )
pixhawk's avatar
pixhawk committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637
        list += it.key();

    return list;
}

/*!
   Resize event
   \param e Event
*/
void QwtLegend::resizeEvent(QResizeEvent *e)
{
    QFrame::resizeEvent(e);
    d_data->view->setGeometry(contentsRect());
}