/** * @file ModelProgressGraphicsItem.cpp * @brief 日志中进度条 */ #include "ModelProgressGraphicsItem.h" #include #include #include #include #include // 设置显示条宽度 int const BARWIDTH = 14; using namespace pai::gui; ModelProgressGraphicsItem::ModelProgressGraphicsItem(int barType, int stepID,QGraphicsItem * pParent) :QGraphicsItem(pParent) { m_width = 60; m_height = 20; m_BarType = barType; m_StepID = stepID; } ModelProgressGraphicsItem::~ModelProgressGraphicsItem() { } int ModelProgressGraphicsItem::type() const { return Type; } void ModelProgressGraphicsItem::SetBarColor(const QColor& beforeColor, const QColor& afterColor, const QColor& fontColor) { m_BeforeColor = beforeColor; m_AfterColor = afterColor; m_FontColor = fontColor; } void ModelProgressGraphicsItem::SetBarText(const QString& barText, const QString& veryText, const QString& topText) { m_BarText = barText; m_VeryText = veryText; m_TopText = topText; } QRectF ModelProgressGraphicsItem::boundingRect () const { qreal dPenWidth = 2; qreal topLeftX = -(m_width + dPenWidth)/2; qreal topLeftY = -(m_height + dPenWidth)/2; return QRectF(topLeftX, topLeftY, m_width + dPenWidth, m_height + dPenWidth); } void ModelProgressGraphicsItem::UpdatePosition() { } int ModelProgressGraphicsItem::GetBarType() const { return m_BarType; } int ModelProgressGraphicsItem::GetStepID() const { return m_StepID; } void ModelProgressGraphicsItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem */*pOption*/, QWidget */*pWidget*/) { QPen pen; //设置显示条宽度 pen.setWidth(BARWIDTH); pen.setCapStyle(Qt::RoundCap); qreal topLeftX = -(m_width + pen.width()) / 2; qreal topLeftY = -(m_height + pen.width()) / 2; //设置字符所占像素宽度 QLabel label; int nLenght = label.fontMetrics().width(m_BarText); //设置渐变效果 QLinearGradient linegradient(topLeftX,topLeftY,topLeftX + nLenght,topLeftY); linegradient.setColorAt(0.3,m_BeforeColor); linegradient.setColorAt(1.0,m_AfterColor); pen.setBrush(linegradient); pPainter->setPen(pen); pPainter->drawLine(topLeftX, topLeftY,topLeftX + nLenght,topLeftY); pen.setCapStyle(Qt::FlatCap); pen.setColor(m_BeforeColor); pPainter->setPen(pen); pPainter->drawLine(topLeftX-10, topLeftY,topLeftX - 1,topLeftY); //绘制显示条文字 pen.setColor(m_FontColor); pPainter->setPen(pen); pPainter->drawText(topLeftX - 7, topLeftY + 4,m_BarText); pPainter->drawText(topLeftX + nLenght + 10,topLeftY + 4,m_VeryText); pPainter->drawText(topLeftX - 10, topLeftY -12,m_TopText); }