C++ 绘制画布标尺

9 篇文章 0 订阅
3 篇文章 0 订阅
目标

关键代码

CRulerDrawer::CRulerDrawer(QPainter& painter, QRect rect, int scalePercent)
    : m_painter(painter), m_rect(rect), m_scalePercent(scalePercent)
{
    m_palette = qApp->palette();
    m_scaleUnitSize = PixelRuler::Instance()->GetScaleUnitSize();
}

void CRulerDrawer::Draw(ScaleDirection direction, bool drawText)
{
    switch (direction)
    {
    case CRulerDrawer::Top:
        DrawHorizontalScale(true, drawText);
        break;
    case CRulerDrawer::Bottom:
        DrawHorizontalScale(false, drawText);
        break;
    case CRulerDrawer::Left:
        DrawVerticalScale(true, drawText);
        break;
    case CRulerDrawer::Right:
        DrawVerticalScale(false, drawText);
        break;
    }
}

void CRulerDrawer::DrawHorizontalScale(int top, bool drawText)
{
    m_painter.save();
    //绘制水平标尺
    int x_pos{ 0 };		//水平刻度
    m_painter.setPen(QPen(m_palette.color(QPalette::Text)));
    //绘制刻度

    int y_pos_start;
    if (top)
        y_pos_start = 0;
    else
        y_pos_start = m_rect.height();

    while (DPI(x_pos) <= m_rect.width() - 2 * SCALE_MARGIN)
    {
        int line_length;		//刻度的长度
        if (x_pos % (m_scaleUnitSize * m_scaleUnitSize) == 0)		//刻度值为100的倍数时绘制刻度的长度为10像素
            line_length = DPI(10);
        else if (x_pos % (m_scaleUnitSize * m_scaleUnitSize / 2) == 0)	//刻度值为50的倍数时绘制刻度的长度为8像素
            line_length = DPI(8);
        else
            line_length = DPI(5);
        int y_pos_end;
        if (top)
            y_pos_end = y_pos_start + line_length;
        else
            y_pos_end = y_pos_start - line_length;
        m_painter.drawLine(QPoint(DPI(x_pos) + SCALE_MARGIN - 1, y_pos_start), QPoint(DPI(x_pos) + SCALE_MARGIN - 1, y_pos_end));
        //画刻度上的文本
        if (drawText && x_pos % (m_scaleUnitSize * m_scaleUnitSize) == 0)		//当刻度值为100的倍数时显示刻度值
        {
            QString str = QString::number(x_pos);
            QSize str_size = m_painter.fontMetrics().size(Qt::TextSingleLine, str);
            int y_pos_text;
            if (top)
                y_pos_text = DPI(12);
            else
                y_pos_text = y_pos_start - DPI(12) - str_size.height();
            QRect rect{ QPoint{ DPI(x_pos) + SCALE_MARGIN - str_size.width() / 2, y_pos_text }, str_size };
            m_painter.drawText(rect, str);
        }
        x_pos += m_scaleUnitSize;
    }
    m_painter.restore();
}

void CRulerDrawer::DrawVerticalScale(int left, bool drawText)
{
    m_painter.save();
    //绘制垂直标尺
    int y_pos{ 0 };		//垂直刻度
    m_painter.setPen(QPen(m_palette.color(QPalette::Text)));
    //绘制刻度

    int x_pos_start;
    if (left)
        x_pos_start = 0;
    else
        x_pos_start = m_rect.width();

    while (DPI(y_pos) <= m_rect.height() - 2 * SCALE_MARGIN)
    {
        int line_length;		//刻度的长度
        if (y_pos % (m_scaleUnitSize * m_scaleUnitSize) == 0)		//刻度值为100的倍数时绘制刻度的长度为10像素
            line_length = DPI(10);
        else if (y_pos % (m_scaleUnitSize * m_scaleUnitSize / 2) == 0)	//刻度值为50的倍数时绘制刻度的长度为8像素
            line_length = DPI(8);
        else
            line_length = DPI(5);
        int x_pos_end;
        if (left)
            x_pos_end = x_pos_start + line_length;
        else
            x_pos_end = x_pos_start - line_length;
        m_painter.drawLine(QPoint(x_pos_start, DPI(y_pos) + SCALE_MARGIN - 1), QPoint(x_pos_end, DPI(y_pos) + SCALE_MARGIN - 1));
        //画刻度上的文本
        if (drawText && y_pos % (m_scaleUnitSize * m_scaleUnitSize) == 0)		//当刻度值为100的倍数时显示刻度值
        {
            QString str = QString::number(y_pos);
            QSize str_size = m_painter.fontMetrics().size(Qt::TextSingleLine, str);
            int x_pos_text;
            if (left)
                x_pos_text = DPI(12);
            else
                x_pos_text = x_pos_start - DPI(12) - str_size.width();
            QRect rect{ QPoint{ x_pos_text, DPI(y_pos) + SCALE_MARGIN - str_size.height() / 2 }, str_size };
            m_painter.drawText(rect, str);
        }
        y_pos += m_scaleUnitSize;
    }
    m_painter.restore();
}

int CRulerDrawer::DPI(int pixel)
{
    return pixel * m_scalePercent / 100;
}
效果


创作不易,小小的支持一下吧!

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值