QCustomPlot 2.0.1 源码分析

标签QFont labelFont() const;QColor labelColor() const;QString label() const;int labelPadding() const;void setLabelFont(const QFont &font);void setLabelColor(const QColor &color);void setLabel(const QString &str);void setLabelPadding(int.
摘要由CSDN通过智能技术生成

z

目录

OC](目录)

The Layering system

/*! \class QCPLayer
\brief A layer that may contain objects, to control the rendering order

The Layering system of QCustomPlot is the mechanism to control the rendering order of the
elements inside the plot.

It is based on the two classes QCPLayer and QCPLayerable. QCustomPlot holds an ordered list of
one or more instances of QCPLayer (see QCustomPlot::addLayer, QCustomPlot::layer,
QCustomPlot::moveLayer, etc.). When replotting, QCustomPlot goes through the list of layers
bottom to top and successively draws the layerables of the layers into the paint buffer(s).

A QCPLayer contains an ordered list of QCPLayerable instances. QCPLayerable is an abstract base
class from which almost all visible objects derive, like axes, grids, graphs, items, etc.

\section qcplayer-defaultlayers Default layers

Initially, QCustomPlot has six layers: “background”, “grid”, “main”, “axes”, “legend” and
“overlay” (in that order). On top is the “overlay” layer, which only contains the QCustomPlot’s
selection rect (\ref QCustomPlot::selectionRect). The next two layers “axes” and “legend” contain
the default axes and legend, so they will be drawn above plottables. In the middle, there is the
“main” layer. It is initially empty and set as the current layer (see
QCustomPlot::setCurrentLayer). This means, all new plottables, items etc. are created on this
layer by default. Then comes the “grid” layer which contains the QCPGrid instances (which belong
tightly to QCPAxis, see \ref QCPAxis::grid). The Axis rect background shall be drawn behind
everything else, thus the default QCPAxisRect instance is placed on the “background” layer. Of
course, the layer affiliation of the individual objects can be changed as required (\ref
QCPLayerable::setLayer).

\section qcplayer-ordering Controlling the rendering order via layers

Controlling the ordering of layerables in the plot is easy: Create a new layer in the position
you want the layerable to be in, e.g. above “main”, with \ref QCustomPlot::addLayer. Then set the
current layer with \ref QCustomPlot::setCurrentLayer to that new layer and finally create the
objects normally. They will be placed on the new layer automatically, due to the current layer
setting. Alternatively you could have also ignored the current layer setting and just moved the
objects with \ref QCPLayerable::setLayer to the desired layer after creating them.

It is also possible to move whole layers. For example, If you want the grid to be shown in front
of all plottables/items on the “main” layer, just move it above “main” with
QCustomPlot::moveLayer.

The rendering order within one layer is simply by order of creation or insertion. The item
created last (or added last to the layer), is drawn on top of all other objects on that layer.

When a layer is deleted, the objects on it are not deleted with it, but fall on the layer below
the deleted layer, see QCustomPlot::removeLayer.

\section qcplayer-buffering Replotting only a specific layer

If the layer mode (\ref setMode) is set to \ref lmBuffered, you can replot only this specific
layer by calling \ref replot. In certain situations this can provide better replot performance,
compared with a full replot of all layers. Upon creation of a new layer, the layer mode is
initialized to \ref lmLogical. The only layer that is set to \ref lmBuffered in a new \ref
QCustomPlot instance is the “overlay” layer, containing the selection rect.
*/

QCustomPlot

构成

QCPLayer

background: defaultAxisRect.
grid: xAxis->grid(), yAxis->grid(), xAxis2->grid(), yAxis2->grid().
main: mPlotLayout.
axes: 坐标轴xAxis, yAxis, xAxis2, yAxis2.
legend: legend.
overlay: mSelectionRect.

QCPGraph

QCPAxis

绘制流程

void QCustomPlot::replot(QCustomPlot::RefreshPriority)
virtual void QCPAxisRect::draw(QCPPainter*)
virtual void QCPGraph::draw(QCPPainter*)
virtual void QCPAxis::draw(QCPPainter*)
virtual void QCPAxis::draw(QCPPainter*)
void QCustomPlot::drawBackground(QCPPainter*)
void QCustomPlot::drawBackground(QCPPainter*)

交互方式

模式

enum Interaction {
    iRangeDrag         = 0x001 ///< <tt>0x001</tt> Axis ranges are draggable (see \ref QCPAxisRect::setRangeDrag, \ref QCPAxisRect::setRangeDragAxes)
                   ,iRangeZoom        = 0x002 ///< <tt>0x002</tt> Axis ranges are zoomable with the mouse wheel (see \ref QCPAxisRect::setRangeZoom, \ref QCPAxisRect::setRangeZoomAxes)
                   ,iMultiSelect      = 0x004 ///< <tt>0x004</tt> The user can select multiple objects by holding the modifier set by \ref QCustomPlot::setMultiSelectModifier while clicking
                   ,iSelectPlottables = 0x008 ///< <tt>0x008</tt> Plottables are selectable (e.g. graphs, curves, bars,... see QCPAbstractPlottable)
                   ,iSelectAxes       = 0x010 ///< <tt>0x010</tt> Axes are selectable (or parts of them, see QCPAxis::setSelectableParts)
                   ,iSelectLegend     = 0x020 ///< <tt>0x020</tt> Legends are selectable (or their child items, see QCPLegend::setSelectableParts)
                   ,iSelectItems      = 0x040 ///< <tt>0x040</tt> Items are selectable (Rectangles, Arrows, Textitems, etc. see \ref QCPAbstractItem)
                   ,iSelectOther      = 0x080 ///< <tt>0x080</tt> All other objects are selectable (e.g. your own derived layerables, other layout elements,...)
                 };

流程

矩形选择模式

模式

流程

QCustomPlot::mousePressEvent
QCustomPlot::mouseMoveEvent
QCustomPlot::mouseReleaseEvent
QCustomPlot::processPointSelection //处理选择
QCPAbstractPlottable::selectEvent
QCPAbstractPlottable::setSelection
    QCustomPlot::replot
      QCPGraph::draw: 选择的部分和未选择的部分颜色不一样
        QCPAbstractPlottable1D<DataType>::getDataSegments
        QCPSelectionDecorator::applyBrush

成员变量

mLayers

QList<QCPLayer*> mLayers;

mGraphs

QList<QCPGraph*> mGraphs;

mCurrentLayer

QCPLayer *mCurrentLayer;

mPlottables

QList<QCPAbstractPlottable*> mPlottables;

xAxis, yAxis, xAxis2, yAxis2

 QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2;

mInteractions 交互方式

QCP::Interactions mInteractions;
enum Interaction {
    iRangeDrag         = 0x001 ///< <tt>0x001</tt> Axis ranges are draggable (see \ref QCPAxisRect::setRangeDrag, \ref QCPAxisRect::setRangeDragAxes)
                   ,iRangeZoom        = 0x002 ///< <tt>0x002</tt> Axis ranges are zoomable with the mouse wheel (see \ref QCPAxisRect::setRangeZoom, \ref QCPAxisRect::setRangeZoomAxes)
                   ,iMultiSelect      = 0x004 ///< <tt>0x004</tt> The user can select multiple objects by holding the modifier set by \ref QCustomPlot::setMultiSelectModifier while clicking
                   ,iSelectPlottables = 0x008 ///< <tt>0x008</tt> Plottables are selectable (e.g. graphs, curves, bars,... see QCPAbstractPlottable)
                   ,iSelectAxes       = 0x010 ///< <tt>0x010</tt> Axes are selectable (or parts of them, see QCPAxis::setSelectableParts)
                   ,iSelectLegend     = 0x020 ///< <tt>0x020</tt> Legends are selectable (or their child items, see QCPLegend::setSelectableParts)
                   ,iSelectItems      = 0x040 ///< <tt>0x040</tt> Items are selectable (Rectangles, Arrows, Textitems, etc. see \ref QCPAbstractItem)
                   ,iSelectOther      = 0x080 ///< <tt>0x080</tt> All other objects are selectable (e.g. your own derived layerables, other layout elements,...)
                 };

mSelectionRectMode 矩形选择模式

QCP::SelectionRectMode mSelectionRectMode

enum SelectionRectMode {
    srmNone    ///< The selection rect is disabled, and all mouse events are forwarded to the underlying objects, e.g. for axis range dragging
                         ,srmZoom   ///< When dragging the mouse, a selection rect becomes active. Upon releasing, the axes that are currently set as range zoom axes (\ref QCPAxisRect::setRangeZoomAxes) will have their ranges zoomed accordingly.
                         ,srmSelect ///< When dragging the mouse, a selection rect becomes active. Upon releasing, plottable data points that were within the selection rect are selected, if the plottable's selectability setting permits. (See  \ref dataselection "data selection mechanism" for details.)
                         ,srmCustom ///< When dragging the mouse, a selection rect becomes active. It is the programmer's responsibility to connect according slots to the selection rect's signals (e.g. \ref QCPSelectionRect::accepted) in order to process the user interaction.
                       };

mMousePressPos

QPoint mMousePressPos; //鼠标按下的位置

事件

paintEvent

void QCustomPlot::paintEvent(QPaintEvent *event)
{
   
  Q_UNUSED(event);
  QCPPainter painter(this);
  if (painter.isActive())
  {
   
    painter.setRenderHint(QPainter::HighQualityAntialiasing); // to make Antialiasing look good if using the OpenGL graphicssystem
    if (mBackgroundBrush.style() != Qt::NoBrush)
      painter.fillRect(mViewport, mBackgroundBrush);
    drawBackground(&painter);
    for (int bufferIndex = 0; bufferIndex < mPaintBuffers.size(); ++bufferIndex)
      mPaintBuffers.at(bufferIndex)->draw(&painter);
  }
}
/*
QList<QSharedPointer<QCPAbstractPaintBuffer> > mPaintBuffers;
/

mousePressEvent

void QCustomPlot::mousePressEvent(QMouseEvent *event)
{
   
  emit mousePress(event);
  // save some state to tell in releaseEvent whether it was a click:
  mMouseHasMoved = false;
  mMousePressPos = event->pos();

  if (mSelectionRect && mSelectionRectMode != QCP::srmNone)
  {
   
    if (mSelectionRectMode != QCP::srmZoom || qobject_cast<QCPAxisRect*>(axisRectAt(mMousePressPos))) // in zoom mode only activate selection rect if on an axis rect
      mSelectionRect->startSelection(event);
  } else
  {
   
    // no selection rect interaction, prepare for click signal emission and forward event to layerable under the cursor:
    QList<QVariant> details;
    QList<QCPLayerable*> candidates = layerableListAt(mMousePressPos, false, &details);
    if (!candidates.isEmpty())
    {
   
      mMouseSignalLayerable = candidates.first(); // candidate for signal emission is always topmost hit layerable (signal emitted in release event)
      mMouseSignalLayerableDetails = details.first();
    }
    // forward event to topmost candidate which accepts the event:
    for (int i=0; i<candidates.size(); ++i)
    {
   
      event->accept(); // default impl of QCPLayerable's mouse events call ignore() on the event, in that case propagate to next candidate in list
      candidates.at(i)->mousePressEvent(event, details.at(i));
      if (event->isAccepted())
      {
   
        mMouseEventLayerable = candidates.at(i);
        mMouseEventLayerableDetails = details.at(i);
        break;
      }
    }
  }
  
  event->accept(); // in case QCPLayerable reimplementation manipulates event accepted state. In QWidget event system, QCustomPlot wants to accept the event.
}

mouseMoveEvent

void QCustomPlot::mouseMoveEvent(QMouseEvent *event)
{
   
  emit mouseMove(event);
  
  if (!mMouseHasMoved && (mMousePressPos-event->pos()).manhattanLength() > 3)
    mMouseHasMoved = true; // moved too far from mouse press position, don't handle as click on mouse release
  
  if (mSelectionRect && mSelectionRect->isActive())
    mSelectionRect->moveSelection(event);
  else if (mMouseEventLayerable) // call event of affected layerable:
    mMouseEventLayerable->mouseMoveEvent(event, mMousePressPos);
  
  event->accept(); // in case QCPLayerable reimplementation manipulates event accepted state. In QWidget event system, QCustomPlot wants to accept the event.
}

mouseReleaseEvent

void QCustomPlot::mouseReleaseEvent(QMouseEvent *event)
{
   
  emit mouseRelease(event);
  
  if (!mMouseHasMoved) // mouse hasn't moved (much) between press and release, so handle as click
  {
   
    if (mSelectionRect && mSelectionRect->isActive()) // a simple click shouldn't successfully finish a selection rect, so cancel it here
      mSelectionRect->cancel();
    if (event->button() == Qt::LeftButton)
      processPointSelection(event);
    
    // emit specialized click signals of QCustomPlot instance:
    if (QCPAbstractPlottable *ap = qobject_cast<QCPAbstractPlottable*>(mMouseSignalLayerable))
    {
   
      int dataIndex = 0;
      if (!mMouseSignalLayerableDetails.value<QCPDataSelection>().isEmpty())
        dataIndex = mMouseSignalLayerableDetails.value<QCPDataSelection>().dataRange().begin();
      emit plottableClick(ap, dataIndex, event);
    } else if (QCPAxis *ax = qobject_cast<QCPAxis*>(mMouseSignalLayerable))
      emit axisClick(ax, mMouseSignalLayerableDetails.value<QCPAxis::SelectablePart>(), event);
    else if (QCPAbstractItem *ai = qobject_cast<QCPAbstractItem*>(mMouseSignalLayerable))
      emit itemClick(ai, event);
    else if (QCPLegend *lg = qobject_cast<QCPLegend*>(mMouseSignalLayerable))
      emit legendClick(lg, 0, event);
    else if (QCPAbstractLegendItem *li = qobject_cast<QCPAbstractLegendItem*>(mMouseSignalLayerable))
      emit legendClick(li->parentLegend(), li, event);
    mMouseSignalLayerable = 0;
  }
  
  if (mSelectionRect && mSelectionRect->isActive()) // Note: if a click was detected above, the selection rect is canceled there
  {
   
    // finish selection rect, the appropriate action will be taken via signal-slot connection:
    mSelectionRect->endSelection(event);
  } else
  {
   
    // call event of affected layerable:
    if (mMouseEventLayerable)
    {
   
      mMouseEventLayerable->mouseReleaseEvent(event, mMousePressPos);
      mMouseEventLayerable = 0;
    }
  }
  
  if (noAntialiasingOnDrag())
    replot(rpQueuedReplot);
  
  event->accept(); // in case QCPLayerable reimplementation manipulates event accepted state. In QWidget event system, QCustomPlot wants to accept the event.
}

函数

replot

/* 
  重点
  foreach (QCPLayer *layer, mLayers)
    layer->drawToPaintBuffer();
*/
void QCustomPlot::replot(QCustomPlot::RefreshPriority refreshPriority)
{
   
  if (refreshPriority == QCustomPlot::rpQueuedReplot
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值