From 24fd7a0f5b72f8d90b519ac5753962094ae4c533 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 26 Jul 2023 02:50:27 -0400 Subject: [PATCH] Use prettier notification images Avatars are shown rounded in the main interface, so they should look the same in the notification tray too. On top of that, if the room is a group then show that group's icon when applicable in the bottom right. --- src/notificationsmanager.cpp | 34 ++++++++++++++++++++++++++++++++-- src/notificationsmanager.h | 3 +++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/notificationsmanager.cpp b/src/notificationsmanager.cpp index 70c14e435..535f0a364 100644 --- a/src/notificationsmanager.cpp +++ b/src/notificationsmanager.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -203,7 +204,7 @@ void NotificationsManager::postNotification(NeoChatRoom *room, } notification->setText(notification->text() + '\n' + entry); - notification->setPixmap(QPixmap::fromImage(icon)); + notification->setPixmap(createNotificationImage(icon, room)); notification->setDefaultAction(i18n("Open NeoChat in this room")); connect(notification, &KNotification::defaultActivated, this, [notification, room]() { @@ -239,7 +240,7 @@ void NotificationsManager::postInviteNotification(NeoChatRoom *room, const QStri KNotification *notification = new KNotification("invite"); notification->setText(i18n("%1 invited you to a room", sender)); notification->setTitle(title); - notification->setPixmap(img); + notification->setPixmap(createNotificationImage(icon, nullptr)); notification->setFlags(KNotification::Persistent); notification->setDefaultAction(i18n("Open this invitation in NeoChat")); connect(notification, &KNotification::defaultActivated, this, [notification, room]() { @@ -282,4 +283,33 @@ void NotificationsManager::clearInvitationNotification(const QString &roomId) } } +QPixmap NotificationsManager::createNotificationImage(const QImage &icon, NeoChatRoom *room) +{ + // Handle avatars that are lopsided in one dimension + const int biggestDimension = std::max(icon.width(), icon.height()); + const QRect imageRect{0, 0, biggestDimension, biggestDimension}; + + QImage roundedImage(imageRect.size(), QImage::Format_ARGB32); + roundedImage.fill(Qt::transparent); + + QPainter painter(&roundedImage); + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush(icon.scaledToHeight(biggestDimension)); + painter.setBrush(brush); + painter.drawRoundedRect(imageRect, imageRect.width(), imageRect.height()); + + if (room != nullptr) { + const QImage roomAvatar = room->avatar(imageRect.width(), imageRect.height()); + if (icon != roomAvatar) { + const QRect lowerQuarter{imageRect.center(), imageRect.size() / 2}; + + painter.setBrush(roomAvatar.scaled(lowerQuarter.size())); + painter.drawRoundedRect(lowerQuarter, lowerQuarter.width(), lowerQuarter.height()); + } + } + + return QPixmap::fromImage(roundedImage); +} + #include "moc_notificationsmanager.cpp" diff --git a/src/notificationsmanager.h b/src/notificationsmanager.h index 2ba087669..ac983ac7c 100644 --- a/src/notificationsmanager.h +++ b/src/notificationsmanager.h @@ -97,4 +97,7 @@ private: private Q_SLOTS: void processNotificationJob(QPointer connection, Quotient::GetNotificationsJob *job, bool initialization); + +private: + QPixmap createNotificationImage(const QImage &icon, NeoChatRoom *room); };