Cleanup allowing for tags to be available at later points, adds TAGMSG
[quassel.git] / src / uisupport / graphicalui.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "uisupport-export.h"
24
25 #include "abstractui.h"
26 #include "singleton.h"
27
28 class ActionCollection;
29 class ContextMenuActionProvider;
30 class ToolBarActionProvider;
31 class UiStyle;
32
33 #ifdef Q_OS_WIN
34 #    include <windows.h>
35 #endif
36 #ifdef Q_OS_MAC
37 #    include <Carbon/Carbon.h>
38 #endif
39
40 class UISUPPORT_EXPORT GraphicalUi : public AbstractUi, protected Singleton<GraphicalUi>
41 {
42     Q_OBJECT
43
44 public:
45     //! Access global ActionCollections.
46     /** These ActionCollections are associated with the main window, i.e. they contain global
47      *  actions (and thus, shortcuts). Widgets providing application-wide shortcuts should
48      *  create appropriate Action objects using GraphicalUi::actionCollection(cat)->add\<Action\>().
49      *  @param category The category (default: "General")
50      */
51     static ActionCollection* actionCollection(const QString& category = "General", const QString& translatedCategory = QString());
52     static QHash<QString, ActionCollection*> actionCollections();
53
54     //! Load custom shortcuts from ShortcutSettings
55     /** @note This method assumes that all configurable actions are defined when being called
56      */
57     static void loadShortcuts();
58
59     //! Save custom shortcuts to ShortcutSettings
60     static void saveShortcuts();
61
62     inline static ContextMenuActionProvider* contextMenuActionProvider();
63     inline static ToolBarActionProvider* toolBarActionProvider();
64     inline static UiStyle* uiStyle();
65     inline static QWidget* mainWidget();
66
67     //! Force the main widget to the front and focus it (may not work in all window systems)
68     static void activateMainWidget();
69
70     //! Hide main widget (storing the current desktop if possible)
71     static void hideMainWidget();
72
73     //! Toggle main widget
74     static void toggleMainWidget();
75
76     //! Check if the main widget if (fully, in KDE) visible
77     static bool isMainWidgetVisible();
78
79 protected:
80     GraphicalUi(QObject* parent = nullptr);
81     void init() override;
82
83     //! This is the widget we associate global actions with, typically the main window
84     void setMainWidget(QWidget*);
85
86     //! Check if the mainWidget is visible and optionally toggle its visibility
87     /** With KDE integration, we check if the mainWidget is (partially) obscured in order to determine if
88      *  it should be activated or hidden. Without KDE, we need to resort to checking the current state
89      *  as Qt knows it, ignoring windows covering it.
90      *  @param  performToggle If true, toggle the window's state in addition to checking visibility
91      *  @return True, if the window is currently *not* visible (needs activation)
92      */
93     bool checkMainWidgetVisibility(bool performToggle);
94
95     //! Minimize to or restore main widget
96     virtual void minimizeRestore(bool show);
97
98     //! Whether it is allowed to hide the mainWidget
99     /** The default implementation returns false, meaning that we won't hide the mainWidget even
100      *  if requested. This is to prevent hiding in case we don't have a tray icon to restore from.
101      */
102     virtual inline bool isHidingMainWidgetAllowed() const;
103
104     void setContextMenuActionProvider(ContextMenuActionProvider*);
105     void setToolBarActionProvider(ToolBarActionProvider*);
106     void setUiStyle(UiStyle*);
107
108     bool eventFilter(QObject* obj, QEvent* event) override;
109
110 protected slots:
111     void disconnectedFromCore() override;
112
113 private:
114     static QWidget* _mainWidget;
115     static QHash<QString, ActionCollection*> _actionCollections;
116     static ContextMenuActionProvider* _contextMenuActionProvider;
117     static ToolBarActionProvider* _toolBarActionProvider;
118     static UiStyle* _uiStyle;
119
120 #ifdef Q_OS_WIN
121     DWORD _dwTickCount;
122 #endif
123 #ifdef Q_OS_MAC
124     ProcessSerialNumber _procNum;
125 #endif
126 };
127
128 // inlines
129
130 ContextMenuActionProvider* GraphicalUi::contextMenuActionProvider()
131 {
132     return _contextMenuActionProvider;
133 }
134
135 ToolBarActionProvider* GraphicalUi::toolBarActionProvider()
136 {
137     return _toolBarActionProvider;
138 }
139
140 UiStyle* GraphicalUi::uiStyle()
141 {
142     return _uiStyle;
143 }
144
145 QWidget* GraphicalUi::mainWidget()
146 {
147     return _mainWidget;
148 }
149
150 bool GraphicalUi::isHidingMainWidgetAllowed() const
151 {
152     return false;
153 }