| 166 | } |
| 167 | |
| 168 | void ElaMessageBar::paintEvent(QPaintEvent* event) |
| 169 | { |
| 170 | Q_D(ElaMessageBar); |
| 171 | QPainter painter(this); |
| 172 | painter.setOpacity(d->_pOpacity); |
| 173 | painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing); |
| 174 | // 高性能阴影 |
| 175 | eTheme->drawEffectShadow(&painter, rect(), d->_shadowBorderWidth, d->_borderRadius); |
| 176 | |
| 177 | // 背景和图标绘制 |
| 178 | painter.save(); |
| 179 | painter.setPen(d->_themeMode == ElaThemeType::Light ? QColor(0xBE, 0xBA, 0xBE) : QColor(0x52, 0x50, 0x52)); |
| 180 | switch (d->_messageMode) |
| 181 | { |
| 182 | case ElaMessageBarType::Success: |
| 183 | { |
| 184 | d->_drawSuccess(&painter); |
| 185 | break; |
| 186 | } |
| 187 | case ElaMessageBarType::Warning: |
| 188 | { |
| 189 | d->_drawWarning(&painter); |
| 190 | break; |
| 191 | } |
| 192 | case ElaMessageBarType::Information: |
| 193 | { |
| 194 | d->_drawInformation(&painter); |
| 195 | break; |
| 196 | } |
| 197 | case ElaMessageBarType::Error: |
| 198 | { |
| 199 | d->_drawError(&painter); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | // 文字绘制 |
| 204 | // 标题 |
| 205 | QFont font = this->font(); |
| 206 | font.setWeight(QFont::Bold); |
| 207 | font.setPixelSize(16); |
| 208 | painter.setFont(font); |
| 209 | int titleTextWidth = painter.fontMetrics().horizontalAdvance(d->_title) + 1; |
| 210 | if (titleTextWidth > 100) |
| 211 | { |
| 212 | titleTextWidth = 100; |
| 213 | } |
| 214 | int textFlags = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap | Qt::TextWrapAnywhere; |
| 215 | painter.drawText(QRect(d->_leftPadding + d->_titleLeftSpacing, -1, titleTextWidth, height()), textFlags, d->_title); |
| 216 | // 正文 |
| 217 | font.setWeight(QFont::Light); |
| 218 | font.setPixelSize(15); |
| 219 | painter.setFont(font); |
| 220 | painter.drawText(QRect(d->_leftPadding + d->_titleLeftSpacing + titleTextWidth + d->_textLeftSpacing, 0, width() - (d->_leftPadding + d->_titleLeftSpacing + titleTextWidth + d->_textLeftSpacing + d->_closeButtonWidth + d->_closeButtonLeftRightMargin / 2), height() - d->_timePercentHeight), textFlags, d->_text); |
| 221 | int textHeight = painter.fontMetrics().boundingRect(QRect(d->_leftPadding + d->_titleLeftSpacing + titleTextWidth + d->_textLeftSpacing, 0, width() - (d->_leftPadding + d->_titleLeftSpacing + titleTextWidth + d->_textLeftSpacing + d->_closeButtonWidth + d->_closeButtonLeftRightMargin), height()), textFlags, d->_text).height(); |
| 222 | if (textHeight >= minimumHeight() - 20) |
| 223 | { |
| 224 | setMinimumHeight(textHeight + 20); |
| 225 | } |
nothing calls this directly
no test coverage detected