23 #include <qpainterpath.h> 24 #include <qtransform.h> 26 #include <qfiledialog.h> 27 #include <qfileinfo.h> 28 #include <qimagewriter.h> 34 #define QWT_FORMAT_SVG 1 39 #define QWT_FORMAT_PDF 1 48 #if QT_VERSION >= 0x050300 50 #ifndef QWT_FORMAT_PDF 51 #define QWT_FORMAT_PDF 1 54 #define QWT_PDF_WRITER 1 61 #if QT_VERSION < 0x050000 62 #define QWT_FORMAT_POSTSCRIPT 1 67 #include <qsvggenerator.h> 71 #include <qpdfwriter.h> 90 return pal.color( QPalette::WindowText );
94 const QWidget* canvas,
const QRectF &canvasRect )
100 int x1 =
qwtCeil( canvasRect.left() );
101 int x2 =
qwtFloor( canvasRect.right() );
102 int y1 =
qwtCeil( canvasRect.top() );
103 int y2 =
qwtFloor( canvasRect.bottom() );
105 const QRect r( x1, y1, x2 - x1 - 1, y2 - y1 - 1 );
107 QPainterPath clipPath;
109 (
void ) QMetaObject::invokeMethod(
110 const_cast< QWidget *>( canvas ),
"borderPath",
111 Qt::DirectConnection,
112 Q_RETURN_ARG( QPainterPath, clipPath ), Q_ARG( QRect, r ) );
119 QFont font = widget->font();
120 font.resolve( QFont::AllPropertiesResolved );
258 const QString &fileName,
const QSizeF &sizeMM,
int resolution )
261 QFileInfo( fileName ).suffix(), sizeMM, resolution );
290 const QString &fileName,
const QString &
format,
291 const QSizeF &sizeMM,
int resolution )
293 if ( plot == NULL || sizeMM.isEmpty() || resolution <= 0 )
297 if ( title.isEmpty() )
298 title =
"Plot Document";
300 const double mmToInch = 1.0 / 25.4;
301 const QSizeF size = sizeMM * mmToInch * resolution;
303 const QRectF documentRect( 0.0, 0.0, size.width(), size.height() );
305 const QString fmt = format.toLower();
306 if ( fmt == QLatin1String(
"pdf" ) )
311 QPdfWriter pdfWriter( fileName );
312 pdfWriter.setPageSize( QPageSize( sizeMM, QPageSize::Millimeter ) );
313 pdfWriter.setTitle( title );
314 pdfWriter.setPageMargins( QMarginsF() );
315 pdfWriter.setResolution( resolution );
317 QPainter painter( &pdfWriter );
318 render( plot, &painter, documentRect );
321 printer.setOutputFormat( QPrinter::PdfFormat );
322 printer.setColorMode( QPrinter::Color );
323 printer.setFullPage(
true );
324 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
325 printer.setDocName( title );
326 printer.setOutputFileName( fileName );
327 printer.setResolution( resolution );
329 QPainter painter( &printer );
330 render( plot, &painter, documentRect );
334 else if ( fmt == QLatin1String(
"ps" ) )
336 #if QWT_FORMAT_POSTSCRIPT 338 printer.setOutputFormat( QPrinter::PostScriptFormat );
339 printer.setColorMode( QPrinter::Color );
340 printer.setFullPage(
true );
341 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
342 printer.setDocName( title );
343 printer.setOutputFileName( fileName );
344 printer.setResolution( resolution );
346 QPainter painter( &printer );
347 render( plot, &painter, documentRect );
350 else if ( fmt == QLatin1String(
"svg" ) )
353 QSvgGenerator generator;
354 generator.setTitle( title );
355 generator.setFileName( fileName );
356 generator.setResolution( resolution );
357 generator.setViewBox( documentRect );
359 QPainter painter( &generator );
360 render( plot, &painter, documentRect );
365 if ( QImageWriter::supportedImageFormats().indexOf(
366 format.toLatin1() ) >= 0 )
368 const QRect imageRect = documentRect.toRect();
369 const int dotsPerMeter = qRound( resolution * mmToInch * 1000.0 );
371 QImage image( imageRect.size(), QImage::Format_ARGB32 );
372 image.setDotsPerMeterX( dotsPerMeter );
373 image.setDotsPerMeterY( dotsPerMeter );
374 image.fill( QColor( Qt::white ).rgb() );
376 QPainter painter( &image );
377 render( plot, &painter, imageRect );
380 image.save( fileName, format.toLatin1() );
399 QwtPlot *plot, QPaintDevice &paintDevice )
const 401 int w = paintDevice.width();
402 int h = paintDevice.height();
404 QPainter p( &paintDevice );
405 render( plot, &p, QRectF( 0, 0, w, h ) );
421 #ifndef QT_NO_PRINTER 424 QwtPlot *plot, QPrinter &printer )
const 426 int w = printer.width();
427 int h = printer.height();
429 QRectF rect( 0, 0, w, h );
430 double aspect = rect.width() / rect.height();
431 if ( ( aspect < 1.0 ) )
432 rect.setHeight( aspect * rect.width() );
434 QPainter p( &printer );
454 QwtPlot *plot, QSvgGenerator &generator )
const 456 QRectF rect = generator.viewBoxF();
457 if ( rect.isEmpty() )
458 rect.setRect( 0, 0, generator.width(), generator.height() );
460 if ( rect.isEmpty() )
461 rect.setRect( 0, 0, 800, 600 );
463 QPainter p( &generator );
479 QPainter *painter,
const QRectF &plotRect )
const 481 if ( painter == 0 || !painter->isActive() ||
482 !plotRect.isValid() || plot->size().isNull() )
495 QTransform transform;
497 double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(),
498 double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() );
500 QRectF layoutRect = transform.inverted().mapRect( plotRect );
506 const QMargins m = plot->contentsMargins();
507 layoutRect.adjust( m.left(), m.top(), -m.right(), -m.bottom() );
517 canvasMargins[ axisId ] = layout->
canvasMargin( axisId );
524 baseLineDists[axisId] = scaleWidget->
margin();
539 layoutRect.adjust( fw, 0, 0, 0 );
542 layoutRect.adjust( 0, 0, -fw, 0 );
545 layoutRect.adjust( 0, fw, 0, 0 );
548 layoutRect.adjust( 0, 0, 0, -fw );
576 layout->
activate( plot, layoutRect, layoutOptions );
587 layout->
activate( plot, layoutRect, layoutOptions );
594 painter->setWorldTransform( transform,
true );
621 int baseDist = scaleWidget->
margin();
623 int startDist, endDist;
626 renderScale( plot, painter, axisId, startDist, endDist,
640 scaleWidget->
setMargin( baseLineDists[axisId] );
658 QPainter *painter,
const QRectF &titleRect )
const 662 const QColor color = plot->
titleLabel()->palette().color(
663 QPalette::Active, QPalette::Text );
665 painter->setPen( color );
677 QPainter *painter,
const QRectF &footerRect )
const 681 const QColor color = plot->
footerLabel()->palette().color(
682 QPalette::Active, QPalette::Text );
684 painter->setPen( color );
696 QPainter *painter,
const QRectF &legendRect )
const 718 int axisId,
int startDist,
int endDist,
int baseDist,
719 const QRectF &scaleRect )
const 745 x = scaleRect.right() - 1.0 - baseDist - off;
746 y = scaleRect.y() + startDist;
747 w = scaleRect.height() - startDist - endDist;
753 x = scaleRect.left() + baseDist + off;
754 y = scaleRect.y() + startDist;
755 w = scaleRect.height() - startDist - endDist;
761 x = scaleRect.left() + startDist;
762 y = scaleRect.bottom() - 1.0 - baseDist - off;
763 w = scaleRect.width() - startDist - endDist;
769 x = scaleRect.left() + startDist;
770 y = scaleRect.top() + baseDist + off;
771 w = scaleRect.width() - startDist - endDist;
779 scaleWidget->
drawTitle( painter, align, scaleRect );
784 const QPointF sdPos = sd->
pos();
785 const double sdLength = sd->
length();
795 QPalette palette = scaleWidget->palette();
796 palette.setCurrentColorGroup( QPalette::Active );
797 sd->
draw( painter, palette );
816 QPainter *painter,
const QRectF &canvasRect,
819 const QWidget *canvas = plot->
canvas();
821 QRectF r = canvasRect.adjusted( 0.0, 0.0, -1.0, -1.0 );
830 pen.setJoinStyle( Qt::MiterJoin );
832 painter->setPen( pen );
834 const qreal pw2 = 0.5 * pen.widthF();
835 r.adjust( -pw2, -pw2, pw2, pw2 );
839 const QBrush bgBrush =
840 canvas->palette().brush( plot->backgroundRole() );
841 painter->setBrush( bgBrush );
849 painter->setClipRect( canvasRect );
850 plot->
drawItems( painter, canvasRect, maps );
854 else if ( canvas->testAttribute( Qt::WA_StyledBackground ) )
856 QPainterPath clipPath;
869 if ( clipPath.isEmpty() )
870 painter->setClipRect( canvasRect );
872 painter->setClipPath( clipPath );
874 plot->
drawItems( painter, canvasRect, maps );
880 QPainterPath clipPath;
886 const QVariant fw = canvas->property(
"frameWidth" );
887 if ( fw.type() == QVariant::Int )
888 frameWidth = fw.toInt();
893 QRectF innerRect = canvasRect.adjusted(
894 frameWidth, frameWidth, -frameWidth, -frameWidth );
898 if ( clipPath.isEmpty() )
900 painter->setClipRect( innerRect );
904 painter->setClipPath( clipPath );
912 plot->
drawItems( painter, innerRect, maps );
916 if ( frameWidth > 0 )
920 const int frameStyle =
921 canvas->property(
"frameShadow" ).toInt() |
922 canvas->property(
"frameShape" ).toInt();
924 const QVariant borderRadius = canvas->property(
"borderRadius" );
925 if ( borderRadius.type() == QVariant::Double
926 && borderRadius.toDouble() > 0.0 )
928 const double radius = borderRadius.toDouble();
931 radius, radius, canvas->palette(), frameWidth, frameStyle );
935 const int midLineWidth = canvas->property(
"midLineWidth" ).toInt();
938 canvas->palette(), canvas->foregroundRole(),
939 frameWidth, midLineWidth, frameStyle );
954 const QRectF &canvasRect,
QwtScaleMap maps[] )
const 974 from = scaleRect.left() + sDist;
975 to = scaleRect.right() - eDist;
979 from = scaleRect.bottom() - eDist;
980 to = scaleRect.top() + sDist;
991 from = canvasRect.bottom() - margin;
992 to = canvasRect.top() + margin;
996 from = canvasRect.left() + margin;
997 to = canvasRect.right() - margin;
1005 const QRectF &canvasRect,
const QwtScaleMap maps[] )
const 1012 bool marginsChanged =
false;
1015 if ( margins[axisId] >= 0.0 )
1017 const int m =
qwtCeil( margins[axisId] );
1019 marginsChanged =
true;
1023 return marginsChanged;
1038 const QSizeF &sizeMM,
int resolution )
1043 QString fileName = documentName;
1047 #ifndef QT_NO_FILEDIALOG 1049 QImageWriter::supportedImageFormats();
1053 filter += QString(
"PDF " ) + tr(
"Documents" ) +
" (*.pdf)";
1056 filter += QString(
"SVG " ) + tr(
"Documents" ) +
" (*.svg)";
1058 #if QWT_FORMAT_POSTSCRIPT 1059 filter += QString(
"Postscript " ) + tr(
"Documents" ) +
" (*.ps)";
1062 if ( imageFormats.size() > 0 )
1064 QString imageFilter( tr(
"Images" ) );
1065 imageFilter +=
" (";
1066 for (
int i = 0; i < imageFormats.size(); i++ )
1070 imageFilter +=
"*.";
1071 imageFilter += imageFormats[i];
1075 filter += imageFilter;
1078 fileName = QFileDialog::getSaveFileName(
1079 NULL, tr(
"Export File Name" ), fileName,
1080 filter.join(
";;" ), NULL, QFileDialog::DontConfirmOverwrite );
1082 if ( fileName.isEmpty() )
1091 #include "moc_qwt_plot_renderer.cpp"
bool exportTo(QwtPlot *, const QString &documentName, const QSizeF &sizeMM=QSizeF(300, 200), int resolution=85)
Execute a file dialog and render the plot to the selected file.
bool updateCanvasMargins(QwtPlot *, const QRectF &, const QwtScaleMap maps[]) const
const QwtScaleDiv & axisScaleDiv(int axisId) const
Return the scale division of a specified axis.
int canvasMargin(int axisId) const
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
void setLength(double length)
virtual void activate(const QwtPlot *, const QRectF &plotRect, Options options=Options())
Recalculate the geometry of all components.
Render all components of the plot.
QRectF legendRect() const
QwtTextLabel * titleLabel()
virtual void getCanvasMarginsHint(const QwtScaleMap maps[], const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const
Calculate the canvas margins.
virtual void drawItems(QPainter *, const QRectF &, const QwtScaleMap maps[axisCnt]) const
virtual void renderScale(const QwtPlot *, QPainter *, int axisId, int startDist, int endDist, int baseDist, const QRectF &scaleRect) const
Paint a scale into a given rectangle. Paint the scale into a given rectangle.
QRectF footerRect() const
void setLayoutFlags(LayoutFlags flags)
void setDiscardFlag(DiscardFlag flag, bool on=true)
QwtPlotRenderer::DiscardFlags discardFlags
Y axis right of the canvas.
void draw(QPainter *painter, const QRectF &rect) const
const QwtText & text() const
Return the text.
A class representing a scale division.
void setCanvasMargin(int margin, int axis=-1)
QwtTextLabel * footerLabel()
static qreal qwtScalePenWidth(const QwtPlot *plot)
Use the default layout as on screen.
bool axisEnabled(int axisId) const
void setScaleInterval(double s1, double s2)
Specify the borders of the scale interval.
QwtTransform * transformation() const
virtual void renderTitle(const QwtPlot *, QPainter *, const QRectF &titleRect) const
Y axis left of the canvas.
static void drawRoundedFrame(QPainter *, const QRectF &, qreal xRadius, qreal yRadius, const QPalette &, int lineWidth, int frameStyle)
bool testLayoutFlag(LayoutFlag flag) const
Renderer for exporting a plot to a document, a printer or anything else, that is supported by QPainte...
int qwtFloor(qreal value)
void buildCanvasMaps(const QwtPlot *, const QRectF &, QwtScaleMap maps[]) const
double upperBound() const
const QwtScaleWidget * axisWidget(int axisId) const
bool testDiscardFlag(DiscardFlag flag) const
QwtAbstractLegend * legend()
LayoutFlags layoutFlags() const
virtual void renderCanvas(const QwtPlot *, QPainter *, const QRectF &canvasRect, const QwtScaleMap *maps) const
static QColor qwtScalePenColor(const QwtPlot *plot)
Don't render the background of the canvas.
virtual bool isEmpty() const =0
Don't render the footer of the plot.
void * align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space, std::size_t &required_space)
QRectF scaleRect(int axis) const
DiscardFlags discardFlags() const
void setLayoutFlag(LayoutFlag flag, bool on=true)
void renderTo(QwtPlot *, QPrinter &) const
Render the plot to a QPrinter.
double lowerBound() const
void setTransformation(QwtTransform *)
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
void setDiscardFlags(DiscardFlags flags)
static void drawFrame(QPainter *, const QRectF &rect, const QPalette &palette, QPalette::ColorRole foregroundRole, int lineWidth, int midLineWidth, int frameStyle)
virtual void renderLegend(const QwtPlot *, QPainter *, const QRectF &legendRect) const
virtual void invalidate()
QFlags< DiscardFlag > DiscardFlags
Disard flags.
virtual void renderFooter(const QwtPlot *, QPainter *, const QRectF &footerRect) const
QRectF canvasRect() const
Backbone = the line where the ticks are located.
static void drawBackgound(QPainter *, const QRectF &, const QWidget *)
static QFont qwtResolvedFont(const QWidget *widget)
static QPainterPath qwtCanvasClip(const QWidget *canvas, const QRectF &canvasRect)
Don't render the title of the plot.
void enableComponent(ScaleComponent, bool enable=true)
virtual void renderLegend(QPainter *painter, const QRectF &rect, bool fillBackground) const =0
Don't render the background of the plot.
void renderDocument(QwtPlot *, const QString &fileName, const QSizeF &sizeMM, int resolution=85)
virtual ~QwtPlotRenderer()
Destructor.
static void drawRect(QPainter *, qreal x, qreal y, qreal w, qreal h)
Wrapper for QPainter::drawRect()
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
bool alignCanvasToScale(int axisId) const
const QwtScaleDraw * axisScaleDraw(int axisId) const
Return the scale draw of a specified axis.
Layout engine for QwtPlot.
QwtScaleEngine * axisScaleEngine(int axisId)
bool hasComponent(ScaleComponent) const
A class for drawing scales.
Don't render the legend of the plot.
QFlags< LayoutFlag > LayoutFlags
Layout flags.
virtual void render(QwtPlot *, QPainter *, const QRectF &plotRect) const
QwtPlotLayout * plotLayout()
QFlags< Option > Options
Layout options.
void move(double x, double y)
QwtPlotRenderer::LayoutFlags layoutFlags
QwtPlotRenderer(QObject *=NULL)