# qwt_plot_printfilter.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 // vim: expandtab
    00011
    00012 #include <qmap.h>
    00013 #include "qwt_plot.h"
    00014 #include "qwt_plot_grid.h"
    00015 #include "qwt_plot_curve.h"
    00016 #include "qwt_plot_marker.h"
    00017 #include "qwt_symbol.h"
    00018 #include "qwt_legend.h"
    00019 #include "qwt_legend_item.h"
    00020 #include "qwt_scale_widget.h"
    00021 #include "qwt_text_label.h"
    00022 #include "qwt_plot_printfilter.h"
    00023
    00024 #if QT_VERSION < 0x040000
    00025 typedef QColorGroup Palette;
    00026 #else
    00027 typedef QPalette Palette;
    00028 #endif
    00029
    00030 class QwtPlotPrintFilter::PrivateData
    00031 {
    00032 public:
    00033     PrivateData():
    00034         options(QwtPlotPrintFilter::PrintAll),
    00035         cache(NULL)
    00036     {
    00037     }
    00038
    00039     ~PrivateData()
    00040     {
    00041         delete cache;
    00042     }
    00043
    00044     class Cache
    00045     {
    00046     public:
    00047         QColor titleColor;
    00048         QFont titleFont;
    00049
    00050         QwtText scaleTitle[QwtPlot::axisCnt];
    00051         QColor scaleColor[QwtPlot::axisCnt];
    00052         QFont scaleFont[QwtPlot::axisCnt];
    00053         QColor scaleTitleColor[QwtPlot::axisCnt];
    00054         QFont scaleTitleFont[QwtPlot::axisCnt];
    00055
    00056         QMap<QWidget *, QFont> legendFonts;
    00057
    00058         QColor widgetBackground;
    00059         QColor canvasBackground;
    00060         QColor gridColors[2];
    00061
    00062         QMap<const QwtPlotItem *, QColor> curveColors;
    00063         QMap<const QwtPlotItem *, QColor> curveSymbolBrushColors;
    00064         QMap<const QwtPlotItem *, QColor> curveSymbolPenColors;
    00065
    00066         QMap<const QwtPlotItem *, QFont> markerFonts;
    00067         QMap<const QwtPlotItem *, QColor> markerLabelColors;
    00068         QMap<const QwtPlotItem *, QColor> markerLineColors;
    00069         QMap<const QwtPlotItem *, QColor> markerSymbolBrushColors;
    00070         QMap<const QwtPlotItem *, QColor> markerSymbolPenColors;
    00071     };
    00072
    00073     int options;
    00074     mutable Cache *cache;
    00075 };
    00076
    00077
    00082 QwtPlotPrintFilter::QwtPlotPrintFilter()
    00083 {
    00084     d_data = new PrivateData;
    00085 }
    00086
    00088 QwtPlotPrintFilter::~QwtPlotPrintFilter()
    00089 {
    00090     delete d_data;
    00091 }
    00092
    00099 void QwtPlotPrintFilter::setOptions(int options)
    00100 {
    00101     d_data->options = options;
    00102 }
    00103
    00108 int QwtPlotPrintFilter::options() const
    00109 {
    00110     return d_data->options;
    00111 }
    00112
    00124 QColor QwtPlotPrintFilter::color(const QColor &c, Item item) const
    00125 {
    00126     if ( !(options() & PrintCanvasBackground))
    00127     {
    00128         switch(item)
    00129         {
    00130             case MajorGrid:
    00131                 return Qt::darkGray;
    00132             case MinorGrid:
    00133                 return Qt::gray;
    00134             default:;
    00135         }
    00136     }
    00137     return c;
    00138 }
    00139
    00148 QFont QwtPlotPrintFilter::font(const QFont &f, Item) const
    00149 {
    00150     return f;
    00151 }
    00152
    00157 void QwtPlotPrintFilter::apply(QwtPlot *plot) const
    00158 {
    00159     const bool doAutoReplot = plot->autoReplot();
    00160     plot->setAutoReplot(false);
    00161
    00162     delete d_data->cache;
    00163     d_data->cache = new PrivateData::Cache;
    00164
    00165     PrivateData::Cache &cache = *d_data->cache;
    00166
    00167     if ( plot->titleLabel() )
    00168     {
    00169         QPalette palette = plot->titleLabel()->palette();
    00170         cache.titleColor = palette.color(
    00171             QPalette::Active, Palette::Text);
    00172         palette.setColor(QPalette::Active, Palette::Text,
    00173                          color(cache.titleColor, Title));
    00174         plot->titleLabel()->setPalette(palette);
    00175
    00176         cache.titleFont = plot->titleLabel()->font();
    00177         plot->titleLabel()->setFont(font(cache.titleFont, Title));
    00178     }
    00179     if ( plot->legend() )
    00180     {
    00181 #if QT_VERSION < 0x040000
    00182         QValueList<QWidget *> list = plot->legend()->legendItems();
    00183         for ( QValueListIterator<QWidget *> it = list.begin();
    00184             it != list.end(); ++it )
    00185 #else
    00186         QList<QWidget *> list = plot->legend()->legendItems();
    00187         for ( QList<QWidget*>::iterator it = list.begin();
    00188             it != list.end(); ++it )
    00189 #endif
    00190         {
    00191             QWidget *w = *it;
    00192
    00193             cache.legendFonts.insert(w, w->font());
    00194             w->setFont(font(w->font(), Legend));
    00195
    00196             if ( w->inherits("QwtLegendItem") )
    00197             {
    00198                 QwtLegendItem *label = (QwtLegendItem *)w;
    00199
    00200                 QwtSymbol symbol = label->symbol();
    00201                 QPen pen = symbol.pen();
    00202                 QBrush brush = symbol.brush();
    00203
    00204                 pen.setColor(color(pen.color(), CurveSymbol));
    00205                 brush.setColor(color(brush.color(), CurveSymbol));
    00206
    00207                 symbol.setPen(pen);
    00208                 symbol.setBrush(brush);
    00209                 label->setSymbol(symbol);
    00210
    00211                 pen = label->curvePen();
    00212                 pen.setColor(color(pen.color(), Curve));
    00213                 label->setCurvePen(pen);
    00214             }
    00215         }
    00216     }
    00217     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    00218     {
    00219         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
    00220         if ( scaleWidget )
    00221         {
    00222             cache.scaleColor[axis] = scaleWidget->palette().color(
    00223                 QPalette::Active, Palette::Foreground);
    00224             QPalette palette = scaleWidget->palette();
    00225             palette.setColor(QPalette::Active, Palette::Foreground,
    00226                              color(cache.scaleColor[axis], AxisScale));
    00227             scaleWidget->setPalette(palette);
    00228
    00229             cache.scaleFont[axis] = scaleWidget->font();
    00230             scaleWidget->setFont(font(cache.scaleFont[axis], AxisScale));
    00231
    00232             cache.scaleTitle[axis] = scaleWidget->title();
    00233
    00234             QwtText scaleTitle = scaleWidget->title();
    00235             if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextColor) )
    00236             {
    00237                 cache.scaleTitleColor[axis] = scaleTitle.color();
    00238                 scaleTitle.setColor(
    00239                     color(cache.scaleTitleColor[axis], AxisTitle));
    00240             }
    00241
    00242             if ( scaleTitle.testPaintAttribute(QwtText::PaintUsingTextFont) )
    00243             {
    00244                 cache.scaleTitleFont[axis] = scaleTitle.font();
    00245                 scaleTitle.setFont(
    00246                     font(cache.scaleTitleFont[axis], AxisTitle));
    00247             }
    00248
    00249             scaleWidget->setTitle(scaleTitle);
    00250
    00251             int startDist, endDist;
    00252             scaleWidget->getBorderDistHint(startDist, endDist);
    00253             scaleWidget->setBorderDist(startDist, endDist);
    00254         }
    00255     }
    00256
    00257
    00258     QPalette p = plot->palette();
    00259     cache.widgetBackground = plot->palette().color(
    00260         QPalette::Active, Palette::Background);
    00261     p.setColor(QPalette::Active, Palette::Background,
    00262         color(cache.widgetBackground, WidgetBackground));
    00263     plot->setPalette(p);
    00264
    00265     cache.canvasBackground = plot->canvasBackground();
    00266     plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));
    00267
    00268     const QwtPlotItemList& itmList = plot->itemList();
    00269     for ( QwtPlotItemIterator it = itmList.begin();
    00270         it != itmList.end(); ++it )
    00271     {
    00272         apply(*it);
    00273     }
    00274
    00275     plot->setAutoReplot(doAutoReplot);
    00276 }
    00277
    00278 void QwtPlotPrintFilter::apply(QwtPlotItem *item) const
    00279 {
    00280     PrivateData::Cache &cache = *d_data->cache;
    00281
    00282     switch(item->rtti())
    00283     {
    00284         case QwtPlotItem::Rtti_PlotGrid:
    00285         {
    00286             QwtPlotGrid *grid = (QwtPlotGrid *)item;
    00287
    00288             QPen pen = grid->majPen();
    00289             cache.gridColors[0] = pen.color();
    00290             pen.setColor(color(pen.color(), MajorGrid));
    00291             grid->setMajPen(pen);
    00292
    00293             pen = grid->minPen();
    00294             cache.gridColors[1] = pen.color();
    00295             pen.setColor(color(pen.color(), MinorGrid));
    00296             grid->setMinPen(pen);
    00297
    00298             break;
    00299         }
    00300         case QwtPlotItem::Rtti_PlotCurve:
    00301         {
    00302             QwtPlotCurve *c = (QwtPlotCurve *)item;
    00303
    00304             QwtSymbol symbol = c->symbol();
    00305
    00306             QPen pen = symbol.pen();
    00307             cache.curveSymbolPenColors.insert(c, pen.color());
    00308             pen.setColor(color(pen.color(), CurveSymbol));
    00309             symbol.setPen(pen);
    00310
    00311             QBrush brush = symbol.brush();
    00312             cache.curveSymbolBrushColors.insert(c, brush.color());
    00313             brush.setColor(color(brush.color(), CurveSymbol));
    00314             symbol.setBrush(brush);
    00315
    00316             c->setSymbol(symbol);
    00317
    00318             pen = c->pen();
    00319             cache.curveColors.insert(c, pen.color());
    00320             pen.setColor(color(pen.color(), Curve));
    00321             c->setPen(pen);
    00322
    00323             break;
    00324         }
    00325         case QwtPlotItem::Rtti_PlotMarker:
    00326         {
    00327             QwtPlotMarker *m = (QwtPlotMarker *)item;
    00328
    00329             QwtText label = m->label();
    00330             cache.markerFonts.insert(m, label.font());
    00331             label.setFont(font(label.font(), Marker));
    00332             cache.markerLabelColors.insert(m, label.color());
    00333             label.setColor(color(label.color(), Marker));
    00334             m->setLabel(label);
    00335
    00336             QPen pen = m->linePen();
    00337             cache.markerLineColors.insert(m, pen.color());
    00338             pen.setColor(color(pen.color(), Marker));
    00339             m->setLinePen(pen);
    00340
    00341             QwtSymbol symbol = m->symbol();
    00342
    00343             pen = symbol.pen();
    00344             cache.markerSymbolPenColors.insert(m, pen.color());
    00345             pen.setColor(color(pen.color(), MarkerSymbol));
    00346             symbol.setPen(pen);
    00347
    00348             QBrush brush = symbol.brush();
    00349             cache.markerSymbolBrushColors.insert(m, brush.color());
    00350             brush.setColor(color(brush.color(), MarkerSymbol));
    00351             symbol.setBrush(brush);
    00352
    00353             m->setSymbol(symbol);
    00354
    00355             break;
    00356         }
    00357         default:
    00358             break;
    00359     }
    00360 }
    00361
    00366 void QwtPlotPrintFilter::reset(QwtPlot *plot) const
    00367 {
    00368     if ( d_data->cache == 0 )
    00369         return;
    00370
    00371     const bool doAutoReplot = plot->autoReplot();
    00372     plot->setAutoReplot(false);
    00373
    00374     const PrivateData::Cache &cache = *d_data->cache;
    00375
    00376     if ( plot->titleLabel() )
    00377     {
    00378         QwtTextLabel* title = plot->titleLabel();
    00379         if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) )
    00380         {
    00381             QwtText text = title->text();
    00382             text.setColor(cache.titleColor);
    00383             title->setText(text);
    00384         }
    00385         else
    00386         {
    00387             QPalette palette = title->palette();
    00388             palette.setColor(
    00389                 QPalette::Active, Palette::Text, cache.titleColor);
    00390             title->setPalette(palette);
    00391         }
    00392
    00393         if ( title->text().testPaintAttribute(QwtText::PaintUsingTextFont) )
    00394         {
    00395             QwtText text = title->text();
    00396             text.setFont(cache.titleFont);
    00397             title->setText(text);
    00398         }
    00399         else
    00400         {
    00401             title->setFont(cache.titleFont);
    00402         }
    00403     }
    00404
    00405     if ( plot->legend() )
    00406     {
    00407 #if QT_VERSION < 0x040000
    00408         QValueList<QWidget *> list = plot->legend()->legendItems();
    00409         for ( QValueListIterator<QWidget *> it = list.begin();
    00410             it != list.end(); ++it )
    00411 #else
    00412         QList<QWidget *> list = plot->legend()->legendItems();
    00413         for ( QList<QWidget*>::iterator it = list.begin();
    00414             it != list.end(); ++it )
    00415 #endif
    00416         {
    00417             QWidget *w = *it;
    00418
    00419             if ( cache.legendFonts.contains(w) )
    00420                 w->setFont(cache.legendFonts[w]);
    00421
    00422             if ( w->inherits("QwtLegendItem") )
    00423             {
    00424                 QwtLegendItem *label = (QwtLegendItem *)w;
    00425                 const QwtPlotItem *plotItem = plot->legend()->find(label);
    00426
    00427                 QwtSymbol symbol = label->symbol();
    00428                 if ( cache.curveSymbolPenColors.contains(plotItem) )
    00429                 {
    00430                     QPen pen = symbol.pen();
    00431                     pen.setColor(cache.curveSymbolPenColors[plotItem]);
    00432                     symbol.setPen(pen);
    00433                 }
    00434
    00435                 if ( cache.curveSymbolBrushColors.contains(plotItem) )
    00436                 {
    00437                     QBrush brush = symbol.brush();
    00438                     brush.setColor(cache.curveSymbolBrushColors[plotItem]);
    00439                     symbol.setBrush(brush);
    00440                 }
    00441                 label->setSymbol(symbol);
    00442
    00443                 if ( cache.curveColors.contains(plotItem) )
    00444                 {
    00445                     QPen pen = label->curvePen();
    00446                     pen.setColor(cache.curveColors[plotItem]);
    00447                     label->setCurvePen(pen);
    00448                 }
    00449             }
    00450         }
    00451     }
    00452     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    00453     {
    00454         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
    00455         if ( scaleWidget )
    00456         {
    00457             QPalette palette = scaleWidget->palette();
    00458             palette.setColor(QPalette::Active, Palette::Foreground,
    00459                              cache.scaleColor[axis]);
    00460             scaleWidget->setPalette(palette);
    00461
    00462             scaleWidget->setFont(cache.scaleFont[axis]);
    00463             scaleWidget->setTitle(cache.scaleTitle[axis]);
    00464
    00465             int startDist, endDist;
    00466             scaleWidget->getBorderDistHint(startDist, endDist);
    00467             scaleWidget->setBorderDist(startDist, endDist);
    00468         }
    00469     }
    00470
    00471     QPalette p = plot->palette();
    00472     p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
    00473     plot->setPalette(p);
    00474
    00475     plot->setCanvasBackground(cache.canvasBackground);
    00476
    00477     const QwtPlotItemList& itmList = plot->itemList();
    00478     for ( QwtPlotItemIterator it = itmList.begin();
    00479         it != itmList.end(); ++it )
    00480     {
    00481         reset(*it);
    00482     }
    00483
    00484     delete d_data->cache;
    00485     d_data->cache = 0;
    00486
    00487     plot->setAutoReplot(doAutoReplot);
    00488 }
    00489
    00490 void QwtPlotPrintFilter::reset(QwtPlotItem *item) const
    00491 {
    00492     if ( d_data->cache == 0 )
    00493         return;
    00494
    00495     const PrivateData::Cache &cache = *d_data->cache;
    00496
    00497     switch(item->rtti())
    00498     {
    00499         case QwtPlotItem::Rtti_PlotGrid:
    00500         {
    00501             QwtPlotGrid *grid = (QwtPlotGrid *)item;
    00502
    00503             QPen pen = grid->majPen();
    00504             pen.setColor(cache.gridColors[0]);
    00505             grid->setMajPen(pen);
    00506
    00507             pen = grid->minPen();
    00508             pen.setColor(cache.gridColors[1]);
    00509             grid->setMinPen(pen);
    00510
    00511             break;
    00512         }
    00513         case QwtPlotItem::Rtti_PlotCurve:
    00514         {
    00515             QwtPlotCurve *c = (QwtPlotCurve *)item;
    00516
    00517             QwtSymbol symbol = c->symbol();
    00518
    00519             if ( cache.curveSymbolPenColors.contains(c) )
    00520             {
    00521                 symbol.setPen(cache.curveSymbolPenColors[c]);
    00522             }
    00523
    00524             if ( cache.curveSymbolBrushColors.contains(c) )
    00525             {
    00526                 QBrush brush = symbol.brush();
    00527                 brush.setColor(cache.curveSymbolBrushColors[c]);
    00528                 symbol.setBrush(brush);
    00529             }
    00530             c->setSymbol(symbol);
    00531
    00532             if ( cache.curveColors.contains(c) )
    00533             {
    00534                 QPen pen = c->pen();
    00535                 pen.setColor(cache.curveColors[c]);
    00536                 c->setPen(pen);
    00537             }
    00538
    00539             break;
    00540         }
    00541         case QwtPlotItem::Rtti_PlotMarker:
    00542         {
    00543             QwtPlotMarker *m = (QwtPlotMarker *)item;
    00544
    00545             if ( cache.markerFonts.contains(m) )
    00546             {
    00547                 QwtText label = m->label();
    00548                 label.setFont(cache.markerFonts[m]);
    00549                 m->setLabel(label);
    00550             }
    00551
    00552             if ( cache.markerLabelColors.contains(m) )
    00553             {
    00554                 QwtText label = m->label();
    00555                 label.setColor(cache.markerLabelColors[m]);
    00556                 m->setLabel(label);
    00557             }
    00558
    00559             if ( cache.markerLineColors.contains(m) )
    00560             {
    00561                 QPen pen = m->linePen();
    00562                 pen.setColor(cache.markerLineColors[m]);
    00563                 m->setLinePen(pen);
    00564             }
    00565
    00566             QwtSymbol symbol = m->symbol();
    00567
    00568             if ( cache.markerSymbolPenColors.contains(m) )
    00569             {
    00570                 QPen pen = symbol.pen();
    00571                 pen.setColor(cache.markerSymbolPenColors[m]);
    00572                 symbol.setPen(pen);
    00573             }
    00574
    00575             if ( cache.markerSymbolBrushColors.contains(m) )
    00576             {
    00577                 QBrush brush = symbol.brush();
    00578                 brush.setColor(cache.markerSymbolBrushColors[m]);
    00579                 symbol.setBrush(brush);
    00580             }
    00581
    00582             m->setSymbol(symbol);
    00583
    00584             break;
    00585         }
    00586         default:
    00587             break;
    00588     }
    00589 }