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

📄 qwt_thermo.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//! Return the scale position.
QwtThermo::ScalePos QwtThermo::scalePosition() const
{
    return d_data->scalePos;
}

//! Notify a font change.
void QwtThermo::fontChange(const QFont &f)
{
    QWidget::fontChange( f );
    layoutThermo();
}

//! Notify a scale change.
void QwtThermo::scaleChange()
{
    update();
    layoutThermo();
}

//! Redraw the liquid in thermometer pipe.
void QwtThermo::drawThermo(QPainter *p)
{
    int alarm  = 0, taval = 0;

    QRect fRect;
    QRect aRect;
    QRect bRect;

    int inverted = ( d_data->maxValue < d_data->minValue );

    //
    //  Determine if value exceeds alarm threshold.
    //  Note: The alarm value is allowed to lie
    //        outside the interval (minValue, maxValue).
    //
    if (d_data->alarmEnabled)
    {
        if (inverted)
        {
            alarm = ((d_data->alarmLevel >= d_data->maxValue)
                 && (d_data->alarmLevel <= d_data->minValue)
                 && (d_data->value >= d_data->alarmLevel));
        
        }
        else
        {
            alarm = (( d_data->alarmLevel >= d_data->minValue)
                 && (d_data->alarmLevel <= d_data->maxValue)
                 && (d_data->value >= d_data->alarmLevel));
        }
    }

    //
    //  transform values
    //
    int tval = transform(d_data->value);

    if (alarm)
       taval = transform(d_data->alarmLevel);

    //
    //  calculate recangles
    //
    if ( d_data->orientation == Qt::Horizontal )
    {
        if (inverted)
        {
            bRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
                  tval - d_data->thermoRect.x(),
                  d_data->thermoRect.height());
        
            if (alarm)
            {
                aRect.setRect(tval, d_data->thermoRect.y(),
                      taval - tval + 1,
                      d_data->thermoRect.height());
                fRect.setRect(taval + 1, d_data->thermoRect.y(),
                      d_data->thermoRect.x() + d_data->thermoRect.width() - (taval + 1),
                      d_data->thermoRect.height());
            }
            else
            {
                fRect.setRect(tval, d_data->thermoRect.y(),
                      d_data->thermoRect.x() + d_data->thermoRect.width() - tval,
                      d_data->thermoRect.height());
            }
        }
        else
        {
            bRect.setRect(tval + 1, d_data->thermoRect.y(),
                  d_data->thermoRect.width() - (tval + 1 - d_data->thermoRect.x()),
                  d_data->thermoRect.height());
        
            if (alarm)
            {
                aRect.setRect(taval, d_data->thermoRect.y(),
                      tval - taval + 1,
                      d_data->thermoRect.height());
                fRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
                      taval - d_data->thermoRect.x(),
                      d_data->thermoRect.height());
            }
            else
            {
                fRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
                      tval - d_data->thermoRect.x() + 1,
                      d_data->thermoRect.height());
            }
        
        }
    }
    else // Qt::Vertical
    {
        if (tval < d_data->thermoRect.y())
            tval = d_data->thermoRect.y();
        else 
        {
            if (tval > d_data->thermoRect.y() + d_data->thermoRect.height())
                tval = d_data->thermoRect.y() + d_data->thermoRect.height();
        }

        if (inverted)
        {
            bRect.setRect(d_data->thermoRect.x(), tval + 1,
            d_data->thermoRect.width(),
            d_data->thermoRect.height() - (tval + 1 - d_data->thermoRect.y()));

            if (alarm)
            {
                aRect.setRect(d_data->thermoRect.x(), taval,
                    d_data->thermoRect.width(),
                    tval - taval + 1);
                fRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
                    d_data->thermoRect.width(),
                taval - d_data->thermoRect.y());
            }
            else
            {
                fRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
                    d_data->thermoRect.width(),
                    tval - d_data->thermoRect.y() + 1);
            }
        }
        else
        {
            bRect.setRect(d_data->thermoRect.x(), d_data->thermoRect.y(),
            d_data->thermoRect.width(),
            tval - d_data->thermoRect.y());
            if (alarm)
            {
                aRect.setRect(d_data->thermoRect.x(),tval,
                    d_data->thermoRect.width(),
                    taval - tval + 1);
                fRect.setRect(d_data->thermoRect.x(),taval + 1,
                    d_data->thermoRect.width(),
                    d_data->thermoRect.y() + d_data->thermoRect.height() - (taval + 1));
            }
            else
            {
                fRect.setRect(d_data->thermoRect.x(),tval,
                    d_data->thermoRect.width(),
                d_data->thermoRect.y() + d_data->thermoRect.height() - tval);
            }
        }
    }

    //
    // paint thermometer
    //
    const QColor bgColor =
#if QT_VERSION < 0x040000
        colorGroup().color(QColorGroup::Background);
#else
        palette().color(QPalette::Background);
#endif
    p->fillRect(bRect, bgColor);

    if (alarm)
       p->fillRect(aRect, d_data->alarmBrush);

    p->fillRect(fRect, d_data->fillBrush);
}

//! Set the border width of the pipe.
void QwtThermo::setBorderWidth(int w)
{
    if ((w >= 0) && (w < (qwtMin(d_data->thermoRect.width(), 
        d_data->thermoRect.height()) + d_data->borderWidth) / 2  - 1))
    {
        d_data->borderWidth = w;
        layoutThermo();
    }
}

//! Return the border width of the thermometer pipe.
int QwtThermo::borderWidth() const
{
    return d_data->borderWidth;
}

/*!
  \brief Set the range
  \param vmin value corresponding lower or left end of the thermometer
  \param vmax value corresponding to the upper or right end of the thermometer
  \param logarithmic logarithmic mapping, true or false 
*/
void QwtThermo::setRange(double vmin, double vmax, bool logarithmic)
{
    d_data->minValue = vmin;
    d_data->maxValue = vmax;

    if ( logarithmic )
        setScaleEngine(new QwtLog10ScaleEngine);
    else
        setScaleEngine(new QwtLinearScaleEngine);

    /*
      There are two different maps, one for the scale, the other
      for the values. This is confusing and will be changed
      in the future. TODO ...
     */

    d_data->map.setTransformation(scaleEngine()->transformation());
    d_data->map.setScaleInterval(d_data->minValue, d_data->maxValue);

    if (autoScale())
        rescale(d_data->minValue, d_data->maxValue);

    layoutThermo();
}

/*!
  \brief Change the brush of the liquid.
  \param brush New brush. The default brush is solid black.
*/
void QwtThermo::setFillBrush(const QBrush& brush)
{
    d_data->fillBrush = brush;
    update();
}

//! Return the liquid brush.
const QBrush& QwtThermo::fillBrush() const
{
    return d_data->fillBrush;
}

/*!
  \brief Change the color of the liquid.
  \param c New color. The default color is black.
*/
void QwtThermo::setFillColor(const QColor &c)
{
    d_data->fillBrush.setColor(c);
    update();
}

//! Return the liquid color.
const QColor &QwtThermo::fillColor() const
{
    return d_data->fillBrush.color();
}

/*!
  \brief Specify the liquid brush above the alarm threshold
  \param brush New brush. The default is solid white.
*/
void QwtThermo::setAlarmBrush(const QBrush& brush)
{
    d_data->alarmBrush = brush;
    update();
}

//! Return the liquid brush above the alarm threshold.
const QBrush& QwtThermo::alarmBrush() const
{
    return d_data->alarmBrush;
}

/*!
  \brief Specify the liquid color above the alarm threshold
  \param c New color. The default is white.
*/
void QwtThermo::setAlarmColor(const QColor &c)
{
    d_data->alarmBrush.setColor(c);
    update();
}

//! Return the liquid color above the alarm threshold.
const QColor &QwtThermo::alarmColor() const
{
    return d_data->alarmBrush.color();
}

//! Specify the alarm threshold.
void QwtThermo::setAlarmLevel(double v)
{
    d_data->alarmLevel = v;
    d_data->alarmEnabled = 1;
    update();
}

//! Return the alarm threshold.
double QwtThermo::alarmLevel() const
{
    return d_data->alarmLevel;
}

//! Change the width of the pipe.
void QwtThermo::setPipeWidth(int w)
{
    if (w > 0)
    {
        d_data->thermoWidth = w;
        layoutThermo();
    }
}

//! Return the width of the pipe.
int QwtThermo::pipeWidth() const
{
    return d_data->thermoWidth;
}


/*!
  \brief Specify the distance between the pipe's endpoints
         and the widget's border

  The margin is used to leave some space for the scale
  labels. If a large font is used, it is advisable to
  adjust the margins.
  \param m New Margin. The default values are 10 for
           horizontal orientation and 20 for vertical
           orientation.
  \warning The margin has no effect if the scale is disabled.
  \warning This function is a NOOP because margins are determined
           automatically.
*/
void QwtThermo::setMargin(int)
{
}


/*!
  \brief Enable or disable the alarm threshold
  \param tf true (disabled) or false (enabled)
*/
void QwtThermo::setAlarmEnabled(bool tf)
{
    d_data->alarmEnabled = tf;
    update();
}

//! Return if the alarm threshold is enabled or disabled.
bool QwtThermo::alarmEnabled() const
{
    return d_data->alarmEnabled;
}

/*!
  \return the minimum size hint
  \sa QwtThermo::minimumSizeHint
*/
QSize QwtThermo::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
  \warning The return value depends on the font and the scale.
  \sa QwtThermo::sizeHint
*/
QSize QwtThermo::minimumSizeHint() const
{
    int w = 0, h = 0;

    if ( d_data->scalePos != NoScale )
    {
        const int sdExtent = scaleDraw()->extent( QPen(), font() );
        const int sdLength = scaleDraw()->minLength( QPen(), font() );

        w = sdLength;
        h = d_data->thermoWidth + sdExtent + 
            d_data->borderWidth + d_data->scaleDist;

    }
    else // no scale
    {
        w = 200;
        h = d_data->thermoWidth;
    }

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

    w += 2 * d_data->borderWidth;
    h += 2 * d_data->borderWidth;

    return QSize( w, h );
}

int QwtThermo::transform(double value) const
{
    const double min = qwtMin(d_data->map.s1(), d_data->map.s2());
    const double max = qwtMax(d_data->map.s1(), d_data->map.s2());

    if ( value > max )
        value = max;
    if ( value < min )
        value = min;

    return d_data->map.transform(value);
}

⌨️ 快捷键说明

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