From 9447491ef7d1b17dba60289a5683967bbff862f7 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 30 Jun 2015 07:36:47 +0200 Subject: [PATCH] Notifications now provide API for invoking targets. --- src/gui/notifications/notification.cpp | 27 ++++++++++++++++++++++---- src/gui/notifications/notification.h | 13 +++++++++++-- src/miscellaneous/application.cpp | 9 +++++---- src/miscellaneous/application.h | 3 ++- src/miscellaneous/systemfactory.cpp | 13 +++++-------- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/gui/notifications/notification.cpp b/src/gui/notifications/notification.cpp index d51075b19..bb4587337 100644 --- a/src/gui/notifications/notification.cpp +++ b/src/gui/notifications/notification.cpp @@ -35,7 +35,8 @@ Notification::Notification() : QWidget(0), m_title(QString()), m_text(QString()), m_icon(QPixmap()), m_screen(-1), - m_width(-1), m_height(-1), m_padding(5), m_widgetMargin(2 * m_padding), m_timerId(0) { + m_width(-1), m_height(-1), m_padding(5), m_widgetMargin(2 * m_padding), m_timerId(0), m_clickTarget(NULL), + m_clickSlot(NULL) { setupWidget(); loadSettings(); } @@ -48,14 +49,22 @@ bool Notification::areNotificationsActivated() { return qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseFancyNotifications)).toBool(); } -void Notification::notify(const QString &text, const QString &title, const QIcon &icon) { +void Notification::notify(const QString &text, const QString &title, const QIcon &icon, + QObject *invokation_target, const char *invokation_slot) { cancel(); // Set new values. + m_clickTarget = invokation_target; + m_clickSlot = invokation_slot; m_text = text; m_title = title; m_icon = icon.pixmap(NOTIFICATION_ICON_SIZE, NOTIFICATION_ICON_SIZE); + if (m_clickTarget != NULL && m_clickSlot != NULL) { + // Connect invokation target. + connect(this, SIGNAL(clicked()), m_clickTarget, m_clickSlot, Qt::QueuedConnection); + } + // Show it. updateGeometries(); repaint(); @@ -64,13 +73,22 @@ void Notification::notify(const QString &text, const QString &title, const QIcon m_timerId = startTimer(10000); } -void Notification::notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon) { - notify(text, title, MessageBox::iconForStatus((QMessageBox::Icon) icon)); +void Notification::notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon, + QObject *invokation_target, const char *invokation_slot) { + notify(text, title, MessageBox::iconForStatus((QMessageBox::Icon) icon), invokation_target, invokation_slot); } void Notification::cancel() { hide(); + if (m_clickTarget != NULL && m_clickSlot != NULL) { + // Disconnect previous bubble click signalling. + disconnect(this, SIGNAL(clicked()), m_clickTarget, m_clickSlot); + } + + m_clickSlot = NULL; + m_clickTarget = NULL; + if (m_timerId != 0) { killTimer(m_timerId); } @@ -169,6 +187,7 @@ void Notification::paintEvent(QPaintEvent *event) { void Notification::mousePressEvent(QMouseEvent *event) { QWidget::mousePressEvent(event); + emit clicked(); cancel(); } diff --git a/src/gui/notifications/notification.h b/src/gui/notifications/notification.h index 2473b99bf..f9a62c1cf 100644 --- a/src/gui/notifications/notification.h +++ b/src/gui/notifications/notification.h @@ -34,8 +34,11 @@ class Notification : public QWidget { static bool areNotificationsActivated(); public slots: - void notify(const QString &text, const QString &title, const QIcon &icon); - void notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information); + // Main methods for using the netofication. + void notify(const QString &text, const QString &title, const QIcon &icon, + QObject *invokation_target = NULL, const char *invokation_slot = NULL); + void notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, + QObject *invokation_target = NULL, const char *invokation_slot = NULL); void cancel(); @@ -46,6 +49,9 @@ class Notification : public QWidget { void leaveEvent(QEvent *event); void timerEvent(QTimerEvent *event); + signals: + void clicked(); + private: void loadSettings(); void setupWidget(); @@ -65,6 +71,9 @@ class Notification : public QWidget { int m_padding; int m_widgetMargin; int m_timerId; + + QObject *m_clickTarget; + const char *m_clickSlot; }; #endif // NOTIFICATION_H diff --git a/src/miscellaneous/application.cpp b/src/miscellaneous/application.cpp index 5da7f0ac0..6fc8c37bb 100755 --- a/src/miscellaneous/application.cpp +++ b/src/miscellaneous/application.cpp @@ -170,18 +170,19 @@ void Application::deleteTrayIcon() { void Application::showGuiMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon message_type, QWidget *parent, - bool show_at_least_msgbox, const QIcon &custom_icon) { + bool show_at_least_msgbox, const QIcon &custom_icon, + QObject *invokation_target, const char *invokation_slot) { if (Notification::areNotificationsActivated()) { // Show OSD instead if tray icon bubble, depending on settings. if (custom_icon.isNull()) { - notification()->notify(message, title, message_type); + notification()->notify(message, title, message_type, invokation_target, invokation_slot); } else { - notification()->notify(message, title, custom_icon); + notification()->notify(message, title, custom_icon, invokation_target, invokation_slot); } } else if (SystemTrayIcon::isSystemTrayActivated()) { - trayIcon()->showMessage(title, message, message_type, TRAY_ICON_BUBBLE_TIMEOUT); + trayIcon()->showMessage(title, message, message_type, TRAY_ICON_BUBBLE_TIMEOUT, invokation_target, invokation_slot); } else if (show_at_least_msgbox) { // Tray icon or OSD is not available, display simple text box. diff --git a/src/miscellaneous/application.h b/src/miscellaneous/application.h index afa6248e3..d8930ee0b 100755 --- a/src/miscellaneous/application.h +++ b/src/miscellaneous/application.h @@ -146,7 +146,8 @@ class Application : public QtSingleApplication { // Displays given simple message in tray icon bubble or OSD // or in message box if tray icon is disabled. void showGuiMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon message_type, - QWidget *parent = NULL, bool show_at_least_msgbox = false, const QIcon &custom_icon = QIcon()); + QWidget *parent = NULL, bool show_at_least_msgbox = false, const QIcon &custom_icon = QIcon(), + QObject *invokation_target = NULL, const char *invokation_slot = NULL); // Returns pointer to "GOD" application singleton. inline static Application *instance() { diff --git a/src/miscellaneous/systemfactory.cpp b/src/miscellaneous/systemfactory.cpp index 13e639667..94fbbd71b 100755 --- a/src/miscellaneous/systemfactory.cpp +++ b/src/miscellaneous/systemfactory.cpp @@ -195,7 +195,7 @@ bool SystemFactory::isUpdateNewer(const QString &update_version) { int current_number = current_version_tkn.takeFirst().toInt(); int new_number = new_version_tkn.takeFirst().toInt(); - if (new_number > current_number) { + if (new_number != current_number) { // New version is indeed higher thatn current version. return true; } @@ -255,12 +255,9 @@ void SystemFactory::checkForUpdatesOnStartup() { UpdateCheck updates = checkForUpdates(); if (updates.second == QNetworkReply::NoError && isUpdateNewer(updates.first.m_availableVersion)) { - if (SystemTrayIcon::isSystemTrayActivated()) { - qApp->trayIcon()->showMessage(tr("New version available"), - tr("Click the bubble for more information."), - QSystemTrayIcon::Information, - TRAY_ICON_BUBBLE_TIMEOUT, - qApp->mainForm(), SLOT(showUpdates())); - } + qApp->showGuiMessage(tr("New version available"), + tr("Click the bubble for more information."), + QSystemTrayIcon::Information, + NULL, false, QIcon(), qApp->mainForm(), SLOT(showUpdates())); } }