21 #include <qpaintengine.h> 22 #include <qtransform.h> 24 #include <qprintdialog.h> 25 #include <qfiledialog.h> 26 #include <qfileinfo.h> 28 #include <qstyleoption.h> 29 #include <qimagewriter.h> 33 #if QT_VERSION >= 0x040500 34 #define QWT_FORMAT_SVG 1 40 #define QWT_FORMAT_PDF 1 49 #if QT_VERSION >= 0x050300 51 #ifndef QWT_FORMAT_PDF 52 #define QWT_FORMAT_PDF 1 55 #define QWT_PDF_WRITER 1 62 #if QT_VERSION < 0x050000 63 #define QWT_FORMAT_POSTSCRIPT 1 68 #include <qsvggenerator.h> 72 #include <qpdfwriter.h> 76 const QWidget* canvas,
const QRectF &canvasRect )
82 int x1 = qCeil( canvasRect.left() );
83 int x2 = qFloor( canvasRect.right() );
84 int y1 = qCeil( canvasRect.top() );
85 int y2 = qFloor( canvasRect.bottom() );
87 const QRect r( x1, y1, x2 - x1 - 1, y2 - y1 - 1 );
89 QPainterPath clipPath;
91 ( void ) QMetaObject::invokeMethod(
92 const_cast< QWidget *>( canvas ),
"borderPath",
94 Q_RETURN_ARG( QPainterPath, clipPath ), Q_ARG( QRect, r ) );
232 const QString &fileName,
const QSizeF &sizeMM,
int resolution )
235 QFileInfo( fileName ).suffix(), sizeMM, resolution );
264 const QString &fileName,
const QString &format,
265 const QSizeF &sizeMM,
int resolution )
267 if ( plot == NULL || sizeMM.isEmpty() || resolution <= 0 )
271 if ( title.isEmpty() )
272 title =
"Plot Document";
274 const double mmToInch = 1.0 / 25.4;
275 const QSizeF
size = sizeMM * mmToInch * resolution;
277 const QRectF documentRect( 0.0, 0.0, size.width(), size.height() );
279 const QString fmt = format.toLower();
280 if ( fmt == QLatin1String(
"pdf" ) )
285 QPdfWriter pdfWriter( fileName );
286 pdfWriter.setPageSizeMM( sizeMM );
287 pdfWriter.setTitle( title );
288 pdfWriter.setPageMargins( QMarginsF() );
289 pdfWriter.setResolution( resolution );
291 QPainter painter( &pdfWriter );
292 render( plot, &painter, documentRect );
295 printer.setOutputFormat( QPrinter::PdfFormat );
296 printer.setColorMode( QPrinter::Color );
297 printer.setFullPage(
true );
298 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
299 printer.setDocName( title );
300 printer.setOutputFileName( fileName );
301 printer.setResolution( resolution );
303 QPainter painter( &printer );
304 render( plot, &painter, documentRect );
308 else if ( fmt == QLatin1String(
"ps" ) )
310 #if QWT_FORMAT_POSTSCRIPT 312 printer.setOutputFormat( QPrinter::PostScriptFormat );
313 printer.setColorMode( QPrinter::Color );
314 printer.setFullPage(
true );
315 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
316 printer.setDocName( title );
317 printer.setOutputFileName( fileName );
318 printer.setResolution( resolution );
320 QPainter painter( &printer );
321 render( plot, &painter, documentRect );
324 else if ( fmt == QLatin1String(
"svg" ) )
327 QSvgGenerator generator;
328 generator.setTitle( title );
329 generator.setFileName( fileName );
330 generator.setResolution( resolution );
331 generator.setViewBox( documentRect );
333 QPainter painter( &generator );
334 render( plot, &painter, documentRect );
339 if ( QImageWriter::supportedImageFormats().indexOf(
340 format.toLatin1() ) >= 0 )
342 const QRect imageRect = documentRect.toRect();
343 const int dotsPerMeter = qRound( resolution * mmToInch * 1000.0 );
345 QImage image( imageRect.size(), QImage::Format_ARGB32 );
346 image.setDotsPerMeterX( dotsPerMeter );
347 image.setDotsPerMeterY( dotsPerMeter );
348 image.fill( QColor( Qt::white ).rgb() );
350 QPainter painter( &image );
351 render( plot, &painter, imageRect );
354 image.save( fileName, format.toLatin1() );
373 QwtPlot *plot, QPaintDevice &paintDevice )
const 375 int w = paintDevice.width();
376 int h = paintDevice.height();
378 QPainter p( &paintDevice );
379 render( plot, &p, QRectF( 0, 0, w, h ) );
395 #ifndef QT_NO_PRINTER 398 QwtPlot *plot, QPrinter &printer )
const 400 int w = printer.width();
401 int h = printer.height();
403 QRectF rect( 0, 0, w, h );
404 double aspect = rect.width() / rect.height();
405 if ( ( aspect < 1.0 ) )
406 rect.setHeight( aspect * rect.width() );
408 QPainter p( &printer );
428 QwtPlot *plot, QSvgGenerator &generator )
const 430 QRectF rect = generator.viewBoxF();
431 if ( rect.isEmpty() )
432 rect.setRect( 0, 0, generator.width(), generator.height() );
434 if ( rect.isEmpty() )
435 rect.setRect( 0, 0, 800, 600 );
437 QPainter p( &generator );
453 QPainter *painter,
const QRectF &plotRect )
const 455 if ( painter == 0 || !painter->isActive() ||
456 !plotRect.isValid() || plot->size().isNull() )
469 QTransform transform;
471 double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(),
472 double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() );
474 QRectF layoutRect = transform.inverted().mapRect( plotRect );
480 int left, top, right, bottom;
481 plot->getContentsMargins( &left, &top, &right, &bottom );
482 layoutRect.adjust( left, top, -right, -bottom );
492 canvasMargins[ axisId ] = layout->
canvasMargin( axisId );
499 baseLineDists[axisId] = scaleWidget->
margin();
517 layoutRect.adjust( 1, 0, 0, 0 );
520 layoutRect.adjust( 0, 0, -1, 0 );
523 layoutRect.adjust( 0, 1, 0, 0 );
526 layoutRect.adjust( 0, 0, 0, -1 );
531 layoutRect.adjust( left, top, right, bottom );
556 layout->
activate( plot, layoutRect, layoutOptions );
567 layout->
activate( plot, layoutRect, layoutOptions );
574 painter->setWorldTransform( transform,
true );
601 int baseDist = scaleWidget->
margin();
603 int startDist, endDist;
606 renderScale( plot, painter, axisId, startDist, endDist,
620 scaleWidget->
setMargin( baseLineDists[axisId] );
638 QPainter *painter,
const QRectF &rect )
const 640 painter->setFont( plot->
titleLabel()->font() );
642 const QColor color = plot->
titleLabel()->palette().color(
643 QPalette::Active, QPalette::Text );
645 painter->setPen( color );
657 QPainter *painter,
const QRectF &rect )
const 661 const QColor color = plot->
footerLabel()->palette().color(
662 QPalette::Active, QPalette::Text );
664 painter->setPen( color );
677 QPainter *painter,
const QRectF &rect )
const 700 int axisId,
int startDist,
int endDist,
int baseDist,
701 const QRectF &rect )
const 723 x = rect.right() - 1.0 - baseDist;
724 y = rect.y() + startDist;
725 w = rect.height() - startDist - endDist;
731 x = rect.left() + baseDist;
732 y = rect.y() + startDist;
733 w = rect.height() - startDist - endDist;
739 x = rect.left() + startDist;
740 y = rect.bottom() - 1.0 - baseDist;
741 w = rect.width() - startDist - endDist;
747 x = rect.left() + startDist;
748 y = rect.top() + baseDist;
749 w = rect.width() - startDist - endDist;
757 scaleWidget->
drawTitle( painter, align, rect );
759 painter->setFont( scaleWidget->font() );
762 const QPointF sdPos = sd->
pos();
763 const double sdLength = sd->
length();
768 QPalette palette = scaleWidget->palette();
769 palette.setCurrentColorGroup( QPalette::Active );
770 sd->
draw( painter, palette );
788 QPainter *painter,
const QRectF &canvasRect,
791 const QWidget *canvas = plot->
canvas();
793 QRectF r = canvasRect.adjusted( 0.0, 0.0, -1.0, -1.0 );
799 r.adjust( -1.0, -1.0, 1.0, 1.0 );
800 painter->setPen( QPen( Qt::black ) );
804 const QBrush bgBrush =
805 canvas->palette().brush( plot->backgroundRole() );
806 painter->setBrush( bgBrush );
814 painter->setClipRect( canvasRect );
815 plot->
drawItems( painter, canvasRect, map );
819 else if ( canvas->testAttribute( Qt::WA_StyledBackground ) )
821 QPainterPath clipPath;
834 if ( clipPath.isEmpty() )
835 painter->setClipRect( canvasRect );
837 painter->setClipPath( clipPath );
839 plot->
drawItems( painter, canvasRect, map );
845 QPainterPath clipPath;
851 const QVariant fw = canvas->property(
"frameWidth" );
852 if ( fw.type() == QVariant::Int )
853 frameWidth = fw.toInt();
858 QRectF innerRect = canvasRect.adjusted(
859 frameWidth, frameWidth, -frameWidth, -frameWidth );
863 if ( clipPath.isEmpty() )
865 painter->setClipRect( innerRect );
869 painter->setClipPath( clipPath );
877 plot->
drawItems( painter, innerRect, map );
881 if ( frameWidth > 0 )
885 const int frameStyle =
886 canvas->property(
"frameShadow" ).toInt() |
887 canvas->property(
"frameShape" ).toInt();
889 const int frameWidth = canvas->property(
"frameWidth" ).toInt();
892 const QVariant borderRadius = canvas->property(
"borderRadius" );
893 if ( borderRadius.type() == QVariant::Double
894 && borderRadius.toDouble() > 0.0 )
896 const double r = borderRadius.toDouble();
899 r, r, canvas->palette(), frameWidth, frameStyle );
903 const int midLineWidth = canvas->property(
"midLineWidth" ).toInt();
906 canvas->palette(), canvas->foregroundRole(),
907 frameWidth, midLineWidth, frameStyle );
922 const QRectF &canvasRect,
QwtScaleMap maps[] )
const 942 from = scaleRect.left() + sDist;
943 to = scaleRect.right() - eDist;
947 from = scaleRect.bottom() - eDist;
948 to = scaleRect.top() + sDist;
959 from = canvasRect.bottom() - margin;
960 to = canvasRect.top() + margin;
964 from = canvasRect.left() + margin;
965 to = canvasRect.right() - margin;
973 const QRectF &canvasRect,
const QwtScaleMap maps[] )
const 980 bool marginsChanged =
false;
983 if ( margins[axisId] >= 0.0 )
985 const int m = qCeil( margins[axisId] );
987 marginsChanged =
true;
991 return marginsChanged;
1006 const QSizeF &sizeMM,
int resolution )
1011 QString fileName = documentName;
1015 #ifndef QT_NO_FILEDIALOG 1016 const QList<QByteArray> imageFormats =
1017 QImageWriter::supportedImageFormats();
1021 filter += QString(
"PDF " ) + tr(
"Documents" ) +
" (*.pdf)";
1024 filter += QString(
"SVG " ) + tr(
"Documents" ) +
" (*.svg)";
1026 #if QWT_FORMAT_POSTSCRIPT 1027 filter += QString(
"Postscript " ) + tr(
"Documents" ) +
" (*.ps)";
1030 if ( imageFormats.size() > 0 )
1032 QString imageFilter( tr(
"Images" ) );
1033 imageFilter +=
" (";
1034 for (
int i = 0;
i < imageFormats.size();
i++ )
1038 imageFilter +=
"*.";
1039 imageFilter += imageFormats[
i];
1043 filter += imageFilter;
1046 fileName = QFileDialog::getSaveFileName(
1047 NULL, tr(
"Export File Name" ), fileName,
1048 filter.join(
";;" ), NULL, QFileDialog::DontConfirmOverwrite );
1050 if ( fileName.isEmpty() )
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.
void setLength(double length)
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
QRectF footerRect() const
void setLayoutFlags(LayoutFlags flags)
void setDiscardFlag(DiscardFlag flag, bool on=true)
QwtPlotRenderer::DiscardFlags discardFlags
Y axis right of the canvas.
static void drawBackgound(QPainter *painter, const QRectF &rect, const QWidget *widget)
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()
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
TFSIMD_FORCE_INLINE const tfScalar & y() const
Y axis left of the canvas.
bool testLayoutFlag(LayoutFlag flag) const
Renderer for exporting a plot to a document, a printer or anything else, that is supported by QPainte...
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
CONSTEXPR_F fields align(second_tag, fields f) noexcept
Don't render the background of the canvas.
virtual bool isEmpty() const =0
Don't render the footer of the plot.
static void drawRoundedFrame(QPainter *, const QRectF &, double xRadius, double yRadius, const QPalette &, int lineWidth, int frameStyle)
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.
int canvasMargin(int axis) const
static void drawRect(QPainter *, double x, double y, double w, double h)
Wrapper for QPainter::drawRect()
virtual void activate(const QwtPlot *, const QRectF &rect, Options options=0x00)
Recalculate the geometry of all components.
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 invalidate()
QFlags< DiscardFlag > DiscardFlags
Disard flags.
TFSIMD_FORCE_INLINE const tfScalar & x() const
QRectF canvasRect() const
static QPainterPath qwtCanvasClip(const QWidget *canvas, const QRectF &canvasRect)
virtual void renderFooter(const QwtPlot *, QPainter *, const QRectF &) const
Don't render the title of the plot.
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.
TFSIMD_FORCE_INLINE const tfScalar & w() const
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
bool alignCanvasToScale(int axisId) const
virtual void renderTitle(const QwtPlot *, QPainter *, const QRectF &) const
Layout engine for QwtPlot.
QwtScaleEngine * axisScaleEngine(int axisId)
virtual void renderScale(const QwtPlot *, QPainter *, int axisId, int startDist, int endDist, int baseDist, const QRectF &) const
Paint a scale into a given rectangle. Paint the scale into a given rectangle.
A class for drawing scales.
Don't render the legend of the plot.
virtual void render(QwtPlot *, QPainter *, const QRectF &rect) const
QFlags< LayoutFlag > LayoutFlags
Layout flags.
QwtPlotLayout * plotLayout()
QFlags< Option > Options
Layout options.
virtual void renderLegend(const QwtPlot *, QPainter *, const QRectF &) const
void move(double x, double y)
QwtPlotRenderer::LayoutFlags layoutFlags
QwtPlotRenderer(QObject *=NULL)