VectorPlot::VectorPlot(QWidget *parent, const QString &title, SetLegend * externalLegend) : QWidget(parent), legend(externalLegend) { this->setMinimumHeight(200); QwtText titleText(title); QFont titleTextFont(titleText.font()); titleTextFont.setPointSize(10); titleText.setFont(titleTextFont); plot = new QwtPlot(titleText, this); plot->setCanvasBackground(Qt::white); //plot->setGeometry(ui.widget_plot->geometry()); plot->setAxisScale(QwtPlot::xBottom, 0, 0.01); plot->setAxisScale(QwtPlot::yLeft, 0, 0.01); for ( int i = 0; i < QwtPlot::axisCnt; i++ ) { plot->setAxisAutoScale(i); QwtScaleWidget *scaleWidget = (QwtScaleWidget *)plot->axisWidget(i); if ( scaleWidget ) scaleWidget->setMargin(0); QwtScaleDraw *scaleDraw = (QwtScaleDraw *)plot->axisScaleDraw(i); if ( scaleDraw ) scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false); } plot->canvas()->setFrameStyle(QFrame::Box|QFrame::Plain); plot->setAutoReplot(true); //plot->setMinimumSize(QSize(200, 200)); //this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); plot->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); QPen pen(QColor(255, 0, 0)); marker1 = new QwtPlotMarker(); marker1->setLineStyle(QwtPlotMarker::VLine); marker1->setLinePen(pen); marker1->setXValue(0); marker2 = new QwtPlotMarker(); marker2->setLineStyle(QwtPlotMarker::VLine); marker2->setLinePen(pen); marker2->setXValue(0); zoomer = 0; }
// // Set a plain canvas frame and align the scales to it // void DataPlot::alignScales() { canvas()->setFrameStyle(QFrame::Box | QFrame::Plain ); canvas()->setLineWidth(1); for ( int i = 0; i < QwtPlot::axisCnt; i++ ) { QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i); if ( scaleWidget ) scaleWidget->setMargin(0); QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i); if ( scaleDraw ) scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false); } }
static void enablePlotAxis( QwtPlot *plot, int axis, bool on ) { // when false we still enable the axis to have an effect // of the minimal extent active. Instead we hide all visible // parts and margins/spacings. plot->enableAxis( axis, true ); QwtScaleDraw *sd = plot->axisScaleDraw( axis ); sd->enableComponent( QwtScaleDraw::Backbone, on ); sd->enableComponent( QwtScaleDraw::Ticks, on ); sd->enableComponent( QwtScaleDraw::Labels, on ); QwtScaleWidget* sw = plot->axisWidget( axis ); sw->setMargin( on ? 4 : 0 ); sw->setSpacing( on ? 20 : 0 ); }
void XYZPlot::alignScales() { // The code below shows how to align the scales to // the canvas frame, but is also a good example demonstrating // why the spreaded API needs polishing. canvas()->setFrameStyle(QFrame::Box | QFrame::Plain); canvas()->setLineWidth(1); for (int i = 0; i < QwtPlot::axisCnt; i++) { QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i); if (scaleWidget) scaleWidget->setMargin(0); QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i); if (scaleDraw) scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false); } }
// // Set a plain canvas frame and align the scales to it // void Plotter::alignScales() { // The code below shows how to align the scales to // the canvas frame, but is also a good example demonstrating // why the spreaded API needs polishing. for ( int i = 0; i < QwtPlot::axisCnt; i++ ) { QwtScaleWidget *scaleWidget = axisWidget( i ); if ( scaleWidget ) scaleWidget->setMargin( 0 ); QwtScaleDraw *scaleDraw = axisScaleDraw( i ); if ( scaleDraw ) scaleDraw->enableComponent( QwtAbstractScaleDraw::Backbone, false ); } plotLayout()->setAlignCanvasToScales( true ); }
/*! \brief Paint the plot into a given rectangle. Paint the contents of a QwtPlot instance into a given rectangle. \param painter Painter \param plotRect Bounding rectangle \param pfilter Print filter \sa QwtPlotPrintFilter */ void QwtPlot::print(QPainter *painter, const QRect &plotRect, const QwtPlotPrintFilter &pfilter) const { int axisId; if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || size().isNull() ) return; painter->save(); #if 1 /* PDF: In Qt4 ( <= 4.3.2 ) the scales are painted in gray instead of black. See http://trolltech.com/developer/task-tracker/index_html?id=184671&method=entry The dummy lines below work around the problem. */ const QPen pen = painter->pen(); painter->setPen(QPen(Qt::black, 1)); painter->setPen(pen); #endif // All paint operations need to be scaled according to // the paint device metrics. QwtPainter::setMetricsMap(this, painter->device()); const QwtMetricsMap &metricsMap = QwtPainter::metricsMap(); // It is almost impossible to integrate into the Qt layout // framework, when using different fonts for printing // and screen. To avoid writing different and Qt unconform // layout engines we change the widget attributes, print and // reset the widget attributes again. This way we produce a lot of // useless layout events ... pfilter.apply((QwtPlot *)this); int baseLineDists[QwtPlot::axisCnt]; if ( pfilter.options() & QwtPlotPrintFilter::PrintFrameWithScales ) { for (axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if ( scaleWidget ) { baseLineDists[axisId] = scaleWidget->margin(); scaleWidget->setMargin(0); } } } // Calculate the layout for the print. int layoutOptions = QwtPlotLayout::IgnoreScrollbars | QwtPlotLayout::IgnoreFrames; if ( !(pfilter.options() & QwtPlotPrintFilter::PrintMargin) ) layoutOptions |= QwtPlotLayout::IgnoreMargin; if ( !(pfilter.options() & QwtPlotPrintFilter::PrintLegend) ) layoutOptions |= QwtPlotLayout::IgnoreLegend; ((QwtPlot *)this)->plotLayout()->activate(this, QwtPainter::metricsMap().deviceToLayout(plotRect), layoutOptions); if ((pfilter.options() & QwtPlotPrintFilter::PrintTitle) && (!titleLabel()->text().isEmpty())) { printTitle(painter, plotLayout()->titleRect()); } if ( (pfilter.options() & QwtPlotPrintFilter::PrintLegend) && legend() && !legend()->isEmpty() ) { printLegend(painter, plotLayout()->legendRect()); } for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if (scaleWidget) { int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint(startDist, endDist); printScale(painter, axisId, startDist, endDist, baseDist, plotLayout()->scaleRect(axisId)); } } QRect canvasRect = plotLayout()->canvasRect(); /* The border of the bounding rect needs to ba scaled to layout coordinates, so that it is aligned to the axes */ QRect boundingRect( canvasRect.left() - 1, canvasRect.top() - 1, canvasRect.width() + 2, canvasRect.height() + 2); boundingRect = metricsMap.layoutToDevice(boundingRect); boundingRect.setWidth(boundingRect.width() - 1); boundingRect.setHeight(boundingRect.height() - 1); canvasRect = metricsMap.layoutToDevice(canvasRect); // When using QwtPainter all sizes where computed in pixel // coordinates and scaled by QwtPainter later. This limits // the precision to screen resolution. A better solution // is to scale the maps and print in unlimited resolution. QwtScaleMap map[axisCnt]; for (axisId = 0; axisId < axisCnt; axisId++) { map[axisId].setTransformation(axisScaleEngine(axisId)->transformation()); const QwtScaleDiv &scaleDiv = *axisScaleDiv(axisId); map[axisId].setScaleInterval(scaleDiv.lBound(), scaleDiv.hBound()); double from, to; if ( axisEnabled(axisId) ) { const int sDist = axisWidget(axisId)->startBorderDist(); const int eDist = axisWidget(axisId)->endBorderDist(); const QRect &scaleRect = plotLayout()->scaleRect(axisId); if ( axisId == xTop || axisId == xBottom ) { from = metricsMap.layoutToDeviceX(scaleRect.left() + sDist); to = metricsMap.layoutToDeviceX(scaleRect.right() + 1 - eDist); } else { from = metricsMap.layoutToDeviceY(scaleRect.bottom() + 1 - eDist ); to = metricsMap.layoutToDeviceY(scaleRect.top() + sDist); } } else { int margin = plotLayout()->canvasMargin(axisId); if ( axisId == yLeft || axisId == yRight ) { margin = metricsMap.layoutToDeviceY(margin); from = canvasRect.bottom() - margin; to = canvasRect.top() + margin; } else { margin = metricsMap.layoutToDeviceX(margin); from = canvasRect.left() + margin; to = canvasRect.right() - margin; } } map[axisId].setPaintXInterval(from, to); } // The canvas maps are already scaled. QwtPainter::setMetricsMap(painter->device(), painter->device()); printCanvas(painter, boundingRect, canvasRect, map, pfilter); QwtPainter::resetMetricsMap(); ((QwtPlot *)this)->plotLayout()->invalidate(); // reset all widgets with their original attributes. if ( pfilter.options() & QwtPlotPrintFilter::PrintFrameWithScales ) { // restore the previous base line dists for (axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if ( scaleWidget ) scaleWidget->setMargin(baseLineDists[axisId]); } } pfilter.reset((QwtPlot *)this); painter->restore(); }
Plot::Plot(QWidget *parent, const char *name) : QwtPlot(parent) { marker_key = 0; curve_key = 0; minTickLength = 5; majTickLength = 9; movedGraph=FALSE; graphToResize=FALSE; ShiftButton=FALSE; setGeometry(QRect(0,0,500,400)); setAxisTitle(QwtPlot::yLeft, tr("Y Axis Title")); setAxisTitle(QwtPlot::xBottom, tr("X Axis Title")); // grid d_grid = new Grid; d_grid->enableX(false); d_grid->enableY(false); d_grid->setMajPen(QPen(Qt::blue, 0, Qt::SolidLine)); d_grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine)); d_grid->attach(this); //custom scale for (int i= 0; i<QwtPlot::axisCnt; i++) { QwtScaleWidget *scale = (QwtScaleWidget *) axisWidget(i); if (scale) { scale->setMargin(0); //the axis title color must be initialized QwtText title = scale->title(); title.setColor(Qt::black); scale->setTitle(title); ScaleDraw *sd = new ScaleDraw(); sd->setTickLength (QwtScaleDiv::MinorTick, minTickLength); sd->setTickLength (QwtScaleDiv::MediumTick, minTickLength); sd->setTickLength (QwtScaleDiv::MajorTick, majTickLength); setAxisScaleDraw (i, sd); } } QwtPlotLayout *pLayout=plotLayout(); pLayout->setCanvasMargin(0); QwtPlotCanvas* plCanvas = canvas(); plCanvas->setFocusPolicy(QWidget::StrongFocus); plCanvas->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator); plCanvas->setFocus(); plCanvas->setFrameShadow(QwtPlot::Plain); plCanvas->setCursor(Qt::arrowCursor); plCanvas->setLineWidth(0); setFocusPolicy(QWidget::StrongFocus); setFocusProxy(plCanvas); setFrameShape (QFrame::Box); setLineWidth(0); }
Plot::Plot(int width, int height, QWidget *parent, const char *) : QwtPlot(parent) { setAutoReplot (false); marker_key = 0; curve_key = 0; minTickLength = 5; majTickLength = 9; setGeometry(QRect(0, 0, width, height)); setAxisTitle(QwtPlot::yLeft, tr("Y Axis Title")); setAxisTitle(QwtPlot::xBottom, tr("X Axis Title")); //due to the plot layout updates, we must always have a non empty title setAxisTitle(QwtPlot::yRight, tr(" ")); setAxisTitle(QwtPlot::xTop, tr(" ")); // grid d_grid = new Grid(); d_grid->attach(this); //custom scale for (int i= 0; i<QwtPlot::axisCnt; i++) { QwtScaleWidget *scale = (QwtScaleWidget *) axisWidget(i); if (scale) { scale->setMargin(0); //the axis title color must be initialized... QwtText title = scale->title(); title.setColor(Qt::black); scale->setTitle(title); //...same for axis color QPalette pal = scale->palette(); pal.setColor(QPalette::Foreground, QColor(Qt::black)); scale->setPalette(pal); ScaleDraw *sd = new ScaleDraw(this); sd->setTickLength(QwtScaleDiv::MinorTick, minTickLength); sd->setTickLength(QwtScaleDiv::MediumTick, minTickLength); sd->setTickLength(QwtScaleDiv::MajorTick, majTickLength); setAxisScaleDraw (i, sd); setAxisScaleEngine (i, new ScaleEngine()); } } QwtPlotLayout *pLayout = plotLayout(); pLayout->setCanvasMargin(0); pLayout->setAlignCanvasToScales (true); QwtPlotCanvas* plCanvas = canvas(); plCanvas->setFocusPolicy(Qt::StrongFocus); plCanvas->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator); //plCanvas->setFocus(); plCanvas->setFrameShadow(QwtPlot::Plain); plCanvas->setCursor(Qt::arrowCursor); plCanvas->setLineWidth(0); plCanvas->setPaintAttribute(QwtPlotCanvas::PaintCached, false); plCanvas->setPaintAttribute(QwtPlotCanvas::PaintPacked, false); QColor background = QColor(Qt::white); background.setAlpha(255); QPalette palette; palette.setColor(QPalette::Window, background); setPalette(palette); setCanvasBackground (background); setFocusPolicy(Qt::StrongFocus); //setFocusProxy(plCanvas); setFrameShape(QFrame::Box); setLineWidth(0); }
Plot::Plot(QWidget *parent): QwtPlot(parent) { setTitle("Interactive Plot"); setCanvasColor(Qt::darkCyan); QwtPlotGrid *grid = new QwtPlotGrid; grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine)); grid->attach(this); // axes setAxisScale(QwtPlot::xBottom, 0.0, 100.0); setAxisScale(QwtPlot::yLeft, 0.0, 100.0); // Avoid jumping when label with 3 digits // appear/disappear when scrolling vertically QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft); sd->setMinimumExtent( sd->extent(axisWidget(QwtPlot::yLeft)->font())); plotLayout()->setAlignCanvasToScales(true); insertCurve(Qt::Vertical, Qt::blue, 30.0); insertCurve(Qt::Vertical, Qt::magenta, 70.0); insertCurve(Qt::Horizontal, Qt::yellow, 30.0); insertCurve(Qt::Horizontal, Qt::white, 70.0); replot(); // ------------------------------------ // We add a color bar to the left axis // ------------------------------------ QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(yLeft); scaleWidget->setMargin(10); // area for the color bar d_colorBar = new ColorBar(Qt::Vertical, scaleWidget); d_colorBar->setRange(Qt::red, Qt::darkBlue); d_colorBar->setFocusPolicy(Qt::TabFocus); connect(d_colorBar, SIGNAL(selected(const QColor &)), SLOT(setCanvasColor(const QColor &))); // we need the resize events, to lay out the color bar scaleWidget->installEventFilter(this); // ------------------------------------ // We add a wheel to the canvas // ------------------------------------ d_wheel = new QwtWheel(canvas()); d_wheel->setOrientation(Qt::Vertical); d_wheel->setRange(-100, 100); d_wheel->setValue(0.0); d_wheel->setMass(0.2); d_wheel->setTotalAngle(4 * 360.0); connect(d_wheel, SIGNAL(valueChanged(double)), SLOT(scrollLeftAxis(double))); // we need the resize events, to lay out the wheel canvas()->installEventFilter(this); d_colorBar->setWhatsThis( "Selecting a color will change the background of the plot."); scaleWidget->setWhatsThis( "Selecting a value at the scale will insert a new curve."); d_wheel->setWhatsThis( "With the wheel you can move the visible area."); axisWidget(xBottom)->setWhatsThis( "Selecting a value at the scale will insert a new curve."); }
/*! Paint the contents of a QwtPlot instance into a given rectangle. \param plot Plot to be rendered \param painter Painter \param plotRect Bounding rectangle \sa renderDocument(), renderTo(), QwtPainter::setRoundingAlignment() */ void QwtPlotRenderer::render( QwtPlot *plot, QPainter *painter, const QRectF &plotRect ) const { int axisId; if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || plot->size().isNull() ) return; if ( !( d_data->discardFlags & DiscardBackground ) ) qwtRenderBackground( painter, plotRect, plot ); /* The layout engine uses the same methods as they are used by the Qt layout system. Therefore we need to calculate the layout in screen coordinates and paint with a scaled painter. */ QTransform transform; transform.scale( double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(), double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() ); QRectF layoutRect = transform.inverted().mapRect( plotRect ); if ( !( d_data->discardFlags & DiscardBackground ) ) { // subtract the contents margins int left, top, right, bottom; plot->getContentsMargins( &left, &top, &right, &bottom ); layoutRect.adjust( left, top, -right, -bottom ); } int baseLineDists[QwtPlot::axisCnt]; if ( d_data->layoutFlags & FrameWithScales ) { for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { baseLineDists[axisId] = scaleWidget->margin(); scaleWidget->setMargin( 0 ); } if ( !plot->axisEnabled( axisId ) ) { int left = 0; int right = 0; int top = 0; int bottom = 0; // When we have a scale the frame is painted on // the position of the backbone - otherwise we // need to introduce a margin around the canvas switch( axisId ) { case QwtPlot::yLeft: layoutRect.adjust( 1, 0, 0, 0 ); break; case QwtPlot::yRight: layoutRect.adjust( 0, 0, -1, 0 ); break; case QwtPlot::xTop: layoutRect.adjust( 0, 1, 0, 0 ); break; case QwtPlot::xBottom: layoutRect.adjust( 0, 0, 0, -1 ); break; default: break; } layoutRect.adjust( left, top, right, bottom ); } } } // Calculate the layout for the document. QwtPlotLayout::Options layoutOptions = QwtPlotLayout::IgnoreScrollbars | QwtPlotLayout::IgnoreFrames; if ( d_data->discardFlags & DiscardLegend ) layoutOptions |= QwtPlotLayout::IgnoreLegend; plot->plotLayout()->activate( plot, layoutRect, layoutOptions ); // now start painting painter->save(); painter->setWorldTransform( transform, true ); // canvas QwtScaleMap maps[QwtPlot::axisCnt]; buildCanvasMaps( plot, plot->plotLayout()->canvasRect(), maps ); renderCanvas( plot, painter, plot->plotLayout()->canvasRect(), maps ); if ( !( d_data->discardFlags & DiscardTitle ) && ( !plot->titleLabel()->text().isEmpty() ) ) { renderTitle( plot, painter, plot->plotLayout()->titleRect() ); } if ( !( d_data->discardFlags & DiscardLegend ) && plot->legend() && !plot->legend()->isEmpty() ) { renderLegend( plot, painter, plot->plotLayout()->legendRect() ); } for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint( startDist, endDist ); renderScale( plot, painter, axisId, startDist, endDist, baseDist, plot->plotLayout()->scaleRect( axisId ) ); } } plot->plotLayout()->invalidate(); // reset all widgets with their original attributes. if ( d_data->layoutFlags & FrameWithScales ) { // restore the previous base line dists for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) scaleWidget->setMargin( baseLineDists[axisId] ); } } painter->restore(); }
/*! Paint the contents of a QwtPlot instance into a given rectangle. \param plot Plot to be rendered \param painter Painter \param plotRect Bounding rectangle \sa renderDocument(), renderTo(), QwtPainter::setRoundingAlignment() */ void QwtPlotRenderer::render( QwtPlot *plot, QPainter *painter, const QRectF &plotRect ) const { int axisId; if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || plot->size().isNull() ) return; if ( !( d_data->discardFlags & DiscardBackground ) ) qwtRenderBackground( painter, plotRect, plot ); /* The layout engine uses the same methods as they are used by the Qt layout system. Therefore we need to calculate the layout in screen coordinates and paint with a scaled painter. */ QTransform transform; transform.scale( double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(), double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() ); painter->save(); int baseLineDists[QwtPlot::axisCnt]; if ( d_data->layoutFlags & FrameWithScales ) { for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { baseLineDists[axisId] = scaleWidget->margin(); scaleWidget->setMargin( 0 ); } } } // Calculate the layout for the print. QwtPlotLayout::Options layoutOptions = QwtPlotLayout::IgnoreScrollbars | QwtPlotLayout::IgnoreFrames; if ( d_data->discardFlags & DiscardLegend ) layoutOptions |= QwtPlotLayout::IgnoreLegend; const QRectF layoutRect = transform.inverted().mapRect( plotRect ); plot->plotLayout()->activate( plot, layoutRect, layoutOptions ); painter->setWorldTransform( transform, true ); // canvas QwtScaleMap maps[QwtPlot::axisCnt]; buildCanvasMaps( plot, plot->plotLayout()->canvasRect(), maps ); renderCanvas( plot, painter, plot->plotLayout()->canvasRect(), maps ); if ( !( d_data->discardFlags & DiscardTitle ) && ( !plot->titleLabel()->text().isEmpty() ) ) { renderTitle( plot, painter, plot->plotLayout()->titleRect() ); } if ( !( d_data->discardFlags & DiscardLegend ) && plot->legend() && !plot->legend()->isEmpty() ) { renderLegend( plot, painter, plot->plotLayout()->legendRect() ); } for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint( startDist, endDist ); renderScale( plot, painter, axisId, startDist, endDist, baseDist, plot->plotLayout()->scaleRect( axisId ) ); } } plot->plotLayout()->invalidate(); // reset all widgets with their original attributes. if ( d_data->layoutFlags & FrameWithScales ) { // restore the previous base line dists for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ) { QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) scaleWidget->setMargin( baseLineDists[axisId] ); } } painter->restore(); }
/*! \brief Paint the plot into a given rectangle. Paint the contents of a QwtPlot instance into a given rectangle (Qwt modified code). \param painter Painter \param plotRect Bounding rectangle \param pfilter Print filter */ void Plot::print(QPainter *painter, const QRect &plotRect, const QwtPlotPrintFilter &pfilter) { int axisId; if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || size().isNull() ) return; QwtText t = title(); printFrame(painter, plotRect); painter->save(); // All paint operations need to be scaled according to // the paint device metrics. QwtPainter::setMetricsMap(this, painter->device()); const QwtMetricsMap &metricsMap = QwtPainter::metricsMap(); // It is almost impossible to integrate into the Qt layout // framework, when using different fonts for printing // and screen. To avoid writing different and Qt unconform // layout engines we change the widget attributes, print and // reset the widget attributes again. This way we produce a lot of // useless layout events ... pfilter.apply((QwtPlot *)this); int baseLineDists[QwtPlot::axisCnt]; if ( !(pfilter.options() & QwtPlotPrintFilter::PrintCanvasBackground) ){ // In case of no background we set the backbone of // the scale on the frame of the canvas. for (axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ){ QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if ( scaleWidget ){ baseLineDists[axisId] = scaleWidget->margin(); scaleWidget->setMargin(0); } } } // Calculate the layout for the print. int layoutOptions = QwtPlotLayout::IgnoreScrollbars | QwtPlotLayout::IgnoreFrames; if ( !(pfilter.options() & QwtPlotPrintFilter::PrintMargin) ) layoutOptions |= QwtPlotLayout::IgnoreMargin; if ( !(pfilter.options() & QwtPlotPrintFilter::PrintLegend) ) layoutOptions |= QwtPlotLayout::IgnoreLegend; ((QwtPlot *)this)->plotLayout()->activate(this, QwtPainter::metricsMap().deviceToLayout(plotRect), layoutOptions); if ((pfilter.options() & QwtPlotPrintFilter::PrintTitle) && (!titleLabel()->text().isEmpty())){ printTitle(painter, plotLayout()->titleRect()); } QRect canvasRect = plotLayout()->canvasRect(); for ( axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ){ QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if (scaleWidget){ int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint(startDist, endDist); QRect scaleRect = plotLayout()->scaleRect(axisId); if (!scaleWidget->margin()){ switch(axisId){ case xBottom: scaleRect.translate(0, canvasRect.bottom() - scaleRect.top()); break; case xTop: scaleRect.translate(0, canvasRect.top() - scaleRect.bottom()); break; case yLeft: scaleRect.translate(canvasRect.left() - scaleRect.right(), 0); break; case yRight: scaleRect.translate(canvasRect.right() - scaleRect.left(), 0); break; } } printScale(painter, axisId, startDist, endDist, baseDist, scaleRect); } } if ( !(pfilter.options() & QwtPlotPrintFilter::PrintCanvasBackground) ) { QRect boundingRect( canvasRect.left() - 1, canvasRect.top() - 1, canvasRect.width() + 2, canvasRect.height() + 2); boundingRect = metricsMap.layoutToDevice(boundingRect); boundingRect.setWidth(boundingRect.width() - 1); boundingRect.setHeight(boundingRect.height() - 1); painter->setPen(QPen(Qt::black)); painter->setBrush(QBrush(Qt::NoBrush)); painter->drawRect(boundingRect); } canvasRect = metricsMap.layoutToDevice(canvasRect); // When using QwtPainter all sizes where computed in pixel // coordinates and scaled by QwtPainter later. This limits // the precision to screen resolution. A much better solution // is to scale the maps and print in unlimited resolution. QwtScaleMap map[axisCnt]; for (axisId = 0; axisId < axisCnt; axisId++){ map[axisId].setTransformation(axisScaleEngine(axisId)->transformation()); const QwtScaleDiv &scaleDiv = *axisScaleDiv(axisId); map[axisId].setScaleInterval(scaleDiv.lBound(), scaleDiv.hBound()); double from, to; if ( axisEnabled(axisId) ){ const int sDist = axisWidget(axisId)->startBorderDist(); const int eDist = axisWidget(axisId)->endBorderDist(); const QRect &scaleRect = plotLayout()->scaleRect(axisId); if ( axisId == xTop || axisId == xBottom ){ from = metricsMap.layoutToDeviceX(scaleRect.left() + sDist); to = metricsMap.layoutToDeviceX(scaleRect.right() + 1 - eDist); } else { from = metricsMap.layoutToDeviceY(scaleRect.bottom() + 1 - eDist ); to = metricsMap.layoutToDeviceY(scaleRect.top() + sDist); } } else { const int margin = plotLayout()->canvasMargin(axisId); if ( axisId == yLeft || axisId == yRight ){ from = metricsMap.layoutToDeviceX(canvasRect.bottom() - margin); to = metricsMap.layoutToDeviceX(canvasRect.top() + margin); } else { from = metricsMap.layoutToDeviceY(canvasRect.left() + margin); to = metricsMap.layoutToDeviceY(canvasRect.right() - margin); } } map[axisId].setPaintXInterval(from, to); } // The canvas maps are already scaled. QwtPainter::setMetricsMap(painter->device(), painter->device()); printCanvas(painter, canvasRect, map, pfilter); QwtPainter::resetMetricsMap(); ((QwtPlot *)this)->plotLayout()->invalidate(); // reset all widgets with their original attributes. if ( !(pfilter.options() & QwtPlotPrintFilter::PrintCanvasBackground) ){ // restore the previous base line dists for (axisId = 0; axisId < QwtPlot::axisCnt; axisId++ ){ QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(axisId); if ( scaleWidget ) scaleWidget->setMargin(baseLineDists[axisId]); } } pfilter.reset((QwtPlot *)this); painter->restore(); setTitle(t);//hack used to avoid bug in Qwt::printTitle(): the title attributes are overwritten }
/*! Paint the contents of a QwtPlot instance into a given rectangle. \param plot Plot to be rendered \param painter Painter \param plotRect Bounding rectangle \sa renderDocument(), renderTo(), QwtPainter::setRoundingAlignment() */ void QwtPlotRenderer::render( QwtPlot *plot, QPainter *painter, const QRectF &plotRect ) const { if ( painter == 0 || !painter->isActive() || !plotRect.isValid() || plot->size().isNull() ) { return; } if ( !( d_data->discardFlags & DiscardBackground ) ) QwtPainter::drawBackgound( painter, plotRect, plot ); /* The layout engine uses the same methods as they are used by the Qt layout system. Therefore we need to calculate the layout in screen coordinates and paint with a scaled painter. */ QTransform transform; transform.scale( double( painter->device()->logicalDpiX() ) / plot->logicalDpiX(), double( painter->device()->logicalDpiY() ) / plot->logicalDpiY() ); QRectF layoutRect = transform.inverted().mapRect( plotRect ); if ( !( d_data->discardFlags & DiscardBackground ) ) { // subtract the contents margins int left, top, right, bottom; plot->getContentsMargins( &left, &top, &right, &bottom ); layoutRect.adjust( left, top, -right, -bottom ); } QwtPlotLayout *layout = plot->plotLayout(); int baseLineDists[QwtAxis::PosCount]; int canvasMargins[QwtAxis::PosCount]; for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { canvasMargins[ axisPos ] = layout->canvasMargin( axisPos ); if ( d_data->layoutFlags & FrameWithScales ) { const QwtAxisId axisId( axisPos, QWT_DUMMY_ID ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { baseLineDists[ axisPos ] = scaleWidget->margin(); scaleWidget->setMargin( 0 ); } if ( !plot->isAxisVisible( axisId ) ) { int left = 0; int right = 0; int top = 0; int bottom = 0; // When we have a scale the frame is painted on // the position of the backbone - otherwise we // need to introduce a margin around the canvas switch( axisPos ) { case QwtAxis::yLeft: layoutRect.adjust( 1, 0, 0, 0 ); break; case QwtAxis::yRight: layoutRect.adjust( 0, 0, -1, 0 ); break; case QwtAxis::xTop: layoutRect.adjust( 0, 1, 0, 0 ); break; case QwtAxis::xBottom: layoutRect.adjust( 0, 0, 0, -1 ); break; } layoutRect.adjust( left, top, right, bottom ); } } } // Calculate the layout for the document. QwtPlotLayout::Options layoutOptions = QwtPlotLayout::IgnoreScrollbars; if ( ( d_data->layoutFlags & FrameWithScales ) || ( d_data->discardFlags & DiscardCanvasFrame ) ) { layoutOptions |= QwtPlotLayout::IgnoreFrames; } if ( d_data->discardFlags & DiscardLegend ) layoutOptions |= QwtPlotLayout::IgnoreLegend; if ( d_data->discardFlags & DiscardTitle ) layoutOptions |= QwtPlotLayout::IgnoreTitle; if ( d_data->discardFlags & DiscardFooter ) layoutOptions |= QwtPlotLayout::IgnoreFooter; layout->update( plot, layoutRect, layoutOptions ); // canvas QwtScaleMapTable mapTable = buildCanvasMaps( plot, layout->canvasRect() ); if ( updateCanvasMargins( plot, layout->canvasRect(), mapTable ) ) { // recalculate maps and layout, when the margins // have been changed layout->update( plot, layoutRect, layoutOptions ); mapTable = buildCanvasMaps( plot, layout->canvasRect() ); } // now start painting painter->save(); painter->setWorldTransform( transform, true ); renderCanvas( plot, painter, layout->canvasRect(), mapTable ); if ( !( d_data->discardFlags & DiscardTitle ) && ( !plot->titleLabel()->text().isEmpty() ) ) { renderTitle( plot, painter, layout->titleRect() ); } if ( !( d_data->discardFlags & DiscardFooter ) && ( !plot->footerLabel()->text().isEmpty() ) ) { renderFooter( plot, painter, layout->footerRect() ); } if ( !( d_data->discardFlags & DiscardLegend ) && plot->legend() && !plot->legend()->isEmpty() ) { renderLegend( plot, painter, layout->legendRect() ); } for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { for ( int i = 0; i < plot->axesCount( i ); i++ ) { const QwtAxisId axisId( axisPos, i ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) { int baseDist = scaleWidget->margin(); int startDist, endDist; scaleWidget->getBorderDistHint( startDist, endDist ); renderScale( plot, painter, axisId, startDist, endDist, baseDist, layout->scaleRect( axisId ) ); } } } painter->restore(); // restore all setting to their original attributes. for ( int axisPos = 0; axisPos < QwtAxis::PosCount; axisPos++ ) { if ( d_data->layoutFlags & FrameWithScales ) { const QwtAxisId axisId( axisPos, QWT_DUMMY_ID ); QwtScaleWidget *scaleWidget = plot->axisWidget( axisId ); if ( scaleWidget ) scaleWidget->setMargin( baseLineDists[axisPos] ); } layout->setCanvasMargin( canvasMargins[axisPos] ); } layout->invalidate(); }