00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <qlabel.h>
00011 #include "qwt_plot.h"
00012 #include "qwt_plot_dict.h"
00013 #include "qwt_legend.h"
00014 #include "qwt_scale.h"
00015 #include "qwt_plot_printfilter.h"
00016
00017 class QwtPlotPrintFilterCache
00018 {
00019 friend class QwtPlotPrintFilter;
00020
00021 protected:
00022 QwtPlotPrintFilterCache()
00023 {
00024 legendFonts.setAutoDelete(TRUE);
00025 curveColors.setAutoDelete(TRUE);
00026 curveSymbolBrushColors.setAutoDelete(TRUE);
00027 curveSymbolPenColors.setAutoDelete(TRUE);
00028 markerFonts.setAutoDelete(TRUE);
00029 markerLabelColors.setAutoDelete(TRUE);
00030 markerLineColors.setAutoDelete(TRUE);
00031 markerSymbolBrushColors.setAutoDelete(TRUE);
00032 markerSymbolPenColors.setAutoDelete(TRUE);
00033 }
00034
00035 QFont titleFont;
00036
00037 QIntDict<QFont> legendFonts;
00038
00039 QFont scaleTitleFont[4];
00040 QFont scaleFont[4];
00041 QColor scaleColor[4];
00042
00043 QColor canvasBackground;
00044 QColor gridColors[2];
00045
00046 QIntDict<QColor> curveColors;
00047 QIntDict<QColor> curveSymbolBrushColors;
00048 QIntDict<QColor> curveSymbolPenColors;
00049
00050 QIntDict<QFont> markerFonts;
00051 QIntDict<QColor> markerLabelColors;
00052 QIntDict<QColor> markerLineColors;
00053 QIntDict<QColor> markerSymbolBrushColors;
00054 QIntDict<QColor> markerSymbolPenColors;
00055 };
00056
00061 QwtPlotPrintFilter::QwtPlotPrintFilter():
00062 d_options(PrintAll),
00063 d_cache(0)
00064 {
00065 }
00066
00068 QwtPlotPrintFilter::~QwtPlotPrintFilter()
00069 {
00070 delete d_cache;
00071 }
00072
00085 QColor QwtPlotPrintFilter::color(const QColor &c, Item item, int) const
00086 {
00087 if ( !(options() & PrintBackground))
00088 {
00089 switch(item)
00090 {
00091 case MajorGrid:
00092 return Qt::darkGray;
00093 case MinorGrid:
00094 return Qt::gray;
00095 default:;
00096 }
00097 }
00098 return c;
00099 }
00100
00110 QFont QwtPlotPrintFilter::font(const QFont &f, Item, int) const
00111 {
00112 return f;
00113 }
00114
00115 void QwtPlotPrintFilter::apply(QwtPlot *plot) const
00116 {
00117 QwtPlotPrintFilter *that = (QwtPlotPrintFilter *)this;
00118
00119 delete that->d_cache;
00120 that->d_cache = new QwtPlotPrintFilterCache;
00121
00122 QwtPlotPrintFilterCache &cache = *that->d_cache;
00123
00124 if ( plot->d_lblTitle )
00125 {
00126 cache.titleFont = plot->d_lblTitle->font();
00127 plot->d_lblTitle->setFont(font(cache.titleFont, Title));
00128 }
00129 if ( plot->d_legend )
00130 {
00131 QIntDictIterator<QWidget> it = plot->d_legend->itemIterator();
00132 for ( QWidget *w = it.toFirst(); w != 0; w = ++it)
00133 {
00134 const int key = it.currentKey();
00135
00136 cache.legendFonts.insert(it.currentKey(), new QFont(w->font()));
00137 w->setFont(font(w->font(), Legend, key));
00138
00139 if ( w->inherits("QwtLegendButton") )
00140 {
00141 QwtLegendButton *btn = (QwtLegendButton *)w;
00142
00143 QwtSymbol symbol = btn->symbol();
00144 QPen pen = symbol.pen();
00145 QBrush brush = symbol.brush();
00146
00147 pen.setColor(color(pen.color(), CurveSymbol, key));
00148 brush.setColor(color(brush.color(), CurveSymbol, key));
00149
00150 symbol.setPen(pen);
00151 symbol.setBrush(brush);
00152 btn->setSymbol(symbol);
00153
00154 pen = btn->curvePen();
00155 pen.setColor(color(pen.color(), Curve, key));
00156 btn->setCurvePen(pen);
00157 }
00158 }
00159 }
00160 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00161 {
00162 QwtScale *scale = plot->d_scale[axis];
00163 if ( scale )
00164 {
00165 cache.scaleColor[axis] = scale->titleColor();
00166 scale->setTitleColor(color(cache.scaleColor[axis],
00167 AxisTitle, axis));
00168
00169 cache.scaleTitleFont[axis] = scale->titleFont();
00170 scale->setTitleFont(font(cache.scaleTitleFont[axis],
00171 AxisTitle, axis));
00172
00173 cache.scaleFont[axis] = scale->font();
00174 scale->setFont(font(cache.scaleFont[axis], AxisScale, axis));
00175
00176 int startDist, endDist;
00177 scale->minBorderDist(startDist, endDist);
00178 scale->setBorderDist(startDist, endDist);
00179 }
00180 }
00181
00182 cache.canvasBackground = plot->canvasBackground();
00183 plot->setCanvasBackground(color(cache.canvasBackground, CanvasBackground));
00184
00185 QPen pen = plot->d_grid->majPen();
00186 cache.gridColors[0] = pen.color();
00187 pen.setColor(color(pen.color(), MajorGrid));
00188 plot->d_grid->setMajPen(pen);
00189
00190 pen = plot->d_grid->minPen();
00191 cache.gridColors[1] = pen.color();
00192 pen.setColor(color(pen.color(), MinorGrid));
00193 plot->d_grid->setMinPen(pen);
00194
00195 QIntDictIterator<QwtPlotCurve> itc(*plot->d_curves);
00196 for (QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
00197 {
00198 const int key = itc.currentKey();
00199
00200 QwtSymbol symbol = c->symbol();
00201
00202 QPen pen = symbol.pen();
00203 cache.curveSymbolPenColors.insert(key, new QColor(pen.color()));
00204 pen.setColor(color(pen.color(), CurveSymbol, key));
00205 symbol.setPen(pen);
00206
00207 QBrush brush = symbol.brush();
00208 cache.curveSymbolBrushColors.insert(key, new QColor(brush.color()));
00209 brush.setColor(color(brush.color(), CurveSymbol, key));
00210 symbol.setBrush(brush);
00211
00212 c->setSymbol(symbol);
00213
00214 pen = c->pen();
00215 cache.curveColors.insert(key, new QColor(pen.color()));
00216 pen.setColor(color(pen.color(), Curve, key));
00217 c->setPen(pen);
00218 }
00219
00220 QIntDictIterator<QwtPlotMarker> itm(*plot->d_markers);
00221 for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
00222 {
00223 const int key = itm.currentKey();
00224
00225 cache.markerFonts.insert(key, new QFont(m->font()));
00226 m->setFont(font(m->font(), Marker, key));
00227
00228 QPen pen = m->labelPen();
00229 cache.markerLabelColors.insert(key, new QColor(pen.color()));
00230 pen.setColor(color(pen.color(), Marker, key));
00231 m->setLabelPen(pen);
00232
00233 pen = m->linePen();
00234 cache.markerLineColors.insert(key, new QColor(pen.color()));
00235 pen.setColor(color(pen.color(), Marker, key));
00236 m->setLinePen(pen);
00237
00238 QwtSymbol symbol = m->symbol();
00239
00240 pen = symbol.pen();
00241 cache.markerSymbolPenColors.insert(key, new QColor(pen.color()));
00242 pen.setColor(color(pen.color(), MarkerSymbol, key));
00243 symbol.setPen(pen);
00244
00245 QBrush brush = symbol.brush();
00246 cache.markerSymbolBrushColors.insert(key, new QColor(brush.color()));
00247 brush.setColor(color(brush.color(), MarkerSymbol, key));
00248 symbol.setBrush(brush);
00249
00250 m->setSymbol(symbol);
00251 }
00252 }
00253
00254 void QwtPlotPrintFilter::reset(QwtPlot *plot) const
00255 {
00256 if ( d_cache == 0 )
00257 return;
00258
00259 QFont *font;
00260 QColor *color;
00261
00262 if ( plot->d_lblTitle )
00263 plot->d_lblTitle->setFont(d_cache->titleFont);
00264
00265 if ( plot->d_legend )
00266 {
00267 QIntDictIterator<QWidget> it = plot->d_legend->itemIterator();
00268 for ( QWidget *w = it.toFirst(); w != 0; w = ++it)
00269 {
00270 const int key = it.currentKey();
00271
00272 font = d_cache->legendFonts.find(key);
00273 if ( font )
00274 w->setFont(*font);
00275
00276 if ( w->inherits("QwtLegendButton") )
00277 {
00278 QwtLegendButton *btn = (QwtLegendButton *)w;
00279
00280 QwtSymbol symbol = btn->symbol();
00281 color = d_cache->curveSymbolPenColors.find(key);
00282 if ( color )
00283 {
00284 QPen pen = symbol.pen();
00285 pen.setColor(*color);
00286 symbol.setPen(pen);
00287 }
00288
00289 color = d_cache->curveSymbolBrushColors.find(key);
00290 if ( color )
00291 {
00292 QBrush brush = symbol.brush();
00293 brush.setColor(*color);
00294 symbol.setBrush(brush);
00295 }
00296 btn->setSymbol(symbol);
00297
00298 color = d_cache->curveColors.find(key);
00299 if ( color )
00300 {
00301 QPen pen = btn->curvePen();
00302 pen.setColor(*color);
00303 btn->setCurvePen(pen);
00304 }
00305 }
00306 }
00307 }
00308 for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
00309 {
00310 QwtScale *scale = plot->d_scale[axis];
00311 if ( scale )
00312 {
00313 scale->setTitleColor(d_cache->scaleColor[axis]);
00314 scale->setTitleFont(d_cache->scaleTitleFont[axis]);
00315 scale->setFont(d_cache->scaleFont[axis]);
00316
00317 int startDist, endDist;
00318 scale->minBorderDist(startDist, endDist);
00319 scale->setBorderDist(startDist, endDist);
00320 }
00321 }
00322
00323 plot->setCanvasBackground(d_cache->canvasBackground);
00324
00325 QPen pen = plot->d_grid->majPen();
00326 pen.setColor(d_cache->gridColors[0]);
00327 plot->d_grid->setMajPen(pen);
00328
00329 pen = plot->d_grid->minPen();
00330 pen.setColor(d_cache->gridColors[1]);
00331 plot->d_grid->setMinPen(pen);
00332
00333 QIntDictIterator<QwtPlotCurve> itc(*plot->d_curves);
00334 for (QwtPlotCurve *c = itc.toFirst(); c != 0; c = ++itc )
00335 {
00336 const int key = itc.currentKey();
00337
00338 QwtSymbol symbol = c->symbol();
00339
00340 color = d_cache->curveSymbolPenColors.find(key);
00341 if ( color )
00342 {
00343 QPen pen = symbol.pen();
00344 pen.setColor(*color);
00345 symbol.setPen(pen);
00346 }
00347
00348 color = d_cache->curveSymbolBrushColors.find(key);
00349 if ( color )
00350 {
00351 QBrush brush = symbol.brush();
00352 brush.setColor(*color);
00353 symbol.setBrush(brush);
00354 }
00355 c->setSymbol(symbol);
00356
00357 color = d_cache->curveColors.find(key);
00358 if ( color )
00359 {
00360 QPen pen = c->pen();
00361 pen.setColor(*color);
00362 c->setPen(pen);
00363 }
00364 }
00365
00366 QIntDictIterator<QwtPlotMarker> itm(*plot->d_markers);
00367 for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm )
00368 {
00369 const int key = itm.currentKey();
00370
00371 font = d_cache->markerFonts.find(key);
00372 if ( font )
00373 m->setFont(*font);
00374
00375 color = d_cache->markerLabelColors.find(key);
00376 if ( color )
00377 {
00378 QPen pen = m->labelPen();
00379 pen.setColor(*color);
00380 m->setLabelPen(pen);
00381 }
00382
00383 color = d_cache->markerLineColors.find(key);
00384 if ( color )
00385 {
00386 QPen pen = m->linePen();
00387 pen.setColor(*color);
00388 m->setLinePen(pen);
00389 }
00390
00391 QwtSymbol symbol = m->symbol();
00392
00393 color = d_cache->markerSymbolPenColors.find(key);
00394 if ( color )
00395 {
00396 QPen pen = symbol.pen();
00397 pen.setColor(*color);
00398 symbol.setPen(pen);
00399 }
00400
00401 color = d_cache->markerSymbolBrushColors.find(key);
00402 if ( color )
00403 {
00404 QBrush brush = symbol.brush();
00405 brush.setColor(*color);
00406 symbol.setBrush(brush);
00407 }
00408
00409 m->setSymbol(symbol);
00410
00411 }
00412
00413 QwtPlotPrintFilter *that = (QwtPlotPrintFilter *)this;
00414 delete that->d_cache;
00415 that->d_cache = 0;
00416 }