Notifications now provide API for invoking targets.

This commit is contained in:
Martin Rotter 2015-06-30 07:36:47 +02:00
parent 1d8c9a2545
commit 9447491ef7
5 changed files with 46 additions and 19 deletions

View File

@ -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();
}

View File

@ -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

View File

@ -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.

View File

@ -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() {

View File

@ -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()));
}
}