#include "weatherwidget.h" #include "mabitimer.h" #include "weather.h" #include "paintershadowtext.h" #include #include #include /** * À§Á¬À» »ý¼ºÇÏ°í ÃʱâÈ­. * @param parent ºÎ¸ð À§Á¬. NULL·Î »ý¼º ÇÏ¸é µÈ´Ù. */ WeatherWidget::WeatherWidget(QWidget *parent) : QWidget(parent) { setObjectName("WeatherWidget"); glMainLayout = new QGridLayout; _amountFont = QFont("Arial", 8); _amountFont.setStyleStrategy(QFont::PreferQuality); _amountFM = new QFontMetrics(_amountFont); _clear = new QImage(":/weather32/clearA.png"); _clearNight = new QImage(":/weather32/clearP.png"); _cloud = new QImage(":/weather32/cloudsA.png"); _cloudNight = new QImage(":/weather32/cloudsP.png"); _rain = new QImage(":/weather32/rain.png"); _storm = new QImage(":/weather32/storm.png"); pWeatherAlignment = Qt::AlignRight; setFont(QFont("Gulim", 9)); _areaNames.append(QString::fromWCharArray(L"Ƽ¸£ ÄÚ³×ÀÏ, µÎ°¥µå ¾ÆÀÏ")); _areaNames.append(QString::fromWCharArray(L"´ø¹ÙÆ°, °¡ÀÌ·¹Èå")); _areaNames.append(QString::fromWCharArray(L"¹ÝÈ£¸£")); _areaNames.append(QString::fromWCharArray(L"ÀÌ¸à ¸¶ÇÏ")); _areaNames.append(QString::fromWCharArray(L"¼¾¸¶ÀÌ Æò¿ø")); _areaNames.append(QString::fromWCharArray(L"ÄÉ¾È Ç×±¸")); _areaNames.append(QString::fromWCharArray(L"¶ó³ë Áö¿ª")); _areaNames.append(QString::fromWCharArray(L"ÄÜ´©½º Áö¿ª")); _areaNames.append(QString::fromWCharArray(L"Äí¸£Å¬·¹ Áö¿ª")); _areaNames.append(QString::fromWCharArray(L"ÀÚ¸£µò Áö¿ª")); _areaNames.append(QString::fromWCharArray(L"±×¸²ÀÚ ¼¼°è")); _areaNames.append(QString::fromWCharArray(L"Żƾ, Ÿ¶ó")); QFontMetrics fm(font()); PainterShadowText drawPainter; drawPainter.setFont(font()); drawPainter.setTextColor(QColor(Qt::white)); drawPainter.setShadowColor(QColor(Qt::black)); for(int i = 0; i < _areaNames.size(); i++) { QLabel *nameLabel; QLabel *imageLabel; imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setScaledContents(true); imageLabel->setPixmap(QPixmap::fromImage(*_clear)); imageLabel->adjustSize(); QRect rect = fm.boundingRect(_areaNames[i]); QImage image(rect.width() + 2, rect.height() + 2, QImage::Format_ARGB32_Premultiplied); drawPainter.drawTextBitmap(1, 1, image, _areaNames[i]); nameLabel = new QLabel; nameLabel->setBackgroundRole(QPalette::Base); nameLabel->setScaledContents(true); nameLabel->setPixmap(QPixmap::fromImage(image)); nameLabel->adjustSize(); _nameLabels.append(nameLabel); _imageLabels.append(imageLabel); _blendStatus.append(BlendStatus()); glMainLayout->addWidget(nameLabel, i, 0, 1, 1, Qt::AlignRight | Qt::AlignVCenter); glMainLayout->addWidget(imageLabel, i, 1, 1, 1, Qt::AlignRight | Qt::AlignVCenter); } setLayout(glMainLayout); setWindowOpacity(0.f); setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); setAttribute(Qt::WA_TranslucentBackground, true); QSize size = glMainLayout->sizeHint(); QRect available = QDesktopWidget().availableGeometry(); setGeometry(available.width() - size.width(), available.height() / 2 - size.height() / 2, size.width(), size.height()); tmBlendTimer.start(33, this); tmWeatherTimer.start(1500, this); } WeatherWidget::~WeatherWidget() { qDebug() << "destroyed: " << objectName(); delete _clear; delete _clearNight; delete _cloud; delete _cloudNight; delete _rain; delete _storm; delete _amountFM; } void WeatherWidget::setWeatherAlignment(Qt::Alignment weatherAlignment) { if(weatherAlignment != Qt::AlignRight) { weatherAlignment = Qt::AlignLeft; } if(pWeatherAlignment != weatherAlignment) { int nameCol = (weatherAlignment == Qt::AlignLeft ? 1 : 0); int imageCol = (weatherAlignment == Qt::AlignLeft ? 0 : 1); for(int i = 0; i < _nameLabels.size(); i++) { glMainLayout->removeWidget(_nameLabels[i]); glMainLayout->removeWidget(_imageLabels[i]); } for(int i = 0; i < _nameLabels.size(); i++) { glMainLayout->addWidget(_nameLabels[i], i, nameCol, 1, 1, weatherAlignment | Qt::AlignVCenter); glMainLayout->addWidget(_imageLabels[i], i, imageCol, 1, 1, weatherAlignment | Qt::AlignVCenter); } QSize size = glMainLayout->sizeHint(); QRect available = QDesktopWidget().availableGeometry(); setGeometry(weatherAlignment == Qt::AlignLeft ? 0 : available.width() - size.width(), available.height() / 2 - size.height() / 2, size.width(), size.height()); pWeatherAlignment = weatherAlignment; } } Qt::Alignment WeatherWidget::weatherAlignment() { return pWeatherAlignment; } void WeatherWidget::timerEvent(QTimerEvent *e) { if(tmBlendTimer.timerId() == e->timerId()) { qreal opacity = windowOpacity(); if(opacity != 1.f) { opacity += 0.1f; if(opacity > 1.f) { opacity = 1.f; } setWindowOpacity(opacity); } for(int i = 0; i < _blendStatus.size(); i++) { BlendStatus *status = &_blendStatus[i]; if(status->duration != 0) { status->duration -= 33; qreal alpha = status->duration / 333.f; QImage image(_clear->size(), QImage::Format_ARGB32_Premultiplied); image.fill(qRgba(0, 0, 0, 0)); QPainter p(&image); p.setOpacity(alpha); p.drawImage(0, 0, *imageFromWeather(status->background)); p.setOpacity(1.0 - alpha); p.drawImage(0, 0, *imageFromWeather(status->foreground)); if(status->foreground == Rain) { QPixmap pixmap; getAmountPixmap(status->amount, pixmap); p.drawPixmap(image.width() - pixmap.width(), image.height() - pixmap.height(), pixmap); } _imageLabels[i]->setPixmap(QPixmap::fromImage(image)); if(status->duration == 0) { status->background = status->foreground; } } } } else if(tmWeatherTimer.timerId() == e->timerId()) { QDateTime midnight; QDateTime realTime; QDateTime erinnTime; realTime = QDateTime::currentDateTime(); midnight.setDate(realTime.date()); erinnTime.setTime_t((realTime.toTime_t() - midnight.toTime_t()) % 2160); uint erinn = erinnTime.toTime_t() * 2 / 3; uint min = 6 * 60; uint max = 18 * 60 - 1; for(int i = 0; i < _imageLabels.size(); i++) { BlendStatus *status = &_blendStatus[i]; Weather *cweather = MabiTimer::weather(i + 1); if(status->duration != 0) { continue; } int amount = 0; float weather = cweather->weather(time(NULL)); switch(cweather->weatherType(weather)) { case Weather::Storm: if(status->foreground != Storm) { status->duration = 330; status->foreground = Storm; } break; case Weather::Rain: amount = (int)floorf(cweather->rainAmount(weather) * 20); if(status->foreground != Rain) { status->duration = 330; status->amount = amount; status->foreground = Rain; } if(status->amount != amount) { status->duration = 330; status->amount = amount; } break; case Weather::Cloud: if(erinn >= min && erinn <= max) { if(status->foreground != Cloud) { status->duration = 330; status->foreground = Cloud; } } else { if(status->foreground != CloudNight) { status->duration = 330; status->foreground = CloudNight; } } break; case Weather::Clear: if(erinn >= min && erinn <= max) { if(status->foreground != Clear) { status->duration = 330; status->foreground = Clear; } } else { if(status->foreground != ClearNight) { status->duration = 330; status->foreground = ClearNight; } } break; default: continue; } } } } QImage *WeatherWidget::imageFromWeather(WeatherImage type) { switch(type) { case Storm: return _storm; case Rain: return _rain; case CloudNight: return _cloudNight; case ClearNight: return _clearNight; case Cloud: return _cloud; default: return _clear; } } void WeatherWidget::getAmountPixmap(const int amount, QPixmap &pixmap) { QString key = QString::number(amount); if(!pcWeatherAmount.find(key, pixmap)) { QRect rect = _amountFM->boundingRect(key); QImage amount(rect.width() + 2, rect.height() + 2, QImage::Format_ARGB32_Premultiplied); PainterShadowText shadow; shadow.setFont(_amountFont); shadow.drawTextBitmap(1, 1, amount, key); pixmap = QPixmap::fromImage(amount); } }