Tray icon menu solution for Windows.

This commit is contained in:
Martin Rotter 2013-06-26 19:04:38 +02:00
parent 9b1f2b3c85
commit f4e3ca1838
10 changed files with 63 additions and 15 deletions

View File

@ -9,6 +9,7 @@
#define APP_NAME "@APP_NAME@"
#define APP_LOW_NAME "@APP_LOW_NAME@"
#define APP_LOW_H_NAME ".@APP_LOW_NAME@"
#define APP_LONG_NAME "@APP_NAME@ @APP_VERSION@"
#define APP_AUTHORS "@APP_AUTHOR@"
#define APP_URL "@APP_URL@"
#define APP_VERSION "@APP_VERSION@"

View File

@ -1,4 +1,5 @@
#include <QCloseEvent>
#include <QMessageBox>
#include "gui/formmain.h"
#include "gui/formsettings.h"
@ -43,7 +44,11 @@ QMenu *FormMain::getTrayMenu() {
void FormMain::prepareMenus() {
// Setup menu for tray icon.
if (SystemTrayIcon::isSystemTrayAvailable()) {
#if defined(Q_OS_WIN)
m_trayMenu = new TrayIconMenu(APP_NAME, this);
#else
m_trayMenu = new QMenu(APP_NAME, this);
#endif
// Add needed items to the menu.
m_trayMenu->addAction(m_ui->m_actionSettings);
@ -91,7 +96,6 @@ void FormMain::cleanupResources() {
qDebug("Cleaning up resources before the application exits.");
}
#if !defined(Q_OS_WIN)
bool FormMain::event(QEvent *event) {
if (event->type() == ThemeFactoryEvent::type()) {
// Handle the change of icon theme.
@ -104,8 +108,9 @@ bool FormMain::event(QEvent *event) {
void FormMain::setupIcons() {
// NOTE: Call QIcon::fromTheme for all needed widgets here.
m_ui->m_actionSettings->setIcon(ThemeFactory::fromTheme("preferences-system"));
m_ui->m_actionQuit->setIcon(ThemeFactory::fromTheme("application-exit"));
}
#endif
void FormMain::createConnections() {
// Menu "File" connections.
@ -137,5 +142,6 @@ void FormMain::closeEvent(QCloseEvent *event) {
}
void FormMain::showSettings() {
FormSettings(this).exec();
QMessageBox::information(this, "tr", "Ptr");
//FormSettings(this).exec();
}

View File

@ -2,6 +2,7 @@
#define FORMMAIN_H
#include <QMainWindow>
#include <QTimer>
#include "ui_formmain.h"
@ -23,12 +24,10 @@ class FormMain : public QMainWindow {
void createConnections();
void closeEvent(QCloseEvent *event);
#if !defined(Q_OS_WIN)
bool event(QEvent *event);
// Sets up proper icons for this widget.
void setupIcons();
#endif
public slots:
void processExecutionMessage(const QString &message);

View File

@ -213,7 +213,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>19</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="m_menuFile">
@ -229,6 +229,7 @@
<property name="title">
<string>&amp;Help</string>
</property>
<addaction name="m_actionAboutGuard"/>
</widget>
<widget class="QMenu" name="menu_View">
<property name="title">
@ -267,6 +268,11 @@
<string>&amp;Settings</string>
</property>
</action>
<action name="m_actionAboutGuard">
<property name="text">
<string>&amp;About RSS Guard</string>
</property>
</action>
</widget>
<resources/>
<connections/>

View File

@ -10,8 +10,9 @@
FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormSettings) {
m_ui->setupUi(this);
// Set flags.
// Set flags and attributes.
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog);
setWindowIcon(ThemeFactory::fromTheme("preferences-system"));
// Establish needed connections.
connect(this, &FormSettings::accepted, this, &FormSettings::saveSettings);
@ -110,7 +111,6 @@ void FormSettings::loadInterface() {
}
else {
#endif
// TODO: Display correct theme on linux.
m_ui->m_cmbIconTheme->setCurrentText(current_theme);
#if defined(Q_OS_LINUX)
}

View File

@ -7,8 +7,29 @@
#include "gui/formsettings.h"
#include "core/settings.h"
#include "core/defs.h"
#include "qtsingleapplication/qtsingleapplication.h"
#if defined(Q_OS_WIN)
TrayIconMenu::TrayIconMenu(const QString &title, QWidget *parent)
: QMenu(title, parent) {
}
TrayIconMenu::~TrayIconMenu() {
}
bool TrayIconMenu::event(QEvent *event) {
if (QtSingleApplication::activeModalWidget() != nullptr &&
event->type() == QEvent::Show) {
QTimer::singleShot(0, this, SLOT(hide()));
SystemTrayIcon::getInstance()->showMessage(APP_LONG_NAME,
tr("Close opened modal dialogs first."),
QSystemTrayIcon::Warning);
}
return QMenu::event(event);
}
#endif
QPointer<SystemTrayIcon> SystemTrayIcon::s_trayIcon;
SystemTrayIcon::SystemTrayIcon(const QString &normal_icon,

View File

@ -3,9 +3,22 @@
#include <QSystemTrayIcon>
#include <QPointer>
#include <QMenu>
class FormMain;
class QEvent;
#if defined(Q_OS_WIN)
class TrayIconMenu : public QMenu {
public:
explicit TrayIconMenu(const QString &title, QWidget *parent);
virtual ~TrayIconMenu();
protected:
bool event(QEvent *event);
};
#endif
class SystemTrayIcon : public QSystemTrayIcon {
Q_OBJECT

View File

@ -97,12 +97,11 @@ void ThemeFactory::loadCurrentIconTheme() {
// In Linux, we need to deliver custom event for all widgets
// to make sure they get a chance to redraw their icons.
#if defined(Q_OS_LINUX)
// NOTE: This is NOT necessarily needed on Windows.
foreach (QWidget *widget, QtSingleApplication::allWidgets()) {
QtSingleApplication::postEvent((QObject*) widget,
new ThemeFactoryEvent());
}
#endif
}
}

View File

@ -38,8 +38,9 @@ class ThemeFactory {
static void setCurrentIconTheme(const QString &theme_name);
// Wrapper for QIcon::fromTheme.
// If icon is not found in user-defined icon theme,
// TODO: If icon is not found in user-defined icon theme,
// then it is searched in system-default theme (ThemeFactory::getSystemIconTheme()).
// BUG: I tried to do that, but QIcon is apparently bugged.
static QIcon fromTheme(const QString & name, const QIcon & fallback = QIcon());
};

View File

@ -57,9 +57,6 @@ int main(int argc, char *argv[]) {
// Add an extra path for non-system icon themes.
ThemeFactory::setupSearchPaths();
// Load icon theme from settings.
ThemeFactory::loadCurrentIconTheme();
// These settings needs to be set before any QSettings object.
QtSingleApplication::setApplicationName(APP_NAME);
QtSingleApplication::setApplicationVersion(APP_VERSION);
@ -71,7 +68,7 @@ int main(int argc, char *argv[]) {
FormMain window;
// Set correct information for main window.
window.setWindowTitle(QString(APP_NAME) + " " + APP_VERSION);
window.setWindowTitle(APP_LONG_NAME);
// Display welcome dialog if application is launched for the first time.
if (Settings::getInstance()->value(APP_CFG_GEN, "first_start", true).toBool()) {
@ -96,6 +93,11 @@ int main(int argc, char *argv[]) {
SystemTrayIcon::getInstance()->show();
}
// Load icon theme from settings.
// NOTE: Make sure that this is done after main window and
// other startup widgets are created.
ThemeFactory::loadCurrentIconTheme();
// Setup single-instance behavior.
QObject::connect(&application, &QtSingleApplication::messageReceived,
&window, &FormMain::processExecutionMessage);