* Move the window activation stuff into GraphicalUI (because that's where it really belongs)
* Make hiding the tray icon work better (i.e. without a restart)
* Ensure that the main window can't be hidden if we don't have a systray
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
-#ifndef QUASSELUI_H
-#define QUASSELUI_H
+#ifndef ABSTRACTUI_H
+#define ABSTRACTUI_H
#include <QObject>
#include <QVariantMap>
move(_normalPos);
#endif
- if(s.value("MainWinHidden").toBool() && systemTray()->isSystemTrayAvailable())
- systemTray()->hideMainWidget();
+ if(s.value("MainWinHidden").toBool() && QtUi::haveSystemTray())
+ QtUi::hideMainWidget();
else if(s.value("MainWinMinimized").toBool())
showMinimized();
else if(maximized)
QtUiSettings s;
QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
Q_ASSERT(app);
- if(!app->isAboutToQuit()
- && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool() && systemTray()->isSystemTrayAvailable()) {
- systemTray()->hideMainWidget();
+ if(!app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
+ QtUi::hideMainWidget();
event->ignore();
} else {
event->accept();
void QtUi::init() {
_mainWin->init();
+ QtUiSettings uiSettings;
+ uiSettings.initAndNotify("UseSystemTrayIcon", this, SLOT(useSystemTrayChanged(QVariant)), true);
}
MessageModel *QtUi::createMessageModel(QObject *parent) {
_mainWin->disconnectedFromCore();
}
+void QtUi::useSystemTrayChanged(const QVariant &v) {
+ _useSystemTray = v.toBool();
+ SystemTray *tray = mainWindow()->systemTray();
+ if(_useSystemTray) {
+ if(tray->isSystemTrayAvailable())
+ tray->setVisible(true);
+ } else {
+ if(tray->isSystemTrayAvailable() && mainWindow()->isVisible())
+ tray->setVisible(false);
+ }
+}
+
bool QtUi::haveSystemTray() {
-#ifdef QT_NO_SYSTEMTRAYICON
- return false;
-#else
- return mainWindow()->systemTray()->isSystemTrayAvailable();
-#endif
+ return mainWindow()->systemTray()->isSystemTrayAvailable() && instance()->_useSystemTray;
+}
+
+bool QtUi::isHidingMainWidgetAllowed() const {
+ return haveSystemTray();
+}
+
+void QtUi::minimizeRestore(bool show) {
+ SystemTray *tray = mainWindow()->systemTray();
+ if(show) {
+ if(tray && !_useSystemTray)
+ tray->setVisible(false);
+ } else {
+ if(tray && _useSystemTray)
+ tray->setVisible(true);
+ }
+ GraphicalUi::minimizeRestore(show);
}
void QtUi::registerNotificationBackend(AbstractNotificationBackend *backend) {
void disconnectedFromCore();
void notificationActivated(uint notificationId);
+protected:
+ virtual void minimizeRestore(bool show);
+ virtual bool isHidingMainWidgetAllowed() const;
+
+private slots:
+ void useSystemTrayChanged(const QVariant &);
+
private:
static QPointer<QtUi> _instance;
static QPointer<MainWin> _mainWin;
static QList<AbstractNotificationBackend *> _notificationBackends;
static QList<AbstractNotificationBackend::Notification> _notifications;
+
+ bool _useSystemTray;
};
QtUi *QtUi::instance() { return _instance ? _instance.data() : new QtUi(); }
* Copyright (C) 2005-2010 by the Quassel Project *
* devel@quassel-irc.org *
* *
- * This contains code from KStatusNotifierItem, part of the KDE libs *
- * Copyright (C) 2009 Marco Martin <notmart@gmail.com> *
- * *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
{
Q_ASSERT(parent);
-#ifdef Q_WS_WIN
- _dwTickCount = 0;
- associatedWidget()->installEventFilter(this);
-#endif
-
qApp->installEventFilter(this);
}
SystemTray::~SystemTray() {
-#ifdef Q_WS_WIN
- associatedWidget()->removeEventFilter(this);
-#endif
-
_trayMenu->deleteLater();
}
emit activated(reason);
if(reason == Trigger && !isActivationInhibited()) {
- toggleMainWidget();
+ GraphicalUi::toggleMainWidget();
}
}
if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
_inhibitActivation = false;
}
-#ifdef Q_WS_WIN
- if(obj == associatedWidget() && event->type() == QEvent::ActivationChange) {
- _dwTickCount = GetTickCount();
- }
-#endif
return QObject::eventFilter(obj, event);
}
-
-// Code taken from KStatusNotifierItem for handling minimize/restore
-
-bool SystemTray::checkVisibility(bool perform) {
-#ifdef Q_WS_WIN
- // the problem is that we lose focus when the systray icon is activated
- // and we don't know the former active window
- // therefore we watch for activation event and use our stopwatch :)
- if(GetTickCount() - _dwTickCount < 300) {
- // we were active in the last 300ms -> hide it
- minimizeRestore(false);
- } else {
- minimizeRestore(true);
- }
-
-#elif defined(HAVE_KDE) && defined(Q_WS_X11)
- KWindowInfo info1 = KWindowSystem::windowInfo(associatedWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
- // mapped = visible (but possibly obscured)
- bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
-
- // - not mapped -> show, raise, focus
- // - mapped
- // - obscured -> raise, focus
- // - not obscured -> hide
- //info1.mappingState() != NET::Visible -> window on another desktop?
- if(!mapped) {
- if(perform)
- minimizeRestore(true);
- return true;
-
- } else {
- QListIterator< WId > it (KWindowSystem::stackingOrder());
- it.toBack();
- while(it.hasPrevious()) {
- WId id = it.previous();
- if(id == associatedWidget()->winId())
- break;
-
- KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
-
- if(info2.mappingState() != NET::Visible)
- continue; // not visible on current desktop -> ignore
-
- if(!info2.geometry().intersects(associatedWidget()->geometry()))
- continue; // not obscuring the window -> ignore
-
- if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
- continue; // obscured by window kept above -> ignore
-
- NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
- | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
- | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
-
- if(type == NET::Dock || type == NET::TopMenu)
- continue; // obscured by dock or topmenu -> ignore
-
- if(perform) {
- KWindowSystem::raiseWindow(associatedWidget()->winId());
- KWindowSystem::activateWindow(associatedWidget()->winId());
- }
- return true;
- }
-
- //not on current desktop?
- if(!info1.isOnCurrentDesktop()) {
- if(perform)
- KWindowSystem::activateWindow(associatedWidget()->winId());
- return true;
- }
-
- if(perform)
- minimizeRestore(false); // hide
- return false;
- }
-#else
-
- if(!associatedWidget()->isVisible() || associatedWidget()->isMinimized()) {
- if(perform)
- minimizeRestore(true);
- return true;
- } else {
- if(perform)
- minimizeRestore(false);
- return false;
- }
-
-#endif
-
- return true;
-}
-
-void SystemTray::minimizeRestore(bool show) {
- if(show)
- GraphicalUi::activateMainWidget();
- else {
- if(isSystemTrayAvailable()) {
- if(!isVisible())
- setVisible();
- GraphicalUi::hideMainWidget();
- }
- }
-}
-
-void SystemTray::hideMainWidget() {
- minimizeRestore(false);
-}
-
-void SystemTray::toggleMainWidget() {
- checkVisibility(true);
-}
#include "icon.h"
-#ifdef Q_WS_WIN
-# include <windows.h>
-#endif
-
class QMenu;
class SystemTray : public QObject {
virtual inline bool isSystemTrayAvailable() const;
void setAlert(bool alerted);
- inline void setInhibitActivation();
- inline virtual bool isVisible() const { return false; }
+ virtual inline void setInhibitActivation();
+ virtual inline bool isVisible() const { return false; }
inline bool isActivationInhibited() const;
QWidget *associatedWidget() const;
virtual void setVisible(bool visible = true);
virtual void setToolTip(const QString &title, const QString &subtitle);
virtual void showMessage(const QString &title, const QString &message, MessageIcon icon = Information, int millisecondsTimeoutHint = 10000);
- void toggleMainWidget();
- void hideMainWidget();
signals:
void activated(SystemTray::ActivationReason);
protected slots:
virtual void activate(SystemTray::ActivationReason = Trigger);
- void minimizeRestore(bool restore);
-
protected:
virtual void setMode(Mode mode);
inline Mode mode() const;
private slots:
private:
- bool checkVisibility(bool performToggle = true);
-
Mode _mode;
State _state;
QMenu *_trayMenu;
QWidget *_associatedWidget;
-
-#ifdef Q_WS_WIN
- DWORD _dwTickCount;
-#endif
-
};
// inlines
/***************************************************************************
- * Copyright (C) 2005-09 by the Quassel Project *
+ * Copyright (C) 2005-2010 by the Quassel Project *
* devel@quassel-irc.org *
* *
+ * This contains code from KStatusNotifierItem, part of the KDE libs *
+ * Copyright (C) 2009 Marco Martin <notmart@gmail.com> *
+ * *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
# include <KWindowSystem>
#endif
+GraphicalUi *GraphicalUi::_instance = 0;
QWidget *GraphicalUi::_mainWidget = 0;
QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
UiStyle *GraphicalUi::_uiStyle = 0;
bool GraphicalUi::_onAllDesktops = false;
-GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent)
-{
+GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent) {
+ Q_ASSERT(!_instance);
+ _instance = this;
+
+#ifdef Q_WS_WIN
+ _dwTickCount = 0;
+ mainWidget()->installEventFilter(this);
+#endif
}
+GraphicalUi::~GraphicalUi() {
+#ifdef Q_WS_WIN
+ mainWidget()->removeEventFilter(this);
+#endif
+}
+
ActionCollection *GraphicalUi::actionCollection(const QString &category) {
if(_actionCollections.contains(category))
return _actionCollections.value(category);
_uiStyle = style;
}
+bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
+#ifdef Q_WS_WIN
+ if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
+ _dwTickCount = GetTickCount();
+ }
+#endif
+ return AbstractUi::eventFilter(obj, event);
+}
+
+// Code taken from KStatusNotifierItem for handling minimize/restore
+
+bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
+#ifdef Q_WS_WIN
+ // the problem is that we lose focus when the systray icon is activated
+ // and we don't know the former active window
+ // therefore we watch for activation event and use our stopwatch :)
+ if(GetTickCount() - _dwTickCount < 300) {
+ // we were active in the last 300ms -> hide it
+ minimizeRestore(false);
+ } else {
+ minimizeRestore(true);
+ }
+
+#elif defined(HAVE_KDE) && defined(Q_WS_X11)
+ KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
+ // mapped = visible (but possibly obscured)
+ bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
+
+ // - not mapped -> show, raise, focus
+ // - mapped
+ // - obscured -> raise, focus
+ // - not obscured -> hide
+ //info1.mappingState() != NET::Visible -> window on another desktop?
+ if(!mapped) {
+ if(perform)
+ minimizeRestore(true);
+ return true;
+
+ } else {
+ QListIterator< WId > it (KWindowSystem::stackingOrder());
+ it.toBack();
+ while(it.hasPrevious()) {
+ WId id = it.previous();
+ if(id == mainWidget()->winId())
+ break;
+
+ KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
+
+ if(info2.mappingState() != NET::Visible)
+ continue; // not visible on current desktop -> ignore
+
+ if(!info2.geometry().intersects(mainWidget()->geometry()))
+ continue; // not obscuring the window -> ignore
+
+ if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
+ continue; // obscured by window kept above -> ignore
+
+ NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
+ | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
+ | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
+
+ if(type == NET::Dock || type == NET::TopMenu)
+ continue; // obscured by dock or topmenu -> ignore
+
+ if(perform) {
+ KWindowSystem::raiseWindow(mainWidget()->winId());
+ KWindowSystem::activateWindow(mainWidget()->winId());
+ }
+ return true;
+ }
+
+ //not on current desktop?
+ if(!info1.isOnCurrentDesktop()) {
+ if(perform)
+ KWindowSystem::activateWindow(mainWidget()->winId());
+ return true;
+ }
+
+ if(perform)
+ minimizeRestore(false); // hide
+ return false;
+ }
+#else
+
+ if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
+ if(perform)
+ minimizeRestore(true);
+ return true;
+ } else {
+ if(perform)
+ minimizeRestore(false);
+ return false;
+ }
+
+#endif
+
+ return true;
+}
+
+void GraphicalUi::minimizeRestore(bool show) {
+ if(show)
+ activateMainWidget();
+ else
+ hideMainWidget();
+}
+
void GraphicalUi::activateMainWidget() {
#ifdef HAVE_KDE
# ifdef Q_WS_X11
_onAllDesktops = info.onAllDesktops();
#endif
- mainWidget()->hide();
+ if(instance()->isHidingMainWidgetAllowed())
+ mainWidget()->hide();
+}
+
+void GraphicalUi::toggleMainWidget() {
+ instance()->checkMainWidgetVisibility(true);
}
class ToolBarActionProvider;
class UiStyle;
+#ifdef Q_WS_WIN
+# include <windows.h>
+#endif
+
class GraphicalUi : public AbstractUi {
Q_OBJECT
public:
GraphicalUi(QObject *parent = 0);
+ virtual ~GraphicalUi();
//! Access global ActionCollections.
/** These ActionCollections are associated with the main window, i.e. they contain global
//! Hide main widget (storing the current desktop if possible)
static void hideMainWidget();
+ //! Toggle main widget
+ static void toggleMainWidget();
+
protected:
//! This is the widget we associate global actions with, typically the main window
void setMainWidget(QWidget *);
+ //! Check if the mainWidget is visible and optionally toggle its visibility
+ /** With KDE integration, we check if the mainWidget is (partially) obscured in order to determine if
+ * it should be activated or hidden. Without KDE, we need to resort to checking the current state
+ * as Qt knows it, ignoring windows covering it.
+ * @param performToggle If true, toggle the window's state in addition to checking visibility
+ * @return True, if the window is currently visible
+ */
+ bool checkMainWidgetVisibility(bool performToggle);
+
+ //! Minimize to or restore main widget
+ virtual void minimizeRestore(bool show);
+
+ //! Whether it is allowed to hide the mainWidget
+ /** The default implementation returns false, meaning that we won't hide the mainWidget even
+ * if requested. This is to prevent hiding in case we don't have a tray icon to restore from.
+ */
+ virtual inline bool isHidingMainWidgetAllowed() const;
+
void setContextMenuActionProvider(ContextMenuActionProvider *);
void setToolBarActionProvider(ToolBarActionProvider *);
void setUiStyle(UiStyle *);
+ virtual bool eventFilter(QObject *obj, QEvent *event);
+
private:
+ static inline GraphicalUi *instance();
+
+ static GraphicalUi *_instance;
static QWidget *_mainWidget;
static QHash<QString, ActionCollection *> _actionCollections;
static ContextMenuActionProvider *_contextMenuActionProvider;
static ToolBarActionProvider *_toolBarActionProvider;
static UiStyle *_uiStyle;
static bool _onAllDesktops;
-};
-ContextMenuActionProvider *GraphicalUi::contextMenuActionProvider() {
- return _contextMenuActionProvider;
-}
+#ifdef Q_WS_WIN
+ DWORD _dwTickCount;
+#endif
-ToolBarActionProvider *GraphicalUi::toolBarActionProvider() {
- return _toolBarActionProvider;
-}
+};
-UiStyle *GraphicalUi::uiStyle() {
- return _uiStyle;
-}
+// inlines
-QWidget *GraphicalUi::mainWidget() {
- return _mainWidget;
-}
+GraphicalUi *GraphicalUi::instance() { return _instance; }
+ContextMenuActionProvider *GraphicalUi::contextMenuActionProvider() { return _contextMenuActionProvider; }
+ToolBarActionProvider *GraphicalUi::toolBarActionProvider() { return _toolBarActionProvider; }
+UiStyle *GraphicalUi::uiStyle() { return _uiStyle; }
+QWidget *GraphicalUi::mainWidget() { return _mainWidget; }
+bool GraphicalUi::isHidingMainWidgetAllowed() const { return false; }
#endif