Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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.paintAttributes() & QwtText::PaintUsingTextColor )
00236             {
00237                 cache.scaleTitleColor[axis] = scaleTitle.color();
00238                 scaleTitle.setColor(
00239                     color(cache.scaleTitleColor[axis], AxisTitle));
00240             }
00241 
00242             if ( scaleTitle.paintAttributes() & 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 #ifdef __GNUC__
00330 #warning filtering of rectPen/rectBrush missing
00331 #endif
00332 
00333             QwtText label = m->label();
00334             cache.markerFonts.insert(m, label.font());
00335             label.setFont(font(label.font(), Marker));
00336             cache.markerLabelColors.insert(m, label.color());
00337             label.setColor(color(label.color(), Marker));
00338             m->setLabel(label);
00339             
00340             QPen pen = m->linePen();
00341             cache.markerLineColors.insert(m, pen.color());
00342             pen.setColor(color(pen.color(), Marker));
00343             m->setLinePen(pen);
00344 
00345             QwtSymbol symbol = m->symbol();
00346 
00347             pen = symbol.pen();
00348             cache.markerSymbolPenColors.insert(m, pen.color());
00349             pen.setColor(color(pen.color(), MarkerSymbol));
00350             symbol.setPen(pen);
00351 
00352             QBrush brush = symbol.brush();
00353             cache.markerSymbolBrushColors.insert(m, brush.color());
00354             brush.setColor(color(brush.color(), MarkerSymbol));
00355             symbol.setBrush(brush);
00356 
00357             m->setSymbol(symbol);
00358 
00359             break;
00360         }
00361         default:    
00362             break;
00363     }
00364 }
00365 
00370 void QwtPlotPrintFilter::reset(QwtPlot *plot) const
00371 {
00372     if ( d_data->cache == 0 )
00373         return;
00374 
00375     const bool doAutoReplot = plot->autoReplot();
00376     plot->setAutoReplot(false);
00377 
00378     const PrivateData::Cache &cache = *d_data->cache;
00379 
00380     if ( plot->titleLabel() )
00381     {
00382         QwtTextLabel* title = plot->titleLabel();
00383         if ( title->text().paintAttributes() & QwtText::PaintUsingTextFont )
00384         {
00385             QwtText text = title->text();
00386             text.setColor(cache.titleColor);
00387             title->setText(text);
00388         }
00389         else
00390         {
00391             QPalette palette = title->palette();
00392             palette.setColor(
00393                 QPalette::Active, Palette::Text, cache.titleColor);
00394             title->setPalette(palette);
00395         }
00396 
00397         if ( title->text().paintAttributes() & QwtText::PaintUsingTextFont )
00398         {
00399             QwtText text = title->text();
00400             text.setFont(cache.titleFont);
00401             title->setText(text);
00402         }
00403         else
00404         {
00405             title->setFont(cache.titleFont);
00406         }
00407     }
00408 
00409     if ( plot->legend() )
00410     {
00411 #if QT_VERSION < 0x040000
00412         QValueList<QWidget *> list = plot->legend()->legendItems();
00413         for ( QValueListIterator<QWidget *> it = list.begin();
00414             it != list.end(); ++it )
00415 #else
00416         QList<QWidget *> list = plot->legend()->legendItems();
00417         for ( QList<QWidget*>::iterator it = list.begin();
00418             it != list.end(); ++it )
00419 #endif
00420         {
00421             QWidget *w = *it;
00422 
00423             if ( cache.legendFonts.contains(w) )
00424                 w->setFont(cache.legendFonts[w]);
00425 
00426             if ( w->inherits("QwtLegendItem") )
00427             {
00428                 QwtLegendItem *label = (QwtLegendItem *)w;
00429                 const QwtPlotItem *plotItem = plot->legend()->find(label);
00430 
00431                 QwtSymbol symbol = label->symbol();
00432                 if ( cache.curveSymbolPenColors.contains(plotItem) )
00433                 {
00434                     QPen pen = symbol.pen();
00435                     pen.setColor(cache.curveSymbolPenColors[plotItem]);
00436                     symbol.setPen(pen);
00437                 }
00438 
00439                 if ( cache.curveSymbolBrushColors.contains(plotItem) )
00440                 {
00441                     QBrush brush = symbol.brush();
00442                     brush.setColor(cache.curveSymbolBrushColors[plotItem]);
00443                     symbol.setBrush(brush);
00444                 }
00445                 label->setSymbol(symbol);
00446 
00447                 if ( cache.curveColors.contains(plotItem) )
00448                 {
00449                     QPen pen = label->curvePen();
00450                     pen.setColor(cache.curveColors[plotItem]);
00451                     label->setCurvePen(pen);
00452                 }
00453             }
00454         }
00455     }
00456     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00457     {
00458         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
00459         if ( scaleWidget )
00460         {
00461             QPalette palette = scaleWidget->palette();
00462             palette.setColor(QPalette::Active, Palette::Foreground,
00463                              cache.scaleColor[axis]);
00464             scaleWidget->setPalette(palette);
00465 
00466             scaleWidget->setFont(cache.scaleFont[axis]);
00467             scaleWidget->setTitle(cache.scaleTitle[axis]);
00468 
00469             int startDist, endDist;
00470             scaleWidget->getBorderDistHint(startDist, endDist);
00471             scaleWidget->setBorderDist(startDist, endDist);
00472         }
00473     }
00474 
00475     QPalette p = plot->palette();
00476     p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
00477     plot->setPalette(p);
00478 
00479     plot->setCanvasBackground(cache.canvasBackground);
00480    
00481     const QwtPlotItemList& itmList = plot->itemList();
00482     for ( QwtPlotItemIterator it = itmList.begin();
00483         it != itmList.end(); ++it )
00484     {
00485         reset(*it);
00486     }
00487 
00488     delete d_data->cache;
00489     d_data->cache = 0;
00490 
00491     plot->setAutoReplot(doAutoReplot);
00492 }
00493 
00494 void QwtPlotPrintFilter::reset(QwtPlotItem *item) const
00495 {
00496     if ( d_data->cache == 0 )
00497         return;
00498 
00499     const PrivateData::Cache &cache = *d_data->cache;
00500 
00501     switch(item->rtti())
00502     {
00503         case QwtPlotItem::Rtti_PlotGrid:
00504         {
00505             QwtPlotGrid *grid = (QwtPlotGrid *)item;
00506 
00507             QPen pen = grid->majPen();
00508             pen.setColor(cache.gridColors[0]);
00509             grid->setMajPen(pen);
00510 
00511             pen = grid->minPen();
00512             pen.setColor(cache.gridColors[1]);
00513             grid->setMinPen(pen);
00514 
00515             break;
00516         }
00517         case QwtPlotItem::Rtti_PlotCurve:
00518         {
00519             QwtPlotCurve *c = (QwtPlotCurve *)item;
00520 
00521             QwtSymbol symbol = c->symbol();
00522 
00523             if ( cache.curveSymbolPenColors.contains(c) )
00524             {
00525                 symbol.setPen(cache.curveSymbolPenColors[c]);
00526             }
00527 
00528             if ( cache.curveSymbolBrushColors.contains(c) )
00529             {
00530                 QBrush brush = symbol.brush();
00531                 brush.setColor(cache.curveSymbolBrushColors[c]);
00532                 symbol.setBrush(brush);
00533             }
00534             c->setSymbol(symbol);
00535 
00536             if ( cache.curveColors.contains(c) )
00537             {
00538                 QPen pen = c->pen();
00539                 pen.setColor(cache.curveColors[c]);
00540                 c->setPen(pen);
00541             }
00542 
00543             break;
00544         }
00545         case QwtPlotItem::Rtti_PlotMarker:
00546         {
00547             QwtPlotMarker *m = (QwtPlotMarker *)item;
00548 
00549             if ( cache.markerFonts.contains(m) )
00550             {
00551                 QwtText label = m->label();
00552                 label.setFont(cache.markerFonts[m]);
00553                 m->setLabel(label);
00554             }
00555 
00556             if ( cache.markerLabelColors.contains(m) )
00557             {
00558                 QwtText label = m->label();
00559                 label.setColor(cache.markerLabelColors[m]);
00560                 m->setLabel(label);
00561             }
00562 
00563             if ( cache.markerLineColors.contains(m) )
00564             {
00565                 QPen pen = m->linePen();
00566                 pen.setColor(cache.markerLineColors[m]);
00567                 m->setLinePen(pen);
00568             }
00569             
00570             QwtSymbol symbol = m->symbol();
00571 
00572             if ( cache.markerSymbolPenColors.contains(m) )
00573             {
00574                 QPen pen = symbol.pen();
00575                 pen.setColor(cache.markerSymbolPenColors[m]);
00576                 symbol.setPen(pen);
00577             }
00578 
00579             if ( cache.markerSymbolBrushColors.contains(m) )
00580             {
00581                 QBrush brush = symbol.brush();
00582                 brush.setColor(cache.markerSymbolBrushColors[m]);
00583                 symbol.setBrush(brush);
00584             }
00585 
00586             m->setSymbol(symbol);
00587 
00588             break;
00589         }
00590         default:
00591             break;
00592     }
00593 }

Generated on Mon Jan 30 22:16:26 2006 for Qwt User's Guide by  doxygen 1.4.4