1 /***************************************************************************
2 * Copyright (C) 2005-2010 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This contains code from KStatusNotifierItem, part of the KDE libs *
6 * Copyright (C) 2009 Marco Martin <notmart@gmail.com> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) version 3. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
24 #include "graphicalui.h"
26 #include "actioncollection.h"
27 #include "contextmenuactionprovider.h"
33 # include <KWindowInfo>
34 # include <KWindowSystem>
37 GraphicalUi *GraphicalUi::_instance = 0;
38 QWidget *GraphicalUi::_mainWidget = 0;
39 QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
40 ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
41 ToolBarActionProvider *GraphicalUi::_toolBarActionProvider = 0;
42 UiStyle *GraphicalUi::_uiStyle = 0;
43 bool GraphicalUi::_onAllDesktops = false;
45 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent) {
54 void GraphicalUi::init() {
56 mainWidget()->installEventFilter(this);
60 GraphicalUi::~GraphicalUi() {
62 mainWidget()->removeEventFilter(this);
66 ActionCollection *GraphicalUi::actionCollection(const QString &category) {
67 if(_actionCollections.contains(category))
68 return _actionCollections.value(category);
69 ActionCollection *coll = new ActionCollection(_mainWidget);
71 coll->addAssociatedWidget(_mainWidget);
72 _actionCollections.insert(category, coll);
76 void GraphicalUi::setMainWidget(QWidget *widget) {
80 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider) {
81 _contextMenuActionProvider = provider;
84 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider) {
85 _toolBarActionProvider = provider;
88 void GraphicalUi::setUiStyle(UiStyle *style) {
92 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
94 if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
95 _dwTickCount = GetTickCount();
98 return AbstractUi::eventFilter(obj, event);
101 // Code taken from KStatusNotifierItem for handling minimize/restore
103 bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
105 // the problem is that we lose focus when the systray icon is activated
106 // and we don't know the former active window
107 // therefore we watch for activation event and use our stopwatch :)
108 if(GetTickCount() - _dwTickCount < 300) {
109 // we were active in the last 300ms -> hide it
110 minimizeRestore(false);
112 minimizeRestore(true);
115 #elif defined(HAVE_KDE) && defined(Q_WS_X11)
116 KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
117 // mapped = visible (but possibly obscured)
118 bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
120 // - not mapped -> show, raise, focus
122 // - obscured -> raise, focus
123 // - not obscured -> hide
124 //info1.mappingState() != NET::Visible -> window on another desktop?
127 minimizeRestore(true);
131 QListIterator< WId > it (KWindowSystem::stackingOrder());
133 while(it.hasPrevious()) {
134 WId id = it.previous();
135 if(id == mainWidget()->winId())
138 KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
140 if(info2.mappingState() != NET::Visible)
141 continue; // not visible on current desktop -> ignore
143 if(!info2.geometry().intersects(mainWidget()->geometry()))
144 continue; // not obscuring the window -> ignore
146 if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
147 continue; // obscured by window kept above -> ignore
149 NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
150 | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
151 | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
153 if(type == NET::Dock || type == NET::TopMenu)
154 continue; // obscured by dock or topmenu -> ignore
157 KWindowSystem::raiseWindow(mainWidget()->winId());
158 KWindowSystem::activateWindow(mainWidget()->winId());
163 //not on current desktop?
164 if(!info1.isOnCurrentDesktop()) {
166 KWindowSystem::activateWindow(mainWidget()->winId());
171 minimizeRestore(false); // hide
176 if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
178 minimizeRestore(true);
182 minimizeRestore(false);
191 void GraphicalUi::minimizeRestore(bool show) {
193 activateMainWidget();
198 void GraphicalUi::activateMainWidget() {
201 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
203 KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
205 KWindowSystem::setCurrentDesktop(info.desktop());
208 mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
209 mainWidget()->show();
210 mainWidget()->raise();
211 KWindowSystem::raiseWindow(mainWidget()->winId());
212 KWindowSystem::activateWindow(mainWidget()->winId());
214 mainWidget()->show();
215 KWindowSystem::raiseWindow(mainWidget()->winId());
216 KWindowSystem::forceActiveWindow(mainWidget()->winId());
222 // Bypass focus stealing prevention
223 QX11Info::setAppUserTime(QX11Info::appTime());
226 if(mainWidget()->windowState() & Qt::WindowMinimized) {
228 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
231 // this does not actually work on all platforms... and causes more evil than good
232 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
233 mainWidget()->show();
234 mainWidget()->raise();
235 mainWidget()->activateWindow();
237 #endif /* HAVE_KDE */
240 void GraphicalUi::hideMainWidget() {
242 #if defined(HAVE_KDE) && defined(Q_WS_X11)
243 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
244 _onAllDesktops = info.onAllDesktops();
247 if(instance()->isHidingMainWidgetAllowed())
248 mainWidget()->hide();
251 void GraphicalUi::toggleMainWidget() {
252 instance()->checkMainWidgetVisibility(true);