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

📄 qwt_slider.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#endif
        );

        qDrawShadeLine(p, pos, sliderRect.y(), 
            pos, sliderRect.y() + sliderRect.height() - 2, 
#if QT_VERSION < 0x040000
            colorGroup(), 
#else
            palette(), 
#endif
            true, 1);
    }
    else // Vertical
    {
        qDrawShadePanel(p,sliderRect.x(), pos - d_data->thumbLength / 2, 
            sliderRect.width(), d_data->thumbLength,
#if QT_VERSION < 0x040000
            colorGroup(),
#else
            palette(), 
#endif
            false, d_data->borderWidth, 
#if QT_VERSION < 0x040000
            &colorGroup().brush(QColorGroup::Button)
#else
            &palette().brush(QPalette::Button)
#endif
        );

        qDrawShadeLine(p, sliderRect.x(), pos,
            sliderRect.x() + sliderRect.width() - 2, pos, 
#if QT_VERSION < 0x040000
            colorGroup(), 
#else
            palette(), 
#endif
            true, 1);
    }
}

//! Find the x/y position for a given value v
int QwtSlider::xyPosition(double v) const
{
    return d_data->map.transform(v);
}

//! Determine the value corresponding to a specified mouse location.
double QwtSlider::getValue(const QPoint &p)
{
    return d_data->map.invTransform(
        orientation() == Qt::Horizontal ? p.x() : p.y());
}


/*!
  \brief Determine scrolling mode and direction
  \param p point
  \param scrollMode Scrolling mode
  \param direction Direction
*/
void QwtSlider::getScrollMode(const QPoint &p, 
    int &scrollMode, int &direction )
{
    if (!d_data->sliderRect.contains(p))
    {
        scrollMode = ScrNone;
        direction = 0;
        return;
    }

    const int pos = ( orientation() == Qt::Horizontal ) ? p.x() : p.y();
    const int markerPos = xyPosition(value());

    if ((pos > markerPos - d_data->thumbLength / 2)
        && (pos < markerPos + d_data->thumbLength / 2))
    {
        scrollMode = ScrMouse;
        direction = 0;
        return;
    }

    scrollMode = ScrPage;
    direction = (pos > markerPos) ? 1 : -1;

    if ( scaleDraw()->map().p1() > scaleDraw()->map().p2() )
        direction = -direction;
}

//! Qt paint event
void QwtSlider::paintEvent(QPaintEvent *e)
{
    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
    }
}

//! Draw the QwtSlider
void QwtSlider::draw(QPainter *painter, const QRect&)
{
    if (d_data->scalePos != NoScale)
    {
#if QT_VERSION < 0x040000
        scaleDraw()->draw(painter, colorGroup());
#else
        scaleDraw()->draw(painter, palette());
#endif
    }

    drawSlider(painter, d_data->sliderRect);

    if ( hasFocus() )
        QwtPainter::drawFocusRect(painter, this, d_data->sliderRect);
}

//! Qt resize event
void QwtSlider::resizeEvent(QResizeEvent *)
{
    layoutSlider( 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 QwtSlider::layoutSlider( bool update_geometry )
{
    int sliderWidth = d_data->thumbWidth;
    int sld1 = d_data->thumbLength / 2 - 1;
    int sld2 = d_data->thumbLength / 2 + d_data->thumbLength % 2;
    if ( d_data->bgStyle & BgTrough )
    {
        sliderWidth += 2 * d_data->borderWidth;
        sld1 += d_data->borderWidth;
        sld2 += d_data->borderWidth;
    }

    int scd = 0;
    if ( d_data->scalePos != NoScale )
    {
        int d1, d2;
        scaleDraw()->getBorderDistHint(font(), d1, d2);
        scd = qwtMax(d1, d2);
    }

    int slo = scd - sld1;
    if ( slo < 0 )
        slo = 0;

    int x, y, length;

    const QRect r = rect();
    if (orientation() == Qt::Horizontal)
    {
        switch (d_data->scalePos)
        {
            case TopScale:
            {
                d_data->sliderRect.setRect(
                    r.x() + d_data->xMargin + slo,
                    r.y() + r.height() -
                    d_data->yMargin - sliderWidth,
                    r.width() - 2 * d_data->xMargin - 2 * slo,
                    sliderWidth);

                x = d_data->sliderRect.x() + sld1;
                y = d_data->sliderRect.y() - d_data->scaleDist;

                break;
            }

            case BottomScale:
            {
                d_data->sliderRect.setRect(
                    r.x() + d_data->xMargin + slo,
                    r.y() + d_data->yMargin,
                    r.width() - 2 * d_data->xMargin - 2 * slo,
                    sliderWidth);
    
                x = d_data->sliderRect.x() + sld1;
                y = d_data->sliderRect.y() + d_data->sliderRect.height() 
                    + d_data->scaleDist;

                break;
            }

            case NoScale: // like Bottom, but no scale. See QwtSlider().
            default:   // inconsistent orientation and scale position
            {
                d_data->sliderRect.setRect(
                    r.x() + d_data->xMargin + slo,
                    r.y() + d_data->yMargin,
                    r.width() - 2 * d_data->xMargin - 2 * slo,
                    sliderWidth);

                x = d_data->sliderRect.x() + sld1;
                y = 0;

                break;
            }
        }
        length = d_data->sliderRect.width() - (sld1 + sld2);
    }
    else // if (orientation() == Qt::Vertical
    {
        switch (d_data->scalePos)
        {
            case RightScale:
                d_data->sliderRect.setRect(
                    r.x() + d_data->xMargin,
                    r.y() + d_data->yMargin + slo,
                    sliderWidth,
                    r.height() - 2 * d_data->yMargin - 2 * slo);

                x = d_data->sliderRect.x() + d_data->sliderRect.width() 
                    + d_data->scaleDist;
                y = d_data->sliderRect.y() + sld1;

                break;

            case LeftScale:
                d_data->sliderRect.setRect(
                    r.x() + r.width() - sliderWidth - d_data->xMargin,
                    r.y() + d_data->yMargin + slo,
                    sliderWidth,
                    r.height() - 2 * d_data->yMargin - 2 * slo);

                x = d_data->sliderRect.x() - d_data->scaleDist;
                y = d_data->sliderRect.y() + sld1;

                break;

            case NoScale: // like Left, but no scale. See QwtSlider().
            default:   // inconsistent orientation and scale position
                d_data->sliderRect.setRect(
                    r.x() + r.width() - sliderWidth - d_data->xMargin,
                    r.y() + d_data->yMargin + slo,
                    sliderWidth,
                    r.height() - 2 * d_data->yMargin - 2 * slo);

                x = 0;
                y = d_data->sliderRect.y() + sld1;

                break;
        }
        length = d_data->sliderRect.height() - (sld1 + sld2);
    }

    scaleDraw()->move(x, y);
    scaleDraw()->setLength(length);

    d_data->map.setPaintXInterval(scaleDraw()->map().p1(),
        scaleDraw()->map().p2());

    if ( update_geometry )
    {
        d_data->sizeHintCache = QSize(); // invalidate
        updateGeometry();
        update();
    }
}

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


//! Notify change of range
void QwtSlider::rangeChange()
{
    d_data->map.setScaleInterval(minValue(), maxValue());

    if (autoScale())
        rescale(minValue(), maxValue());

    QwtAbstractSlider::rangeChange();
    layoutSlider();
}

/*!
  \brief Set distances between the widget's border and internals.
  \param xMargin Horizontal margin
  \param yMargin Vertical margin
*/
void QwtSlider::setMargins(int xMargin, int yMargin)
{
    if ( xMargin < 0 )
        xMargin = 0;
    if ( yMargin < 0 )
        yMargin = 0;

    if ( xMargin != d_data->xMargin || yMargin != d_data->yMargin )
    {
        d_data->xMargin = xMargin;
        d_data->yMargin = yMargin;
        layoutSlider();
    }
}

/*!
  Set the background style.
*/
void QwtSlider::setBgStyle(BGSTYLE st) 
{
    d_data->bgStyle = st; 
    layoutSlider();
}

/*!
  \return the background style.
*/
QwtSlider::BGSTYLE QwtSlider::bgStyle() const 
{ 
    return d_data->bgStyle; 
}

/*!
  \return the thumb length.
*/
int QwtSlider::thumbLength() const 
{
    return d_data->thumbLength;
}

/*!
  \return the thumb width.
*/
int QwtSlider::thumbWidth() const 
{
    return d_data->thumbWidth;
}

/*!
  \return the border width.
*/
int QwtSlider::borderWidth() const 
{
    return d_data->borderWidth;
}

/*!
  \return QwtSlider::minimumSizeHint()
*/
QSize QwtSlider::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
  \warning The return value of QwtSlider::minimumSizeHint() depends on 
           the font and the scale.
*/
QSize QwtSlider::minimumSizeHint() const
{
    if (!d_data->sizeHintCache.isEmpty()) 
        return d_data->sizeHintCache;

    int sliderWidth = d_data->thumbWidth;
    if (d_data->bgStyle & BgTrough)
        sliderWidth += 2 * d_data->borderWidth;

    int w = 0, h = 0;
    if (d_data->scalePos != NoScale)
    {
        int d1, d2;
        scaleDraw()->getBorderDistHint(font(), d1, d2);
        int msMbd = qwtMax(d1, d2);

        int mbd = d_data->thumbLength / 2;
        if (d_data->bgStyle & BgTrough)
            mbd += d_data->borderWidth;

        if ( mbd < msMbd )
            mbd = msMbd;

        const int sdExtent = scaleDraw()->extent( QPen(), font() );
        const int sdLength = scaleDraw()->minLength( QPen(), font() );

        h = sliderWidth + sdExtent + d_data->scaleDist;
        w = sdLength - 2 * msMbd + 2 * mbd;
    }
    else  // no scale
    {
        w = 200;
        h = sliderWidth;
    }

    if ( orientation() == Qt::Vertical )
        qSwap(w, h);

    w += 2 * d_data->xMargin;
    h += 2 * d_data->yMargin;

    d_data->sizeHintCache = QSize(w, h);
    return d_data->sizeHintCache;
}

⌨️ 快捷键说明

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