| 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
|---|
| 2 | * Qwt Widget Library
|
|---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
|---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
|---|
| 5 | *
|
|---|
| 6 | * This library is free software; you can redistribute it and/or
|
|---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
|---|
| 8 | *****************************************************************************/
|
|---|
| 9 |
|
|---|
| 10 | #include "qwt_plot_renderer.h"
|
|---|
| 11 | #include "qwt_plot.h"
|
|---|
| 12 | #include "qwt_painter.h"
|
|---|
| 13 | #include "qwt_plot_canvas.h"
|
|---|
| 14 | #include "qwt_plot_layout.h"
|
|---|
| 15 | #include "qwt_legend.h"
|
|---|
| 16 | #include "qwt_legend_item.h"
|
|---|
| 17 | #include "qwt_dyngrid_layout.h"
|
|---|
| 18 | #include "qwt_scale_widget.h"
|
|---|
| 19 | #include "qwt_scale_engine.h"
|
|---|
| 20 | #include "qwt_text.h"
|
|---|
| 21 | #include "qwt_text_label.h"
|
|---|
| 22 | #include "qwt_math.h"
|
|---|
| 23 | #include <qpainter.h>
|
|---|
| 24 | #include <qpaintengine.h>
|
|---|
| 25 | #include <qtransform.h>
|
|---|
| 26 | #include <qprinter.h>
|
|---|
| 27 | #include <qstyle.h>
|
|---|
| 28 | #include <qstyleoption.h>
|
|---|
| 29 | #include <qimagewriter.h>
|
|---|
| 30 | #include <qfileinfo.h>
|
|---|
| 31 | #ifndef QWT_NO_SVG
|
|---|
| 32 | #ifdef QT_SVG_LIB
|
|---|
| 33 | #include <qsvggenerator.h>
|
|---|
| 34 | #endif
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 37 | class QwtPlotRenderer::PrivateData
|
|---|
| 38 | {
|
|---|
| 39 | public:
|
|---|
| 40 | PrivateData():
|
|---|
| 41 | discardFlags( QwtPlotRenderer::DiscardBackground ),
|
|---|
| 42 | layoutFlags( QwtPlotRenderer::DefaultLayout )
|
|---|
| 43 | {
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | QwtPlotRenderer::DiscardFlags discardFlags;
|
|---|
| 47 | QwtPlotRenderer::LayoutFlags layoutFlags;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | static void qwtRenderBackground( QPainter *painter,
|
|---|
| 51 | const QRectF &rect, const QWidget *widget )
|
|---|
| 52 | {
|
|---|
| 53 | if ( widget->testAttribute( Qt::WA_StyledBackground ) )
|
|---|
| 54 | {
|
|---|
| 55 | QStyleOption opt;
|
|---|
| 56 | opt.initFrom( widget );
|
|---|
| 57 | opt.rect = rect.toAlignedRect();
|
|---|
| 58 |
|
|---|
| 59 | widget->style()->drawPrimitive(
|
|---|
| 60 | QStyle::PE_Widget, &opt, painter, widget);
|
|---|
| 61 | }
|
|---|
| 62 | else
|
|---|
| 63 | {
|
|---|
| 64 | const QBrush brush =
|
|---|
| 65 | widget->palette().brush( widget->backgroundRole() );
|
|---|
| 66 |
|
|---|
| 67 | painter->fillRect( rect, brush );
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /*!
|
|---|
| 72 | Constructor
|
|---|
| 73 | \param parent Parent object
|
|---|
| 74 | */
|
|---|
| 75 | QwtPlotRenderer::QwtPlotRenderer( QObject *parent ):
|
|---|
| 76 | QObject( parent )
|
|---|
| 77 | {
|
|---|
| 78 | d_data = new PrivateData;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | //! Destructor
|
|---|
| 82 | QwtPlotRenderer::~QwtPlotRenderer()
|
|---|
| 83 | {
|
|---|
| 84 | delete d_data;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*!
|
|---|
| 88 | Change a flag, indicating what to discard from rendering
|
|---|
| 89 |
|
|---|
| 90 | \param flag Flag to change
|
|---|
| 91 | \param on On/Off
|
|---|
| 92 |
|
|---|
| 93 | \sa DiscardFlag, testDiscardFlag(), setDiscardFlags(), discardFlags()
|
|---|
| 94 | */
|
|---|
| 95 | void QwtPlotRenderer::setDiscardFlag( DiscardFlag flag, bool on )
|
|---|
| 96 | {
|
|---|
| 97 | if ( on )
|
|---|
| 98 | d_data->discardFlags |= flag;
|
|---|
| 99 | else
|
|---|
| 100 | d_data->discardFlags &= ~flag;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /*!
|
|---|
| 104 | Check if a flag is set.
|
|---|
| 105 |
|
|---|
| 106 | \param flag Flag to be tested
|
|---|
| 107 | \sa DiscardFlag, setDiscardFlag(), setDiscardFlags(), discardFlags()
|
|---|
| 108 | */
|
|---|
| 109 | bool QwtPlotRenderer::testDiscardFlag( DiscardFlag flag ) const
|
|---|
| 110 | {
|
|---|
| 111 | return d_data->discardFlags & flag;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /*!
|
|---|
| 115 | Set the flags, indicating what to discard from rendering
|
|---|
| 116 |
|
|---|
| 117 | \param flags Flags
|
|---|
| 118 | \sa DiscardFlag, setDiscardFlag(), testDiscardFlag(), discardFlags()
|
|---|
| 119 | */
|
|---|
| 120 | void QwtPlotRenderer::setDiscardFlags( DiscardFlags flags )
|
|---|
| 121 | {
|
|---|
| 122 | d_data->discardFlags = flags;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /*!
|
|---|
| 126 | \return Flags, indicating what to discard from rendering
|
|---|
| 127 | \sa DiscardFlag, setDiscardFlags(), setDiscardFlag(), testDiscardFlag()
|
|---|
| 128 | */
|
|---|
| 129 | QwtPlotRenderer::DiscardFlags QwtPlotRenderer::discardFlags() const
|
|---|
| 130 | {
|
|---|
| 131 | return d_data->discardFlags;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*!
|
|---|
| 135 | Change a layout flag
|
|---|
| 136 |
|
|---|
| 137 | \param flag Flag to change
|
|---|
| 138 | \param on On/Off
|
|---|
| 139 |
|
|---|
| 140 | \sa LayoutFlag, testLayoutFlag(), setLayoutFlags(), layoutFlags()
|
|---|
| 141 | */
|
|---|
| 142 | void QwtPlotRenderer::setLayoutFlag( LayoutFlag flag, bool on )
|
|---|
| 143 | {
|
|---|
| 144 | if ( on )
|
|---|
| 145 | d_data->layoutFlags |= flag;
|
|---|
| 146 | else
|
|---|
| 147 | d_data->layoutFlags &= ~flag;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /*!
|
|---|
| 151 | Check if a flag is set.
|
|---|
| 152 |
|
|---|
| 153 | \param flag Flag to be tested
|
|---|
| 154 | \sa LayoutFlag, setLayoutFlag(), setLayoutFlags(), layoutFlags()
|
|---|
| 155 | */
|
|---|
| 156 | bool QwtPlotRenderer::testLayoutFlag( LayoutFlag flag ) const
|
|---|
| 157 | {
|
|---|
| 158 | return d_data->layoutFlags & flag;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /*!
|
|---|
| 162 | Set the layout flags
|
|---|
| 163 |
|
|---|
| 164 | \param flags Flags
|
|---|
| 165 | \sa LayoutFlag, setLayoutFlag(), testLayoutFlag(), layoutFlags()
|
|---|
| 166 | */
|
|---|
| 167 | void QwtPlotRenderer::setLayoutFlags( LayoutFlags flags )
|
|---|
| 168 | {
|
|---|
| 169 | d_data->layoutFlags = flags;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /*!
|
|---|
| 173 | \return Layout flags
|
|---|
| 174 | \sa LayoutFlag, setLayoutFlags(), setLayoutFlag(), testLayoutFlag()
|
|---|
| 175 | */
|
|---|
| 176 | QwtPlotRenderer::LayoutFlags QwtPlotRenderer::layoutFlags() const
|
|---|
| 177 | {
|
|---|
| 178 | return d_data->layoutFlags;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*!
|
|---|
| 182 | Render a plot to a file
|
|---|
| 183 |
|
|---|
| 184 | The format of the document will be autodetected from the
|
|---|
| 185 | suffix of the filename.
|
|---|
| 186 |
|
|---|
| 187 | \param plot Plot widget
|
|---|
| 188 | \param fileName Path of the file, where the document will be stored
|
|---|
| 189 | \param sizeMM Size for the document in millimeters.
|
|---|
| 190 | \param resolution Resolution in dots per Inch (dpi)
|
|---|
| 191 | */
|
|---|
| 192 | void QwtPlotRenderer::renderDocument( QwtPlot *plot,
|
|---|
| 193 | const QString &fileName, const QSizeF &sizeMM, int resolution )
|
|---|
| 194 | {
|
|---|
| 195 | renderDocument( plot, fileName,
|
|---|
| 196 | QFileInfo( fileName ).suffix(), sizeMM, resolution );
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /*!
|
|---|
| 200 | Render a plot to a file
|
|---|
| 201 |
|
|---|
| 202 | Supported formats are:
|
|---|
| 203 |
|
|---|
| 204 | - pdf\n
|
|---|
| 205 | Portable Document Format PDF
|
|---|
| 206 | - ps\n
|
|---|
| 207 | Postcript
|
|---|
| 208 | - svg\n
|
|---|
| 209 | Scalable Vector Graphics SVG
|
|---|
| 210 | - all image formats supported by Qt\n
|
|---|
| 211 | see QImageWriter::supportedImageFormats()
|
|---|
| 212 |
|
|---|
| 213 | Scalable vector graphic formats like PDF or SVG are superior to
|
|---|
| 214 | raster graphics formats.
|
|---|
| 215 |
|
|---|
| 216 | \param plot Plot widget
|
|---|
| 217 | \param fileName Path of the file, where the document will be stored
|
|---|
| 218 | \param format Format for the document
|
|---|
| 219 | \param sizeMM Size for the document in millimeters.
|
|---|
| 220 | \param resolution Resolution in dots per Inch (dpi)
|
|---|
| 221 |
|
|---|
| 222 | \sa renderTo(), render(), QwtPainter::setRoundingAlignment()
|
|---|
| 223 | */
|
|---|
| 224 | void QwtPlotRenderer::renderDocument( QwtPlot *plot,
|
|---|
| 225 | const QString &fileName, const QString &format,
|
|---|
| 226 | const QSizeF &sizeMM, int resolution )
|
|---|
| 227 | {
|
|---|
| 228 | if ( plot == NULL || sizeMM.isEmpty() || resolution <= 0 )
|
|---|
| 229 | return;
|
|---|
| 230 |
|
|---|
| 231 | QString title = plot->title().text();
|
|---|
| 232 | if ( title.isEmpty() )
|
|---|
| 233 | title = "Plot Document";
|
|---|
| 234 |
|
|---|
| 235 | const double mmToInch = 1.0 / 25.4;
|
|---|
| 236 | const QSizeF size = sizeMM * mmToInch * resolution;
|
|---|
| 237 |
|
|---|
| 238 | const QRectF documentRect( 0.0, 0.0, size.width(), size.height() );
|
|---|
| 239 |
|
|---|
| 240 | const QString fmt = format.toLower();
|
|---|
| 241 | if ( fmt == "pdf" || fmt == "ps" )
|
|---|
| 242 | {
|
|---|
| 243 | #ifndef QT_NO_PRINTER
|
|---|
| 244 | QPrinter printer;
|
|---|
| 245 | printer.setFullPage( true );
|
|---|
| 246 | printer.setPaperSize( sizeMM, QPrinter::Millimeter );
|
|---|
| 247 | printer.setDocName( title );
|
|---|
| 248 | printer.setOutputFileName( fileName );
|
|---|
| 249 | printer.setOutputFormat( ( format == "pdf" )
|
|---|
| 250 | ? QPrinter::PdfFormat : QPrinter::PostScriptFormat );
|
|---|
| 251 | printer.setResolution( resolution );
|
|---|
| 252 |
|
|---|
| 253 | QPainter painter( &printer );
|
|---|
| 254 | render( plot, &painter, documentRect );
|
|---|
| 255 | #endif
|
|---|
| 256 | }
|
|---|
| 257 | else if ( fmt == "svg" )
|
|---|
| 258 | {
|
|---|
| 259 | #ifndef QWT_NO_SVG
|
|---|
| 260 | #ifdef QT_SVG_LIB
|
|---|
| 261 | #if QT_VERSION >= 0x040500
|
|---|
| 262 | QSvgGenerator generator;
|
|---|
| 263 | generator.setTitle( title );
|
|---|
| 264 | generator.setFileName( fileName );
|
|---|
| 265 | generator.setResolution( resolution );
|
|---|
| 266 | generator.setViewBox( documentRect );
|
|---|
| 267 |
|
|---|
| 268 | QPainter painter( &generator );
|
|---|
| 269 | render( plot, &painter, documentRect );
|
|---|
| 270 | #endif
|
|---|
| 271 | #endif
|
|---|
| 272 | #endif
|
|---|
| 273 | }
|
|---|
| 274 | else
|
|---|
| 275 | {
|
|---|
| 276 | if ( QImageWriter::supportedImageFormats().indexOf(
|
|---|
| 277 | format.toLatin1() ) >= 0 )
|
|---|
| 278 | {
|
|---|
| 279 | const QRect imageRect = documentRect.toRect();
|
|---|
| 280 | const int dotsPerMeter = qRound( resolution * mmToInch * 1000.0 );
|
|---|
| 281 |
|
|---|
| 282 | QImage image( imageRect.size(), QImage::Format_ARGB32 );
|
|---|
| 283 | image.setDotsPerMeterX( dotsPerMeter );
|
|---|
| 284 | image.setDotsPerMeterY( dotsPerMeter );
|
|---|
| 285 | image.fill( QColor( Qt::white ).rgb() );
|
|---|
| 286 |
|
|---|
| 287 | QPainter painter( &image );
|
|---|
| 288 | render( plot, &painter, imageRect );
|
|---|
| 289 | painter.end();
|
|---|
| 290 |
|
|---|
| 291 | image.save( fileName, format.toLatin1() );
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /*!
|
|---|
| 297 | \brief Render the plot to a \c QPaintDevice
|
|---|
| 298 |
|
|---|
| 299 | This function renders the contents of a QwtPlot instance to
|
|---|
| 300 | \c QPaintDevice object. The target rectangle is derived from
|
|---|
| 301 | its device metrics.
|
|---|
| 302 |
|
|---|
| 303 | \param plot Plot to be rendered
|
|---|
| 304 | \param paintDevice device to paint on, f.e a QImage
|
|---|
| 305 |
|
|---|
| 306 | \sa renderDocument(), render(), QwtPainter::setRoundingAlignment()
|
|---|
| 307 | */
|
|---|
| 308 |
|
|---|
| 309 | void QwtPlotRenderer::renderTo(
|
|---|
| 310 | QwtPlot *plot, QPaintDevice &paintDevice ) const
|
|---|
| 311 | {
|
|---|
| 312 | int w = paintDevice.width();
|
|---|
| 313 | int h = paintDevice.height();
|
|---|
| 314 |
|
|---|
| 315 | QPainter p( &paintDevice );
|
|---|
| 316 | render( plot, &p, QRectF( 0, 0, w, h ) );
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /*!
|
|---|
| 320 | \brief Render the plot to a QPrinter
|
|---|
| 321 |
|
|---|
| 322 | This function renders the contents of a QwtPlot instance to
|
|---|
| 323 | \c QPaintDevice object. The size is derived from the printer
|
|---|
| 324 | metrics.
|
|---|
| 325 |
|
|---|
| 326 | \param plot Plot to be rendered
|
|---|
| 327 | \param printer Printer to paint on
|
|---|
| 328 |
|
|---|
| 329 | \sa renderDocument(), render(), QwtPainter::setRoundingAlignment()
|
|---|
| 330 | */
|
|---|
| 331 |
|
|---|
| 332 | #ifndef QT_NO_PRINTER
|
|---|
| 333 |
|
|---|
| 334 | void QwtPlotRenderer::renderTo(
|
|---|
| 335 | QwtPlot *plot, QPrinter &printer ) const
|
|---|
| 336 | {
|
|---|
| 337 | int w = printer.width();
|
|---|
| 338 | int h = printer.height();
|
|---|
| 339 |
|
|---|
| 340 | QRectF rect( 0, 0, w, h );
|
|---|
| 341 | double aspect = rect.width() / rect.height();
|
|---|
| 342 | if ( ( aspect < 1.0 ) )
|
|---|
| 343 | rect.setHeight( aspect * rect.width() );
|
|---|
| 344 |
|
|---|
| 345 | QPainter p( &printer );
|
|---|
| 346 | render( plot, &p, rect );
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | #endif
|
|---|
| 350 |
|
|---|
| 351 | #ifndef QWT_NO_SVG
|
|---|
| 352 | #ifdef QT_SVG_LIB
|
|---|
| 353 | #if QT_VERSION >= 0x040500
|
|---|
| 354 |
|
|---|
| 355 | /*!
|
|---|
| 356 | \brief Render the plot to a QSvgGenerator
|
|---|
| 357 |
|
|---|
| 358 | If the generator has a view box, the plot will be rendered into it.
|
|---|
| 359 | If it has no viewBox but a valid size the target coordinates
|
|---|
| 360 | will be (0, 0, generator.width(), generator.height()). Otherwise
|
|---|
| 361 | the target rectangle will be QRectF(0, 0, 800, 600);
|
|---|
| 362 |
|
|---|
| 363 | \param plot Plot to be rendered
|
|---|
| 364 | \param generator SVG generator
|
|---|
| 365 | */
|
|---|
| 366 | void QwtPlotRenderer::renderTo(
|
|---|
| 367 | QwtPlot *plot, QSvgGenerator &generator ) const
|
|---|
| 368 | {
|
|---|
| 369 | QRectF rect = generator.viewBoxF();
|
|---|
| 370 | if ( rect.isEmpty() )
|
|---|
| 371 | rect.setRect( 0, 0, generator.width(), generator.height() );
|
|---|
| 372 |
|
|---|
| 373 | if ( rect.isEmpty() )
|
|---|
| 374 | rect.setRect( 0, 0, 800, 600 ); // something
|
|---|
| 375 |
|
|---|
| 376 | QPainter p( &generator );
|
|---|
| 377 | render( plot, &p, rect );
|
|---|
| 378 | }
|
|---|
| 379 | #endif
|
|---|
| 380 | #endif
|
|---|
| 381 | #endif
|
|---|
| 382 |
|
|---|
| 383 | /*!
|
|---|
| 384 | Paint the contents of a QwtPlot instance into a given rectangle.
|
|---|
| 385 |
|
|---|
| 386 | \param plot Plot to be rendered
|
|---|
| 387 | \param painter Painter
|
|---|
| 388 | \param plotRect Bounding rectangle
|
|---|
| 389 |
|
|---|
| 390 | \sa renderDocument(), renderTo(), QwtPainter::setRoundingAlignment()
|
|---|
| 391 | */
|
|---|
| 392 | void QwtPlotRenderer::render( QwtPlot *plot,
|
|---|
| 393 | QPainter *painter, const QRectF &plotRect ) const
|
|---|
| 394 | {
|
|---|
| 395 | int axisId;
|
|---|
| 396 |
|
|---|
| 397 | if ( painter == 0 || !painter->isActive() ||
|
|---|
| 398 | !plotRect.isValid() || plot->size().isNull() )
|
|---|
| 399 | return;
|
|---|
| 400 |
|
|---|
| 401 | if ( !( d_data->discardFlags & DiscardBackground ) )
|
|---|
| 402 | qwtRenderBackground( painter, plotRect, plot );
|
|---|
| 403 |
|
|---|
| 404 | /*
|
|---|
| 405 | The layout engine uses the same methods as they are used
|
|---|
| 406 | by the Qt layout system. Therefore we need to calculate the
|
|---|
| 407 | layout in screen coordinates and paint with a scaled painter.
|
|---|
| 408 | */
|
|---|
| 409 | QTransform transform;
|
|---|
| 410 | transform.scale(
|
|---|
| 411 | double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(),
|
|---|
| 412 | double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() );
|
|---|
| 413 |
|
|---|
| 414 | painter->save();
|
|---|
| 415 |
|
|---|
| 416 | int baseLineDists[QwtPlot::axisCnt];
|
|---|
| 417 | if ( d_data->layoutFlags & FrameWithScales )
|
|---|
| 418 | {
|
|---|
| 419 | for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
|---|
| 420 | {
|
|---|
| 421 | QwtScaleWidget *scaleWidget = plot->axisWidget( axisId );
|
|---|
| 422 | if ( scaleWidget )
|
|---|
| 423 | {
|
|---|
| 424 | baseLineDists[axisId] = scaleWidget->margin();
|
|---|
| 425 | scaleWidget->setMargin( 0 );
|
|---|
| 426 | }
|
|---|
| 427 | }
|
|---|
| 428 | }
|
|---|
| 429 | // Calculate the layout for the print.
|
|---|
| 430 |
|
|---|
| 431 | QwtPlotLayout::Options layoutOptions =
|
|---|
| 432 | QwtPlotLayout::IgnoreScrollbars | QwtPlotLayout::IgnoreFrames;
|
|---|
| 433 | if ( d_data->discardFlags & DiscardLegend )
|
|---|
| 434 | layoutOptions |= QwtPlotLayout::IgnoreLegend;
|
|---|
| 435 |
|
|---|
| 436 | const QRectF layoutRect = transform.inverted().mapRect( plotRect );
|
|---|
| 437 | plot->plotLayout()->activate( plot, layoutRect, layoutOptions );
|
|---|
| 438 |
|
|---|
| 439 | painter->setWorldTransform( transform, true );
|
|---|
| 440 |
|
|---|
| 441 | // canvas
|
|---|
| 442 |
|
|---|
| 443 | QwtScaleMap maps[QwtPlot::axisCnt];
|
|---|
| 444 | buildCanvasMaps( plot, plot->plotLayout()->canvasRect(), maps );
|
|---|
| 445 | renderCanvas( plot, painter, plot->plotLayout()->canvasRect(), maps );
|
|---|
| 446 |
|
|---|
| 447 | if ( !( d_data->discardFlags & DiscardTitle )
|
|---|
| 448 | && ( !plot->titleLabel()->text().isEmpty() ) )
|
|---|
| 449 | {
|
|---|
| 450 | renderTitle( plot, painter, plot->plotLayout()->titleRect() );
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | if ( !( d_data->discardFlags & DiscardLegend )
|
|---|
| 454 | && plot->legend() && !plot->legend()->isEmpty() )
|
|---|
| 455 | {
|
|---|
| 456 | renderLegend( plot, painter, plot->plotLayout()->legendRect() );
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
|---|
| 460 | {
|
|---|
| 461 | QwtScaleWidget *scaleWidget = plot->axisWidget( axisId );
|
|---|
| 462 | if ( scaleWidget )
|
|---|
| 463 | {
|
|---|
| 464 | int baseDist = scaleWidget->margin();
|
|---|
| 465 |
|
|---|
| 466 | int startDist, endDist;
|
|---|
| 467 | scaleWidget->getBorderDistHint( startDist, endDist );
|
|---|
| 468 |
|
|---|
| 469 | renderScale( plot, painter, axisId, startDist, endDist,
|
|---|
| 470 | baseDist, plot->plotLayout()->scaleRect( axisId ) );
|
|---|
| 471 | }
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 | plot->plotLayout()->invalidate();
|
|---|
| 476 |
|
|---|
| 477 | // reset all widgets with their original attributes.
|
|---|
| 478 | if ( d_data->layoutFlags & FrameWithScales )
|
|---|
| 479 | {
|
|---|
| 480 | // restore the previous base line dists
|
|---|
| 481 |
|
|---|
| 482 | for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
|---|
| 483 | {
|
|---|
| 484 | QwtScaleWidget *scaleWidget = plot->axisWidget( axisId );
|
|---|
| 485 | if ( scaleWidget )
|
|---|
| 486 | scaleWidget->setMargin( baseLineDists[axisId] );
|
|---|
| 487 | }
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | painter->restore();
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /*!
|
|---|
| 494 | Render the title into a given rectangle.
|
|---|
| 495 |
|
|---|
| 496 | \param plot Plot widget
|
|---|
| 497 | \param painter Painter
|
|---|
| 498 | \param rect Bounding rectangle
|
|---|
| 499 | */
|
|---|
| 500 | void QwtPlotRenderer::renderTitle( const QwtPlot *plot,
|
|---|
| 501 | QPainter *painter, const QRectF &rect ) const
|
|---|
| 502 | {
|
|---|
| 503 | painter->setFont( plot->titleLabel()->font() );
|
|---|
| 504 |
|
|---|
| 505 | const QColor color = plot->titleLabel()->palette().color(
|
|---|
| 506 | QPalette::Active, QPalette::Text );
|
|---|
| 507 |
|
|---|
| 508 | painter->setPen( color );
|
|---|
| 509 | plot->titleLabel()->text().draw( painter, rect );
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | /*!
|
|---|
| 513 | Render the legend into a given rectangle.
|
|---|
| 514 |
|
|---|
| 515 | \param plot Plot widget
|
|---|
| 516 | \param painter Painter
|
|---|
| 517 | \param rect Bounding rectangle
|
|---|
| 518 | */
|
|---|
| 519 | void QwtPlotRenderer::renderLegend( const QwtPlot *plot,
|
|---|
| 520 | QPainter *painter, const QRectF &rect ) const
|
|---|
| 521 | {
|
|---|
| 522 | if ( !plot->legend() || plot->legend()->isEmpty() )
|
|---|
| 523 | return;
|
|---|
| 524 |
|
|---|
| 525 | if ( !( d_data->discardFlags & DiscardBackground ) )
|
|---|
| 526 | {
|
|---|
| 527 | if ( plot->legend()->autoFillBackground() ||
|
|---|
| 528 | plot->legend()->testAttribute( Qt::WA_StyledBackground ) )
|
|---|
| 529 | {
|
|---|
| 530 | qwtRenderBackground( painter, rect, plot->legend() );
|
|---|
| 531 | }
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | const QwtDynGridLayout *legendLayout = qobject_cast<QwtDynGridLayout *>(
|
|---|
| 535 | plot->legend()->contentsWidget()->layout() );
|
|---|
| 536 | if ( legendLayout == NULL )
|
|---|
| 537 | return;
|
|---|
| 538 |
|
|---|
| 539 | uint numCols = legendLayout->columnsForWidth( rect.width() );
|
|---|
| 540 | QList<QRect> itemRects =
|
|---|
| 541 | legendLayout->layoutItems( rect.toRect(), numCols );
|
|---|
| 542 |
|
|---|
| 543 | int index = 0;
|
|---|
| 544 |
|
|---|
| 545 | for ( int i = 0; i < legendLayout->count(); i++ )
|
|---|
| 546 | {
|
|---|
| 547 | QLayoutItem *item = legendLayout->itemAt( i );
|
|---|
| 548 | QWidget *w = item->widget();
|
|---|
| 549 | if ( w )
|
|---|
| 550 | {
|
|---|
| 551 | painter->save();
|
|---|
| 552 |
|
|---|
| 553 | painter->setClipRect( itemRects[index] );
|
|---|
| 554 | renderLegendItem( plot, painter, w, itemRects[index] );
|
|---|
| 555 |
|
|---|
| 556 | index++;
|
|---|
| 557 | painter->restore();
|
|---|
| 558 | }
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | /*!
|
|---|
| 563 | Render the legend item into a given rectangle.
|
|---|
| 564 |
|
|---|
| 565 | \param plot Plot widget
|
|---|
| 566 | \param painter Painter
|
|---|
| 567 | \param widget Widget representing a legend item
|
|---|
| 568 | \param rect Bounding rectangle
|
|---|
| 569 |
|
|---|
| 570 | \note When widget is not derived from QwtLegendItem renderLegendItem
|
|---|
| 571 | does nothing and needs to be overloaded
|
|---|
| 572 | */
|
|---|
| 573 | void QwtPlotRenderer::renderLegendItem( const QwtPlot *plot,
|
|---|
| 574 | QPainter *painter, const QWidget *widget, const QRectF &rect ) const
|
|---|
| 575 | {
|
|---|
| 576 | if ( !( d_data->discardFlags & DiscardBackground ) )
|
|---|
| 577 | {
|
|---|
| 578 | if ( widget->autoFillBackground() ||
|
|---|
| 579 | widget->testAttribute( Qt::WA_StyledBackground ) )
|
|---|
| 580 | {
|
|---|
| 581 | qwtRenderBackground( painter, rect, widget );
|
|---|
| 582 | }
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | const QwtLegendItem *item = qobject_cast<const QwtLegendItem *>( widget );
|
|---|
| 586 | if ( item )
|
|---|
| 587 | {
|
|---|
| 588 | const QSize sz = item->identifierSize();
|
|---|
| 589 |
|
|---|
| 590 | const QRectF identifierRect( rect.x() + item->margin(),
|
|---|
| 591 | rect.center().y() - 0.5 * sz.height(), sz.width(), sz.height() );
|
|---|
| 592 |
|
|---|
| 593 | QwtLegendItemManager *itemManger = plot->legend()->find( item );
|
|---|
| 594 | if ( itemManger )
|
|---|
| 595 | {
|
|---|
| 596 | painter->save();
|
|---|
| 597 | painter->setClipRect( identifierRect, Qt::IntersectClip );
|
|---|
| 598 | itemManger->drawLegendIdentifier( painter, identifierRect );
|
|---|
| 599 | painter->restore();
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | // Label
|
|---|
| 603 |
|
|---|
| 604 | QRectF titleRect = rect;
|
|---|
| 605 | titleRect.setX( identifierRect.right() + 2 * item->spacing() );
|
|---|
| 606 |
|
|---|
| 607 | painter->setFont( item->font() );
|
|---|
| 608 | item->text().draw( painter, titleRect );
|
|---|
| 609 | }
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | /*!
|
|---|
| 613 | \brief Paint a scale into a given rectangle.
|
|---|
| 614 | Paint the scale into a given rectangle.
|
|---|
| 615 |
|
|---|
| 616 | \param plot Plot widget
|
|---|
| 617 | \param painter Painter
|
|---|
| 618 | \param axisId Axis
|
|---|
| 619 | \param startDist Start border distance
|
|---|
| 620 | \param endDist End border distance
|
|---|
| 621 | \param baseDist Base distance
|
|---|
| 622 | \param rect Bounding rectangle
|
|---|
| 623 | */
|
|---|
| 624 | void QwtPlotRenderer::renderScale( const QwtPlot *plot,
|
|---|
| 625 | QPainter *painter,
|
|---|
| 626 | int axisId, int startDist, int endDist, int baseDist,
|
|---|
| 627 | const QRectF &rect ) const
|
|---|
| 628 | {
|
|---|
| 629 | if ( !plot->axisEnabled( axisId ) )
|
|---|
| 630 | return;
|
|---|
| 631 |
|
|---|
| 632 | const QwtScaleWidget *scaleWidget = plot->axisWidget( axisId );
|
|---|
| 633 | if ( scaleWidget->isColorBarEnabled()
|
|---|
| 634 | && scaleWidget->colorBarWidth() > 0 )
|
|---|
| 635 | {
|
|---|
| 636 | scaleWidget->drawColorBar( painter, scaleWidget->colorBarRect( rect ) );
|
|---|
| 637 |
|
|---|
| 638 | const int off = scaleWidget->colorBarWidth() + scaleWidget->spacing();
|
|---|
| 639 | if ( scaleWidget->scaleDraw()->orientation() == Qt::Horizontal )
|
|---|
| 640 | baseDist += off;
|
|---|
| 641 | else
|
|---|
| 642 | baseDist += off;
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | painter->save();
|
|---|
| 646 |
|
|---|
| 647 | QwtScaleDraw::Alignment align;
|
|---|
| 648 | double x, y, w;
|
|---|
| 649 |
|
|---|
| 650 | switch ( axisId )
|
|---|
| 651 | {
|
|---|
| 652 | case QwtPlot::yLeft:
|
|---|
| 653 | {
|
|---|
| 654 | x = rect.right() - 1.0 - baseDist;
|
|---|
| 655 | y = rect.y() + startDist;
|
|---|
| 656 | w = rect.height() - startDist - endDist;
|
|---|
| 657 | align = QwtScaleDraw::LeftScale;
|
|---|
| 658 | break;
|
|---|
| 659 | }
|
|---|
| 660 | case QwtPlot::yRight:
|
|---|
| 661 | {
|
|---|
| 662 | x = rect.left() + baseDist;
|
|---|
| 663 | y = rect.y() + startDist;
|
|---|
| 664 | w = rect.height() - startDist - endDist;
|
|---|
| 665 | align = QwtScaleDraw::RightScale;
|
|---|
| 666 | break;
|
|---|
| 667 | }
|
|---|
| 668 | case QwtPlot::xTop:
|
|---|
| 669 | {
|
|---|
| 670 | x = rect.left() + startDist;
|
|---|
| 671 | y = rect.bottom() - 1.0 - baseDist;
|
|---|
| 672 | w = rect.width() - startDist - endDist;
|
|---|
| 673 | align = QwtScaleDraw::TopScale;
|
|---|
| 674 | break;
|
|---|
| 675 | }
|
|---|
| 676 | case QwtPlot::xBottom:
|
|---|
| 677 | {
|
|---|
| 678 | x = rect.left() + startDist;
|
|---|
| 679 | y = rect.top() + baseDist;
|
|---|
| 680 | w = rect.width() - startDist - endDist;
|
|---|
| 681 | align = QwtScaleDraw::BottomScale;
|
|---|
| 682 | break;
|
|---|
| 683 | }
|
|---|
| 684 | default:
|
|---|
| 685 | return;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | scaleWidget->drawTitle( painter, align, rect );
|
|---|
| 689 |
|
|---|
| 690 | painter->setFont( scaleWidget->font() );
|
|---|
| 691 |
|
|---|
| 692 | QwtScaleDraw *sd = const_cast<QwtScaleDraw *>( scaleWidget->scaleDraw() );
|
|---|
| 693 | const QPointF sdPos = sd->pos();
|
|---|
| 694 | const double sdLength = sd->length();
|
|---|
| 695 |
|
|---|
| 696 | sd->move( x, y );
|
|---|
| 697 | sd->setLength( w );
|
|---|
| 698 |
|
|---|
| 699 | QPalette palette = scaleWidget->palette();
|
|---|
| 700 | palette.setCurrentColorGroup( QPalette::Active );
|
|---|
| 701 | sd->draw( painter, palette );
|
|---|
| 702 |
|
|---|
| 703 | // reset previous values
|
|---|
| 704 | sd->move( sdPos );
|
|---|
| 705 | sd->setLength( sdLength );
|
|---|
| 706 |
|
|---|
| 707 | painter->restore();
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | /*!
|
|---|
| 711 | Render the canvas into a given rectangle.
|
|---|
| 712 |
|
|---|
| 713 | \param plot Plot widget
|
|---|
| 714 | \param painter Painter
|
|---|
| 715 | \param map Maps mapping between plot and paint device coordinates
|
|---|
| 716 | \param canvasRect Canvas rectangle
|
|---|
| 717 | */
|
|---|
| 718 | void QwtPlotRenderer::renderCanvas( const QwtPlot *plot,
|
|---|
| 719 | QPainter *painter, const QRectF &canvasRect,
|
|---|
| 720 | const QwtScaleMap *map ) const
|
|---|
| 721 | {
|
|---|
| 722 | painter->save();
|
|---|
| 723 |
|
|---|
| 724 | QPainterPath clipPath;
|
|---|
| 725 |
|
|---|
| 726 | QRectF r = canvasRect.adjusted( 0.0, 0.0, -1.0, -1.0 );
|
|---|
| 727 |
|
|---|
| 728 | if ( d_data->layoutFlags & FrameWithScales )
|
|---|
| 729 | {
|
|---|
| 730 | r.adjust( -1.0, -1.0, 1.0, 1.0 );
|
|---|
| 731 | painter->setPen( QPen( Qt::black ) );
|
|---|
| 732 |
|
|---|
| 733 | if ( !( d_data->discardFlags & DiscardCanvasBackground ) )
|
|---|
| 734 | {
|
|---|
| 735 | const QBrush bgBrush =
|
|---|
| 736 | plot->canvas()->palette().brush( plot->backgroundRole() );
|
|---|
| 737 | painter->setBrush( bgBrush );
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | QwtPainter::drawRect( painter, r );
|
|---|
| 741 | }
|
|---|
| 742 | else
|
|---|
| 743 | {
|
|---|
| 744 | if ( !( d_data->discardFlags & DiscardCanvasBackground ) )
|
|---|
| 745 | {
|
|---|
| 746 | qwtRenderBackground( painter, r, plot->canvas() );
|
|---|
| 747 |
|
|---|
| 748 | if ( plot->canvas()->testAttribute( Qt::WA_StyledBackground ) )
|
|---|
| 749 | {
|
|---|
| 750 | // The clip region is calculated in integers
|
|---|
| 751 | // To avoid too much rounding errors better
|
|---|
| 752 | // calculate it in target device resolution
|
|---|
| 753 | // TODO ...
|
|---|
| 754 |
|
|---|
| 755 | int x1 = qCeil( canvasRect.left() );
|
|---|
| 756 | int x2 = qFloor( canvasRect.right() );
|
|---|
| 757 | int y1 = qCeil( canvasRect.top() );
|
|---|
| 758 | int y2 = qFloor( canvasRect.bottom() );
|
|---|
| 759 |
|
|---|
| 760 | clipPath = plot->canvas()->borderPath(
|
|---|
| 761 | QRect( x1, y1, x2 - x1 - 1, y2 - y1 - 1 ) );
|
|---|
| 762 | }
|
|---|
| 763 | }
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | painter->restore();
|
|---|
| 767 |
|
|---|
| 768 | painter->save();
|
|---|
| 769 |
|
|---|
| 770 | if ( clipPath.isEmpty() )
|
|---|
| 771 | painter->setClipRect( canvasRect );
|
|---|
| 772 | else
|
|---|
| 773 | painter->setClipPath( clipPath );
|
|---|
| 774 |
|
|---|
| 775 | plot->drawItems( painter, canvasRect, map );
|
|---|
| 776 |
|
|---|
| 777 | painter->restore();
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | /*!
|
|---|
| 781 | Calculated the scale maps for rendering the canvas
|
|---|
| 782 |
|
|---|
| 783 | \param plot Plot widget
|
|---|
| 784 | \param canvasRect Target rectangle
|
|---|
| 785 | \param maps Scale maps to be calculated
|
|---|
| 786 | */
|
|---|
| 787 | void QwtPlotRenderer::buildCanvasMaps( const QwtPlot *plot,
|
|---|
| 788 | const QRectF &canvasRect, QwtScaleMap maps[] ) const
|
|---|
| 789 | {
|
|---|
| 790 | for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
|
|---|
| 791 | {
|
|---|
| 792 | maps[axisId].setTransformation(
|
|---|
| 793 | plot->axisScaleEngine( axisId )->transformation() );
|
|---|
| 794 |
|
|---|
| 795 | const QwtScaleDiv &scaleDiv = *plot->axisScaleDiv( axisId );
|
|---|
| 796 | maps[axisId].setScaleInterval(
|
|---|
| 797 | scaleDiv.lowerBound(), scaleDiv.upperBound() );
|
|---|
| 798 |
|
|---|
| 799 | double from, to;
|
|---|
| 800 | if ( plot->axisEnabled( axisId ) )
|
|---|
| 801 | {
|
|---|
| 802 | const int sDist = plot->axisWidget( axisId )->startBorderDist();
|
|---|
| 803 | const int eDist = plot->axisWidget( axisId )->endBorderDist();
|
|---|
| 804 | const QRectF &scaleRect = plot->plotLayout()->scaleRect( axisId );
|
|---|
| 805 |
|
|---|
| 806 | if ( axisId == QwtPlot::xTop || axisId == QwtPlot::xBottom )
|
|---|
| 807 | {
|
|---|
| 808 | from = scaleRect.left() + sDist;
|
|---|
| 809 | to = scaleRect.right() - eDist;
|
|---|
| 810 | }
|
|---|
| 811 | else
|
|---|
| 812 | {
|
|---|
| 813 | from = scaleRect.bottom() - eDist;
|
|---|
| 814 | to = scaleRect.top() + sDist;
|
|---|
| 815 | }
|
|---|
| 816 | }
|
|---|
| 817 | else
|
|---|
| 818 | {
|
|---|
| 819 | int margin = plot->plotLayout()->canvasMargin( axisId );
|
|---|
| 820 | if ( axisId == QwtPlot::yLeft || axisId == QwtPlot::yRight )
|
|---|
| 821 | {
|
|---|
| 822 | from = canvasRect.bottom() - margin;
|
|---|
| 823 | to = canvasRect.top() + margin;
|
|---|
| 824 | }
|
|---|
| 825 | else
|
|---|
| 826 | {
|
|---|
| 827 | from = canvasRect.left() + margin;
|
|---|
| 828 | to = canvasRect.right() - margin;
|
|---|
| 829 | }
|
|---|
| 830 | }
|
|---|
| 831 | maps[axisId].setPaintInterval( from, to );
|
|---|
| 832 | }
|
|---|
| 833 | }
|
|---|