Main Page | Class Hierarchy | Alphabetical List | Class List | 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 "qwt_plot.h"
    00014 #include "qwt_plot_dict.h"
    00015 #include "qwt_legend.h"
    00016 #include "qwt_scale.h"
    00017 #include "qwt_plot_printfilter.h"
    00018
    00019 class QwtPlotPrintFilterCache
    00020 {
    00021     friend class QwtPlotPrintFilter;
    00022
    00023 protected:
    00024     QwtPlotPrintFilterCache()
    00025     {
    00026         legendFonts.setAutoDelete(TRUE);
    00027         curveColors.setAutoDelete(TRUE);
    00028         curveSymbolBrushColors.setAutoDelete(TRUE);
    00029         curveSymbolPenColors.setAutoDelete(TRUE);
    00030         markerFonts.setAutoDelete(TRUE);
    00031         markerLabelColors.setAutoDelete(TRUE);
    00032         markerLineColors.setAutoDelete(TRUE);
    00033         markerSymbolBrushColors.setAutoDelete(TRUE);
    00034         markerSymbolPenColors.setAutoDelete(TRUE);
    00035     }
    00036
    00037     QColor titleColor;
    00038     QFont titleFont;
    00039
    00040     QColor scaleColor[4];
    00041     QFont scaleFont[4];
    00042     QColor scaleTitleColor[4];
    00043     QFont scaleTitleFont[4];
    00044
    00045     QIntDict<QFont> legendFonts;
    00046
    00047     QColor widgetBackground;
    00048     QColor canvasBackground;
    00049     QColor gridColors[2];
    00050
    00051     QIntDict<QColor> curveColors;
    00052     QIntDict<QColor> curveSymbolBrushColors;
    00053     QIntDict<QColor> curveSymbolPenColors;
    00054
    00055     QIntDict<QFont> markerFonts;
    00056     QIntDict<QColor> markerLabelColors;
    00057     QIntDict<QColor> markerLineColors;
    00058     QIntDict<QColor> markerSymbolBrushColors;
    00059     QIntDict<QColor> markerSymbolPenColors;
    00060 };
    00061
    00066 QwtPlotPrintFilter::QwtPlotPrintFilter():
    00067     d_options(PrintAll),
    00068     d_cache(0)
    00069 {
    00070 }
    00071
    00073 QwtPlotPrintFilter::~QwtPlotPrintFilter()
    00074 {
    00075     delete d_cache;
    00076 }
    00077
    00090 QColor QwtPlotPrintFilter::color(const QColor &c, Item item, int) const
    00091 {
    00092     if ( !(options() & PrintCanvasBackground))
    00093     {
    00094         switch(item)
    00095         {
    00096             case MajorGrid:
    00097                 return Qt::darkGray;
    00098             case MinorGrid:
    00099                 return Qt::gray;
    00100             default:;
    00101         }
    00102     }
    00103     return c;
    00104 }
    00105
    00115 QFont QwtPlotPrintFilter::font(const QFont &f, Item, int) const
    00116 {
    00117     return f;
    00118 }
    00119
    00124 void QwtPlotPrintFilter::apply(QwtPlot *plot) const
    00125 {
    00126     QwtPlotPrintFilter *that = (QwtPlotPrintFilter *)this;
    00127
    00128     delete that->d_cache;
    00129     that->d_cache = new QwtPlotPrintFilterCache;
    00130
    00131     QwtPlotPrintFilterCache &cache = *that->d_cache;
    00132
    00133     if ( plot->d_lblTitle )
    00134     {
    00135         QPalette palette = plot->d_lblTitle->palette();
    00136         cache.titleColor = palette.color(
    00137             QPalette::Active, QColorGroup::Foreground);
    00138         palette.setColor(QPalette::Active, QColorGroup::Foreground,
    00139                          color(cache.titleColor, Title));
    00140         plot->d_lblTitle->setPalette(palette);
    00141
    00142         cache.titleFont = plot->d_lblTitle->font();
    00143         plot->d_lblTitle->setFont(font(cache.titleFont, Title));
    00144     }
    00145     if ( plot->d_legend )
    00146     {
    00147         QIntDictIterator<QWidget> it = plot->d_legend->itemIterator();
    00148         for ( QWidget *w = it.toFirst(); w != 0; w = ++it)
    00149         {
    00150             const int key = it.currentKey();
    00151
    00152             cache.legendFonts.insert(it.currentKey(), new QFont(w->font()));
    00153             w->setFont(font(w->font(), Legend, key));
    00154
    00155             if ( w->inherits("QwtLegendButton") )
    00156             {
    00157                 QwtLegendButton *btn = (QwtLegendButton *)w;
    00158
    00159                 QwtSymbol symbol = btn->symbol();
    00160                 QPen pen = symbol.pen();
    00161                 QBrush brush = symbol.brush();
    00162
    00163                 pen.setColor(color(pen.color(), CurveSymbol, key));
    00164                 brush.setColor(color(brush.color(), CurveSymbol, key));
    00165
    00166                 symbol.setPen(pen);
    00167                 symbol.setBrush(brush);
    00168                 btn->setSymbol(symbol);
    00169
    00170                 pen = btn->curvePen();
    00171                 pen.setColor(color(pen.color(), Curve, key));
    00172                 btn->setCurvePen(pen);
    00173             }
    00174         }
    00175     }
    00176     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    00177     {
    00178         QwtScale *scale = plot->d_scale[axis];
    00179         if ( scale )
    00180         {
    00181             cache.scaleColor[axis] = scale->palette().color(
    00182                 QPalette::Active, QColorGroup::Foreground);
    00183             QPalette palette = scale->palette();
    00184             palette.setColor(QPalette::Active, QColorGroup::Foreground,
    00185                              color(cache.scaleColor[axis], AxisScale, axis));
    00186             scale->setPalette(palette);
    00187
    00188             cache.scaleFont[axis] = scale->font();
    00189             scale->setFont(font(cache.scaleFont[axis], AxisScale, axis));
    00190
    00191             cache.scaleTitleColor[axis] = scale->titleColor();
    00192             scale->setTitleColor(
    00193                 color(cache.scaleTitleColor[axis], AxisTitle, axis));
    00194
    00195             cache.scaleTitleFont[axis] = scale->titleFont();
    00196             scale->setTitleFont(
    00197                 font(cache.scaleTitleFont[axis], AxisTitle, axis));
    00198
    00199             int startDist, endDist;
    00200             scale->minBorderDist(startDist, endDist);
    00201             scale->setBorderDist(startDist, endDist);
    00202         }
    00203     }
    00204
    00205     cache.widgetBackground = plot->backgroundColor();
    00206     plot->setBackgroundColor(color(cache.widgetBackground, WidgetBackground));
    00207
    00208     cache.canvasBackground = plot->canvasBackground();
    00209     plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));
    00210
    00211     QPen pen = plot->d_grid->majPen();
    00212     cache.gridColors[0] = pen.color();
    00213     pen.setColor(color(pen.color(), MajorGrid));
    00214     plot->d_grid->setMajPen(pen);
    00215
    00216     pen = plot->d_grid->minPen();
    00217     cache.gridColors[1] = pen.color();
    00218     pen.setColor(color(pen.color(), MinorGrid));
    00219     plot->d_grid->setMinPen(pen);
    00220
    00221     QIntDictIterator<QwtPlotCurve> itc(*plot->d_curves);
    00222     for (QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
    00223     {
    00224         const int key = itc.currentKey();
    00225
    00226         QwtSymbol symbol = c->symbol();
    00227
    00228         QPen pen = symbol.pen();
    00229         cache.curveSymbolPenColors.insert(key, new QColor(pen.color()));
    00230         pen.setColor(color(pen.color(), CurveSymbol, key));
    00231         symbol.setPen(pen);
    00232
    00233         QBrush brush = symbol.brush();
    00234         cache.curveSymbolBrushColors.insert(key, new QColor(brush.color()));
    00235         brush.setColor(color(brush.color(), CurveSymbol, key));
    00236         symbol.setBrush(brush);
    00237
    00238         c->setSymbol(symbol);
    00239
    00240         pen = c->pen();
    00241         cache.curveColors.insert(key, new QColor(pen.color()));
    00242         pen.setColor(color(pen.color(), Curve, key));
    00243         c->setPen(pen);
    00244     }
    00245
    00246     QIntDictIterator<QwtPlotMarker> itm(*plot->d_markers);
    00247     for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
    00248     {
    00249         const int key = itm.currentKey();
    00250
    00251         cache.markerFonts.insert(key, new QFont(m->font()));
    00252         m->setFont(font(m->font(), Marker, key));
    00253
    00254         QPen pen = m->labelPen();
    00255         cache.markerLabelColors.insert(key, new QColor(pen.color()));
    00256         pen.setColor(color(pen.color(), Marker, key));
    00257         m->setLabelPen(pen);
    00258
    00259         pen = m->linePen();
    00260         cache.markerLineColors.insert(key, new QColor(pen.color()));
    00261         pen.setColor(color(pen.color(), Marker, key));
    00262         m->setLinePen(pen);
    00263
    00264         QwtSymbol symbol = m->symbol();
    00265
    00266         pen = symbol.pen();
    00267         cache.markerSymbolPenColors.insert(key, new QColor(pen.color()));
    00268         pen.setColor(color(pen.color(), MarkerSymbol, key));
    00269         symbol.setPen(pen);
    00270
    00271         QBrush brush = symbol.brush();
    00272         cache.markerSymbolBrushColors.insert(key, new QColor(brush.color()));
    00273         brush.setColor(color(brush.color(), MarkerSymbol, key));
    00274         symbol.setBrush(brush);
    00275
    00276         m->setSymbol(symbol);
    00277     }
    00278 }
    00279
    00284 void QwtPlotPrintFilter::reset(QwtPlot *plot) const
    00285 {
    00286     if ( d_cache == 0 )
    00287         return;
    00288
    00289     QFont *font;
    00290     QColor *color;
    00291
    00292     if ( plot->d_lblTitle )
    00293     {
    00294         QPalette palette = plot->d_lblTitle->palette();
    00295         palette.setColor(
    00296             QPalette::Active, QColorGroup::Foreground, d_cache->titleColor);
    00297         plot->d_lblTitle->setPalette(palette);
    00298
    00299         plot->d_lblTitle->setFont(d_cache->titleFont);
    00300     }
    00301
    00302     if ( plot->d_legend )
    00303     {
    00304         QIntDictIterator<QWidget> it = plot->d_legend->itemIterator();
    00305         for ( QWidget *w = it.toFirst(); w != 0; w = ++it)
    00306         {
    00307             const int key = it.currentKey();
    00308
    00309             font = d_cache->legendFonts.find(key);
    00310             if ( font )
    00311                 w->setFont(*font);
    00312
    00313             if ( w->inherits("QwtLegendButton") )
    00314             {
    00315                 QwtLegendButton *btn = (QwtLegendButton *)w;
    00316
    00317                 QwtSymbol symbol = btn->symbol();
    00318                 color = d_cache->curveSymbolPenColors.find(key);
    00319                 if ( color )
    00320                 {
    00321                     QPen pen = symbol.pen();
    00322                     pen.setColor(*color);
    00323                     symbol.setPen(pen);
    00324                 }
    00325
    00326                 color = d_cache->curveSymbolBrushColors.find(key);
    00327                 if ( color )
    00328                 {
    00329                     QBrush brush = symbol.brush();
    00330                     brush.setColor(*color);
    00331                     symbol.setBrush(brush);
    00332                 }
    00333                 btn->setSymbol(symbol);
    00334
    00335                 color = d_cache->curveColors.find(key);
    00336                 if ( color )
    00337                 {
    00338                     QPen pen = btn->curvePen();
    00339                     pen.setColor(*color);
    00340                     btn->setCurvePen(pen);
    00341                 }
    00342             }
    00343         }
    00344     }
    00345     for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    00346     {
    00347         QwtScale *scale = plot->d_scale[axis];
    00348         if ( scale )
    00349         {
    00350             QPalette palette = scale->palette();
    00351             palette.setColor(QPalette::Active, QColorGroup::Foreground,
    00352                              d_cache->scaleColor[axis]);
    00353             scale->setPalette(palette);
    00354             scale->setFont(d_cache->scaleFont[axis]);
    00355
    00356             scale->setTitleColor(d_cache->scaleTitleColor[axis]);
    00357             scale->setTitleFont(d_cache->scaleTitleFont[axis]);
    00358
    00359             int startDist, endDist;
    00360             scale->minBorderDist(startDist, endDist);
    00361             scale->setBorderDist(startDist, endDist);
    00362         }
    00363     }
    00364
    00365     plot->setBackgroundColor(d_cache->widgetBackground);
    00366     plot->setCanvasBackground(d_cache->canvasBackground);
    00367
    00368     QPen pen = plot->d_grid->majPen();
    00369     pen.setColor(d_cache->gridColors[0]);
    00370     plot->d_grid->setMajPen(pen);
    00371
    00372     pen = plot->d_grid->minPen();
    00373     pen.setColor(d_cache->gridColors[1]);
    00374     plot->d_grid->setMinPen(pen);
    00375
    00376     QIntDictIterator<QwtPlotCurve> itc(*plot->d_curves);
    00377     for (QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
    00378     {
    00379         const int key = itc.currentKey();
    00380
    00381         QwtSymbol symbol = c->symbol();
    00382
    00383         color = d_cache->curveSymbolPenColors.find(key);
    00384         if ( color )
    00385         {
    00386             QPen pen = symbol.pen();
    00387             pen.setColor(*color);
    00388             symbol.setPen(pen);
    00389         }
    00390
    00391         color = d_cache->curveSymbolBrushColors.find(key);
    00392         if ( color )
    00393         {
    00394             QBrush brush = symbol.brush();
    00395             brush.setColor(*color);
    00396             symbol.setBrush(brush);
    00397         }
    00398         c->setSymbol(symbol);
    00399
    00400         color = d_cache->curveColors.find(key);
    00401         if ( color )
    00402         {
    00403             QPen pen = c->pen();
    00404             pen.setColor(*color);
    00405             c->setPen(pen);
    00406         }
    00407     }
    00408
    00409     QIntDictIterator<QwtPlotMarker> itm(*plot->d_markers);
    00410     for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
    00411     {
    00412         const int key = itm.currentKey();
    00413
    00414         font = d_cache->markerFonts.find(key);
    00415         if ( font )
    00416             m->setFont(*font);
    00417
    00418         color = d_cache->markerLabelColors.find(key);
    00419         if ( color )
    00420         {
    00421             QPen pen = m->labelPen();
    00422             pen.setColor(*color);
    00423             m->setLabelPen(pen);
    00424         }
    00425
    00426         color = d_cache->markerLineColors.find(key);
    00427         if ( color )
    00428         {
    00429             QPen pen = m->linePen();
    00430             pen.setColor(*color);
    00431             m->setLinePen(pen);
    00432         }
    00433
    00434         QwtSymbol symbol = m->symbol();
    00435
    00436         color = d_cache->markerSymbolPenColors.find(key);
    00437         if ( color )
    00438         {
    00439             QPen pen = symbol.pen();
    00440             pen.setColor(*color);
    00441             symbol.setPen(pen);
    00442         }
    00443
    00444         color = d_cache->markerSymbolBrushColors.find(key);
    00445         if ( color )
    00446         {
    00447             QBrush brush = symbol.brush();
    00448             brush.setColor(*color);
    00449             symbol.setBrush(brush);
    00450         }
    00451
    00452         m->setSymbol(symbol);
    00453
    00454     }
    00455
    00456     QwtPlotPrintFilter *that = (QwtPlotPrintFilter *)this;
    00457     delete that->d_cache;
    00458     that->d_cache = 0;
    00459 }
    00460
    00461 // Local Variables:
    00462 // mode: C++
    00463 // c-file-style: "stroustrup"
    00464 // indent-tabs-mode: nil
    00465 // End: