QCustomPlot 1.3.0 修改源码 v2

QCPMeasureGraph

新增

class QCP_LIB_DECL QCPMeasureGraph : public QCPLayerable
{
    Q_OBJECT
    Q_PROPERTY(QCP::SelectionMode selectionMode READ selectionMode WRITE setSelectionMode NOTIFY selectionModeChanged)
    Q_PROPERTY(QPoint  startPoint READ startPoint WRITE setStartPoint NOTIFY startPointChanged)
    Q_PROPERTY(QPoint  endPoint READ endPoint WRITE setEndPoint NOTIFY endPointChanged)
    Q_PROPERTY(State  state READ state WRITE setState NOTIFY stateChanged)
    enum State { mNoPointState, mStartPointState, mEndPointState};
     Q_ENUM(State)
public:
    QCPMeasureGraph(QCustomPlot *plot, QString targetLayer = QString(), QCPLayerable *parentLayerable = nullptr);
    ~QCPMeasureGraph();
signals:
    void selectionModeChanged(QCP::SelectionMode mode);
    void startPointChanged(QPoint point);
    void endPointChanged(QPoint point);
    void stateChanged(State s);
public:
    QCP::SelectionMode selectionMode() const { return mSelectionMode; }
    QPoint startPoint() const { return mStartPoint; }
    QPoint endPoint() const { return mEndPoint; }
    State state() const { return mState; }
    Q_SLOT void setSelectionMode(QCP::SelectionMode mode);
    Q_SLOT void setStartPoint(QPoint point);
    Q_SLOT void setEndPoint(QPoint point);
    Q_SLOT void setState(State s);
protected:
    virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
    virtual void draw(QCPPainter *painter);
private:
    QCP::SelectionMode mSelectionMode;
    QPoint mStartPoint;
    QPoint mEndPoint;
    State mState;
};


QCPMeasureGraph::QCPMeasureGraph(QCustomPlot *plot, QString targetLayer, QCPLayerable *parentLayerable):
    QCPLayerable(plot, targetLayer, parentLayerable)
{

}

QCPMeasureGraph::~QCPMeasureGraph()
{

}

void QCPMeasureGraph::setSelectionMode(QCP::SelectionMode mode)
{
   if (mSelectionMode != mode)
   {
       mSelectionMode = mode;
       emit selectionModeChanged(mSelectionMode);
   }
}
void QCPMeasureGraph::setStartPoint(QPoint point)
{
   if (mStartPoint != point)
   {
       mStartPoint = point;
       emit startPointChanged(mStartPoint);
   }
}

void QCPMeasureGraph::setEndPoint(QPoint point)
{
   if (mEndPoint != point)
   {
       mEndPoint = point;
       emit endPointChanged(mEndPoint);
   }
}
void QCPMeasureGraph::setState(State s)
{
    if (mState != s)
    {
        mState = s;
        emit stateChanged(mState);
    }
}


void QCPMeasureGraph::applyDefaultAntialiasingHint(QCPPainter *painter) const
{
    Q_UNUSED(painter)
}

void QCPMeasureGraph::draw(QCPPainter *painter)
{
    int midX = (mStartPoint.x() + mEndPoint.x()) / 2;
    int h = mStartPoint.y() - mEndPoint.y();
    int w = mStartPoint.x() - mEndPoint.x();
    if (h < 0)
        h *= -1;
    if (w < 0)
        w *= -1;
    painter->save();
    painter->fillRect(QRect(0, 0, 100, 100), Qt::red);
    painter->setPen(Qt::white);
    painter->drawText(QRect(mStartPoint.x(), mEndPoint.y(),  w, h),
                      Qt::AlignTop | Qt::AlignHCenter, QString::number(w));
    painter->drawText(QRect(midX, mStartPoint.y(),  w / 2, h),
                      Qt::AlignLeft, QString::number(h));
    painter->drawLine(QPoint(mStartPoint.x(), 0), QPoint(mStartPoint.x(), painter->device()->height()));
    painter->drawLine(QPoint(mEndPoint.x(), 0), QPoint(mEndPoint.x(), painter->device()->height()));
    painter->drawLine(QPoint(midX , mStartPoint.y()), QPoint(midX, mEndPoint.y()));
    painter->drawLine(QPoint(mStartPoint.x(), mStartPoint.y()), QPoint(mEndPoint.x(), mStartPoint.y()));
    painter->drawLine(QPoint(mStartPoint.x(), mEndPoint.y()), QPoint(mEndPoint.x(), mEndPoint.y()));
    painter->restore();
}

QCustomPlot

新增

//构造函数
measure = new QCPMeasureGraph(this, QString(), nullptr);
measure->setLayer(QLatin1String("background"));
// 类
enum SelectionMode { snone, smode1,smode2,mode3,smode4,};
 Q_PROPERTY(QCP::SelectionMode selectionMode READ selectionMode WRITE setSelectionMode NOTIFY selectionModeChanged)
   Q_PROPERTY(QPoint  startPoint READ startPoint WRITE setStartPoint NOTIFY startPointChanged)
  Q_PROPERTY(QPoint  endPoint READ endPoint WRITE setEndPoint NOTIFY endPointChanged)
 int mSelectionMode;
 QPoint mStartPoint;
 QPoint mEndPoint;
 QCPMeasureGraph *measure;
 //: signals
   void selectionModeChanged(QCP::SelectionMode mode);
   void startPointChanged(QPoint point);
   void endPointChanged(QPoint point);
 //: slots
  Q_SLOT void setSelectionMode(QCP::SelectionMode mode);
  Q_SLOT void setStartPoint(QPoint point);
  Q_SLOT void setEndPoint(QPoint point);  
 //: getters
 QCP::SelectionMode selectionMode() const { return mSelectionMode; }
 QPoint startPoint() const { return mStartPoint; }
 QPoint endPoint() const { return mEndPoint; }
 //: setters
 void QCustomPlot::setSelectionMode(QCP::SelectionMode mode)
{
    if (mSelectionMode != mode)
    {
        mSelectionMode = mode;
        emit selectionModeChanged(mSelectionMode);
    }
}
void QCustomPlot::setStartPoint(QPoint point)
{
    if (mStartPoint != point)
    {
        mStartPoint = point;
        emit startPointChanged(mStartPoint);
    }
}

void QCustomPlot::setEndPoint(QPoint point)
{
    if (mEndPoint != point)
    {
        mEndPoint = point;
        emit endPointChanged(mEndPoint);
    }
}

mousePressEvent

void QCustomPlot::mousePressEvent(QMouseEvent *event)
{
  emit mousePress(event);
  mMousePressPos = event->pos();
  if (mSelectionMode == QCP::smode1) {
      qDebug() << __FUNCTION__ << mSelectionMode;
      switch (measure->state()) {
          case QCPMeasureGraph::mNoPointState:
              measure->setStartPoint(event->pos());
              measure->setEndPoint(event->pos());
              measure->setState(QCPMeasureGraph::mStartPointState);
              replot();
              break;
          case QCPMeasureGraph::mStartPointState:
              measure->setEndPoint(event->pos());
              measure->setState(QCPMeasureGraph::mEndPointState);
              replot();
              break;
          default: break;
      }
  } else {
        // need this to determine in releaseEvent whether it was a click (no position change between press and release)
        // call event of affected layout element:
        mMouseEventElement = layoutElementAt(event->pos());
        if (mMouseEventElement)
            mMouseEventElement->mousePressEvent(event);
  }
  event->accept();
}

mouseReleaseEvent

void QCustomPlot::mouseReleaseEvent(QMouseEvent *event)
{
    emit mouseRelease(event);
    bool doReplot = false;
    if (mSelectionMode == QCP::smode1) {
        qDebug() << __FUNCTION__ << mSelectionMode;
    } else {
        if ((mMousePressPos - event->pos()).manhattanLength() < 5) // determine whether it was a click operation
        {
            if (event->button() == Qt::LeftButton)
            {
              // handle selection mechanism:
              QVariant details;
              QCPLayerable *clickedLayerable = layerableAt(event->pos(), true, &details);
              bool selectionStateChanged = false;
              bool additive = mInteractions.testFlag(QCP::iMultiSelect) && event->modifiers().testFlag(mMultiSelectModifier);
              // deselect all other layerables if not additive selection:
              if (!additive)
              {
                foreach (QCPLayer *layer, mLayers)
                {
                  foreach (QCPLayerable *layerable, layer->children())
                  {
                    if (layerable != clickedLayerable && mInteractions.testFlag(layerable->selectionCategory()))
                    {
                      bool selChanged = false;
                      layerable->deselectEvent(&selChanged);
                      selectionStateChanged |= selChanged;
                    }
                  }
                }
              }
              if (clickedLayerable && mInteractions.testFlag(clickedLayerable->selectionCategory()))
              {
                // a layerable was actually clicked, call its selectEvent:
                bool selChanged = false;
                clickedLayerable->selectEvent(event, additive, details, &selChanged);
                selectionStateChanged |= selChanged;
              }
              doReplot = true;
              if (selectionStateChanged)
                emit selectionChangedByUser();
            }

            // emit specialized object click signals:
            QVariant details;
            QCPLayerable *clickedLayerable = layerableAt(event->pos(), false, &details); // for these signals, selectability is ignored, that's why we call this again with onlySelectable set to false
            if (QCPAbstractPlottable *ap = qobject_cast<QCPAbstractPlottable*>(clickedLayerable))
              emit plottableClick(ap, event);
            else if (QCPAxis *ax = qobject_cast<QCPAxis*>(clickedLayerable))
              emit axisClick(ax, details.value<QCPAxis::SelectablePart>(), event);
            else if (QCPAbstractItem *ai = qobject_cast<QCPAbstractItem*>(clickedLayerable))
              emit itemClick(ai, event);
            else if (QCPLegend *lg = qobject_cast<QCPLegend*>(clickedLayerable))
              emit legendClick(lg, 0, event);
            else if (QCPAbstractLegendItem *li = qobject_cast<QCPAbstractLegendItem*>(clickedLayerable))
              emit legendClick(li->parentLegend(), li, event);
            else if (QCPPlotTitle *pt = qobject_cast<QCPPlotTitle*>(clickedLayerable))
              emit titleClick(event, pt);
        }

        // call event of affected layout element:
        if (mMouseEventElement)
        {
            mMouseEventElement->mouseReleaseEvent(event);
            mMouseEventElement = 0;
        }

        if (doReplot || noAntialiasingOnDrag())
            replot();
    }
    event->accept();
}

mouseMoveEvent

void QCustomPlot::mouseMoveEvent(QMouseEvent *event)
{
    emit mouseMove(event);
    if (mSelectionMode == QCP::smode1) {
#if 1
        QCPAbstractPlottable *plot = plottableAt(event->pos(), true);
        if (plot) {
            switch (measure->state()) {
                case QCPMeasureGraph::mNoPointState:
                    measure->setStartPoint(event->pos());
                    measure->setEndPoint(event->pos());
                    replot();
                    break;
                case QCPMeasureGraph::mStartPointState:
                    measure->setEndPoint(event->pos());
                    replot();
                    break;
                default: break;
            }
            qDebug() << plot->name()
                     << measure->startPoint() << measure->endPoint()
                     << xAxis->pixelToCoord(measure->startPoint().x())
                     << yAxis->pixelToCoord(measure->startPoint().y());
        }
#endif
    } else {
        // call event of affected layout element:
        if (mMouseEventElement)
            mMouseEventElement->mouseMoveEvent(event);
   }
  event->accept();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值