Make ActionCollection load and save custom shortcuts
[quassel.git] / src / uisupport / graphicalui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This contains code from KStatusNotifierItem, part of the KDE libs     *
6  *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
7  *                                                                         *
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.                                           *
12  *                                                                         *
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.                          *
17  *                                                                         *
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  ***************************************************************************/
23
24 #include "graphicalui.h"
25
26 #include "actioncollection.h"
27 #include "uisettings.h"
28 #include "contextmenuactionprovider.h"
29 #include "toolbaractionprovider.h"
30
31 #ifdef Q_WS_X11
32 #  include <QX11Info>
33 #endif
34 #ifdef HAVE_KDE
35 #  include <KWindowInfo>
36 #  include <KWindowSystem>
37 #endif
38
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;
46
47 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent) {
48   Q_ASSERT(!_instance);
49   _instance = this;
50
51 #ifdef Q_WS_WIN
52   _dwTickCount = 0;
53 #endif
54 }
55
56 void GraphicalUi::init() {
57 #ifdef Q_WS_WIN
58   mainWidget()->installEventFilter(this);
59 #endif
60 }
61
62 ActionCollection *GraphicalUi::actionCollection(const QString &category) {
63   if(_actionCollections.contains(category))
64     return _actionCollections.value(category);
65   ActionCollection *coll = new ActionCollection(_mainWidget);
66   coll->setProperty("Category", category);
67   if(_mainWidget)
68     coll->addAssociatedWidget(_mainWidget);
69   _actionCollections.insert(category, coll);
70   return coll;
71 }
72
73 QHash<QString, ActionCollection *> GraphicalUi::actionCollections() {
74   return _actionCollections;
75 }
76
77 void GraphicalUi::loadShortcuts() {
78   foreach(ActionCollection *coll, actionCollections())
79     coll->readSettings();
80 }
81
82 void GraphicalUi::saveShortcuts() {
83   ShortcutSettings s;
84   s.clear();
85   foreach(ActionCollection *coll, actionCollections())
86     coll->writeSettings();
87 }
88
89 void GraphicalUi::setMainWidget(QWidget *widget) {
90   _mainWidget = widget;
91 }
92
93 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider) {
94   _contextMenuActionProvider = provider;
95 }
96
97 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider) {
98   _toolBarActionProvider = provider;
99 }
100
101 void GraphicalUi::setUiStyle(UiStyle *style) {
102   _uiStyle = style;
103 }
104
105 void GraphicalUi::disconnectedFromCore() {
106   _contextMenuActionProvider->disconnectedFromCore();
107   _toolBarActionProvider->disconnectedFromCore();
108   AbstractUi::disconnectedFromCore();
109 }
110
111 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
112 #ifdef Q_WS_WIN
113   if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
114     _dwTickCount = GetTickCount();
115   }
116 #endif
117   return AbstractUi::eventFilter(obj, event);
118 }
119
120 // Code taken from KStatusNotifierItem for handling minimize/restore
121
122 bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
123 #ifdef Q_WS_WIN
124   // the problem is that we lose focus when the systray icon is activated
125   // and we don't know the former active window
126   // therefore we watch for activation event and use our stopwatch :)
127   if(GetTickCount() - _dwTickCount < 300) {
128     // we were active in the last 300ms -> hide it
129     if(perform)
130       minimizeRestore(false);
131     return false;
132   } else {
133     if(perform)
134       minimizeRestore(true);
135     return true;
136   }
137
138 #elif defined(HAVE_KDE) && defined(Q_WS_X11)
139   KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
140   // mapped = visible (but possibly obscured)
141   bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
142
143   //    - not mapped -> show, raise, focus
144   //    - mapped
145   //        - obscured -> raise, focus
146   //        - not obscured -> hide
147   //info1.mappingState() != NET::Visible -> window on another desktop?
148   if(!mapped) {
149     if(perform)
150       minimizeRestore(true);
151     return true;
152
153   } else {
154     QListIterator< WId > it (KWindowSystem::stackingOrder());
155     it.toBack();
156     while(it.hasPrevious()) {
157       WId id = it.previous();
158       if(id == mainWidget()->winId())
159         break;
160
161       KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
162
163       if(info2.mappingState() != NET::Visible)
164         continue; // not visible on current desktop -> ignore
165
166       if(!info2.geometry().intersects(mainWidget()->geometry()))
167         continue; // not obscuring the window -> ignore
168
169       if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
170         continue; // obscured by window kept above -> ignore
171
172       NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
173                                               | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
174                                               | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
175
176       if(type == NET::Dock || type == NET::TopMenu)
177         continue; // obscured by dock or topmenu -> ignore
178
179       if(perform) {
180         KWindowSystem::raiseWindow(mainWidget()->winId());
181         KWindowSystem::activateWindow(mainWidget()->winId());
182       }
183       return true;
184     }
185
186     //not on current desktop?
187     if(!info1.isOnCurrentDesktop()) {
188       if(perform)
189         KWindowSystem::activateWindow(mainWidget()->winId());
190       return true;
191     }
192
193     if(perform)
194       minimizeRestore(false); // hide
195     return false;
196   }
197 #else
198
199   if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
200     if(perform)
201       minimizeRestore(true);
202     return true;
203   } else {
204     if(perform)
205       minimizeRestore(false);
206     return false;
207   }
208
209 #endif
210
211   return true;
212 }
213
214 bool GraphicalUi::isMainWidgetVisible() {
215   return !instance()->checkMainWidgetVisibility(false);
216 }
217
218 void GraphicalUi::minimizeRestore(bool show) {
219   if(show)
220     activateMainWidget();
221   else
222     hideMainWidget();
223 }
224
225 void GraphicalUi::activateMainWidget() {
226 #ifdef HAVE_KDE
227 #  ifdef Q_WS_X11
228     KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
229     if(_onAllDesktops) {
230       KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
231     } else {
232       KWindowSystem::setCurrentDesktop(info.desktop());
233     }
234
235     mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
236     mainWidget()->show();
237     mainWidget()->raise();
238     KWindowSystem::raiseWindow(mainWidget()->winId());
239     KWindowSystem::activateWindow(mainWidget()->winId());
240 #  else
241     mainWidget()->show();
242     KWindowSystem::raiseWindow(mainWidget()->winId());
243     KWindowSystem::forceActiveWindow(mainWidget()->winId());
244 #  endif
245
246 #else /* HAVE_KDE */
247
248 #ifdef Q_WS_X11
249   // Bypass focus stealing prevention
250   QX11Info::setAppUserTime(QX11Info::appTime());
251 #endif
252
253   if(mainWidget()->windowState() & Qt::WindowMinimized) {
254     // restore
255     mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
256   }
257
258   // this does not actually work on all platforms... and causes more evil than good
259   // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
260   mainWidget()->show();
261   mainWidget()->raise();
262   mainWidget()->activateWindow();
263
264 #endif /* HAVE_KDE */
265 }
266
267 void GraphicalUi::hideMainWidget() {
268
269 #if defined(HAVE_KDE) && defined(Q_WS_X11)
270   KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
271   _onAllDesktops = info.onAllDesktops();
272 #endif
273
274   if(instance()->isHidingMainWidgetAllowed())
275     mainWidget()->hide();
276 }
277
278 void GraphicalUi::toggleMainWidget() {
279   instance()->checkMainWidgetVisibility(true);
280 }