From 58f95602cefb00bfc5ba56e8a1c6cd41fbb3365f Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Sat, 15 Feb 2014 19:41:07 +0100 Subject: [PATCH] cleaned up icon --- src/core/notification/icon.cpp | 18 ++++++++++++++---- src/core/notification/icon.h | 12 ++++++------ src/core/notification/icon_p.cpp | 23 +++-------------------- src/core/notification/icon_p.h | 12 ------------ src/plugins/backends/snarl/snarl.cpp | 5 +++-- 5 files changed, 26 insertions(+), 44 deletions(-) diff --git a/src/core/notification/icon.cpp b/src/core/notification/icon.cpp index e86008c..7e85825 100644 --- a/src/core/notification/icon.cpp +++ b/src/core/notification/icon.cpp @@ -25,6 +25,15 @@ using namespace Snore; +QByteArray Icon::dataFromImage(const QImage &image) +{ + QByteArray data; + QBuffer buffer( &data ); + buffer.open( QBuffer::WriteOnly ); + image.save( &buffer, "PNG" ); + return data; +} + Icon::Icon() : d(NULL) { @@ -65,10 +74,6 @@ QString Icon::localUrl()const return d->localUrl(); } -const QByteArray &Icon::imageData() const{ - return d->imageData(); -} - bool Icon::isLocalFile() const { return d->m_isLocalFile; @@ -79,6 +84,11 @@ bool Icon::isValid() const return d && !(d->m_img.isNull() && d->m_url.isEmpty()); } +Icon Icon::scaled(const QSize &s) const +{ + return Icon(image().scaled(s,Qt::KeepAspectRatio, Qt::SmoothTransformation)); +} + QString Icon::url() const { return d->m_url; diff --git a/src/core/notification/icon.h b/src/core/notification/icon.h index 6c59560..7724455 100644 --- a/src/core/notification/icon.h +++ b/src/core/notification/icon.h @@ -23,6 +23,7 @@ #include #include + namespace Snore{ class Icon; } @@ -41,6 +42,9 @@ class IconData; class SNORE_EXPORT Icon { public: + + static QByteArray dataFromImage(const QImage &image); + Icon(); /** @@ -87,12 +91,6 @@ public: */ QString url() const; - /** - * - * @return a QByteArray containing the data of a png representing the Icon - */ - const QByteArray &imageData() const ; - /** * * @return whether the Icon was created from a local file @@ -111,6 +109,8 @@ public: */ bool isValid() const; + Icon scaled(const QSize &s) const; + private: QExplicitlySharedDataPointer d; friend SNORE_EXPORT QDebug (::operator<<) ( QDebug, const Snore::Icon &); diff --git a/src/core/notification/icon_p.cpp b/src/core/notification/icon_p.cpp index f9e6c6e..30d0777 100644 --- a/src/core/notification/icon_p.cpp +++ b/src/core/notification/icon_p.cpp @@ -46,8 +46,7 @@ IconData::IconData(const QString &url): IconData::IconData(const QImage &img): m_img(img), - m_data(dataFromImage(img)), - m_hash(SnoreCorePrivate::computeHash(m_data)), + m_hash(SnoreCorePrivate::computeHash(Icon::dataFromImage(img))), m_localUrl(createLocalFileName(m_hash)), m_isLocalFile(false), m_isResource(false), @@ -60,20 +59,6 @@ IconData::~IconData() } - -const QByteArray &Snore::IconData::imageData() -{ - QMutexLocker lock(&m_mutex); - if(m_data.isEmpty()) - { - if(m_isRemoteFile) - { - download(); - } - } - return m_data; -} - const QImage &IconData::image() { QMutexLocker lock(&m_mutex); @@ -107,7 +92,7 @@ QString IconData::localUrl() void IconData::download() { - if(m_isRemoteFile && m_data.isEmpty()) + if(m_isRemoteFile) { if(!QFile(m_localUrl).exists()) { @@ -122,15 +107,13 @@ void IconData::download() loop.exec(); if(reply->isFinished()) { - m_data = reply->readAll(); - m_img = QImage::fromData(m_data, "PNG"); + m_img = QImage::fromData(reply->readAll(), "PNG"); m_img.save(m_localUrl,"PNG"); } } else { m_img = QImage(m_localUrl); - m_data = dataFromImage(m_img); } } } diff --git a/src/core/notification/icon_p.h b/src/core/notification/icon_p.h index c63d68e..a40f374 100644 --- a/src/core/notification/icon_p.h +++ b/src/core/notification/icon_p.h @@ -41,15 +41,12 @@ public: IconData(const QImage &img); ~IconData(); - - const QByteArray &imageData(); const QImage &image(); QString localUrl(); void download(); QImage m_img; - QByteArray m_data; QString m_url; QString m_hash; QString m_localUrl; @@ -61,15 +58,6 @@ public: private: Q_DISABLE_COPY(IconData) - inline QByteArray dataFromImage(const QImage &image) - { - QByteArray data; - QBuffer buffer( &data ); - buffer.open( QBuffer::WriteOnly ); - image.save( &buffer, "PNG" ); - return data; - } - inline QString createLocalFileName(const QString &hash) { static QString tmp; diff --git a/src/plugins/backends/snarl/snarl.cpp b/src/plugins/backends/snarl/snarl.cpp index 68f3832..09b99ed 100644 --- a/src/plugins/backends/snarl/snarl.cpp +++ b/src/plugins/backends/snarl/snarl.cpp @@ -222,6 +222,7 @@ void SnarlBackend::slotNotify(Notification notification){ ULONG32 id = 0; snoreDebug( SNORE_DEBUG ) << notification.icon(); + if(!notification.isUpdate()) { id = snarlInterface->Notify(notification.alert().name().toUtf8().constData(), @@ -229,7 +230,7 @@ void SnarlBackend::slotNotify(Notification notification){ Snore::toPlainText(notification.text()).toUtf8().constData(), notification.timeout(), notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0, - !notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0, + !notification.icon().isLocalFile()?Icon::dataFromImage(notification.icon().image()).toBase64().constData():0, priority); foreach(const Action &a, notification.actions()) @@ -249,7 +250,7 @@ void SnarlBackend::slotNotify(Notification notification){ Snore::toPlainText(notification.text()).toUtf8().constData(), notification.timeout(), notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0, - !notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0, + !notification.icon().isLocalFile()?Icon::dataFromImage(notification.icon().image()).toBase64().constData():0, priority); }