1 /***************************************************************************
2 * Copyright (C) 2005-2018 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)
56 GetFrontProcess(&_procNum);
61 void GraphicalUi::init()
64 mainWidget()->installEventFilter(this);
69 ActionCollection *GraphicalUi::actionCollection(const QString &category, const QString &translatedCategory)
71 if (_actionCollections.contains(category))
72 return _actionCollections.value(category);
73 ActionCollection *coll = new ActionCollection(_mainWidget);
75 if (!translatedCategory.isEmpty())
76 coll->setProperty("Category", translatedCategory);
78 coll->setProperty("Category", category);
81 coll->addAssociatedWidget(_mainWidget);
82 _actionCollections.insert(category, coll);
87 QHash<QString, ActionCollection *> GraphicalUi::actionCollections()
89 return _actionCollections;
93 void GraphicalUi::loadShortcuts()
95 foreach(ActionCollection *coll, actionCollections())
100 void GraphicalUi::saveShortcuts()
104 foreach(ActionCollection *coll, actionCollections())
105 coll->writeSettings();
109 void GraphicalUi::setMainWidget(QWidget *widget)
111 _mainWidget = widget;
115 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider)
117 _contextMenuActionProvider = provider;
121 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider)
123 _toolBarActionProvider = provider;
127 void GraphicalUi::setUiStyle(UiStyle *style)
133 void GraphicalUi::disconnectedFromCore()
135 _contextMenuActionProvider->disconnectedFromCore();
136 _toolBarActionProvider->disconnectedFromCore();
137 AbstractUi::disconnectedFromCore();
141 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event)
144 if (obj == mainWidget() && event->type() == QEvent::ActivationChange) {
145 _dwTickCount = GetTickCount();
148 return AbstractUi::eventFilter(obj, event);
152 // NOTE: Window activation stuff seems to work just fine in Plasma 5 without requiring X11 hacks.
153 // TODO: Evaluate cleaning all this up once we can get rid of Qt4/KDE4
155 // Code taken from KStatusNotifierItem for handling minimize/restore
157 bool GraphicalUi::checkMainWidgetVisibility(bool perform)
160 // the problem is that we lose focus when the systray icon is activated
161 // and we don't know the former active window
162 // therefore we watch for activation event and use our stopwatch :)
163 if (GetTickCount() - _dwTickCount < 300) {
164 // we were active in the last 300ms -> hide it
166 minimizeRestore(false);
171 minimizeRestore(true);
175 #elif defined(HAVE_KDE4) && defined(Q_WS_X11)
176 KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
177 // mapped = visible (but possibly obscured)
178 bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
180 // - not mapped -> show, raise, focus
182 // - obscured -> raise, focus
183 // - not obscured -> hide
184 //info1.mappingState() != NET::Visible -> window on another desktop?
187 minimizeRestore(true);
191 QListIterator<WId> it(KWindowSystem::stackingOrder());
193 while (it.hasPrevious()) {
194 WId id = it.previous();
195 if (id == mainWidget()->winId())
198 KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
200 if (info2.mappingState() != NET::Visible)
201 continue; // not visible on current desktop -> ignore
203 if (!info2.geometry().intersects(mainWidget()->geometry()))
204 continue; // not obscuring the window -> ignore
206 if (!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
207 continue; // obscured by window kept above -> ignore
209 NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
210 | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
211 | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
213 if (type == NET::Dock || type == NET::TopMenu)
214 continue; // obscured by dock or topmenu -> ignore
217 KWindowSystem::raiseWindow(mainWidget()->winId());
218 KWindowSystem::activateWindow(mainWidget()->winId());
223 //not on current desktop?
224 if (!info1.isOnCurrentDesktop()) {
226 KWindowSystem::activateWindow(mainWidget()->winId());
231 minimizeRestore(false); // hide
236 if (!mainWidget()->isVisible() || mainWidget()->isMinimized() || !mainWidget()->isActiveWindow()) {
238 minimizeRestore(true);
243 minimizeRestore(false);
253 bool GraphicalUi::isMainWidgetVisible()
255 return !instance()->checkMainWidgetVisibility(false);
259 void GraphicalUi::minimizeRestore(bool show)
262 activateMainWidget();
268 void GraphicalUi::activateMainWidget()
272 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
273 if (_onAllDesktops) {
274 KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
277 KWindowSystem::setCurrentDesktop(info.desktop());
280 mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
281 mainWidget()->show();
282 mainWidget()->raise();
283 KWindowSystem::raiseWindow(mainWidget()->winId());
284 KWindowSystem::activateWindow(mainWidget()->winId());
286 mainWidget()->show();
287 KWindowSystem::raiseWindow(mainWidget()->winId());
288 KWindowSystem::forceActiveWindow(mainWidget()->winId());
291 #else /* HAVE_KDE4 */
294 // Bypass focus stealing prevention
295 QX11Info::setAppUserTime(QX11Info::appTime());
298 if (mainWidget()->windowState() & Qt::WindowMinimized) {
300 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
303 // this does not actually work on all platforms... and causes more evil than good
304 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
306 SetFrontProcess(&instance()->_procNum);
308 mainWidget()->show();
309 mainWidget()->raise();
310 mainWidget()->activateWindow();
313 #endif /* HAVE_KDE4 */
317 void GraphicalUi::hideMainWidget()
319 #if defined(HAVE_KDE4) && defined(Q_WS_X11)
320 KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
321 _onAllDesktops = info.onAllDesktops();
324 if (instance()->isHidingMainWidgetAllowed())
326 ShowHideProcess(&instance()->_procNum, false);
328 mainWidget()->hide();
333 void GraphicalUi::toggleMainWidget()
335 instance()->checkMainWidgetVisibility(true);