1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 "contextmenuactionprovider.h"
28 #include "toolbaractionprovider.h"
29 #include "uisettings.h"
35 QWidget* GraphicalUi::_mainWidget = nullptr;
36 QHash<QString, ActionCollection*> GraphicalUi::_actionCollections;
37 ContextMenuActionProvider* GraphicalUi::_contextMenuActionProvider = nullptr;
38 ToolBarActionProvider* GraphicalUi::_toolBarActionProvider = nullptr;
39 UiStyle* GraphicalUi::_uiStyle = nullptr;
41 GraphicalUi::GraphicalUi(QObject* parent)
43 , Singleton<GraphicalUi>(this)
45 Q_INIT_RESOURCE(pics);
46 Q_INIT_RESOURCE(hicolor_icons);
48 Q_INIT_RESOURCE(icons);
49 Q_INIT_RESOURCE(iconthemes);
56 GetFrontProcess(&_procNum);
60 void GraphicalUi::init()
63 mainWidget()->installEventFilter(this);
67 ActionCollection* GraphicalUi::actionCollection(const QString& category, const QString& translatedCategory)
69 if (_actionCollections.contains(category))
70 return _actionCollections.value(category);
71 auto* coll = new ActionCollection(_mainWidget);
73 if (!translatedCategory.isEmpty())
74 coll->setProperty("Category", translatedCategory);
76 coll->setProperty("Category", category);
79 coll->addAssociatedWidget(_mainWidget);
80 _actionCollections.insert(category, coll);
84 QHash<QString, ActionCollection*> GraphicalUi::actionCollections()
86 return _actionCollections;
89 void GraphicalUi::loadShortcuts()
91 foreach (ActionCollection* coll, actionCollections())
95 void GraphicalUi::saveShortcuts()
99 foreach (ActionCollection* coll, actionCollections())
100 coll->writeSettings();
103 void GraphicalUi::setMainWidget(QWidget* widget)
105 _mainWidget = widget;
108 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider* provider)
110 _contextMenuActionProvider = provider;
113 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider* provider)
115 _toolBarActionProvider = provider;
118 void GraphicalUi::setUiStyle(UiStyle* style)
123 void GraphicalUi::disconnectedFromCore()
125 _contextMenuActionProvider->disconnectedFromCore();
126 _toolBarActionProvider->disconnectedFromCore();
127 AbstractUi::disconnectedFromCore();
130 bool GraphicalUi::eventFilter(QObject* obj, QEvent* event)
133 if (obj == mainWidget() && event->type() == QEvent::ActivationChange) {
134 _dwTickCount = GetTickCount();
137 return AbstractUi::eventFilter(obj, event);
140 // NOTE: Window activation stuff seems to work just fine in Plasma 5 without requiring X11 hacks.
141 // TODO: Evaluate cleaning all this up once we can get rid of Qt4/KDE4
143 // Code taken from KStatusNotifierItem for handling minimize/restore
145 bool GraphicalUi::checkMainWidgetVisibility(bool perform)
147 bool needsActivation{true};
150 // the problem is that we lose focus when the systray icon is activated
151 // and we don't know the former active window
152 // therefore we watch for activation event and use our stopwatch :)
153 if (GetTickCount() - _dwTickCount < 300) {
154 // we were active in the last 300ms -> hide it
155 needsActivation = false;
158 if (mainWidget()->isVisible() && !mainWidget()->isMinimized() && mainWidget()->isActiveWindow()) {
159 needsActivation = false;
164 minimizeRestore(needsActivation);
166 return needsActivation;
169 bool GraphicalUi::isMainWidgetVisible()
171 return !instance()->checkMainWidgetVisibility(false);
174 void GraphicalUi::minimizeRestore(bool show)
177 activateMainWidget();
182 void GraphicalUi::activateMainWidget()
185 // Bypass focus stealing prevention
186 QX11Info::setAppUserTime(QX11Info::appTime());
189 if (mainWidget()->windowState() & Qt::WindowMinimized) {
191 mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
194 // this does not actually work on all platforms... and causes more evil than good
195 // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
197 SetFrontProcess(&instance()->_procNum);
199 mainWidget()->show();
200 mainWidget()->raise();
201 mainWidget()->activateWindow();
205 void GraphicalUi::hideMainWidget()
207 if (instance()->isHidingMainWidgetAllowed())
209 ShowHideProcess(&instance()->_procNum, false);
211 mainWidget()->hide();
215 void GraphicalUi::toggleMainWidget()
217 instance()->checkMainWidgetVisibility(true);