Work on notifications.

This commit is contained in:
Martin Rotter 2015-06-29 10:27:28 +02:00
parent bdcd47a946
commit fb824c82b4
7 changed files with 40 additions and 36 deletions

View File

@ -82,6 +82,7 @@
#define ACCEPT_HEADER_FOR_FEED_DOWNLOADER "application/atom+xml,application/xml;q=0.9,text/xml;q=0.8,*/*;q=0.7"
#define MIME_TYPE_ITEM_POINTER "@APP_LOW_NAME@/itempointer"
#define DOWNLOADER_ICON_SIZE 48
#define NOTIFICATION_ICON_SIZE 64
#define GOOGLE_SEARCH_URL "https://www.google.com/search?q=%1&ie=utf-8&oe=utf-8"
#define GOOGLE_SUGGEST_URL "http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=%1"

View File

@ -40,13 +40,13 @@ Notification::Notification() : QWidget(0), m_title(QString()), m_text(QString())
Notification::~Notification() {
}
void Notification::notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon) {
void Notification::notify(const QString &text, const QString &title, const QIcon &icon) {
hide();
// Set new values.
m_text = text;
m_title = title;
m_icon = MessageBox::iconForStatus((QMessageBox::Icon) icon).pixmap(64, 64);
m_icon = icon.pixmap(NOTIFICATION_ICON_SIZE, NOTIFICATION_ICON_SIZE);
// Show it.
updateGeometries();
@ -54,6 +54,10 @@ void Notification::notify(const QString &text, const QString &title, QSystemTray
show();
}
void Notification::notify(const QString &text, const QString &title, QSystemTrayIcon::MessageIcon icon) {
notify(text, title, MessageBox::iconForStatus((QMessageBox::Icon) icon));
}
void Notification::updateGeometries() {
// Calculate width and height of notification with given icon and text.
m_width = m_padding +
@ -69,7 +73,6 @@ void Notification::updateGeometries() {
int x, y;
QRect screen_geometry = QApplication::desktop()->availableGeometry(m_screen);
// TODO: dodělat pozice x, y
switch (m_position) {
case Qt::BottomLeftCorner:
x = m_widgetMargin;
@ -96,25 +99,12 @@ void Notification::updateGeometries() {
setGeometry(x, y, m_width, m_height);
}
void Notification::focusInEvent(QFocusEvent *event) {
QWidget::focusInEvent(event);
repaint();
}
void Notification::focusOutEvent(QFocusEvent *event) {
QWidget::focusOutEvent(event);
repaint();
}
void Notification::paintEvent(QPaintEvent *event) {
Q_UNUSED(event)
QPainter painter(this);
painter.setFont(font());
QFontMetrics font_metrics = painter.fontMetrics();
if (!underMouse()) {
painter.setOpacity(0.7);
}
@ -133,7 +123,7 @@ void Notification::paintEvent(QPaintEvent *event) {
painter.drawPixmap(m_padding, m_padding, m_icon);
// Draw text.
painter.setPen( Qt::black );
painter.setPen(Qt::black);
// Needed heighs/widths.
int title_height = stringHeight(m_title);

View File

@ -32,11 +32,10 @@ class Notification : public QWidget {
virtual ~Notification();
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);
protected:
void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event);
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void enterEvent(QEvent *event);

View File

@ -35,8 +35,7 @@ TrayIconMenu::~TrayIconMenu() {
}
bool TrayIconMenu::event(QEvent *event) {
if (Application::activeModalWidget() != NULL &&
event->type() == QEvent::Show) {
if (Application::activeModalWidget() != NULL && event->type() == QEvent::Show) {
QTimer::singleShot(0, this, SLOT(hide()));
qApp->trayIcon()->showMessage(QSL(APP_LONG_NAME),
tr("Close opened modal dialogs first."),
@ -46,9 +45,7 @@ bool TrayIconMenu::event(QEvent *event) {
}
#endif
SystemTrayIcon::SystemTrayIcon(const QString &normal_icon,
const QString &plain_icon,
FormMain *parent)
SystemTrayIcon::SystemTrayIcon(const QString &normal_icon, const QString &plain_icon, FormMain *parent)
: QSystemTrayIcon(parent),
m_normalIcon(normal_icon),
m_plainPixmap(plain_icon),
@ -116,7 +113,7 @@ void SystemTrayIcon::show() {
#endif
}
void SystemTrayIcon::setNumber(int number, bool any_unread_message) {
void SystemTrayIcon::setNumber(int number, bool any_new_message) {
if (number <= 0) {
setToolTip(QSL(APP_LONG_NAME));
QSystemTrayIcon::setIcon(QIcon(m_normalIcon));
@ -129,7 +126,7 @@ void SystemTrayIcon::setNumber(int number, bool any_unread_message) {
// TODO: Here draw different background instead of different color of number.
tray_painter.begin(&background);
tray_painter.setPen(any_unread_message ? Qt::blue : Qt::black);
tray_painter.setPen(any_new_message ? Qt::blue : Qt::black);
tray_painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
tray_painter.setRenderHint(QPainter::TextAntialiasing, true);
@ -137,7 +134,6 @@ void SystemTrayIcon::setNumber(int number, bool any_unread_message) {
// infinity symbol in that case.
if (number > 999) {
m_font.setPixelSize(100);
tray_painter.setFont(m_font);
tray_painter.drawText(QRect(0, 0, 128, 128), Qt::AlignVCenter | Qt::AlignCenter, QChar(8734));
}

View File

@ -54,7 +54,7 @@ class SystemTrayIcon : public QSystemTrayIcon {
virtual ~SystemTrayIcon();
// Sets the number to be visible in the tray icon, number <= 0 removes it.
void setNumber(int number = -1, bool any_unread_message = false);
void setNumber(int number = -1, bool any_new_message = false);
void showMessage(const QString &title, const QString &message, MessageIcon icon = Information,
int milliseconds_timeout_hint = TRAY_ICON_BUBBLE_TIMEOUT, QObject *click_target = NULL,

View File

@ -26,19 +26,19 @@
#include "gui/statusbar.h"
#include "gui/dialogs/formmain.h"
#include "exceptions/applicationexception.h"
#include "adblock/adblockmanager.h"
#include <QSessionManager>
#include <QThread>
#include <QProcess>
#include <adblock/adblockmanager.h>
Application::Application(const QString &id, int &argc, char **argv)
: QtSingleApplication(id, argc, argv),
m_updateFeedsLock(NULL), m_userActions(QList<QAction*>()), m_mainForm(NULL),
m_trayIcon(NULL), m_settings(NULL), m_system(NULL), m_skins(NULL),
m_localization(NULL), m_icons(NULL), m_database(NULL), m_downloadManager(NULL), m_shouldRestart(false) {
m_localization(NULL), m_icons(NULL), m_database(NULL), m_downloadManager(NULL), m_shouldRestart(false),
m_notification(NULL) {
connect(this, SIGNAL(aboutToQuit()), this, SLOT(onAboutToQuit()));
connect(this, SIGNAL(commitDataRequest(QSessionManager&)), this, SLOT(onCommitData(QSessionManager&)));
connect(this, SIGNAL(saveStateRequest(QSessionManager&)), this, SLOT(onSaveState(QSessionManager&)));
@ -84,7 +84,7 @@ Mutex *Application::feedUpdateLock() {
}
void Application::backupDatabaseSettings(bool backup_database, bool backup_settings,
const QString &target_path, const QString &backup_name) {
const QString &target_path, const QString &backup_name) {
if (!QFileInfo(target_path).isWritable()) {
throw ApplicationException(tr("Output directory is not writable."));
}
@ -168,15 +168,18 @@ void Application::deleteTrayIcon() {
}
}
void Application::showGuiMessage(const QString& title, const QString& message,
void Application::showGuiMessage(const QString &title, const QString &message,
QSystemTrayIcon::MessageIcon message_type,
QWidget *parent, int duration) {
if (SystemTrayIcon::isSystemTrayActivated()) {
// TODO: Maybe show OSD instead if tray icon bubble, depending on settings.
/*if (true) {
// Show OSD instead if tray icon bubble, depending on settings.
notification()->notify(message, title, message_type);
}
else */if (SystemTrayIcon::isSystemTrayActivated()) {
trayIcon()->showMessage(title, message, message_type, duration);
}
else {
// TODO: Tray icon or OSD is not available, display simple text box.
// Tray icon or OSD is not available, display simple text box.
MessageBox::show(parent, (QMessageBox::Icon) message_type, title, message);
}
}
@ -225,6 +228,11 @@ void Application::onAboutToQuit() {
qDebug("Close lock timed-out.");
}
if (m_notification != NULL) {
m_notification->deleteLater();
m_notification = NULL;
}
// Now, we can check if application should just quit or restart itself.
if (m_shouldRestart) {
finish();

View File

@ -28,6 +28,7 @@
#include "miscellaneous/databasefactory.h"
#include "miscellaneous/iofactory.h"
#include "gui/systemtrayicon.h"
#include "gui/notifications/notification.h"
#include "network-web/downloadmanager.h"
#include <QList>
@ -63,6 +64,14 @@ class Application : public QtSingleApplication {
return m_system;
}
inline Notification *notification() {
if (m_notification == NULL) {
m_notification = new Notification();
}
return m_notification;
}
inline SkinFactory *skins() {
if (m_skins == NULL) {
m_skins = new SkinFactory(this);
@ -184,6 +193,7 @@ class Application : public QtSingleApplication {
DatabaseFactory *m_database;
DownloadManager *m_downloadManager;
bool m_shouldRestart;
Notification *m_notification;
};
#endif // APPLICATION_H