KDDockWidgets API Documentation 2.1
Loading...
Searching...
No Matches
qtwidgets/Platform.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: SĂ©rgio Martins <sergio.martins@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
8
9 Contact KDAB at <info@kdab.com> for commercial licensing options.
10*/
11
12#include "Platform.h"
13#include "kddockwidgets/KDDockWidgets.h"
14
15#include "Window_p.h"
16#include "DebugWindow.h"
17#include "views/ViewWrapper_p.h"
18#include "views/View.h"
19#include "core/Platform_p.h"
20#include "ViewFactory.h"
21
22#include <QScreen>
23#include <QApplication>
24#include <QLineEdit>
25#include <QAbstractButton>
26#include <QStyleFactory>
27
28#include <memory.h>
29
30#if defined(KDDOCKWIDGETS_STATICLIB) || defined(QT_STATIC)
31static void initResources()
32{
33 Q_INIT_RESOURCE(kddockwidgets_resources);
34}
35#endif
36
37using namespace KDDockWidgets;
38using namespace KDDockWidgets::QtWidgets;
39
40// clazy:excludeall=missing-qobject-macro
41
42class Platform::GlobalEventFilter : public QObject
43{
44public:
45 GlobalEventFilter()
46 {
47 if (qGuiApp)
48 qGuiApp->installEventFilter(this);
49 }
50
51 bool eventFilter(QObject *o, QEvent *ev) override
52 {
53 // We're only interested in window activation events
55 return false;
56
57 // QWindow is not receiving it
58 if (!o->isWidgetType())
59 return false;
60
61 if (auto w = qobject_cast<QWidget *>(o)) {
62 if (w->isWindow()) {
63 if (ev->type() == QEvent::WindowActivate) {
64 Platform::instance()->d->windowActivated.emit(
65 QtWidgets::ViewWrapper::create(w));
66 }
67
68 if (ev->type() == QEvent::WindowDeactivate) {
69 Platform::instance()->d->windowDeactivated.emit(
70 QtWidgets::ViewWrapper::create(w));
71 }
72 }
73 }
74
75 return false;
76 }
77
78 ~GlobalEventFilter() override;
79};
80
81Platform::GlobalEventFilter::~GlobalEventFilter() = default;
82
84 : m_globalEventFilter(new GlobalEventFilter())
85{
86 init();
87}
88
90{
91#if defined(KDDOCKWIDGETS_STATICLIB) || defined(QT_STATIC)
92 initResources();
93#endif
94
95#ifdef DOCKS_DEVELOPER_MODE
96 if (qEnvironmentVariableIntValue("KDDOCKWIDGETS_SHOW_DEBUG_WINDOW") == 1) {
97 auto dv = new Debug::DebugWindow();
98 dv->show();
99 }
100#endif
101
102 qGuiApp->connect(qApp, &QGuiApplication::focusObjectChanged, qApp, [this](QObject *obj) {
103 d->focusedViewChanged.emit(QtWidgets::ViewWrapper::create(obj));
104 });
105}
106
111
112const char *Platform::name() const
113{
114 return "qtwidgets";
115}
116
118{
119 return qApp->activePopupWidget() != nullptr;
120}
121
122std::shared_ptr<Core::View> Platform::qobjectAsView(QObject *obj) const
123{
124 return QtWidgets::ViewWrapper::create(obj);
125}
126
127std::shared_ptr<Core::Window> Platform::windowFromQWindow(QWindow *qwindow) const
128{
129 Q_ASSERT(qwindow);
130 return std::shared_ptr<Core::Window>(new Window(qwindow));
131}
132
137
138Core::Window::Ptr Platform::windowAt(QPoint globalPos) const
139{
140 if (auto qwindow = qGuiApp->QGuiApplication::topLevelAt(globalPos)) {
141 auto window = new Window(qwindow);
142 return Core::Window::Ptr(window);
143 }
144
145 return {};
146}
147
149{
150 if (auto widget = QtCommon::View_qt::asQWidget(view)) {
151 if (QWindow *qtwindow = widget->window()->windowHandle())
152 return screenNumberForQWindow(qtwindow);
153 }
154
155 return -1;
156}
157
159{
160 if (auto widget = QtCommon::View_qt::asQWidget(view)) {
161 if (QScreen *screen = widget->screen()) {
162 return screen->size();
163 }
164 }
165
166 return {};
167}
168
173
175{
176 return new QtWidgets::View<QWidget>(controller, Core::ViewType::None,
177 QtCommon::View_qt::asQWidget(parent));
178}
179
181{
182 // For QtWidgets we just use QWidget::grabMouse()
183 return false;
184}
185
187{
188 QWidget *widget = qApp->widgetAt(globalPos);
189 if (!widget)
190 return false;
191
192 // User might have a line edit on the toolbar.
193 // Not so elegant fix, we should make the user's tabbar implement some virtual method...
194 return qobject_cast<QAbstractButton *>(widget) || qobject_cast<QLineEdit *>(widget);
195}
196
198{
199 if (QWidget *grabber = QWidget::mouseGrabber())
200 grabber->releaseMouse();
201}
202
203#ifdef DOCKS_TESTING_METHODS
204
205inline QCoreApplication *createCoreApplication(int &argc, char **argv, bool defaultToOffscreenQPA)
206{
207 if (defaultToOffscreenQPA)
208 QtCommon::Platform_qt::maybeSetOffscreenQPA(argc, argv);
209
210 return new QApplication(argc, argv);
211}
212
213Platform::Platform(int &argc, char **argv, bool defaultToOffscreenQPA)
214 : Platform_qt(createCoreApplication(argc, argv, defaultToOffscreenQPA))
215 , m_globalEventFilter(new Platform::GlobalEventFilter())
216{
217 qputenv("KDDOCKWIDGETS_SHOW_DEBUG_WINDOW", "");
218 qApp->setStyle(QStyleFactory::create(QStringLiteral("fusion")));
219 init();
220}
221
222#endif
Window to show debug information. Used for debugging only, for apps that don't support GammaRay.
static Platform * instance()
Returns the platform singleton.
A factory class for allowing the user to customize some internal views. This is optional,...
implements functions specific to a particular platform A platform can be for example qtwidgets,...
Core::View * createView(Core::Controller *controller, Core::View *parent=nullptr) const override
Create an empty view For Qt this would just returns a empty QWidget or QQuickItem other frontends can...
QSize screenSizeFor(Core::View *) const override
Returns the size of the screen where this view is in.
std::shared_ptr< Core::View > qobjectAsView(QObject *) const override
Returns the specified QObject casted to View Nullptr if it's not a view.
bool inDisallowedDragView(QPoint globalPos) const override
Core::ViewFactory * createDefaultViewFactory() override
Creates and returns the default ViewFactory.
const char * name() const override
Returns the name of the platform, only "qtwidgets" and "qtquick".
void ungrabMouse() override
Releases the mouse grab, if any.
bool usesFallbackMouseGrabber() const override
Return whether we use the global event filter based mouse grabber.
bool hasActivePopup() const override
Returns whether a popup is open Usually not needed to override. Investigate further in case side bars...
std::shared_ptr< Core::Window > windowAt(QPoint globalPos) const override
int screenNumberForView(Core::View *) const override
Returns the screen index for the specified view or window. It's up to the platform to decide how scre...
GlobalEventFilter *const m_globalEventFilter
std::shared_ptr< Core::Window > windowFromQWindow(QWindow *) const override
The default ViewFactory for QtWidgets frontend.
Class to abstract QAction, so code still works with QtQuick and Flutter.
QEvent::Type type() const const
void focusObjectChanged(QObject *focusObject)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
bool isWidgetType() const const
QStyle * create(const QString &key)
A factory class for allowing the user to customize some internal widgets.
QWidget * mouseGrabber()

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8