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 <qlabel.h>
00013 #include <qmap.h>
00014 #include "qwt_plot.h"
00015 #include "qwt_plot_grid.h"
00016 #include "qwt_plot_curve.h"
00017 #include "qwt_plot_marker.h"
00018 #include "qwt_symbol.h"
00019 #include "qwt_legend.h"
00020 #include "qwt_legend_item.h"
00021 #include "qwt_scale_widget.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         QColor scaleColor[4];
00051         QFont scaleFont[4];
00052         QColor scaleTitleColor[4];
00053         QFont scaleTitleFont[4];
00054 
00055         QMap<QWidget *, QFont> legendFonts;
00056 
00057         QColor widgetBackground;
00058         QColor canvasBackground;
00059         QColor gridColors[2];
00060 
00061         QMap<const QwtPlotItem *, QColor> curveColors;
00062         QMap<const QwtPlotItem *, QColor> curveSymbolBrushColors;
00063         QMap<const QwtPlotItem *, QColor> curveSymbolPenColors;
00064 
00065         QMap<const QwtPlotItem *, QFont> markerFonts;
00066         QMap<const QwtPlotItem *, QColor> markerLabelColors;
00067         QMap<const QwtPlotItem *, QColor> markerLineColors;
00068         QMap<const QwtPlotItem *, QColor> markerSymbolBrushColors;
00069         QMap<const QwtPlotItem *, QColor> markerSymbolPenColors;
00070     };
00071 
00072     int options;
00073     mutable Cache *cache;
00074 };
00075 
00076 
00081 QwtPlotPrintFilter::QwtPlotPrintFilter()
00082 {
00083     d_data = new PrivateData;
00084 }
00085 
00087 QwtPlotPrintFilter::~QwtPlotPrintFilter()
00088 {
00089     delete d_data;
00090 }
00091 
00098 void QwtPlotPrintFilter::setOptions(int options) 
00099 { 
00100     d_data->options = options; 
00101 }
00102 
00107 int QwtPlotPrintFilter::options() const 
00108 { 
00109     return d_data->options; 
00110 }
00111 
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 
00149 QFont QwtPlotPrintFilter::font(const QFont &f, Item) const
00150 {
00151     return f;
00152 }
00153 
00158 void QwtPlotPrintFilter::apply(QwtPlot *plot) const
00159 {
00160     delete d_data->cache;
00161     d_data->cache = new PrivateData::Cache;
00162 
00163     PrivateData::Cache &cache = *d_data->cache;
00164 
00165     if ( plot->titleLabel() )
00166     {
00167         QPalette palette = plot->titleLabel()->palette();
00168         cache.titleColor = palette.color(
00169             QPalette::Active, Palette::Foreground);
00170         palette.setColor(QPalette::Active, Palette::Foreground,
00171                          color(cache.titleColor, Title));
00172         plot->titleLabel()->setPalette(palette);
00173 
00174         cache.titleFont = plot->titleLabel()->font();
00175         plot->titleLabel()->setFont(font(cache.titleFont, Title));
00176     }
00177     if ( plot->legend() )
00178     {
00179 #if QT_VERSION < 0x040000
00180         QValueList<QWidget *> list = plot->legend()->legendItems();
00181         for ( QValueListIterator<QWidget *> it = list.begin();
00182             it != list.end(); ++it )
00183 #else
00184         QList<QWidget *> list = plot->legend()->legendItems();
00185         for ( QList<QWidget*>::iterator it = list.begin();
00186             it != list.end(); ++it )
00187 #endif
00188         {
00189             QWidget *w = *it;
00190 
00191             cache.legendFonts.insert(w, w->font());
00192             w->setFont(font(w->font(), Legend));
00193 
00194             if ( w->inherits("QwtLegendItem") )
00195             {
00196                 QwtLegendItem *label = (QwtLegendItem *)w;
00197 
00198                 QwtSymbol symbol = label->symbol();
00199                 QPen pen = symbol.pen();
00200                 QBrush brush = symbol.brush();
00201 
00202                 pen.setColor(color(pen.color(), CurveSymbol));
00203                 brush.setColor(color(brush.color(), CurveSymbol));
00204 
00205                 symbol.setPen(pen);
00206                 symbol.setBrush(brush);
00207                 label->setSymbol(symbol);
00208 
00209                 pen = label->curvePen();
00210                 pen.setColor(color(pen.color(), Curve));
00211                 label->setCurvePen(pen);
00212             }
00213         }
00214     }
00215     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00216     {
00217         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
00218         if ( scaleWidget )
00219         {
00220             cache.scaleColor[axis] = scaleWidget->palette().color(
00221                 QPalette::Active, Palette::Foreground);
00222             QPalette palette = scaleWidget->palette();
00223             palette.setColor(QPalette::Active, Palette::Foreground,
00224                              color(cache.scaleColor[axis], AxisScale));
00225             scaleWidget->setPalette(palette);
00226 
00227             cache.scaleFont[axis] = scaleWidget->font();
00228             scaleWidget->setFont(font(cache.scaleFont[axis], AxisScale));
00229 
00230             cache.scaleTitleColor[axis] = scaleWidget->titleColor();
00231             scaleWidget->setTitleColor(
00232                 color(cache.scaleTitleColor[axis], AxisTitle));
00233 
00234             cache.scaleTitleFont[axis] = scaleWidget->titleFont();
00235             scaleWidget->setTitleFont(
00236                 font(cache.scaleTitleFont[axis], AxisTitle));
00237 
00238             int startDist, endDist;
00239             scaleWidget->minBorderDist(startDist, endDist);
00240             scaleWidget->setBorderDist(startDist, endDist);
00241         }
00242     }
00243 
00244 
00245     QPalette p = plot->palette();
00246     cache.widgetBackground = plot->palette().color(
00247         QPalette::Active, Palette::Background);
00248     p.setColor(QPalette::Active, Palette::Background, 
00249         color(cache.widgetBackground, WidgetBackground));
00250     plot->setPalette(p);
00251 
00252     cache.canvasBackground = plot->canvasBackground();
00253     plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));
00254 
00255     const QwtPlotItemList& itmList = plot->itemList();
00256     for ( QwtPlotItemIterator it = itmList.begin();
00257         it != itmList.end(); ++it )
00258     {
00259         apply(*it);
00260     }
00261 }
00262 
00263 void QwtPlotPrintFilter::apply(QwtPlotItem *item) const
00264 {
00265     PrivateData::Cache &cache = *d_data->cache;
00266 
00267     switch(item->rtti())
00268     {
00269         case QwtPlotItem::Rtti_PlotGrid:
00270         {
00271             QwtPlotGrid *grid = (QwtPlotGrid *)item;
00272 
00273             QPen pen = grid->majPen();
00274             cache.gridColors[0] = pen.color();
00275             pen.setColor(color(pen.color(), MajorGrid));
00276             grid->setMajPen(pen);
00277 
00278             pen = grid->minPen();
00279             cache.gridColors[1] = pen.color();
00280             pen.setColor(color(pen.color(), MinorGrid));
00281             grid->setMinPen(pen);
00282 
00283             break;
00284         }
00285         case QwtPlotItem::Rtti_PlotCurve:
00286         {
00287             QwtPlotCurve *c = (QwtPlotCurve *)item;
00288 
00289             QwtSymbol symbol = c->symbol();
00290 
00291             QPen pen = symbol.pen();
00292             cache.curveSymbolPenColors.insert(c, pen.color());
00293             pen.setColor(color(pen.color(), CurveSymbol));
00294             symbol.setPen(pen);
00295 
00296             QBrush brush = symbol.brush();
00297             cache.curveSymbolBrushColors.insert(c, brush.color());
00298             brush.setColor(color(brush.color(), CurveSymbol));
00299             symbol.setBrush(brush);
00300 
00301             c->setSymbol(symbol);
00302 
00303             pen = c->pen();
00304             cache.curveColors.insert(c, pen.color());
00305             pen.setColor(color(pen.color(), Curve));
00306             c->setPen(pen);
00307 
00308             break;
00309         }
00310         case QwtPlotItem::Rtti_PlotMarker:
00311         {
00312             QwtPlotMarker *m = (QwtPlotMarker *)item;
00313 
00314             cache.markerFonts.insert(m, m->font());
00315             m->setFont(font(m->font(), Marker));
00316 
00317             QPen pen = m->labelPen();
00318             cache.markerLabelColors.insert(m, pen.color());
00319             pen.setColor(color(pen.color(), Marker));
00320             m->setLabelPen(pen);
00321             
00322             pen = m->linePen();
00323             cache.markerLineColors.insert(m, pen.color());
00324             pen.setColor(color(pen.color(), Marker));
00325             m->setLinePen(pen);
00326 
00327             QwtSymbol symbol = m->symbol();
00328 
00329             pen = symbol.pen();
00330             cache.markerSymbolPenColors.insert(m, pen.color());
00331             pen.setColor(color(pen.color(), MarkerSymbol));
00332             symbol.setPen(pen);
00333 
00334             QBrush brush = symbol.brush();
00335             cache.markerSymbolBrushColors.insert(m, brush.color());
00336             brush.setColor(color(brush.color(), MarkerSymbol));
00337             symbol.setBrush(brush);
00338 
00339             m->setSymbol(symbol);
00340 
00341             break;
00342         }
00343         default:    
00344             break;
00345     }
00346 }
00347 
00352 void QwtPlotPrintFilter::reset(QwtPlot *plot) const
00353 {
00354     if ( d_data->cache == 0 )
00355         return;
00356 
00357     const PrivateData::Cache &cache = *d_data->cache;
00358 
00359     if ( plot->titleLabel() )
00360     {
00361         QPalette palette = plot->titleLabel()->palette();
00362         palette.setColor(
00363             QPalette::Active, Palette::Foreground, cache.titleColor);
00364         plot->titleLabel()->setPalette(palette);
00365 
00366         plot->titleLabel()->setFont(cache.titleFont);
00367     }
00368 
00369     if ( plot->legend() )
00370     {
00371 #if QT_VERSION < 0x040000
00372         QValueList<QWidget *> list = plot->legend()->legendItems();
00373         for ( QValueListIterator<QWidget *> it = list.begin();
00374             it != list.end(); ++it )
00375 #else
00376         QList<QWidget *> list = plot->legend()->legendItems();
00377         for ( QList<QWidget*>::iterator it = list.begin();
00378             it != list.end(); ++it )
00379 #endif
00380         {
00381             QWidget *w = *it;
00382 
00383             if ( cache.legendFonts.contains(w) )
00384                 w->setFont(cache.legendFonts[w]);
00385 
00386             if ( w->inherits("QwtLegendItem") )
00387             {
00388                 QwtLegendItem *label = (QwtLegendItem *)w;
00389                 const QwtPlotItem *plotItem = plot->legend()->find(label);
00390 
00391                 QwtSymbol symbol = label->symbol();
00392                 if ( cache.curveSymbolPenColors.contains(plotItem) )
00393                 {
00394                     QPen pen = symbol.pen();
00395                     pen.setColor(cache.curveSymbolPenColors[plotItem]);
00396                     symbol.setPen(pen);
00397                 }
00398 
00399                 if ( cache.curveSymbolBrushColors.contains(plotItem) )
00400                 {
00401                     QBrush brush = symbol.brush();
00402                     brush.setColor(cache.curveSymbolBrushColors[plotItem]);
00403                     symbol.setBrush(brush);
00404                 }
00405                 label->setSymbol(symbol);
00406 
00407                 if ( cache.curveColors.contains(plotItem) )
00408                 {
00409                     QPen pen = label->curvePen();
00410                     pen.setColor(cache.curveColors[plotItem]);
00411                     label->setCurvePen(pen);
00412                 }
00413             }
00414         }
00415     }
00416     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00417     {
00418         QwtScaleWidget *scaleWidget = plot->axisWidget(axis);
00419         if ( scaleWidget )
00420         {
00421             QPalette palette = scaleWidget->palette();
00422             palette.setColor(QPalette::Active, Palette::Foreground,
00423                              cache.scaleColor[axis]);
00424             scaleWidget->setPalette(palette);
00425             scaleWidget->setFont(cache.scaleFont[axis]);
00426 
00427             scaleWidget->setTitleColor(cache.scaleTitleColor[axis]);
00428             scaleWidget->setTitleFont(cache.scaleTitleFont[axis]);
00429 
00430             int startDist, endDist;
00431             scaleWidget->minBorderDist(startDist, endDist);
00432             scaleWidget->setBorderDist(startDist, endDist);
00433         }
00434     }
00435 
00436     QPalette p = plot->palette();
00437     p.setColor(QPalette::Active, Palette::Background, cache.widgetBackground);
00438     plot->setPalette(p);
00439 
00440     plot->setCanvasBackground(cache.canvasBackground);
00441    
00442     const QwtPlotItemList& itmList = plot->itemList();
00443     for ( QwtPlotItemIterator it = itmList.begin();
00444         it != itmList.end(); ++it )
00445     {
00446         reset(*it);
00447     }
00448 
00449     delete d_data->cache;
00450     d_data->cache = 0;
00451 }
00452 
00453 void QwtPlotPrintFilter::reset(QwtPlotItem *item) const
00454 {
00455     if ( d_data->cache == 0 )
00456         return;
00457 
00458     const PrivateData::Cache &cache = *d_data->cache;
00459 
00460     switch(item->rtti())
00461     {
00462         case QwtPlotItem::Rtti_PlotGrid:
00463         {
00464             QwtPlotGrid *grid = (QwtPlotGrid *)item;
00465 
00466             QPen pen = grid->majPen();
00467             pen.setColor(cache.gridColors[0]);
00468             grid->setMajPen(pen);
00469 
00470             pen = grid->minPen();
00471             pen.setColor(cache.gridColors[1]);
00472             grid->setMinPen(pen);
00473 
00474             break;
00475         }
00476         case QwtPlotItem::Rtti_PlotCurve:
00477         {
00478             QwtPlotCurve *c = (QwtPlotCurve *)item;
00479 
00480             QwtSymbol symbol = c->symbol();
00481 
00482             if ( cache.curveSymbolPenColors.contains(c) )
00483             {
00484                 symbol.setPen(cache.curveSymbolPenColors[c]);
00485             }
00486 
00487             if ( cache.curveSymbolBrushColors.contains(c) )
00488             {
00489                 QBrush brush = symbol.brush();
00490                 brush.setColor(cache.curveSymbolBrushColors[c]);
00491                 symbol.setBrush(brush);
00492             }
00493             c->setSymbol(symbol);
00494 
00495             if ( cache.curveColors.contains(c) )
00496             {
00497                 QPen pen = c->pen();
00498                 pen.setColor(cache.curveColors[c]);
00499                 c->setPen(pen);
00500             }
00501 
00502             break;
00503         }
00504         case QwtPlotItem::Rtti_PlotMarker:
00505         {
00506             QwtPlotMarker *m = (QwtPlotMarker *)item;
00507 
00508             if ( cache.markerFonts.contains(m) )
00509                 m->setFont(cache.markerFonts[m]);
00510 
00511             if ( cache.markerLabelColors.contains(m) )
00512             {
00513                 QPen pen = m->labelPen();
00514                 pen.setColor(cache.markerLabelColors[m]);
00515                 m->setLabelPen(pen);
00516             }
00517 
00518             if ( cache.markerLineColors.contains(m) )
00519             {
00520                 QPen pen = m->linePen();
00521                 pen.setColor(cache.markerLineColors[m]);
00522                 m->setLinePen(pen);
00523             }
00524             
00525             QwtSymbol symbol = m->symbol();
00526 
00527             if ( cache.markerSymbolPenColors.contains(m) )
00528             {
00529                 QPen pen = symbol.pen();
00530                 pen.setColor(cache.markerSymbolPenColors[m]);
00531                 symbol.setPen(pen);
00532             }
00533 
00534             if ( cache.markerSymbolBrushColors.contains(m) )
00535             {
00536                 QBrush brush = symbol.brush();
00537                 brush.setColor(cache.markerSymbolBrushColors[m]);
00538                 symbol.setBrush(brush);
00539             }
00540 
00541             m->setSymbol(symbol);
00542 
00543             break;
00544         }
00545         default:
00546             break;
00547     }
00548 }

Generated on Wed Aug 31 23:02:29 2005 for Qwt User's Guide by  doxygen 1.4.1