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) {
51 mainWidget()->installEventFilter(this);
56 GraphicalUi::~GraphicalUi() {
58 mainWidget()->removeEventFilter(this);
62 ActionCollection *GraphicalUi::actionCollection(const QString &category) {
63 if(_actionCollections.contains(category))
64 return _actionCollections.value(category);
65 ActionCollection *coll = new ActionCollection(_mainWidget);
67 coll->addAssociatedWidget(_mainWidget);
68 _actionCollections.insert(category, coll);
72 void GraphicalUi::setMainWidget(QWidget *widget) {
76 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider) {
77 _contextMenuActionProvider = provider;
80 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider) {
81 _toolBarActionProvider = provider;
84 void GraphicalUi::setUiStyle(UiStyle *style) {
88 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
90 if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
91 _dwTickCount = GetTickCount();
94 return AbstractUi::eventFilter(obj, event);
97 // Code taken from KStatusNotifierItem for handling minimize/restore
99 bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
101 // the problem is that we lose focus when the systray icon is activated
102 // and we don't know the former active window
103 // therefore we watch for activation event and use our stopwatch :)
104 if(GetTickCount() - _dwTickCount < 300) {
105 // we were active in the last 300ms -> hide it
106 minimizeRestore(false);
108 minimizeRestore(true);
111 #elif defined(HAVE_KDE) && defined(Q_WS_X11)
112 KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
113 // mapped = visible (but possibly obscured)
114 bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
116 // - not mapped -> show, raise, focus
118 // - obscured -> raise, focus
119 // - not obscured -> hide
120 //info1.mappingState() != NET::Visible -> window on another desktop?
123 minimizeRestore(true);
127 QListIterator< WId > it (KWindowSystem::stackingOrder());
129 while(it.hasPrevious()) {
130 WId id = it.previous();
131 if(id == mainWidget()->winId())
134 KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
136 if(info2.mappingState() != NET::Visible)
137 continue; // not visible on current desktop -> ignore
139 if(!info2.geometry().intersects(mainWidget()->geometry()))
140 continue; // not obscuring the window -> ignore
142 if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
143 continue; // obscured by window kept above -> ignore
145 NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
146 | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
147 | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
149 if(type == NET::Dock || type == NET::TopMenu)
150 continue; // obscured by dock or topmenu -> ignore
153 KWindowSystem::raiseWindow(mainWidget()->winId());
154 KWindowSystem::activateWindow(mainWidget()->winId());
159 //not on current desktop?
160 if(!info1.isOnCurrentDesktop()) {
162 KWindowSystem::activateWindow(mainWidget()->winId());
167 minimizeRestore(false); // hide
172 if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
174 minimizeRestore(true);
178 minimizeRestore(false);
187 void GraphicalUi::minimizeRestore(bool show) {
189 activateMainWidget();
194 void GraphicalUi::activateMainWidget() {
197 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
199 KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
201 KWindowSystem::setCurrentDesktop(info.desktop());
204 mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
205 mainWidget()->show();
206 mainWidget()->raise();
207 KWindowSystem::raiseWindow(mainWidget()->winId());
208 KWindowSystem::activateWindow(mainWidget()->winId());
210 mainWidget()->show();
211 KWindowSystem::raiseWindow(mainWidget()->winId());
212 KWindowSystem::forceActiveWindow(mainWidget()->winId());
218 // Bypass focus stealing prevention
219 QX11Info::setAppUserTime(QX11Info::appTime());
222 if(mainWidget()->windowState() & Qt::WindowMinimized) {
224 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
227 // this does not actually work on all platforms... and causes more evil than good
228 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
229 mainWidget()->show();
230 mainWidget()->raise();
231 mainWidget()->activateWindow();
233 #endif /* HAVE_KDE */
236 void GraphicalUi::hideMainWidget() {
238 #if defined(HAVE_KDE) && defined(Q_WS_X11)
239 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
240 _onAllDesktops = info.onAllDesktops();
243 if(instance()->isHidingMainWidgetAllowed())
244 mainWidget()->hide();
247 void GraphicalUi::toggleMainWidget() {
248 instance()->checkMainWidgetVisibility(true);