#include "alertwidget.h" #include "imageeffect.h" #include "paintershadowtext.h" #include #include #include AlertWidget::AlertWidget(QWidget *parent) : QWidget(parent) { setObjectName("AlertWidget"); lbPixmap = new QLabel(this); setFont(QFont("Gulim", 24, QFont::Bold)); setMinimumSize(1, 1); setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); setAttribute(Qt::WA_TranslucentBackground, true); _radius = 0; _timerAlpha = false; _currentAlpha = 0; _duration = 333; _delay = 8000; } AlertWidget::~AlertWidget() { qDebug() << "destroyed: " << objectName(); } void AlertWidget::setRadius(qreal radius) { _radius = radius; updateSize(); updatePixmap(); } void AlertWidget::setMessage(const QString &message) { _message = message.trimmed(); _timerAlpha = false; _currentAlpha = 255; _timer.stop(); _timer.start(_delay, this); updateSize(); updatePixmap(); setWindowOpacity(1.0); } void AlertWidget::setColor(QColor &text, QColor &shadow) { _text = text; _shadow = shadow; updatePixmap(); } void AlertWidget::updateSize() { QRect fontRect = fontMetrics().boundingRect(QRect(), 0, _message); QRect available = QApplication::desktop()->availableGeometry(pos()); setGeometry( available.width() / 2 - fontRect.width() / 2, available.height() / 2 - fontRect.height(), fontRect.width() + _radius * 2, fontRect.height() + _radius * 2); } void AlertWidget::updatePixmap() { if(_currentAlpha > 0) { QImage image(size(), QImage::Format_ARGB32_Premultiplied); PainterShadowText shadow; shadow.setFont(font()); shadow.setRadius(_radius); shadow.setTextColor(_text); shadow.setShadowColor(_shadow); shadow.drawTextPen(0, 0, image, _message); lbPixmap->setPixmap(QPixmap::fromImage(image)); lbPixmap->adjustSize(); } else { QPixmap pixmap(size()); pixmap.fill(QColor(0, 0, 0, 0)); lbPixmap->setPixmap(pixmap); } } /** * ÆäÀÌµå ¿ªÈ°À» ´ã´çÇÏ´Â ¸Þ¼Òµå. * 33ms¸¶´Ù opacity¸¦ Á¶ÀýÇÑ´Ù. * @param event timerEventÀÇ ±âº» º¯¼ö. */ void AlertWidget::timerEvent(QTimerEvent *event) { Q_UNUSED(event); if(_timerAlpha) { _currentAlpha -= 255 * 33 / _duration; if(_currentAlpha <= 0) { setWindowOpacity(0.0f); _timer.stop(); _timerAlpha = false; _currentAlpha = 0; updatePixmap(); } else { setWindowOpacity(_currentAlpha / 255.f); } } else { _timer.stop(); _timer.start(33, this); _timerAlpha = true; } }