139 const QString& fileName,
const QString& format,
140 const QSizeF& sizeMM,
int resolution )
142 if ( plot == NULL || sizeMM.isEmpty() || resolution <= 0 )
146 if ( title.isEmpty() )
147 title =
"Plot Document";
149 const double mmToInch = 1.0 / 25.4;
150 const QSizeF size = sizeMM * mmToInch * resolution;
152 const QRectF documentRect( 0.0, 0.0, size.width(), size.height() );
154 const QString fmt = format.toLower();
155 if ( format ==
"pdf" )
159 QPdfWriter pdfWriter( fileName );
160 pdfWriter.setPageSize( QPageSize( sizeMM, QPageSize::Millimeter ) );
161 pdfWriter.setTitle( title );
162 pdfWriter.setPageMargins( QMarginsF() );
163 pdfWriter.setResolution( resolution );
165 QPainter painter( &pdfWriter );
166 render( plot, &painter, documentRect );
170 printer.setOutputFormat( QPrinter::PdfFormat );
171 printer.setColorMode( QPrinter::Color );
172 printer.setFullPage(
true );
173 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
174 printer.setDocName( title );
175 printer.setOutputFileName( fileName );
176 printer.setResolution( resolution );
178 QPainter painter( &printer );
179 render( plot, &painter, documentRect );
183 else if ( format ==
"ps" )
185#if QWT_FORMAT_POSTSCRIPT
187 printer.setColorMode( QPrinter::Color );
188 printer.setFullPage(
true );
189 printer.setPaperSize( sizeMM, QPrinter::Millimeter );
190 printer.setDocName( title );
191 printer.setOutputFileName( fileName );
192 printer.setOutputFormat( QPrinter::PostScriptFormat );
193 printer.setResolution( resolution );
195 QPainter painter( &printer );
196 render( plot, &painter, documentRect );
199 else if ( format ==
"svg" )
202 QSvgGenerator generator;
203 generator.setTitle( title );
204 generator.setFileName( fileName );
205 generator.setResolution( resolution );
206 generator.setViewBox( documentRect );
208 QPainter painter( &generator );
209 render( plot, &painter, documentRect );
214 if ( QImageWriter::supportedImageFormats().indexOf(
215 format.toLatin1() ) >= 0 )
217 const QRect imageRect = documentRect.toRect();
218 const int dotsPerMeter = qRound( resolution * mmToInch * 1000.0 );
220 QImage image( imageRect.size(), QImage::Format_ARGB32 );
221 image.setDotsPerMeterX( dotsPerMeter );
222 image.setDotsPerMeterY( dotsPerMeter );
223 image.fill( QColor( Qt::white ).rgb() );
225 QPainter painter( &image );
226 render( plot, &painter, imageRect );
229 image.save( fileName, format.toLatin1() );
327 QPainter* painter,
const QRectF& plotRect )
const
329 if ( plot == NULL || painter == NULL || !painter->isActive() ||
330 !plotRect.isValid() || plot->size().isNull() )
342 QTransform transform;
344 double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(),
345 double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() );
347 const QRectF layoutRect = transform.inverted().mapRect( plotRect );
357 layout->
activate( plot, layoutRect, layoutOptions );
360 painter->setWorldTransform( transform,
true );
370 const QRectF canvasRect = layout->
canvasRect();
373 painter->setClipRect( canvasRect );
431 const QString& documentName,
const QSizeF& sizeMM,
int resolution )
436 QString fileName = documentName;
440#ifndef QT_NO_FILEDIALOG
442 QImageWriter::supportedImageFormats();
446 filter += QString(
"PDF " ) + tr(
"Documents" ) +
" (*.pdf)";
449 filter += QString(
"SVG " ) + tr(
"Documents" ) +
" (*.svg)";
452 filter += QString(
"Postscript " ) + tr(
"Documents" ) +
" (*.ps)";
455 if ( imageFormats.size() > 0 )
457 QString imageFilter( tr(
"Images" ) );
459 for (
int i = 0; i < imageFormats.size(); i++ )
464 imageFilter += imageFormats[i];
468 filter += imageFilter;
471 fileName = QFileDialog::getSaveFileName(
472 NULL, tr(
"Export File Name" ), fileName,
473 filter.join(
";;" ), NULL, QFileDialog::DontConfirmOverwrite );
475 if ( fileName.isEmpty() )
void draw(QPainter *painter, const QRectF &rect) const
A Widget which displays a QwtText.
const QwtText & text() const
Return the text.