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

📄 qwt_scale_widget.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
        r.setTop(r.top() + d_data->borderDist[0]);
        r.setHeight(r.height() - d_data->borderDist[1]);
    }

    if ( !d_data->title.isEmpty() )
    {
        QRect tr = r;
        switch(d_data->scaleDraw->alignment())
        {
            case QwtScaleDraw::LeftScale:
                tr.setRight( r.right() - d_data->titleOffset );
                break;

            case QwtScaleDraw::RightScale:
                tr.setLeft( r.left() + d_data->titleOffset );
                break;

            case QwtScaleDraw::BottomScale:
                tr.setTop( r.top() + d_data->titleOffset );
                break;

            case QwtScaleDraw::TopScale:
            default:
                tr.setBottom( r.bottom() - d_data->titleOffset );
                break;
        }

        drawTitle(painter, d_data->scaleDraw->alignment(), tr);
    }
}

QRect QwtScaleWidget::colorBarRect(const QRect& rect) const
{
    QRect cr = rect;

    if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
    {
        cr.setLeft(cr.left() + d_data->borderDist[0]);
        cr.setWidth(cr.width() - d_data->borderDist[1]);
    }
    else
    {
        cr.setTop(cr.top() + d_data->borderDist[0]);
        cr.setHeight(cr.height() - d_data->borderDist[1]);
    }

    switch(d_data->scaleDraw->alignment())
    {
        case QwtScaleDraw::LeftScale:
        {
            cr.setLeft( cr.right() - d_data->spacing 
                - d_data->colorBar.width + 1 );
            cr.setWidth(d_data->colorBar.width);
            break;
        }

        case QwtScaleDraw::RightScale:
        {
            cr.setLeft( cr.left() + d_data->spacing );
            cr.setWidth(d_data->colorBar.width);
            break;
        }

        case QwtScaleDraw::BottomScale:
        {
            cr.setTop( cr.top() + d_data->spacing );
            cr.setHeight(d_data->colorBar.width);
            break;
        }

        case QwtScaleDraw::TopScale:
        {
            cr.setTop( cr.bottom() - d_data->spacing
                - d_data->colorBar.width + 1 );
            cr.setHeight(d_data->colorBar.width);
            break;
        }
    }

    return cr;
}

/*!
  \brief resizeEvent
*/
void QwtScaleWidget::resizeEvent(QResizeEvent *)
{
    layoutScale(false);
}

//! Recalculate the scale'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 QwtScaleWidget::layoutScale( bool update_geometry )
{
    int bd0, bd1;
    getBorderDistHint(bd0, bd1);
    if ( d_data->borderDist[0] > bd0 )
        bd0 = d_data->borderDist[0];
    if ( d_data->borderDist[1] > bd1 )
        bd1 = d_data->borderDist[1];

    int colorBarWidth = 0;
    if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
        colorBarWidth = d_data->colorBar.width + d_data->spacing;

    const QRect r = rect();
    int x, y, length;

    if ( d_data->scaleDraw->orientation() == Qt::Vertical )
    {
        y = r.top() + bd0;
        length = r.height() - (bd0 + bd1);

        if ( d_data->scaleDraw->alignment() == QwtScaleDraw::LeftScale )
            x = r.right() - d_data->margin - colorBarWidth;
        else
            x = r.left() + d_data->margin + colorBarWidth;
    }
    else
    {
        x = r.left() + bd0; 
        length = r.width() - (bd0 + bd1);

        if ( d_data->scaleDraw->alignment() == QwtScaleDraw::BottomScale )
            y = r.top() + d_data->margin + colorBarWidth;
        else
            y = r.bottom() - d_data->margin - colorBarWidth;
    }

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

    d_data->titleOffset = d_data->margin + d_data->spacing +
        colorBarWidth +
        d_data->scaleDraw->extent(QPen(Qt::black, d_data->penWidth), font());

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

void QwtScaleWidget::drawColorBar(QPainter *painter, const QRect& rect) const
{
    if ( !d_data->colorBar.interval.isValid() )
        return;

    const QwtScaleDraw* sd = d_data->scaleDraw;

    QwtPainter::drawColorBar(painter, *d_data->colorBar.colorMap, 
        d_data->colorBar.interval.normalized(), sd->map(), 
        sd->orientation(), rect);
}

/*!
  Rotate and paint a title according to its position into a given rectangle.
  \param painter Painter
  \param align Alignment
  \param rect Bounding rectangle
*/

void QwtScaleWidget::drawTitle(QPainter *painter,
    QwtScaleDraw::Alignment align, const QRect &rect) const
{
    QRect r;
    double angle;
    int flags = d_data->title.renderFlags() & 
        ~(Qt::AlignTop | Qt::AlignBottom | Qt::AlignVCenter);

    switch(align)
    {
        case QwtScaleDraw::LeftScale:
            flags |= Qt::AlignTop;
            angle = -90.0;
            r.setRect(rect.left(), rect.bottom(), rect.height(), rect.width());
            break;
        case QwtScaleDraw::RightScale:
            flags |= Qt::AlignTop;
            angle = 90.0;
            r.setRect(rect.right(), rect.top(), rect.height(), rect.width());
            break;
        case QwtScaleDraw::TopScale:
            flags |= Qt::AlignTop;
            angle = 0.0;
            r = rect;
            break;
        case QwtScaleDraw::BottomScale:
        default:
            flags |= Qt::AlignBottom;
            angle = 0.0;
            r = rect;
            break;
    }

    painter->save();
    painter->setFont(font());
#if QT_VERSION < 0x040000
    painter->setPen(colorGroup().color(QColorGroup::Text));
#else
    painter->setPen(palette().color(QPalette::Text));
#endif

    painter->translate(r.x(), r.y());
    if (angle != 0.0)
        painter->rotate(angle);

    QwtText title = d_data->title;
    title.setRenderFlags(flags);
    title.draw(painter, QRect(0, 0, r.width(), r.height()));

    painter->restore();
}

/*!
  \brief Notify a change of the scale

  This virtual function can be overloaded by derived
  classes. The default implementation updates the geometry
  and repaints the widget.
*/

void QwtScaleWidget::scaleChange()
{
    layoutScale();
}

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

/*!
  \return a minimum size hint
*/
QSize QwtScaleWidget::minimumSizeHint() const
{
    const Qt::Orientation o = d_data->scaleDraw->orientation();

    // Border Distance cannot be less than the scale borderDistHint
    // Note, the borderDistHint is already included in minHeight/minWidth
    int length = 0;
    int mbd1, mbd2;
    getBorderDistHint(mbd1, mbd2);
    length += qwtMax( 0, d_data->borderDist[0] - mbd1 );
    length += qwtMax( 0, d_data->borderDist[1] - mbd2 );
    length += d_data->scaleDraw->minLength(
        QPen(Qt::black, d_data->penWidth), font());

    int dim = dimForLength(length, font());
    if ( length < dim )
    {
        // compensate for long titles
        length = dim;
        dim = dimForLength(length, font());
    }

    QSize size(length + 2, dim);
    if ( o == Qt::Vertical )
        size.transpose();

    return size;
}

/*!
  \brief Find the height of the title for a given width.
  \param width Width
  \return height Height
 */

int QwtScaleWidget::titleHeightForWidth(int width) const
{
    return d_data->title.heightForWidth(width, font());
}

/*!
  \brief Find the minimum dimension for a given length.
         dim is the height, length the width seen in
         direction of the title.
  \param length width for horizontal, height for vertical scales
  \param scaleFont Font of the scale
  \return height for horizontal, width for vertical scales
*/

int QwtScaleWidget::dimForLength(int length, const QFont &scaleFont) const
{
    int dim = d_data->margin;
    dim += d_data->scaleDraw->extent(
        QPen(Qt::black, d_data->penWidth), scaleFont);

    if ( !d_data->title.isEmpty() )
        dim += titleHeightForWidth(length) + d_data->spacing;

    if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
        dim += d_data->colorBar.width + d_data->spacing;

    return dim;
}

/*!
  \brief Calculate a hint for the border distances.

  This member function calculates the distance
  of the scale's endpoints from the widget borders which
  is required for the mark labels to fit into the widget.
  The maximum of this distance an the minimum border distance
  is returned.

  \warning
  <ul> <li>The minimum border distance depends on the font.</ul>
  \sa setMinBorderDist(), getMinBorderDist(), setBorderDist()
*/
void QwtScaleWidget::getBorderDistHint(int &start, int &end) const
{
    d_data->scaleDraw->getBorderDistHint(font(), start, end);

    if ( start < d_data->minBorderDist[0] )
        start = d_data->minBorderDist[0];

    if ( end < d_data->minBorderDist[1] )
        end = d_data->minBorderDist[1];
}

/*!
  Set a minimum value for the distances of the scale's endpoints from 
  the widget borders. This is useful to avoid that the scales
  are "jumping", when the tick labels or their positions change 
  often.

  \sa getMinBorderDist(), getBorderDistHint()
*/
void QwtScaleWidget::setMinBorderDist(int start, int end)
{
    d_data->minBorderDist[0] = start;
    d_data->minBorderDist[1] = end;
}

/*!
  Get the minimum value for the distances of the scale's endpoints from 
  the widget borders.

  \sa setMinBorderDist(), getBorderDistHint()
*/
void QwtScaleWidget::getMinBorderDist(int &start, int &end) const
{
    start = d_data->minBorderDist[0];
    end = d_data->minBorderDist[1];
}

#if QT_VERSION < 0x040000

/*!
  \brief Notify a change of the font

  This virtual function may be overloaded by derived widgets.
  The default implementation resizes the scale and repaints
  the widget.
  \param oldFont Previous font
*/
void QwtScaleWidget::fontChange(const QFont &oldFont)
{
    QWidget::fontChange( oldFont );
    layoutScale();
}

#endif

/*!
  \brief Assign a scale division

  The scale division determines where to set the tick marks.

  \param transformation Transformation, needed to translate between
                        scale and pixal values
  \param scaleDiv Scale Division
  \sa For more information about scale divisions, see QwtScaleDiv.
*/
void QwtScaleWidget::setScaleDiv(
    QwtScaleTransformation *transformation,
    const QwtScaleDiv &scaleDiv)
{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if (sd->scaleDiv() != scaleDiv ||
        sd->map().transformation()->type() != transformation->type() )
    {
        sd->setTransformation(transformation);
        sd->setScaleDiv(scaleDiv);
        layoutScale();

        emit scaleDivChanged();
    }
    else
        delete transformation;
}

void QwtScaleWidget::setColorBarEnabled(bool on)
{
    if ( on != d_data->colorBar.isEnabled )
    {
        d_data->colorBar.isEnabled = on;
        layoutScale();
    }
}

bool QwtScaleWidget::isColorBarEnabled() const
{
    return d_data->colorBar.isEnabled;
}


void QwtScaleWidget::setColorBarWidth(int width)
{
    if ( width != d_data->colorBar.width )
    {
        d_data->colorBar.width = width;
        if ( isColorBarEnabled() )
            layoutScale();
    }
}

int QwtScaleWidget::colorBarWidth() const
{
    return d_data->colorBar.width;
}

QwtDoubleInterval QwtScaleWidget::colorBarInterval() const
{
    return d_data->colorBar.interval;
}

void QwtScaleWidget::setColorMap(const QwtDoubleInterval &interval,
    const QwtColorMap &colorMap)
{
    d_data->colorBar.interval = interval;

    delete d_data->colorBar.colorMap;
    d_data->colorBar.colorMap = colorMap.copy();

    if ( isColorBarEnabled() )
        layoutScale();
}

const QwtColorMap &QwtScaleWidget::colorMap() const
{
    return *d_data->colorBar.colorMap;
}

⌨️ 快捷键说明

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