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

📄 qwt_wheel.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if ( !testAttribute(Qt::WA_WState_OwnSizePolicy) )
#else
    if ( !testWState( WState_OwnSizePolicy ) ) 
#endif
    {
        QSizePolicy sp = sizePolicy();
        sp.transpose();
        setSizePolicy(sp);

#if QT_VERSION >= 0x040000
        setAttribute(Qt::WA_WState_OwnSizePolicy, false);
#else
        clearWState( WState_OwnSizePolicy );
#endif
    }

    QwtAbstractSlider::setOrientation(o);
    layoutWheel();
}

/*!
  \brief Specify the visible portion of the wheel.

  You may use this function for fine-tuning the appearance of
  the wheel. The default value is 175 degrees. The value is
  limited from 10 to 175 degrees.
  \param angle Visible angle in degrees
*/
void QwtWheel::setViewAngle(double angle)
{
    d_data->viewAngle = qwtLim( angle, 10.0, 175.0 );
    update();
}

double QwtWheel::viewAngle() const 
{
    return d_data->viewAngle;
}

/*!
  \brief Redraw the wheel
  \param p painter
  \param r contents rectangle
*/
void QwtWheel::drawWheel( QPainter *p, const QRect &r )
{
    //
    // draw background gradient
    //
    drawWheelBackground( p, r );

    if ( maxValue() == minValue() || d_data->totalAngle == 0.0 )
        return;

#if QT_VERSION < 0x040000
    const QColor light = colorGroup().light();
    const QColor dark = colorGroup().dark();
#else
    const QColor light = palette().color(QPalette::Light);
    const QColor dark = palette().color(QPalette::Dark);
#endif

    const double sign = (minValue() < maxValue()) ? 1.0 : -1.0;
    double cnvFactor = qwtAbs(d_data->totalAngle / (maxValue() - minValue()));
    const double halfIntv = 0.5 * d_data->viewAngle / cnvFactor;
    const double loValue = value() - halfIntv;
    const double hiValue = value() + halfIntv;
    const double tickWidth = 360.0 / double(d_data->tickCnt) / cnvFactor;
    const double sinArc = sin(d_data->viewAngle * M_PI / 360.0);
    cnvFactor *= M_PI / 180.0;


    //
    // draw grooves
    //
    if ( orientation() == Qt::Horizontal )
    {
        const double halfSize = double(r.width()) * 0.5;

        int l1 = r.y() + d_data->intBorder;
        int l2 = r.y() + r.height() - d_data->intBorder - 1;

        // draw one point over the border if border > 1
        if ( d_data->intBorder > 1 )
        {
            l1 --;
            l2 ++;
        }

        const int maxpos = r.x() + r.width() - 2;
        const int minpos = r.x() + 2;

        //
        // draw tick marks
        //
        for ( double tickValue = ceil(loValue / tickWidth) * tickWidth;
            tickValue < hiValue; tickValue += tickWidth )
        {
            //
            //  calculate position
            //
            const int tickPos = r.x() + r.width()
                - int( halfSize
                    * (sinArc + sign *  sin((tickValue - value()) * cnvFactor))
                    / sinArc);
            //
            // draw vertical line
            //
            if ( (tickPos <= maxpos) && (tickPos > minpos) )
            {
                p->setPen(dark);
                p->drawLine(tickPos -1 , l1, tickPos - 1,  l2 );  
                p->setPen(light);
                p->drawLine(tickPos, l1, tickPos, l2);  
            }
        }
    }
    else if ( orientation() == Qt::Vertical )
    {
        const double halfSize = double(r.height()) * 0.5;

        int l1 = r.x() + d_data->intBorder;
        int l2 = r.x() + r.width() - d_data->intBorder - 1;

        if ( d_data->intBorder > 1 )
        {
            l1--;
            l2++;
        }

        const int maxpos = r.y() + r.height() - 2;
        const int minpos = r.y() + 2;

        //
        // draw tick marks
        //
        for ( double tickValue = ceil(loValue / tickWidth) * tickWidth;
            tickValue < hiValue; tickValue += tickWidth )
        {

            //
            // calculate position
            //
            const int tickPos = r.y() + int( halfSize *
                (sinArc + sign * sin((tickValue - value()) * cnvFactor))
                / sinArc);

            //
            //  draw horizontal line
            //
            if ( (tickPos <= maxpos) && (tickPos > minpos) )
            {
                p->setPen(dark);
                p->drawLine(l1, tickPos - 1 ,l2, tickPos - 1);  
                p->setPen(light);
                p->drawLine(l1, tickPos, l2, tickPos);  
            }
        }
    }
}


//! Determine the value corresponding to a specified point
double QwtWheel::getValue( const QPoint &p )
{
    // The reference position is arbitrary, but the
    // sign of the offset is important
    int w, dx;
    if ( orientation() == Qt::Vertical )
    {
        w = d_data->sliderRect.height();
        dx = d_data->sliderRect.y() - p.y();
    }
    else
    {
        w = d_data->sliderRect.width();
        dx = p.x() - d_data->sliderRect.x();
    }

    // w pixels is an arc of viewAngle degrees,
    // so we convert change in pixels to change in angle
    const double ang = dx * d_data->viewAngle / w;

    // value range maps to totalAngle degrees,
    // so convert the change in angle to a change in value
    const double val = ang * ( maxValue() - minValue() ) / d_data->totalAngle;

    // Note, range clamping and rasterizing to step is automatically
    // handled by QwtAbstractSlider, so we simply return the change in value
    return val;
}

//! Qt Resize Event
void QwtWheel::resizeEvent(QResizeEvent *)
{
    layoutWheel( false );
}

//! Recalculate the slider's geometry and layout based on
//  the current rect and fonts.
//  \param update_geometry  notify the layout system and call update
//         to redraw the scale
void QwtWheel::layoutWheel( bool update_geometry )
{
    const QRect r = this->rect();
    d_data->sliderRect.setRect(r.x() + d_data->borderWidth, r.y() + d_data->borderWidth,
        r.width() - 2*d_data->borderWidth, r.height() - 2*d_data->borderWidth);

    if ( update_geometry )
    {
        updateGeometry();
        update();
    }
}

//! Qt Paint Event
void QwtWheel::paintEvent(QPaintEvent *e)
{
    // Use double-buffering
    const QRect &ur = e->rect();
    if ( ur.isValid() )
    {
#if QT_VERSION < 0x040000
        QwtPaintBuffer paintBuffer(this, ur);
        draw(paintBuffer.painter(), ur);
#else
        QPainter painter(this);
        draw(&painter, ur);
#endif
    }
}

//! Redraw panel and wheel
void QwtWheel::draw(QPainter *painter, const QRect&)
{
    qDrawShadePanel( painter, rect().x(), rect().y(),
        rect().width(), rect().height(),
#if QT_VERSION < 0x040000
        colorGroup(), 
#else
        palette(), 
#endif
        true, d_data->borderWidth );

    drawWheel( painter, d_data->sliderRect );

    if ( hasFocus() )
        QwtPainter::drawFocusRect(painter, this);
}

//! Notify value change 
void QwtWheel::valueChange()
{
    QwtAbstractSlider::valueChange();
    update();
}


/*!
  \brief Determine the scrolling mode and direction corresponding
         to a specified point
  \param p point
  \param scrollMode scrolling mode
  \param direction direction
*/
void QwtWheel::getScrollMode( const QPoint &p, int &scrollMode, int &direction)
{
    if ( d_data->sliderRect.contains(p) )
        scrollMode = ScrMouse;
    else
        scrollMode = ScrNone;

    direction = 0;
}

/*!
  \brief Set the mass of the wheel

  Assigning a mass turns the wheel into a flywheel.
  \param val the wheel's mass
*/
void QwtWheel::setMass(double val)
{
    QwtAbstractSlider::setMass(val);
}

/*!
  \brief Set the width of the wheel

  Corresponds to the wheel height for horizontal orientation,
  and the wheel width for vertical orientation.
  \param w the wheel's width
*/
void QwtWheel::setWheelWidth(int w)
{
    d_data->wheelWidth = w;
    layoutWheel();
}

/*!
  \return a size hint
*/
QSize QwtWheel::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
  \warning The return value is based on the wheel width.
*/
QSize QwtWheel::minimumSizeHint() const
{
    QSize sz( 3*d_data->wheelWidth + 2*d_data->borderWidth,
    d_data->wheelWidth + 2*d_data->borderWidth );
    if ( orientation() != Qt::Horizontal )
        sz.transpose();
    return sz;
}

/*!
  \brief Call update() when the palette changes
*/
void QwtWheel::paletteChange( const QPalette& )
{
    update();
}

⌨️ 快捷键说明

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