Skip to content
qwt_plot_renderer.cpp 27.5 KiB
Newer Older
Bryant's avatar
Bryant committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_plot_renderer.h"
#include "qwt_plot.h"
#include "qwt_painter.h"
#include "qwt_plot_layout.h"
#include "qwt_abstract_legend.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_engine.h"
#include "qwt_text.h"
#include "qwt_text_label.h"
#include "qwt_math.h"
#include <qpainter.h>
#include <qpaintengine.h>
#include <qtransform.h>
#include <qprinter.h>
#include <qprintdialog.h>
#include <qfiledialog.h>
#include <qfileinfo.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qimagewriter.h>
#ifndef QWT_NO_SVG
#ifdef QT_SVG_LIB
#include <qsvggenerator.h>
#endif
#endif

static QPainterPath qwtCanvasClip( 
    const QWidget* canvas, const QRectF &canvasRect )
{
    // The clip region is calculated in integers
    // To avoid too much rounding errors better
    // calculate it in target device resolution

    int x1 = qCeil( canvasRect.left() );
    int x2 = qFloor( canvasRect.right() );
    int y1 = qCeil( canvasRect.top() );
    int y2 = qFloor( canvasRect.bottom() );

    const QRect r( x1, y1, x2 - x1 - 1, y2 - y1 - 1 );

    QPainterPath clipPath;

    ( void ) QMetaObject::invokeMethod(
        const_cast< QWidget *>( canvas ), "borderPath",
        Qt::DirectConnection,
        Q_RETURN_ARG( QPainterPath, clipPath ), Q_ARG( QRect, r ) );

    return clipPath;
}

class QwtPlotRenderer::PrivateData
{
public:
    PrivateData():
        discardFlags( QwtPlotRenderer::DiscardNone ),
        layoutFlags( QwtPlotRenderer::DefaultLayout )
    {
    }

    QwtPlotRenderer::DiscardFlags discardFlags;
    QwtPlotRenderer::LayoutFlags layoutFlags;
};

/*! 
   Constructor
   \param parent Parent object
*/
QwtPlotRenderer::QwtPlotRenderer( QObject *parent ):
    QObject( parent )
{
    d_data = new PrivateData;
}

//! Destructor
QwtPlotRenderer::~QwtPlotRenderer()
{
    delete d_data;
}

/*!
  Change a flag, indicating what to discard from rendering

  \param flag Flag to change
  \param on On/Off

  \sa DiscardFlag, testDiscardFlag(), setDiscardFlags(), discardFlags()
*/
void QwtPlotRenderer::setDiscardFlag( DiscardFlag flag, bool on )
{
    if ( on )
        d_data->discardFlags |= flag;
    else
        d_data->discardFlags &= ~flag;
}

/*!
  \return True, if flag is enabled.
  \param flag Flag to be tested
  \sa DiscardFlag, setDiscardFlag(), setDiscardFlags(), discardFlags()
*/
bool QwtPlotRenderer::testDiscardFlag( DiscardFlag flag ) const
{
    return d_data->discardFlags & flag;
}

/*!
  Set the flags, indicating what to discard from rendering

  \param flags Flags
  \sa DiscardFlag, setDiscardFlag(), testDiscardFlag(), discardFlags()
*/
void QwtPlotRenderer::setDiscardFlags( DiscardFlags flags )
{
    d_data->discardFlags = flags;
}

/*!
  \return Flags, indicating what to discard from rendering
  \sa DiscardFlag, setDiscardFlags(), setDiscardFlag(), testDiscardFlag()
*/
QwtPlotRenderer::DiscardFlags QwtPlotRenderer::discardFlags() const
{
    return d_data->discardFlags;
}

/*!
  Change a layout flag

  \param flag Flag to change
  \param on On/Off

  \sa LayoutFlag, testLayoutFlag(), setLayoutFlags(), layoutFlags()
*/
void QwtPlotRenderer::setLayoutFlag( LayoutFlag flag, bool on )
{
    if ( on )
        d_data->layoutFlags |= flag;
    else
        d_data->layoutFlags &= ~flag;
}

/*!
  \return True, if flag is enabled.
  \param flag Flag to be tested
  \sa LayoutFlag, setLayoutFlag(), setLayoutFlags(), layoutFlags()
*/
bool QwtPlotRenderer::testLayoutFlag( LayoutFlag flag ) const
{
    return d_data->layoutFlags & flag;
}

/*!
  Set the layout flags

  \param flags Flags
  \sa LayoutFlag, setLayoutFlag(), testLayoutFlag(), layoutFlags()
*/
void QwtPlotRenderer::setLayoutFlags( LayoutFlags flags )
{
    d_data->layoutFlags = flags;
}

/*!
  \return Layout flags
  \sa LayoutFlag, setLayoutFlags(), setLayoutFlag(), testLayoutFlag()
*/
QwtPlotRenderer::LayoutFlags QwtPlotRenderer::layoutFlags() const
{
    return d_data->layoutFlags;
}

/*!
  Render a plot to a file

  The format of the document will be auto-detected from the
  suffix of the file name.

  \param plot Plot widget
  \param fileName Path of the file, where the document will be stored
  \param sizeMM Size for the document in millimeters.
  \param resolution Resolution in dots per Inch (dpi)
*/
void QwtPlotRenderer::renderDocument( QwtPlot *plot,
    const QString &fileName, const QSizeF &sizeMM, int resolution )
{
    renderDocument( plot, fileName,
        QFileInfo( fileName ).suffix(), sizeMM, resolution );
}

/*!
  Render a plot to a file
Loading
Loading full blame...