qwt_plot_printfilter.cpp 15.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 28 29 30 31 32 33 34
 * 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

#include <qmap.h>
#include "qwt_plot.h"
#include "qwt_plot_grid.h"
#include "qwt_plot_curve.h"
#include "qwt_plot_marker.h"
#include "qwt_symbol.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_scale_widget.h"
#include "qwt_text_label.h"
#include "qwt_plot_printfilter.h"

#if QT_VERSION < 0x040000
typedef QColorGroup Palette;
#else
typedef QPalette Palette;
#endif

class QwtPlotPrintFilter::PrivateData
{
public:
    PrivateData():
        options(QwtPlotPrintFilter::PrintAll),
35
        cache(NULL) {
pixhawk's avatar
pixhawk committed
36 37
    }

38
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
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 77
        delete cache;
    }

    class Cache
    {
    public:
        QColor titleColor;
        QFont titleFont;

        QwtText scaleTitle[QwtPlot::axisCnt];
        QColor scaleColor[QwtPlot::axisCnt];
        QFont scaleFont[QwtPlot::axisCnt];
        QColor scaleTitleColor[QwtPlot::axisCnt];
        QFont scaleTitleFont[QwtPlot::axisCnt];

        QMap<QWidget *, QFont> legendFonts;

        QColor widgetBackground;
        QColor canvasBackground;
        QColor gridColors[2];

        QMap<const QwtPlotItem *, QColor> curveColors;
        QMap<const QwtPlotItem *, QColor> curveSymbolBrushColors;
        QMap<const QwtPlotItem *, QColor> curveSymbolPenColors;

        QMap<const QwtPlotItem *, QFont> markerFonts;
        QMap<const QwtPlotItem *, QColor> markerLabelColors;
        QMap<const QwtPlotItem *, QColor> markerLineColors;
        QMap<const QwtPlotItem *, QColor> markerSymbolBrushColors;
        QMap<const QwtPlotItem *, QColor> markerSymbolPenColors;
    };

    int options;
    mutable Cache *cache;
};


/*!
  Sets filter options to PrintAll
78
*/
pixhawk's avatar
pixhawk committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

QwtPlotPrintFilter::QwtPlotPrintFilter()
{
    d_data = new PrivateData;
}

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

/*!
  \brief Set plot print options
  \param options Or'd QwtPlotPrintFilter::Options values

  \sa options()
*/
97 98 99
void QwtPlotPrintFilter::setOptions(int options)
{
    d_data->options = options;
pixhawk's avatar
pixhawk committed
100 101
}

102
/*!
pixhawk's avatar
pixhawk committed
103 104 105
  \brief Get plot print options
  \sa setOptions()
*/
106 107 108
int QwtPlotPrintFilter::options() const
{
    return d_data->options;
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116
}

/*!
  \brief Modifies a color for printing
  \param c Color to be modified
  \param item Type of item where the color belongs
  \return Modified color.

117 118
  In case of !(QwtPlotPrintFilter::options() & PrintBackground)
  MajorGrid is modified to Qt::darkGray, MinorGrid to Qt::gray.
pixhawk's avatar
pixhawk committed
119 120 121 122 123
  All other colors are returned unmodified.
*/

QColor QwtPlotPrintFilter::color(const QColor &c, Item item) const
{
124 125 126 127 128 129 130 131
    if ( !(options() & PrintBackground)) {
        switch(item) {
        case MajorGrid:
            return Qt::darkGray;
        case MinorGrid:
            return Qt::gray;
        default:
            ;
pixhawk's avatar
pixhawk committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        }
    }
    return c;
}

/*!
  \brief Modifies a font for printing
  \param f Font to be modified
  \param item Type of item where the font belongs

  All fonts are returned unmodified
*/

QFont QwtPlotPrintFilter::font(const QFont &f, Item) const
{
    return f;
}

150
/*!
pixhawk's avatar
pixhawk committed
151 152 153 154 155 156 157 158 159 160 161 162 163
  Change color and fonts of a plot
  \sa apply
*/
void QwtPlotPrintFilter::apply(QwtPlot *plot) const
{
    const bool doAutoReplot = plot->autoReplot();
    plot->setAutoReplot(false);

    delete d_data->cache;
    d_data->cache = new PrivateData::Cache;

    PrivateData::Cache &cache = *d_data->cache;

164
    if ( plot->titleLabel() ) {
pixhawk's avatar
pixhawk committed
165 166
        QPalette palette = plot->titleLabel()->palette();
        cache.titleColor = palette.color(
167
                               QPalette::Active, Palette::Text);
pixhawk's avatar
pixhawk committed
168 169 170 171 172 173 174
        palette.setColor(QPalette::Active, Palette::Text,
                         color(cache.titleColor, Title));
        plot->titleLabel()->setPalette(palette);

        cache.titleFont = plot->titleLabel()->font();
        plot->titleLabel()->setFont(font(cache.titleFont, Title));
    }
175
    if ( plot->legend() ) {
pixhawk's avatar
pixhawk committed
176 177 178
#if QT_VERSION < 0x040000
        QValueList<QWidget *> list = plot->legend()->legendItems();
        for ( QValueListIterator<QWidget *> it = list.begin();
179
                it != list.end(); ++it )
pixhawk's avatar
pixhawk committed
180 181 182
#else
        QList<QWidget *> list = plot->legend()->legendItems();
        for ( QList<QWidget*>::iterator it = list.begin();
183
                it != list.end(); ++it )
pixhawk's avatar
pixhawk committed
184 185 186 187 188 189 190
#endif
        {
            QWidget *w = *it;

            cache.legendFonts.insert(w, w->font());
            w->setFont(font(w->font(), Legend));

191
            if ( w->inherits("QwtLegendItem") ) {
pixhawk's avatar
pixhawk committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
                QwtLegendItem *label = (QwtLegendItem *)w;

                QwtSymbol symbol = label->symbol();
                QPen pen = symbol.pen();
                QBrush brush = symbol.brush();

                pen.setColor(color(pen.color(), CurveSymbol));
                brush.setColor(color(brush.color(), CurveSymbol));

                symbol.setPen(pen);
                symbol.setBrush(brush);
                label->setSymbol(symbol);

                pen = label->curvePen();
                pen.setColor(color(pen.color(), Curve));
                label->setCurvePen(pen);
            }
        }
    }
211
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
212
        QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
213
        if ( scaleWidget ) {
pixhawk's avatar
pixhawk committed
214
            cache.scaleColor[axis] = scaleWidget->palette().color(
215
                                         QPalette::Active, Palette::Foreground);
pixhawk's avatar
pixhawk committed
216 217 218 219 220 221 222 223 224 225 226
            QPalette palette = scaleWidget->palette();
            palette.setColor(QPalette::Active, Palette::Foreground,
                             color(cache.scaleColor[axis], AxisScale));
            scaleWidget->setPalette(palette);

            cache.scaleFont[axis] = scaleWidget->font();
            scaleWidget->setFont(font(cache.scaleFont[axis], AxisScale));

            cache.scaleTitle[axis] = scaleWidget->title();

            QwtText scaleTitle = scaleWidget->title();
227
            if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextColor) ) {
pixhawk's avatar
pixhawk committed
228 229 230 231 232
                cache.scaleTitleColor[axis] = scaleTitle.color();
                scaleTitle.setColor(
                    color(cache.scaleTitleColor[axis], AxisTitle));
            }

233
            if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextFont) ) {
pixhawk's avatar
pixhawk committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                cache.scaleTitleFont[axis] = scaleTitle.font();
                scaleTitle.setFont(
                    font(cache.scaleTitleFont[axis], AxisTitle));
            }

            scaleWidget->setTitle(scaleTitle);

            int startDist, endDist;
            scaleWidget->getBorderDistHint(startDist, endDist);
            scaleWidget->setBorderDist(startDist, endDist);
        }
    }


    QPalette p = plot->palette();
    cache.widgetBackground = plot->palette().color(
250 251 252
                                 QPalette::Active, Palette::Background);
    p.setColor(QPalette::Active, Palette::Background,
               color(cache.widgetBackground, WidgetBackground));
pixhawk's avatar
pixhawk committed
253 254 255 256 257 258 259
    plot->setPalette(p);

    cache.canvasBackground = plot->canvasBackground();
    plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));

    const QwtPlotItemList& itmList = plot->itemList();
    for ( QwtPlotItemIterator it = itmList.begin();
260
            it != itmList.end(); ++it ) {
pixhawk's avatar
pixhawk committed
261 262 263 264 265 266 267 268 269 270
        apply(*it);
    }

    plot->setAutoReplot(doAutoReplot);
}

void QwtPlotPrintFilter::apply(QwtPlotItem *item) const
{
    PrivateData::Cache &cache = *d_data->cache;

271 272 273
    switch(item->rtti()) {
    case QwtPlotItem::Rtti_PlotGrid: {
        QwtPlotGrid *grid = (QwtPlotGrid *)item;
pixhawk's avatar
pixhawk committed
274

275 276 277 278
        QPen pen = grid->majPen();
        cache.gridColors[0] = pen.color();
        pen.setColor(color(pen.color(), MajorGrid));
        grid->setMajPen(pen);
pixhawk's avatar
pixhawk committed
279

280 281 282 283
        pen = grid->minPen();
        cache.gridColors[1] = pen.color();
        pen.setColor(color(pen.color(), MinorGrid));
        grid->setMinPen(pen);
pixhawk's avatar
pixhawk committed
284

285 286 287 288
        break;
    }
    case QwtPlotItem::Rtti_PlotCurve: {
        QwtPlotCurve *c = (QwtPlotCurve *)item;
pixhawk's avatar
pixhawk committed
289

290
        QwtSymbol symbol = c->symbol();
pixhawk's avatar
pixhawk committed
291

292 293 294 295
        QPen pen = symbol.pen();
        cache.curveSymbolPenColors.insert(c, pen.color());
        pen.setColor(color(pen.color(), CurveSymbol));
        symbol.setPen(pen);
pixhawk's avatar
pixhawk committed
296

297 298 299 300
        QBrush brush = symbol.brush();
        cache.curveSymbolBrushColors.insert(c, brush.color());
        brush.setColor(color(brush.color(), CurveSymbol));
        symbol.setBrush(brush);
pixhawk's avatar
pixhawk committed
301

302
        c->setSymbol(symbol);
pixhawk's avatar
pixhawk committed
303

304 305 306 307
        pen = c->pen();
        cache.curveColors.insert(c, pen.color());
        pen.setColor(color(pen.color(), Curve));
        c->setPen(pen);
pixhawk's avatar
pixhawk committed
308

309 310 311 312
        break;
    }
    case QwtPlotItem::Rtti_PlotMarker: {
        QwtPlotMarker *m = (QwtPlotMarker *)item;
pixhawk's avatar
pixhawk committed
313

314 315 316 317 318 319
        QwtText label = m->label();
        cache.markerFonts.insert(m, label.font());
        label.setFont(font(label.font(), Marker));
        cache.markerLabelColors.insert(m, label.color());
        label.setColor(color(label.color(), Marker));
        m->setLabel(label);
pixhawk's avatar
pixhawk committed
320

321 322 323 324
        QPen pen = m->linePen();
        cache.markerLineColors.insert(m, pen.color());
        pen.setColor(color(pen.color(), Marker));
        m->setLinePen(pen);
pixhawk's avatar
pixhawk committed
325

326
        QwtSymbol symbol = m->symbol();
pixhawk's avatar
pixhawk committed
327

328 329 330 331
        pen = symbol.pen();
        cache.markerSymbolPenColors.insert(m, pen.color());
        pen.setColor(color(pen.color(), MarkerSymbol));
        symbol.setPen(pen);
pixhawk's avatar
pixhawk committed
332

333 334 335 336
        QBrush brush = symbol.brush();
        cache.markerSymbolBrushColors.insert(m, brush.color());
        brush.setColor(color(brush.color(), MarkerSymbol));
        symbol.setBrush(brush);
pixhawk's avatar
pixhawk committed
337

338 339 340 341 342 343
        m->setSymbol(symbol);

        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
344 345 346
    }
}

347
/*!
pixhawk's avatar
pixhawk committed
348 349 350 351 352 353 354 355 356 357 358 359 360
   Reset color and fonts of a plot
   \sa apply
*/
void QwtPlotPrintFilter::reset(QwtPlot *plot) const
{
    if ( d_data->cache == 0 )
        return;

    const bool doAutoReplot = plot->autoReplot();
    plot->setAutoReplot(false);

    const PrivateData::Cache &cache = *d_data->cache;

361
    if ( plot->titleLabel() ) {
pixhawk's avatar
pixhawk committed
362
        QwtTextLabel* title = plot->titleLabel();
363
        if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) ) {
pixhawk's avatar
pixhawk committed
364 365 366
            QwtText text = title->text();
            text.setColor(cache.titleColor);
            title->setText(text);
367
        } else {
pixhawk's avatar
pixhawk committed
368 369 370 371 372 373
            QPalette palette = title->palette();
            palette.setColor(
                QPalette::Active, Palette::Text, cache.titleColor);
            title->setPalette(palette);
        }

374
        if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) ) {
pixhawk's avatar
pixhawk committed
375 376 377
            QwtText text = title->text();
            text.setFont(cache.titleFont);
            title->setText(text);
378
        } else {
pixhawk's avatar
pixhawk committed
379 380 381 382
            title->setFont(cache.titleFont);
        }
    }

383
    if ( plot->legend() ) {
pixhawk's avatar
pixhawk committed
384 385 386
#if QT_VERSION < 0x040000
        QValueList<QWidget *> list = plot->legend()->legendItems();
        for ( QValueListIterator<QWidget *> it = list.begin();
387
                it != list.end(); ++it )
pixhawk's avatar
pixhawk committed
388 389 390
#else
        QList<QWidget *> list = plot->legend()->legendItems();
        for ( QList<QWidget*>::iterator it = list.begin();
391
                it != list.end(); ++it )
pixhawk's avatar
pixhawk committed
392 393 394 395 396 397 398
#endif
        {
            QWidget *w = *it;

            if ( cache.legendFonts.contains(w) )
                w->setFont(cache.legendFonts[w]);

399
            if ( w->inherits("QwtLegendItem") ) {
pixhawk's avatar
pixhawk committed
400
                QwtLegendItem *label = (QwtLegendItem *)w;
401
                const QwtPlotItem *plotItem =
pixhawk's avatar
pixhawk committed
402 403 404
                    (const QwtPlotItem*)plot->legend()->find(label);

                QwtSymbol symbol = label->symbol();
405
                if ( cache.curveSymbolPenColors.contains(plotItem) ) {
pixhawk's avatar
pixhawk committed
406 407 408 409 410
                    QPen pen = symbol.pen();
                    pen.setColor(cache.curveSymbolPenColors[plotItem]);
                    symbol.setPen(pen);
                }

411
                if ( cache.curveSymbolBrushColors.contains(plotItem) ) {
pixhawk's avatar
pixhawk committed
412 413 414 415 416 417
                    QBrush brush = symbol.brush();
                    brush.setColor(cache.curveSymbolBrushColors[plotItem]);
                    symbol.setBrush(brush);
                }
                label->setSymbol(symbol);

418
                if ( cache.curveColors.contains(plotItem) ) {
pixhawk's avatar
pixhawk committed
419 420 421 422 423 424 425
                    QPen pen = label->curvePen();
                    pen.setColor(cache.curveColors[plotItem]);
                    label->setCurvePen(pen);
                }
            }
        }
    }
426
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) {
pixhawk's avatar
pixhawk committed
427
        QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
428
        if ( scaleWidget ) {
pixhawk's avatar
pixhawk committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
            QPalette palette = scaleWidget->palette();
            palette.setColor(QPalette::Active, Palette::Foreground,
                             cache.scaleColor[axis]);
            scaleWidget->setPalette(palette);

            scaleWidget->setFont(cache.scaleFont[axis]);
            scaleWidget->setTitle(cache.scaleTitle[axis]);

            int startDist, endDist;
            scaleWidget->getBorderDistHint(startDist, endDist);
            scaleWidget->setBorderDist(startDist, endDist);
        }
    }

    QPalette p = plot->palette();
    p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
    plot->setPalette(p);

    plot->setCanvasBackground(cache.canvasBackground);
448

pixhawk's avatar
pixhawk committed
449 450
    const QwtPlotItemList& itmList = plot->itemList();
    for ( QwtPlotItemIterator it = itmList.begin();
451
            it != itmList.end(); ++it ) {
pixhawk's avatar
pixhawk committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        reset(*it);
    }

    delete d_data->cache;
    d_data->cache = 0;

    plot->setAutoReplot(doAutoReplot);
}

void QwtPlotPrintFilter::reset(QwtPlotItem *item) const
{
    if ( d_data->cache == 0 )
        return;

    const PrivateData::Cache &cache = *d_data->cache;

468 469 470
    switch(item->rtti()) {
    case QwtPlotItem::Rtti_PlotGrid: {
        QwtPlotGrid *grid = (QwtPlotGrid *)item;
pixhawk's avatar
pixhawk committed
471

472 473 474
        QPen pen = grid->majPen();
        pen.setColor(cache.gridColors[0]);
        grid->setMajPen(pen);
pixhawk's avatar
pixhawk committed
475

476 477 478
        pen = grid->minPen();
        pen.setColor(cache.gridColors[1]);
        grid->setMinPen(pen);
pixhawk's avatar
pixhawk committed
479

480 481 482 483
        break;
    }
    case QwtPlotItem::Rtti_PlotCurve: {
        QwtPlotCurve *c = (QwtPlotCurve *)item;
pixhawk's avatar
pixhawk committed
484

485
        QwtSymbol symbol = c->symbol();
pixhawk's avatar
pixhawk committed
486

487 488 489
        if ( cache.curveSymbolPenColors.contains(c) ) {
            symbol.setPen(cache.curveSymbolPenColors[c]);
        }
pixhawk's avatar
pixhawk committed
490

491 492 493 494 495 496
        if ( cache.curveSymbolBrushColors.contains(c) ) {
            QBrush brush = symbol.brush();
            brush.setColor(cache.curveSymbolBrushColors[c]);
            symbol.setBrush(brush);
        }
        c->setSymbol(symbol);
pixhawk's avatar
pixhawk committed
497

498 499 500 501
        if ( cache.curveColors.contains(c) ) {
            QPen pen = c->pen();
            pen.setColor(cache.curveColors[c]);
            c->setPen(pen);
pixhawk's avatar
pixhawk committed
502 503
        }

504 505 506 507
        break;
    }
    case QwtPlotItem::Rtti_PlotMarker: {
        QwtPlotMarker *m = (QwtPlotMarker *)item;
pixhawk's avatar
pixhawk committed
508

509 510 511 512 513
        if ( cache.markerFonts.contains(m) ) {
            QwtText label = m->label();
            label.setFont(cache.markerFonts[m]);
            m->setLabel(label);
        }
pixhawk's avatar
pixhawk committed
514

515 516 517 518 519
        if ( cache.markerLabelColors.contains(m) ) {
            QwtText label = m->label();
            label.setColor(cache.markerLabelColors[m]);
            m->setLabel(label);
        }
pixhawk's avatar
pixhawk committed
520

521 522 523 524 525
        if ( cache.markerLineColors.contains(m) ) {
            QPen pen = m->linePen();
            pen.setColor(cache.markerLineColors[m]);
            m->setLinePen(pen);
        }
pixhawk's avatar
pixhawk committed
526

527
        QwtSymbol symbol = m->symbol();
pixhawk's avatar
pixhawk committed
528

529 530 531 532 533
        if ( cache.markerSymbolPenColors.contains(m) ) {
            QPen pen = symbol.pen();
            pen.setColor(cache.markerSymbolPenColors[m]);
            symbol.setPen(pen);
        }
pixhawk's avatar
pixhawk committed
534

535 536 537 538
        if ( cache.markerSymbolBrushColors.contains(m) ) {
            QBrush brush = symbol.brush();
            brush.setColor(cache.markerSymbolBrushColors[m]);
            symbol.setBrush(brush);
pixhawk's avatar
pixhawk committed
539
        }
540 541 542 543 544 545 546

        m->setSymbol(symbol);

        break;
    }
    default:
        break;
pixhawk's avatar
pixhawk committed
547 548
    }
}