⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qwt_plot_zoomer.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* -*- 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
 *****************************************************************************/

// vim: expandtab

#include <math.h>
#include "qwt_plot.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_zoomer.h"
#include "qwt_scale_div.h"
#if QT_VERSION < 0x040000
typedef QValueStack<QwtDoubleRect> QwtZoomStack;
#else
typedef QStack<QwtDoubleRect> QwtZoomStack;
#endif

class QwtPlotZoomer::PrivateData
{
public:
    uint zoomRectIndex;
    QwtZoomStack zoomStack;

    int maxStackDepth;
};

/*!
  \brief Create a zoomer for a plot canvas.

  The zoomer is set to those x- and y-axis of the parent plot of the
  canvas that are enabled. If both or no x-axis are enabled, the picker
  is set to QwtPlot::xBottom. If both or no y-axis are
  enabled, it is set to QwtPlot::yLeft.

  The selectionFlags() are set to 
  QwtPicker::RectSelection & QwtPicker::ClickSelection, the
  tracker mode to QwtPicker::ActiveOnly.

  \param canvas Plot canvas to observe, also the parent object
  \param doReplot Call replot for the attached plot before initializing
                  the zoomer with its scales. This might be necessary, 
                  when the plot is in a state with pending scale changes.

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), QwtPlotPicker::setZoomBase()
*/
QwtPlotZoomer::QwtPlotZoomer(QwtPlotCanvas *canvas, bool doReplot):
    QwtPlotPicker(canvas)
{
    if ( canvas )
        init(RectSelection & ClickSelection, ActiveOnly, doReplot);
}

/*!
  \brief Create a zoomer for a plot canvas.

  The selectionFlags() are set to 
  QwtPicker::RectSelection & QwtPicker::ClickSelection, the
  tracker mode to QwtPicker::ActiveOnly. 

  \param xAxis X axis of the zoomer
  \param yAxis Y axis of the zoomer
  \param canvas Plot canvas to observe, also the parent object
  \param doReplot Call replot for the attached plot before initializing
                  the zoomer with its scales. This might be necessary, 
                  when the plot is in a state with pending scale changes.

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), QwtPlotPicker::setZoomBase()
*/

QwtPlotZoomer::QwtPlotZoomer(int xAxis, int yAxis,
        QwtPlotCanvas *canvas, bool doReplot):
    QwtPlotPicker(xAxis, yAxis, canvas)
{
    if ( canvas )
        init(RectSelection & ClickSelection, ActiveOnly, doReplot);
}

/*!
  Create a zoomer for a plot canvas.

  \param xAxis X axis of the zoomer
  \param yAxis Y axis of the zoomer
  \param selectionFlags Or'd value of QwtPicker::RectSelectionType and
                        QwtPicker::SelectionMode. 
                        QwtPicker::RectSelection will be auto added.
  \param trackerMode Tracker mode
  \param canvas Plot canvas to observe, also the parent object
  \param doReplot Call replot for the attached plot before initializing
                  the zoomer with its scales. This might be necessary, 
                  when the plot is in a state with pending scale changes.

  \sa QwtPicker, QwtPicker::setSelectionFlags(), QwtPicker::setRubberBand(),
      QwtPicker::setTrackerMode

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), setZoomBase()
*/

QwtPlotZoomer::QwtPlotZoomer(int xAxis, int yAxis, int selectionFlags,
        DisplayMode trackerMode, QwtPlotCanvas *canvas, bool doReplot):
    QwtPlotPicker(xAxis, yAxis, canvas)
{
    if ( canvas )
        init(selectionFlags, trackerMode, doReplot);
}

//! Init the zoomer, used by the constructors
void QwtPlotZoomer::init(int selectionFlags, 
    DisplayMode trackerMode, bool doReplot)
{
    d_data = new PrivateData;

    d_data->maxStackDepth = -1;

    setSelectionFlags(selectionFlags);
    setTrackerMode(trackerMode);
    setRubberBand(RectRubberBand);

    if ( doReplot && plot() )
        plot()->replot();

    setZoomBase(scaleRect());
}

QwtPlotZoomer::~QwtPlotZoomer()
{
    delete d_data;
}

/*!
  \brief Limit the number of recursive zoom operations to depth.

  A value of -1 set the depth to unlimited, 0 disables zooming.
  If the current zoom rectangle is below depth, the plot is unzoomed.

  \param depth Maximum for the stack depth
  \sa maxStackDepth()
  \note depth doesn't include the zoom base, so zoomStack().count() might be
              maxStackDepth() + 1.
*/
void QwtPlotZoomer::setMaxStackDepth(int depth)
{
    d_data->maxStackDepth = depth;

    if ( depth >= 0 )
    {
        // unzoom if the current depth is below d_data->maxStackDepth

        const int zoomOut = 
            int(d_data->zoomStack.count()) - 1 - depth; // -1 for the zoom base

        if ( zoomOut > 0 )
        {
            zoom(-zoomOut);
            for ( int i = int(d_data->zoomStack.count()) - 1; 
                i > int(d_data->zoomRectIndex); i-- )
            {
                (void)d_data->zoomStack.pop(); // remove trailing rects
            }
        }
    }
}

/*!
  \return Maximal depth of the zoom stack.
  \sa setMaxStackDepth()
*/
int QwtPlotZoomer::maxStackDepth() const
{
    return d_data->maxStackDepth;
}

/*!
  Return the zoom stack. zoomStack()[0] is the zoom base,
  zoomStack()[1] the first zoomed rectangle.
*/
const QwtZoomStack &QwtPlotZoomer::zoomStack() const
{
    return d_data->zoomStack;
}

/*!
  \return Initial rectangle of the zoomer
  \sa setZoomBase(), zoomRect()
*/
QwtDoubleRect QwtPlotZoomer::zoomBase() const
{
    return d_data->zoomStack[0];
}

/*!
  Reinitialized the zoom stack with scaleRect() as base.

  \sa zoomBase(), scaleRect()

  \warning Calling QwtPlot::setAxisScale() while QwtPlot::autoReplot() is false
           leaves the axis in an 'intermediate' state.
           In this case, to prevent buggy behaviour, you must call
           QwtPlot::replot() before calling QwtPlotZoomer::setZoomBase().
           This quirk will be removed in a future release.

  \sa QwtPlot::autoReplot(), QwtPlot::replot().
*/
void QwtPlotZoomer::setZoomBase()
{
    const QwtPlot *plt = plot();
    if ( !plt )
        return;

    d_data->zoomStack.clear();
    d_data->zoomStack.push(scaleRect());
    d_data->zoomRectIndex = 0;

    rescale();
}

/*!
  \brief Set the initial size of the zoomer.

  base is united with the current scaleRect() and the zoom stack is
  reinitalized with it as zoom base. plot is zoomed to scaleRect().
  
  \param base Zoom base
  
  \sa zoomBase(), scaleRect()
*/
void QwtPlotZoomer::setZoomBase(const QwtDoubleRect &base)
{
    const QwtPlot *plt = plot();
    if ( !plt )
        return;

    const QwtDoubleRect sRect = scaleRect();
    const QwtDoubleRect bRect = base | sRect;

    d_data->zoomStack.clear();
    d_data->zoomStack.push(bRect);
    d_data->zoomRectIndex = 0;

    if ( base != sRect )
    {
        d_data->zoomStack.push(sRect);
        d_data->zoomRectIndex++;
    }

    rescale();
}

/*! 
  Rectangle at the current position on the zoom stack. 

  \sa QwtPlotZoomer::zoomRectIndex(), QwtPlotZoomer::scaleRect().
*/
QwtDoubleRect QwtPlotZoomer::zoomRect() const
{
    return d_data->zoomStack[d_data->zoomRectIndex];
}

/*! 
  \return Index of current position of zoom stack.
*/
uint QwtPlotZoomer::zoomRectIndex() const
{
    return d_data->zoomRectIndex;
}

/*!
  \brief Zoom in

  Clears all rectangles above the current position of the
  zoom stack and pushs the intersection of zoomRect() and 
  the normalized rect on it.

  \note If the maximal stack depth is reached, zoom is ignored.
  \note The zoomed signal is emitted.
*/

void QwtPlotZoomer::zoom(const QwtDoubleRect &rect)
{   
    if ( d_data->maxStackDepth >= 0 && 
        int(d_data->zoomRectIndex) >= d_data->maxStackDepth )
    {
        return;
    }

    const QwtDoubleRect zoomRect = d_data->zoomStack[0] & rect.normalized();
    if ( zoomRect != d_data->zoomStack[d_data->zoomRectIndex] )
    {
        for ( uint i = int(d_data->zoomStack.count()) - 1; 
           i > d_data->zoomRectIndex; i-- )
        {
            (void)d_data->zoomStack.pop();
        }

        d_data->zoomStack.push(zoomRect);
        d_data->zoomRectIndex++;

        rescale();

        emit zoomed(zoomRect);
    }
}

/*!
  \brief Zoom in or out

  Activate a rectangle on the zoom stack with an offset relative
  to the current position. Negative values of offest will zoom out,
  positive zoom in. A value of 0 zooms out to the zoom base.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -