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

📄 qwt_scale_draw.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    switch(alignment())
    {
        case LeftScale:
        {
#if QT_VERSION < 0x040000
            QwtPainter::drawLine(painter, pos.x() + pw2, tval,
                pos.x() - len - 2 * pw2, tval);
#else
            QwtPainter::drawLine(painter, pos.x() - pw2, tval,
                pos.x() - len, tval);
#endif
            break;
        }

        case RightScale:
        {
#if QT_VERSION < 0x040000
            QwtPainter::drawLine(painter, pos.x(), tval,
                pos.x() + len + pw2, tval);
#else
            QwtPainter::drawLine(painter, pos.x() + pw2, tval,
                pos.x() + len, tval);
#endif
            break;
        }
    
        case BottomScale:
        {
#if QT_VERSION < 0x040000
            QwtPainter::drawLine(painter, tval, pos.y(),
                tval, pos.y() + len + 2 * pw2);
#else
            QwtPainter::drawLine(painter, tval, pos.y() + pw2,
                tval, pos.y() + len);
#endif
            break;
        }

        case TopScale:
        {
#if QT_VERSION < 0x040000
            QwtPainter::drawLine(painter, tval, pos.y() + pw2,
                tval, pos.y() - len - 2 * pw2);
#else
            QwtPainter::drawLine(painter, tval, pos.y() - pw2,
                tval, pos.y() - len);
#endif
            break;
        }
    }
    QwtPainter::setMetricsMap(metricsMap); // restore metrics map
}

/*! 
   Draws the baseline of the scale
   \param painter Painter

   \sa drawTick(), drawLabel()
*/
void QwtScaleDraw::drawBackbone(QPainter *painter) const
{
    const int bw2 = painter->pen().width() / 2;

    const QPoint &pos = d_data->pos;
    const int len = d_data->len - 1;

    switch(alignment())
    {
        case LeftScale:
            QwtPainter::drawLine(painter, pos.x() - bw2,
                pos.y(), pos.x() - bw2, pos.y() + len );
            break;
        case RightScale:
            QwtPainter::drawLine(painter, pos.x() + bw2,
                pos.y(), pos.x() + bw2, pos.y() + len);
            break;
        case TopScale:
            QwtPainter::drawLine(painter, pos.x(), pos.y() - bw2,
                pos.x() + len, pos.y() - bw2);
            break;
        case BottomScale:
            QwtPainter::drawLine(painter, pos.x(), pos.y() + bw2,
                pos.x() + len, pos.y() + bw2);
            break;
    }
}

/*!
  \brief Move the position of the scale

  The meaning of the parameter pos depends on the alignment:
  <dl>
  <dt>QwtScaleDraw::LeftScale
  <dd>The origin is the topmost point of the
      backbone. The backbone is a vertical line. 
      Scale marks and labels are drawn 
      at the left of the backbone.
  <dt>QwtScaleDraw::RightScale
  <dd>The origin is the topmost point of the
      backbone. The backbone is a vertical line. 
      Scale marks and labels are drawn
      at the right of the backbone.
  <dt>QwtScaleDraw::TopScale
  <dd>The origin is the leftmost point of the
      backbone. The backbone is a horizontal line. 
      Scale marks and labels are drawn
      above the backbone.
  <dt>QwtScaleDraw::BottomScale
  <dd>The origin is the leftmost point of the
      backbone. The backbone is a horizontal line 
      Scale marks and labels are drawn
      below the backbone.
  </dl>

  \param pos Origin of the scale

  \sa pos(), setLength()
*/
void QwtScaleDraw::move(const QPoint &pos)
{
    d_data->pos = pos;
    updateMap();
}

/*! 
   \return Origin of the scale
   \sa move(), length()
*/
QPoint QwtScaleDraw::pos() const
{
    return d_data->pos;
}

/*!
  Set the length of the backbone.
  
  The length doesn't include the space needed for
  overlapping labels.

  \sa move(), minLabelDist()
*/
void QwtScaleDraw::setLength(int length)
{
    d_data->len = qwtMax(length, 10);
    updateMap();
}

/*! 
   \return the length of the backbone
   \sa setLength(), pos()
*/
int QwtScaleDraw::length() const
{
    return d_data->len;
}

/*! 
   Draws the label for a major scale tick

   \param painter Painter
   \param value Value

   \sa drawTick, drawBackbone
*/
void QwtScaleDraw::drawLabel(QPainter *painter, double value) const
{
    QwtText lbl = tickLabel(painter->font(), value);
    if ( lbl.isEmpty() )
        return; 

    const QPoint pos = labelPosition(value);

    QSize labelSize = lbl.textSize(painter->font());
    if ( labelSize.height() % 2 )
        labelSize.setHeight(labelSize.height() + 1);
    
    const QwtMatrix m = labelMatrix( pos, labelSize);

    painter->save();
#if QT_VERSION < 0x040000
    painter->setWorldMatrix(m, true);
#else
    painter->setMatrix(m, true);
#endif

    lbl.draw (painter, QRect(QPoint(0, 0), labelSize) );
    painter->restore();
}

/*!
   Calculate the matrix that is needed to paint a label
   depending on its alignment and rotation.

   \param pos Position where to paint the label
   \param size Size of the label

   \sa setLabelAlignment(), setLabelRotation()
*/
QwtMatrix QwtScaleDraw::labelMatrix( 
    const QPoint &pos, const QSize &size) const
{   
    QwtMatrix m;
    m.translate(pos.x(), pos.y());
    m.rotate(labelRotation());
    
    int flags = labelAlignment();
    if ( flags == 0 )
    {
        switch(alignment())
        {
            case RightScale:
            {
                if ( flags == 0 )
                    flags = Qt::AlignRight | Qt::AlignVCenter;
                break;
            }
            case LeftScale:
            {
                if ( flags == 0 )
                    flags = Qt::AlignLeft | Qt::AlignVCenter;
                break;
            }
            case BottomScale:
            {
                if ( flags == 0 )
                    flags = Qt::AlignHCenter | Qt::AlignBottom;
                break;
            }
            case TopScale:
            {
                if ( flags == 0 )
                    flags = Qt::AlignHCenter | Qt::AlignTop;
                break;
            }
        }
    }

    const int w = size.width();
    const int h = size.height();

    int x, y;
    
    if ( flags & Qt::AlignLeft )
        x = -w;
    else if ( flags & Qt::AlignRight )
        x = -(w % 2); 
    else // Qt::AlignHCenter
        x = -(w / 2);
        
    if ( flags & Qt::AlignTop )
        y =  -h ;
    else if ( flags & Qt::AlignBottom )
        y = -(h % 2); 
    else // Qt::AlignVCenter
        y = -(h/2);
        
    m.translate(x, y);
    
    return m;
}   

/*!
  Find the bounding rect for the label. The coordinates of
  the rect are relative to spacing + ticklength from the backbone
  in direction of the tick.

  \param font Font used for painting
  \param value Value
*/
QRect QwtScaleDraw::labelRect(const QFont &font, double value) const
{   
    QwtText lbl = tickLabel(font, value);
    if ( lbl.isEmpty() )
        return QRect(0, 0, 0, 0);

    const QPoint pos = labelPosition(value);

    QSize labelSize = lbl.textSize(font);
    if ( labelSize.height() % 2 )
    {
        labelSize.setHeight(labelSize.height() + 1);
    }

    const QwtMatrix m = labelMatrix(pos, labelSize);

#if 0
    QRect br = QwtMetricsMap::translate(m, QRect(QPoint(0, 0), labelSize));
#else
    QwtPolygon pol(4);
    pol.setPoint(0, 0, 0); 
    pol.setPoint(1, 0, labelSize.height() - 1 );
    pol.setPoint(2, labelSize.width() - 1, 0);
    pol.setPoint(3, labelSize.width() - 1, labelSize.height() - 1 );

    pol = QwtMetricsMap::translate(m, pol);
    QRect br = pol.boundingRect();
#endif

#if QT_VERSION < 0x040000
    br.moveBy(-pos.x(), -pos.y());
#else
    br.translate(-pos.x(), -pos.y());
#endif

    return br;
}

/*!
   Calculate the size that is needed to draw a label

   \param font Label font
   \param value Value
*/
QSize QwtScaleDraw::labelSize(const QFont &font, double value) const
{
    return labelRect(font, value).size();
}

/*!
  Rotate all labels.

  When changing the rotation, it might be necessary to
  adjust the label flags too. Finding a useful combination is
  often the result of try and error.

  \param rotation Angle in degrees. When changing the label rotation,
                  the label flags often needs to be adjusted too.

  \sa setLabelAlignment(), labelRotation(), labelAlignment().

*/
void QwtScaleDraw::setLabelRotation(double rotation)
{
    d_data->labelRotation = rotation;
}

/*!
  \return the label rotation
  \sa setLabelRotation(), labelAlignment()
*/
double QwtScaleDraw::labelRotation() const
{
    return d_data->labelRotation;
}

/*!
  \brief Change the label flags

  Labels are aligned to the point ticklength + spacing away from the backbone.

  The alignment is relative to the orientation of the label text.
  In case of an flags of 0 the label will be aligned  
  depending on the orientation of the scale: 
  
      QwtScaleDraw::TopScale: Qt::AlignHCenter | Qt::AlignTop\n
      QwtScaleDraw::BottomScale: Qt::AlignHCenter | Qt::AlignBottom\n
      QwtScaleDraw::LeftScale: Qt::AlignLeft | Qt::AlignVCenter\n
      QwtScaleDraw::RightScale: Qt::AlignRight | Qt::AlignVCenter\n
  
  Changing the alignment is often necessary for rotated labels.
  
  \param alignment Or'd Qt::AlignmentFlags <see qnamespace.h>

  \sa setLabelRotation(), labelRotation(), labelAlignment()
  \warning The various alignments might be confusing. 
           The alignment of the label is not the alignment
           of the scale and is not the alignment of the flags
           (QwtText::flags()) returned from QwtAbstractScaleDraw::label().
*/    
      
#if QT_VERSION < 0x040000
void QwtScaleDraw::setLabelAlignment(int alignment)
#else
void QwtScaleDraw::setLabelAlignment(Qt::Alignment alignment)
#endif
{
    d_data->labelAlignment = alignment;
}   

/*!
  \return the label flags
  \sa setLabelAlignment(), labelRotation()
*/
#if QT_VERSION < 0x040000
int QwtScaleDraw::labelAlignment() const
#else
Qt::Alignment QwtScaleDraw::labelAlignment() const
#endif
{
    return d_data->labelAlignment;
}

/*!
  \param font Font
  \return the maximum width of a label
*/
int QwtScaleDraw::maxLabelWidth(const QFont &font) const
{
    int maxWidth = 0;

    const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
    for (uint i = 0; i < (uint)ticks.count(); i++)
    {
        const double v = ticks[i];
        if ( scaleDiv().contains(v) )
        {
            const int w = labelSize(font, ticks[i]).width();
            if ( w > maxWidth )
                maxWidth = w;
        }
    }

    return maxWidth;
}

/*!
  \param font Font
  \return the maximum height of a label
*/
int QwtScaleDraw::maxLabelHeight(const QFont &font) const
{
    int maxHeight = 0;
    
    const QwtValueList &ticks = scaleDiv().ticks(QwtScaleDiv::MajorTick);
    for (uint i = 0; i < (uint)ticks.count(); i++)
    {
        const double v = ticks[i];
        if ( scaleDiv().contains(v) )
        {
            const int h = labelSize(font, ticks[i]).height();
            if ( h > maxHeight )
                maxHeight = h; 
        }       
    }   
    
    return maxHeight;
}   

void QwtScaleDraw::updateMap()
{
    QwtScaleMap &sm = scaleMap();
    if ( orientation() == Qt::Vertical )
        sm.setPaintInterval(d_data->pos.y() + d_data->len - 1, d_data->pos.y());
    else
        sm.setPaintInterval(d_data->pos.x(), d_data->pos.x() + d_data->len - 1);
}

⌨️ 快捷键说明

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