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 QWidget *GraphicalUi::_mainWidget = 0;
36 QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
37 ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
38 ToolBarActionProvider *GraphicalUi::_toolBarActionProvider = 0;
39 UiStyle *GraphicalUi::_uiStyle = 0;
42 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent), Singleton<GraphicalUi>(this)
44 Q_INIT_RESOURCE(pics);
45 Q_INIT_RESOURCE(hicolor_icons);
47 Q_INIT_RESOURCE(icons);
48 Q_INIT_RESOURCE(iconthemes);
55 GetFrontProcess(&_procNum);
60 void GraphicalUi::init()
63 mainWidget()->installEventFilter(this);
68 ActionCollection *GraphicalUi::actionCollection(const QString &category, const QString &translatedCategory)
70 if (_actionCollections.contains(category))
71 return _actionCollections.value(category);
72 ActionCollection *coll = new ActionCollection(_mainWidget);
74 if (!translatedCategory.isEmpty())
75 coll->setProperty("Category", translatedCategory);
77 coll->setProperty("Category", category);
80 coll->addAssociatedWidget(_mainWidget);
81 _actionCollections.insert(category, coll);
86 QHash<QString, ActionCollection *> GraphicalUi::actionCollections()
88 return _actionCollections;
92 void GraphicalUi::loadShortcuts()
94 foreach(ActionCollection *coll, actionCollections())
99 void GraphicalUi::saveShortcuts()
103 foreach(ActionCollection *coll, actionCollections())
104 coll->writeSettings();
108 void GraphicalUi::setMainWidget(QWidget *widget)
110 _mainWidget = widget;
114 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider)
116 _contextMenuActionProvider = provider;
120 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider)
122 _toolBarActionProvider = provider;
126 void GraphicalUi::setUiStyle(UiStyle *style)
132 void GraphicalUi::disconnectedFromCore()
134 _contextMenuActionProvider->disconnectedFromCore();
135 _toolBarActionProvider->disconnectedFromCore();
136 AbstractUi::disconnectedFromCore();
140 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event)
143 if (obj == mainWidget() && event->type() == QEvent::ActivationChange) {
144 _dwTickCount = GetTickCount();
147 return AbstractUi::eventFilter(obj, event);
151 // NOTE: Window activation stuff seems to work just fine in Plasma 5 without requiring X11 hacks.
152 // TODO: Evaluate cleaning all this up once we can get rid of Qt4/KDE4
154 // Code taken from KStatusNotifierItem for handling minimize/restore
156 bool GraphicalUi::checkMainWidgetVisibility(bool perform)
159 // the problem is that we lose focus when the systray icon is activated
160 // and we don't know the former active window
161 // therefore we watch for activation event and use our stopwatch :)
162 if (GetTickCount() - _dwTickCount < 300) {
163 // we were active in the last 300ms -> hide it
165 minimizeRestore(false);
170 minimizeRestore(true);
176 if (!mainWidget()->isVisible() || mainWidget()->isMinimized() || !mainWidget()->isActiveWindow()) {
178 minimizeRestore(true);
183 minimizeRestore(false);
193 bool GraphicalUi::isMainWidgetVisible()
195 return !instance()->checkMainWidgetVisibility(false);
199 void GraphicalUi::minimizeRestore(bool show)
202 activateMainWidget();
208 void GraphicalUi::activateMainWidget()
211 // Bypass focus stealing prevention
212 QX11Info::setAppUserTime(QX11Info::appTime());
215 if (mainWidget()->windowState() & Qt::WindowMinimized) {
217 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
220 // this does not actually work on all platforms... and causes more evil than good
221 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
223 SetFrontProcess(&instance()->_procNum);
225 mainWidget()->show();
226 mainWidget()->raise();
227 mainWidget()->activateWindow();
232 void GraphicalUi::hideMainWidget()
234 if (instance()->isHidingMainWidgetAllowed())
236 ShowHideProcess(&instance()->_procNum, false);
238 mainWidget()->hide();
243 void GraphicalUi::toggleMainWidget()
245 instance()->checkMainWidgetVisibility(true);