📄 qwt_plot.cpp
字号:
/* -*- 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 <qpainter.h>
#if QT_VERSION < 0x040000
#include <qguardedptr.h>
#include <qfocusdata.h>
#else
#include <qpointer.h>
#include <qpaintengine.h>
#endif
#include <qapplication.h>
#include <qevent.h>
#include "qwt_plot.h"
#include "qwt_plot_dict.h"
#include "qwt_plot_layout.h"
#include "qwt_rect.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_engine.h"
#include "qwt_text_label.h"
#include "qwt_legend.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_plot_canvas.h"
#include "qwt_paint_buffer.h"
class QwtPlot::PrivateData
{
public:
#if QT_VERSION < 0x040000
QGuardedPtr<QwtTextLabel> lblTitle;
QGuardedPtr<QwtPlotCanvas> canvas;
QGuardedPtr<QwtLegend> legend;
#else
QPointer<QwtTextLabel> lblTitle;
QPointer<QwtPlotCanvas> canvas;
QPointer<QwtLegend> legend;
#endif
QwtPlotLayout *layout;
bool autoReplot;
};
/*!
\brief Constructor
\param parent Parent widget
*/
QwtPlot::QwtPlot(QWidget *parent):
QFrame(parent)
{
initPlot(QwtText());
}
/*!
\brief Constructor
\param title Title text
\param parent Parent widget
*/
QwtPlot::QwtPlot(const QwtText &title, QWidget *parent):
QFrame(parent)
{
initPlot(title);
}
#if QT_VERSION < 0x040000
/*!
\brief Constructor
\param parent Parent widget
\param name Object name
*/
QwtPlot::QwtPlot(QWidget *parent, const char *name):
QFrame(parent, name)
{
initPlot(QwtText());
}
#endif
//! Destructor
QwtPlot::~QwtPlot()
{
detachItems(QwtPlotItem::Rtti_PlotItem, autoDelete());
delete d_data->layout;
deleteAxesData();
delete d_data;
}
/*!
\brief Initializes a QwtPlot instance
\param title Title text
*/
void QwtPlot::initPlot(const QwtText &title)
{
d_data = new PrivateData;
#if QT_VERSION < 0x040000
setWFlags(Qt::WNoAutoErase);
#endif
d_data->layout = new QwtPlotLayout;
d_data->autoReplot = false;
d_data->lblTitle = new QwtTextLabel(title, this);
d_data->lblTitle->setFont(QFont(fontInfo().family(), 14, QFont::Bold));
QwtText text(title);
int flags = Qt::AlignCenter;
#if QT_VERSION < 0x040000
flags |= Qt::WordBreak | Qt::ExpandTabs;
#else
flags |= Qt::TextWordWrap;
#endif
text.setRenderFlags(flags);
d_data->lblTitle->setText(text);
d_data->legend = NULL;
initAxesData();
d_data->canvas = new QwtPlotCanvas(this);
d_data->canvas->setFrameStyle(QFrame::Panel|QFrame::Sunken);
d_data->canvas->setLineWidth(2);
d_data->canvas->setMidLineWidth(0);
updateTabOrder();
setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
}
/*!
\brief Adds handling of layout requests
*/
bool QwtPlot::event(QEvent *e)
{
bool ok = QFrame::event(e);
switch(e->type())
{
#if QT_VERSION < 0x040000
case QEvent::LayoutHint:
#else
case QEvent::LayoutRequest:
#endif
updateLayout();
break;
#if QT_VERSION >= 0x040000
case QEvent::PolishRequest:
polish();
break;
#endif
default:;
}
return ok;
}
/*!
\brief Replots the plot if QwtPlot::autoReplot() is \c true.
*/
void QwtPlot::autoRefresh()
{
if (d_data->autoReplot)
replot();
}
/*!
\brief Set or reset the autoReplot option
If the autoReplot option is set, the plot will be
updated implicitly by manipulating member functions.
Since this may be time-consuming, it is recommended
to leave this option switched off and call replot()
explicitly if necessary.
The autoReplot option is set to false by default, which
means that the user has to call replot() in order to make
changes visible.
\param tf \c true or \c false. Defaults to \c true.
\sa replot()
*/
void QwtPlot::setAutoReplot(bool tf)
{
d_data->autoReplot = tf;
}
/*!
\return true if the autoReplot option is set.
*/
bool QwtPlot::autoReplot() const
{
return d_data->autoReplot;
}
/*!
\brief Change the plot's title
\param t new title
*/
void QwtPlot::setTitle(const QString &t)
{
if ( t != d_data->lblTitle->text().text() )
{
d_data->lblTitle->setText(t);
updateLayout();
}
}
/*!
\brief Change the plot's title
\param t new title
*/
void QwtPlot::setTitle(const QwtText &t)
{
if ( t != d_data->lblTitle->text() )
{
d_data->lblTitle->setText(t);
updateLayout();
}
}
/*!
\return the plot's title
*/
QwtText QwtPlot::title() const
{
return d_data->lblTitle->text();
}
/*!
\return the plot's layout
*/
QwtPlotLayout *QwtPlot::plotLayout()
{
return d_data->layout;
}
/*!
\return the plot's layout
*/
const QwtPlotLayout *QwtPlot::plotLayout() const
{
return d_data->layout;
}
/*!
\return the plot's titel label.
*/
QwtTextLabel *QwtPlot::titleLabel()
{
return d_data->lblTitle;
}
/*!
\return the plot's titel label.
*/
const QwtTextLabel *QwtPlot::titleLabel() const
{
return d_data->lblTitle;
}
/*!
\return the plot's legend
\sa printLegendItem()
*/
QwtLegend *QwtPlot::legend()
{
return d_data->legend;
}
/*!
\return the plot's legend
\sa printLegendItem()
*/
const QwtLegend *QwtPlot::legend() const
{
return d_data->legend;
}
/*!
\return the plot's canvas
*/
QwtPlotCanvas *QwtPlot::canvas()
{
return d_data->canvas;
}
/*!
\return the plot's canvas
*/
const QwtPlotCanvas *QwtPlot::canvas() const
{
return d_data->canvas;
}
void QwtPlot::polish()
{
replot();
#if QT_VERSION < 0x040000
QFrame::polish();
#endif
}
/*!
Return sizeHint
\sa QwtPlot::minimumSizeHint()
*/
QSize QwtPlot::sizeHint() const
{
int dw = 0;
int dh = 0;
for ( int axisId = 0; axisId < axisCnt; axisId++ )
{
if ( axisEnabled(axisId) )
{
const int niceDist = 40;
const QwtScaleWidget *scaleWidget = axisWidget(axisId);
const QwtScaleDiv &scaleDiv = scaleWidget->scaleDraw()->scaleDiv();
const int majCnt = scaleDiv.ticks(QwtScaleDiv::MajorTick).count();
if ( axisId == yLeft || axisId == yRight )
{
int hDiff = (majCnt - 1) * niceDist
- scaleWidget->minimumSizeHint().height();
if ( hDiff > dh )
dh = hDiff;
}
else
{
int wDiff = (majCnt - 1) * niceDist
- scaleWidget->minimumSizeHint().width();
if ( wDiff > dw )
dw = wDiff;
}
}
}
return minimumSizeHint() + QSize(dw, dh);
}
/*!
\brief Return a minimum size hint
*/
QSize QwtPlot::minimumSizeHint() const
{
QSize hint = d_data->layout->minimumSizeHint(this);
hint += QSize(2 * frameWidth(), 2 * frameWidth());
return hint;
}
//! Resize and update internal layout
void QwtPlot::resizeEvent(QResizeEvent *e)
{
QFrame::resizeEvent(e);
updateLayout();
}
/*!
\brief Redraw the plot
If the autoReplot option is not set (which is the default)
or if any curves are attached to raw data, the plot has to
be refreshed explicitly in order to make changes visible.
\sa setAutoReplot()
\warning Calls canvas()->repaint, take care of infinite recursions
*/
void QwtPlot::replot()
{
bool doAutoReplot = autoReplot();
setAutoReplot(false);
updateAxes();
/*
Maybe the layout needs to be updated, because of changed
axes labels. We need to process them here before painting
to avoid that scales and canvas get out of sync.
*/
#if QT_VERSION >= 0x040000
QApplication::sendPostedEvents(this, QEvent::LayoutRequest);
#else
QApplication::sendPostedEvents(this, QEvent::LayoutHint);
#endif
QwtPlotCanvas &canvas = *d_data->canvas;
canvas.invalidatePaintCache();
/*
In case of cached or packed painting the canvas
is repainted completely and doesn't need to be erased.
*/
const bool erase =
!canvas.testPaintAttribute(QwtPlotCanvas::PaintPacked)
&& !canvas.testPaintAttribute(QwtPlotCanvas::PaintCached);
#if QT_VERSION >= 0x040000
const bool noBackgroundMode = canvas.testAttribute(Qt::WA_NoBackground);
if ( !erase && !noBackgroundMode )
canvas.setAttribute(Qt::WA_NoBackground, true);
canvas.repaint(canvas.contentsRect());
if ( !erase && !noBackgroundMode )
canvas.setAttribute(Qt::WA_NoBackground, false);
#else
canvas.repaint(canvas.contentsRect(), erase);
#endif
setAutoReplot(doAutoReplot);
}
/*!
\brief Adjust plot content to its current size.
\sa QwtPlot::resizeEvent
*/
void QwtPlot::updateLayout()
{
d_data->layout->activate(this, contentsRect());
//
// resize and show the visible widgets
//
if (!d_data->lblTitle->text().isEmpty())
{
d_data->lblTitle->setGeometry(d_data->layout->titleRect());
if (!d_data->lblTitle->isVisible())
d_data->lblTitle->show();
}
else
d_data->lblTitle->hide();
for (int axisId = 0; axisId < axisCnt; axisId++ )
{
if (axisEnabled(axisId) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -