1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************/
24 #include "graphicalui.h"
26 #include "actioncollection.h"
27 #include "uisettings.h"
28 #include "contextmenuactionprovider.h"
29 #include "toolbaractionprovider.h"
35 # include <KWindowInfo>
36 # include <KWindowSystem>
39 GraphicalUi *GraphicalUi::_instance = 0;
40 QWidget *GraphicalUi::_mainWidget = 0;
41 QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
42 ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
43 ToolBarActionProvider *GraphicalUi::_toolBarActionProvider = 0;
44 UiStyle *GraphicalUi::_uiStyle = 0;
45 bool GraphicalUi::_onAllDesktops = false;
47 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent)
58 void GraphicalUi::init()
61 mainWidget()->installEventFilter(this);
66 ActionCollection *GraphicalUi::actionCollection(const QString &category, const QString &translatedCategory)
68 if (_actionCollections.contains(category))
69 return _actionCollections.value(category);
70 ActionCollection *coll = new ActionCollection(_mainWidget);
72 if (!translatedCategory.isEmpty())
73 coll->setProperty("Category", translatedCategory);
75 coll->setProperty("Category", category);
78 coll->addAssociatedWidget(_mainWidget);
79 _actionCollections.insert(category, coll);
84 QHash<QString, ActionCollection *> GraphicalUi::actionCollections()
86 return _actionCollections;
90 void GraphicalUi::loadShortcuts()
92 foreach(ActionCollection *coll, actionCollections())
97 void GraphicalUi::saveShortcuts()
101 foreach(ActionCollection *coll, actionCollections())
102 coll->writeSettings();
106 void GraphicalUi::setMainWidget(QWidget *widget)
108 _mainWidget = widget;
112 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider)
114 _contextMenuActionProvider = provider;
118 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider)
120 _toolBarActionProvider = provider;
124 void GraphicalUi::setUiStyle(UiStyle *style)
130 void GraphicalUi::disconnectedFromCore()
132 _contextMenuActionProvider->disconnectedFromCore();
133 _toolBarActionProvider->disconnectedFromCore();
134 AbstractUi::disconnectedFromCore();
138 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event)
141 if (obj == mainWidget() && event->type() == QEvent::ActivationChange) {
142 _dwTickCount = GetTickCount();
145 return AbstractUi::eventFilter(obj, event);
149 // NOTE: Window activation stuff seems to work just fine in Plasma 5 without requiring X11 hacks.
150 // TODO: Evaluate cleaning all this up once we can get rid of Qt4/KDE4
152 // Code taken from KStatusNotifierItem for handling minimize/restore
154 bool GraphicalUi::checkMainWidgetVisibility(bool perform)
157 // the problem is that we lose focus when the systray icon is activated
158 // and we don't know the former active window
159 // therefore we watch for activation event and use our stopwatch :)
160 if (GetTickCount() - _dwTickCount < 300) {
161 // we were active in the last 300ms -> hide it
163 minimizeRestore(false);
168 minimizeRestore(true);
172 #elif defined(HAVE_KDE4) && defined(Q_WS_X11)
173 KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
174 // mapped = visible (but possibly obscured)
175 bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
177 // - not mapped -> show, raise, focus
179 // - obscured -> raise, focus
180 // - not obscured -> hide
181 //info1.mappingState() != NET::Visible -> window on another desktop?
184 minimizeRestore(true);
188 QListIterator<WId> it(KWindowSystem::stackingOrder());
190 while (it.hasPrevious()) {
191 WId id = it.previous();
192 if (id == mainWidget()->winId())
195 KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
197 if (info2.mappingState() != NET::Visible)
198 continue; // not visible on current desktop -> ignore
200 if (!info2.geometry().intersects(mainWidget()->geometry()))
201 continue; // not obscuring the window -> ignore
203 if (!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
204 continue; // obscured by window kept above -> ignore
206 NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
207 | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
208 | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
210 if (type == NET::Dock || type == NET::TopMenu)
211 continue; // obscured by dock or topmenu -> ignore
214 KWindowSystem::raiseWindow(mainWidget()->winId());
215 KWindowSystem::activateWindow(mainWidget()->winId());
220 //not on current desktop?
221 if (!info1.isOnCurrentDesktop()) {
223 KWindowSystem::activateWindow(mainWidget()->winId());
228 minimizeRestore(false); // hide
233 if (!mainWidget()->isVisible() || mainWidget()->isMinimized() || !mainWidget()->isActiveWindow()) {
235 minimizeRestore(true);
240 minimizeRestore(false);
250 bool GraphicalUi::isMainWidgetVisible()
252 return !instance()->checkMainWidgetVisibility(false);
256 void GraphicalUi::minimizeRestore(bool show)
259 activateMainWidget();
265 void GraphicalUi::activateMainWidget()
269 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
270 if (_onAllDesktops) {
271 KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
274 KWindowSystem::setCurrentDesktop(info.desktop());
277 mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
278 mainWidget()->show();
279 mainWidget()->raise();
280 KWindowSystem::raiseWindow(mainWidget()->winId());
281 KWindowSystem::activateWindow(mainWidget()->winId());
283 mainWidget()->show();
284 KWindowSystem::raiseWindow(mainWidget()->winId());
285 KWindowSystem::forceActiveWindow(mainWidget()->winId());
288 #else /* HAVE_KDE4 */
291 // Bypass focus stealing prevention
292 QX11Info::setAppUserTime(QX11Info::appTime());
295 if (mainWidget()->windowState() & Qt::WindowMinimized) {
297 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
300 // this does not actually work on all platforms... and causes more evil than good
301 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
302 mainWidget()->show();
303 mainWidget()->raise();
304 mainWidget()->activateWindow();
306 #endif /* HAVE_KDE4 */
310 void GraphicalUi::hideMainWidget()
312 #if defined(HAVE_KDE4) && defined(Q_WS_X11)
313 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
314 _onAllDesktops = info.onAllDesktops();
317 if (instance()->isHidingMainWidgetAllowed())
318 mainWidget()->hide();
322 void GraphicalUi::toggleMainWidget()
324 instance()->checkMainWidgetVisibility(true);