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() & PrintBackground))
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 = 
00426                     (const QwtPlotItem*)plot->legend()->find(label);
00427 
00428                 QwtSymbol symbol = label->symbol();
00429                 if ( cache.curveSymbolPenColors.contains(plotItem) )
00430                 {
00431                     QPen pen = symbol.pen();
00432                     pen.setColor(cache.curveSymbolPenColors[plotItem]);
00433                     symbol.setPen(pen);
00434                 }
00435 
00436                 if ( cache.curveSymbolBrushColors.contains(plotItem) )
00437                 {
00438                     QBrush brush = symbol.brush();
00439                     brush.setColor(cache.curveSymbolBrushColors[plotItem]);
00440                     symbol.setBrush(brush);
00441                 }
00442                 label->setSymbol(symbol);
00443 
00444                 if ( cache.curveColors.contains(plotItem) )
00445                 {
00446                     QPen pen = label->curvePen();
00447                     pen.setColor(cache.curveColors[plotItem]);
00448                     label->setCurvePen(pen);
00449                 }
00450             }
00451         }
00452     }
00453     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00454     {
00455         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
00456         if ( scaleWidget )
00457         {
00458             QPalette palette = scaleWidget->palette();
00459             palette.setColor(QPalette::Active, Palette::Foreground,
00460                              cache.scaleColor[axis]);
00461             scaleWidget->setPalette(palette);
00462 
00463             scaleWidget->setFont(cache.scaleFont[axis]);
00464             scaleWidget->setTitle(cache.scaleTitle[axis]);
00465 
00466             int startDist, endDist;
00467             scaleWidget->getBorderDistHint(startDist, endDist);
00468             scaleWidget->setBorderDist(startDist, endDist);
00469         }
00470     }
00471 
00472     QPalette p = plot->palette();
00473     p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
00474     plot->setPalette(p);
00475 
00476     plot->setCanvasBackground(cache.canvasBackground);
00477    
00478     const QwtPlotItemList& itmList = plot->itemList();
00479     for ( QwtPlotItemIterator it = itmList.begin();
00480         it != itmList.end(); ++it )
00481     {
00482         reset(*it);
00483     }
00484 
00485     delete d_data->cache;
00486     d_data->cache = 0;
00487 
00488     plot->setAutoReplot(doAutoReplot);
00489 }
00490 
00491 void QwtPlotPrintFilter::reset(QwtPlotItem *item) const
00492 {
00493     if ( d_data->cache == 0 )
00494         return;
00495 
00496     const PrivateData::Cache &cache = *d_data->cache;
00497 
00498     switch(item->rtti())
00499     {
00500         case QwtPlotItem::Rtti_PlotGrid:
00501         {
00502             QwtPlotGrid *grid = (QwtPlotGrid *)item;
00503 
00504             QPen pen = grid->majPen();
00505             pen.setColor(cache.gridColors[0]);
00506             grid->setMajPen(pen);
00507 
00508             pen = grid->minPen();
00509             pen.setColor(cache.gridColors[1]);
00510             grid->setMinPen(pen);
00511 
00512             break;
00513         }
00514         case QwtPlotItem::Rtti_PlotCurve:
00515         {
00516             QwtPlotCurve *c = (QwtPlotCurve *)item;
00517 
00518             QwtSymbol symbol = c->symbol();
00519 
00520             if ( cache.curveSymbolPenColors.contains(c) )
00521             {
00522                 symbol.setPen(cache.curveSymbolPenColors[c]);
00523             }
00524 
00525             if ( cache.curveSymbolBrushColors.contains(c) )
00526             {
00527                 QBrush brush = symbol.brush();
00528                 brush.setColor(cache.curveSymbolBrushColors[c]);
00529                 symbol.setBrush(brush);
00530             }
00531             c->setSymbol(symbol);
00532 
00533             if ( cache.curveColors.contains(c) )
00534             {
00535                 QPen pen = c->pen();
00536                 pen.setColor(cache.curveColors[c]);
00537                 c->setPen(pen);
00538             }
00539 
00540             break;
00541         }
00542         case QwtPlotItem::Rtti_PlotMarker:
00543         {
00544             QwtPlotMarker *m = (QwtPlotMarker *)item;
00545 
00546             if ( cache.markerFonts.contains(m) )
00547             {
00548                 QwtText label = m->label();
00549                 label.setFont(cache.markerFonts[m]);
00550                 m->setLabel(label);
00551             }
00552 
00553             if ( cache.markerLabelColors.contains(m) )
00554             {
00555                 QwtText label = m->label();
00556                 label.setColor(cache.markerLabelColors[m]);
00557                 m->setLabel(label);
00558             }
00559 
00560             if ( cache.markerLineColors.contains(m) )
00561             {
00562                 QPen pen = m->linePen();
00563                 pen.setColor(cache.markerLineColors[m]);
00564                 m->setLinePen(pen);
00565             }
00566             
00567             QwtSymbol symbol = m->symbol();
00568 
00569             if ( cache.markerSymbolPenColors.contains(m) )
00570             {
00571                 QPen pen = symbol.pen();
00572                 pen.setColor(cache.markerSymbolPenColors[m]);
00573                 symbol.setPen(pen);
00574             }
00575 
00576             if ( cache.markerSymbolBrushColors.contains(m) )
00577             {
00578                 QBrush brush = symbol.brush();
00579                 brush.setColor(cache.markerSymbolBrushColors[m]);
00580                 symbol.setBrush(brush);
00581             }
00582 
00583             m->setSymbol(symbol);
00584 
00585             break;
00586         }
00587         default:
00588             break;
00589     }
00590 }

Generated on Sat May 24 18:47:40 2008 for Qwt User's Guide by  doxygen 1.5.0