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

📄 qwt_dial.cpp

📁 软件无线电的平台
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    double origin = d_origin;    if ( mode() == RotateScale )    {        origin -= direction - d_origin;        direction = d_origin;    }    painter->save();    drawScale(painter, center, radius, origin, d_minScaleArc, d_maxScaleArc);    painter->restore();    if ( isValid() )    {        QPalette::ColorGroup cg;        if ( isEnabled() )            cg = hasFocus() ? QPalette::Active : QPalette::Inactive;        else            cg = QPalette::Disabled;        painter->save();        drawNeedle(painter, center, radius, direction, cg);        painter->restore();    }}/*!  Draw the needle  Qwt is missing a set of good looking needles.  Contributions are very welcome.  \param painter Painter  \param center Center of the dial  \param radius Length for the needle  \param direction Direction of the needle in degrees, counter clockwise  \param cg ColorGroup*/void QwtDial::drawNeedle(QPainter *painter, const QPoint &center,     int radius, double direction, QPalette::ColorGroup cg) const{    if ( d_needle )    {        direction = 360.0 - direction; // counter clockwise        d_needle->draw(painter, center, radius, direction, cg);    }}/*!  Draw the scale  \param painter Painter  \param center Center of the dial  \param radius Radius of the scale  \param origin Origin of the scale  \param minArc Minimum of the arc   \param maxArc Minimum of the arc     \sa QwtScaleDraw::setAngleRange*/void QwtDial::drawScale(QPainter *painter, const QPoint &center,    int radius, double origin, double minArc, double maxArc) const{    if ( d_scaleDraw == NULL )        return;    origin -= 270.0; // hardcoded origin of QwtScaleDraw    double angle = maxArc - minArc;    if ( angle > 360.0 )        angle = fmod(angle, 360.0);    minArc += origin;    if ( minArc < -360.0 )        minArc = fmod(minArc, 360.0);        maxArc = minArc + angle;    if ( maxArc > 360.0 )    {        // QwtScaleDraw::setAngleRange accepts only values        // in the range [-360.0..360.0]        minArc -= 360.0;        maxArc -= 360.0;    }        painter->setFont(font());    painter->setPen(QPen(colorGroup().text(), d_scaleDraw->penWidth()));    d_scaleDraw->setAngleRange(minArc, maxArc);    d_scaleDraw->setGeometry(        center.x() - radius + 1,         center.y() - radius + 1,        2 * radius, QwtScaleDraw::Round);    d_scaleDraw->draw(painter);}void QwtDial::drawScaleContents(QPainter *,     const QPoint &, int) const{    // empty default implementation}/*!  Set a needle for the dial  Qwt is missing a set of good looking needles.   Contributions are very welcome.  \param needle Needle  \warning The needle will be deleted, when a different needle is    set or in ~QwtDial*/void QwtDial::setNeedle(QwtDialNeedle *needle){    if ( needle != d_needle )    {        if ( d_needle )            delete d_needle;        d_needle = needle;        update();    }}/*!   \return needle  \sa QwtDial::setNeedle*/const QwtDialNeedle *QwtDial::needle() const {     return d_needle; }/*!   \return needle  \sa QwtDial::setNeedle*/QwtDialNeedle *QwtDial::needle() {     return d_needle; }//! QwtDblRange update hookvoid QwtDial::rangeChange(){    updateScale();}/*!   Update the scale with the current attributes  \sa QwtDial::setScale*/void QwtDial::updateScale(){    if ( d_scaleDraw )    {        d_scaleDraw->setScale(minValue(), maxValue(),            d_maxMajIntv, d_maxMinIntv, d_scaleStep);    }}/*!  Set an individual scale draw  \warning The previous scale draw is deleted*/void QwtDial::setScaleDraw(QwtDialScaleDraw *scaleDraw){    if ( scaleDraw != d_scaleDraw )    {        if ( d_scaleDraw )            delete d_scaleDraw;            d_scaleDraw = scaleDraw;        updateScale();        update();    }}/*!  Change the intervals of the scale  \sa QwtScaleDraw::setScale*/void QwtDial::setScale(int maxMajIntv, int maxMinIntv, double step){    d_maxMajIntv = maxMajIntv;    d_maxMinIntv = maxMinIntv;    d_scaleStep = step;    updateScale();}/*!  A wrapper method for accessing the scale draw.   - options == 0\n    No visible scale: setScaleDraw(NULL)  - options & ScaleBackbone\n    En/disable the backbone of the scale.  - options & ScaleTicks\n    Don磘 change anything. Otherwise set all tick lengths to 0.   - options & ScaleLabel\n    En/disable scale labels      \sa QwtScaleDraw:.setOptions, QwtScaleDraw::setTickLength,       QwtDialScaleDraw::showLabels*/void QwtDial::setScaleOptions(int options){    if ( options == 0 )        setScaleDraw(NULL);    if ( d_scaleDraw == NULL )        return;    int flags = d_scaleDraw->options();    if ( options & ScaleBackbone )        flags |= QwtScaleDraw::Backbone;    else        flags &= ~QwtScaleDraw::Backbone;    d_scaleDraw->setOptions(flags);    if ( !(options & ScaleTicks) )        d_scaleDraw->setTickLength(0, 0, 0);    d_scaleDraw->showLabels(options & ScaleLabel);}//! See: QwtScaleDraw::setTickLength, QwtDialScaleDraw::setPenWidthvoid QwtDial::setScaleTicks(int minLen, int medLen,     int majLen, int penWidth){    if ( d_scaleDraw )    {        d_scaleDraw->setTickLength(minLen, medLen, majLen);        d_scaleDraw->setPenWidth(penWidth);    }}/*!   \return the label for a value*/QString QwtDial::scaleLabel(double value) const{#if 1    if ( value == -0 )        value = 0;#endif    QString text;    text.sprintf("%g", value);    return text;}/*!  \brief Change the origin    The origin is the angle where scale and needle is relative to.  \param origin New origin  \sa QwtDial::origin()*/void QwtDial::setOrigin(double origin){    d_origin = origin;    update();}/*!  The origin is the angle where scale and needle is relative to.  \return Origin of the dial  \sa QwtDial::setOrigin()*/double QwtDial::origin() const{    return d_origin;}/*!  Change the arc of the scale  \param minArc Lower limit  \param maxArc Upper limit*/void QwtDial::setScaleArc(double minArc, double maxArc){    if ( minArc != 360.0 && minArc != -360.0 )        minArc = fmod(minArc, 360.0);    if ( maxArc != 360.0 && maxArc != -360.0 )        maxArc = fmod(maxArc, 360.0);    d_minScaleArc = QMIN(minArc, maxArc);    d_maxScaleArc = QMAX(minArc, maxArc);    if ( d_maxScaleArc - d_minScaleArc > 360.0 )        d_maxScaleArc = d_minScaleArc + 360.0;        update();}//! QwtDblRange update hookvoid QwtDial::valueChange(){    update();    QwtSliderBase::valueChange();}/*!  \return QwtDial::sizeHint()*/QSize QwtDial::sizeHint() const{    int sh = 0;    if ( d_scaleDraw )        sh = d_scaleDraw->minHeight( QPen(), fontMetrics() );    const int d = 6 * sh + 2 * lineWidth();        return QSize( d, d );}/*!   \brief Return a minimum size hint  \warning The return value of QwtDial::minimumSizeHint() depends on the           font and the scale.*/  QSize QwtDial::minimumSizeHint() const{       int sh = 0;    if ( d_scaleDraw )        sh = d_scaleDraw->minHeight( QPen(), fontMetrics() );    const int d = 3 * sh + 2 * lineWidth();        return QSize( d, d );}static double line2Radians(const QPoint &p1, const QPoint &p2){    const QPoint p = p2 - p1;    double angle;    if ( p.x() == 0 )        angle = ( p.y() <= 0 ) ? M_PI_2 : 3 * M_PI_2;    else    {        angle = atan(double(-p.y()) / double(p.x()));        if ( p.x() < 0 )            angle += M_PI;        if ( angle < 0.0 )            angle += 2 * M_PI;    }    return 360.0 - angle * 180.0 / M_PI;}/*!  Find the value for a given position  \param pos  \return Value*/double QwtDial::getValue(const QPoint &pos){    if ( d_maxScaleArc == d_minScaleArc || maxValue() == minValue() )        return minValue();    double dir = line2Radians(rect().center(), pos) - d_origin;    if ( dir < 0.0 )        dir += 360.0;    if ( mode() == RotateScale )        dir = 360.0 - dir;    // The position might be in the area that is outside the scale arc.    // We need the range of the scale if it was a complete circle.    const double completeCircle = 360.0 / (d_maxScaleArc - d_minScaleArc)         * (maxValue() - minValue());    double posValue = minValue() + completeCircle * dir / 360.0;    if ( d_scrollMode == ScrMouse )    {        if ( d_previousDir >= 0.0 ) // valid direction        {            // We have to find out whether the mouse is moving            // clock or counter clockwise            bool clockWise = FALSE;            const double angle = dir - d_previousDir;            if ( (angle >= 0.0 && angle <= 180.0) || angle < -180.0 )                clockWise = TRUE;            if ( clockWise )            {                if ( dir < d_previousDir && d_mouseOffset > 0.0 )                {                    // We passed 360 -> 0                    d_mouseOffset -= completeCircle;                    }                if ( wrapping() )                {                    if ( posValue - d_mouseOffset > maxValue() )                    {                        // We passed maxValue and the value will be set                        // to minValue. We have to adjust the d_mouseOffset.                        d_mouseOffset = posValue - minValue();                    }                }                else                {                    if ( posValue - d_mouseOffset > maxValue() ||                        value() == maxValue() )                    {                        // We fix the value at maxValue by adjusting                        // the mouse offset.                        d_mouseOffset = posValue - maxValue();                    }                }            }            else            {                if ( dir > d_previousDir && d_mouseOffset < 0.0 )                {                    // We passed 0 -> 360                     d_mouseOffset += completeCircle;                    }                if ( wrapping() )                {                    if ( posValue - d_mouseOffset < minValue() )                    {                        // We passed minValue and the value will be set                        // to maxValue. We have to adjust the d_mouseOffset.                        d_mouseOffset = posValue - maxValue();                    }                }                else                {                    if ( posValue - d_mouseOffset < minValue() ||                        value() == minValue() )                    {                        // We fix the value at minValue by adjusting                        // the mouse offset.                        d_mouseOffset = posValue - minValue();                    }                }            }        }        d_previousDir = dir;    }    return posValue;}/*!  \sa QwtSliderBase::getScrollMode*/void QwtDial::getScrollMode(const QPoint &p, int &scrollMode, int &direction){    direction = 0;    scrollMode = ScrNone;    const QRegion region(contentsRect(), QRegion::Ellipse);    if ( region.contains(p) && p != rect().center() )    {        scrollMode = ScrMouse;        d_previousDir = -1.0;    }}/*!   Handles key events  - Key_Down, KeyLeft\n    Decrement by 1  - Key_Prior\n    Decrement by pageSize()  - Key_Home\n    Set the value to minValue()  - Key_Up, KeyRight\n    Increment by 1  - Key_Next\n    Increment by pageSize()  - Key_End\n    Set the value to maxValue()  \sa isReadOnly()*/void QwtDial::keyPressEvent(QKeyEvent *e){    if ( isReadOnly() )    {#if QT_VERSION >= 300        e->ignore();#endif        return;    }    if ( !isValid() )        return;    double previous = prevValue();    switch ( e->key() )    {        case Key_Down:        case Key_Left:            QwtDblRange::incValue(-1);            break;        case Key_Prior:            QwtDblRange::incValue(-pageSize());            break;        case Key_Home:            setValue(minValue());            break;        case Key_Up:        case Key_Right:            QwtDblRange::incValue(1);            break;        case Key_Next:            QwtDblRange::incValue(pageSize());            break;        case Key_End:            setValue(maxValue());            break;        default:;#if QT_VERSION >= 300            e->ignore();#endif    }    if (value() != previous)        emit sliderMoved(value());}

⌨️ 快捷键说明

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