KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
core/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 "core/Platform.h"
13#include "core/Platform_p.h"
14#include "core/Logging_p.h"
15#include "core/Window_p.h"
16#include "core/Utils_p.h"
18#include "core/Separator.h"
19#include "core/layouting/LayoutingSeparator_p.h"
20#include <core/DockRegistry.h>
21
22#ifdef KDDW_FRONTEND_QTWIDGETS
23#include "qtwidgets/Platform.h"
24#endif
25
26#ifdef KDDW_FRONTEND_QTQUICK
27#include "qtquick/Platform.h"
28#endif
29
30#ifdef KDDW_FRONTEND_FLUTTER
31#include "flutter/Platform.h"
32#endif
33
34#include "Config.h"
35#include "core/layouting/Item_p.h"
36#include "core/Screen_p.h"
37
38#include "QtCompat_p.h"
39
40#include "kdbindings/signal.h"
41
42#include <iostream>
43#include <fstream>
44#include <cstdlib>
45
46using namespace KDDockWidgets;
47using namespace KDDockWidgets::Core;
48
49static Platform *s_platform = nullptr;
50
52
54 : d(new Private())
55{
56 assert(!s_platform);
57 s_platform = this;
58
59 Item::setDumpScreenInfoFunc([] {
60 const auto screens = Platform::instance()->screens();
61 for (const auto &screen : screens) {
62 std::cerr << "Screen: " << screen->geometry() << "; " << screen->availableGeometry()
63 << "; drp=" << screen->devicePixelRatio() << "\n";
64 }
65 });
66}
67
69{
70 Item::setDumpScreenInfoFunc(nullptr);
71 s_platform = nullptr;
72 delete d;
73}
74
75Platform::Private::Private()
76{
80 Core::Item::setCreateSeparatorFunc([](Core::LayoutingHost *host, Qt::Orientation orientation, Core::ItemBoxContainer *container) -> Core::LayoutingSeparator * {
81 return (new Core::Separator(host, orientation, container))->asLayoutingSeparator();
82 });
83}
84
86{
87 if (!s_platform) {
88 static bool guard = false;
89 if (guard)
90 return nullptr;
91 guard = true;
92
93 // For convenience, if there's only 1 frontend supported then don't
94 // require the user to call initFrontend(), just do it here.
95 const auto types = Platform::frontendTypes();
96 if (types.size() == 1)
98 guard = false;
99 }
100
101 return s_platform;
102}
103
105{
106 return s_platform != nullptr;
107}
108
110{
111 return false;
112}
113
115{
116 return strcmp(name(), "qtwidgets") == 0;
117}
118
120{
121 return strcmp(name(), "qtquick") == 0;
122}
123
124bool Platform::isQt() const
125{
126 static const bool is = isQtWidgets() || isQtQuick();
127 return is;
128}
129
131{
132 const int userRequestedDistance = Config::self().startDragDistance();
133 if (userRequestedDistance > -1)
134 return userRequestedDistance;
135
136 return startDragDistance_impl();
137}
138
140{
141 // Override this method in derived classes for some different value if needed
142 return 4;
143}
144
146std::vector<KDDockWidgets::FrontendType> Platform::frontendTypes()
147{
148 std::vector<KDDockWidgets::FrontendType> types;
149
150#ifdef DOCKS_DEVELOPER_MODE
151 // During development it's useful to quickly run tests only on the frontend we're developing.
152 // The developer can set, for example, KDDW_TEST_FRONTEND=2 to run only the QtQuick tests
153 bool ok = false;
154 const int frontendId = envVarIntValue("KDDW_TEST_FRONTEND", /*by-ref*/ ok);
155 if (ok) {
156 types.push_back(FrontendType(frontendId));
157 return types;
158 }
159
160#endif
161
162#ifdef KDDW_FRONTEND_QTQUICK
163 types.push_back(FrontendType::QtQuick);
164#endif
165
166#ifdef KDDW_FRONTEND_QTWIDGETS
167 types.push_back(FrontendType::QtWidgets);
168#endif
169
170#ifdef KDDW_FRONTEND_FLUTTER
171 types.push_back(FrontendType::Flutter);
172#endif
173
174 return types;
175}
176
177/*static */
179{
180 return s_platform != nullptr;
181}
182
183#ifdef DOCKS_TESTING_METHODS
184
185void Platform::pauseForDebugger()
186{
187}
188
189Platform::WarningObserver::~WarningObserver() = default;
190
191#endif
192
193#ifdef DOCKS_DEVELOPER_MODE
194/*static*/
195void Platform::tests_initPlatform(int &argc, char **argv, KDDockWidgets::FrontendType type, bool defaultToOffscreenQPA)
196{
198 return;
199
200 Platform *platform = nullptr;
201
202 switch (type) {
204#ifdef KDDW_FRONTEND_QTWIDGETS
205 platform = new QtWidgets::Platform(argc, argv, defaultToOffscreenQPA);
206#endif
207 break;
209#ifdef KDDW_FRONTEND_QTQUICK
210 platform = new QtQuick::Platform(argc, argv, defaultToOffscreenQPA);
211#endif
212 break;
214#ifdef KDDW_FRONTEND_FLUTTER
215 platform = nullptr;
216 KDDW_UNUSED(argc);
217 KDDW_UNUSED(argv);
218#endif
219 break;
220 }
221
222 if (!platform) {
223 KDDW_ERROR("Could not initialize platform for type={}. KDDockWidgets was built without support for it");
224 std::abort();
225 return;
226 }
227
230
232 Platform::instance()->tests_initPlatform_impl();
233}
234
235/*static */
236void Platform::tests_deinitPlatform()
237{
238 auto plat = Platform::instance();
239 plat->d->m_inDestruction = true;
240
241 plat->tests_deinitPlatform_impl();
242 delete DockRegistry::self();
243 delete plat;
244}
245#endif
246
248{
249 d->m_globalEventFilters.push_back(filter);
250}
251
253{
254 d->m_globalEventFilters.erase(
255 std::remove(d->m_globalEventFilters.begin(), d->m_globalEventFilters.end(), filter),
256 d->m_globalEventFilters.end());
257}
258
262
266
270
274
275QByteArray Platform::readFile(const QString &fileName, bool &ok) const
276{
277 ok = true;
278
279 std::ifstream file(fileName.toStdString(), std::ios::binary);
280 if (!file.is_open()) {
281 KDDW_ERROR("Failed to open {}", fileName);
282 ok = false;
283 return {};
284 }
285
286 QByteArray data;
287
288 file.seekg(0, std::ios::end);
289 std::streampos fileSize = file.tellg();
290 file.seekg(0, std::ios::beg);
291
292 data.resize(int(fileSize));
293
294 file.read(data.data(), fileSize);
295 file.close();
296
297 return data;
298}
299
301{
302 return false;
303}
304
306{
307 return m_enabled;
308}
309
311{
312 m_enabled = enabled;
313}
Application-wide config to tune certain behaviours of the framework.
#define KDDW_UNUSED(name)
static Config & self()
returns the singleton Config instance
Definition Config.cpp:88
int startDragDistance() const
Returns the value set by setStartDragDistance() Returns -1 if setStartDragDistance() wasn't call,...
Definition Config.cpp:414
void setViewFactory(Core::ViewFactory *)
Sets the ViewFactory.
Definition Config.cpp:175
bool enabled() const
Returns whether mouse filtering is enabled. Default true.
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
implements functions specific to a particular platform A platform can be for example qtwidgets,...
static std::vector< KDDockWidgets::FrontendType > frontendTypes()
list the list of frontend types supported by this build
virtual QByteArray readFile(const QString &, bool &ok) const
virtual void onMainWindowDestroyed(Core::MainWindow *)
Called when a main window is created. Overridden by flutter, so it can destroy the window.
virtual const char * name() const =0
Returns the name of the platform, only "qtwidgets" and "qtquick".
bool isQt() const
Returns whether this platform is Qt based.
virtual void onFloatingWindowDestroyed(Core::FloatingWindow *)
Called when a floating window is created. Overridden by flutter, so it can destroy the window.
bool isQtQuick() const
Returns whether this platform is QtQuick.
static Platform * instance()
Returns the platform singleton.
virtual bool hasActivePopup() const
Returns whether a popup is open Usually not needed to override. Investigate further in case side bars...
void removeGlobalEventFilter(EventFilterInterface *)
Removes a global event filter.
virtual int startDragDistance_impl() const
virtual ViewFactory * createDefaultViewFactory()=0
Creates and returns the default ViewFactory.
virtual bool supportsAeroSnap() const
Only supported on Qt, for windows.
int startDragDistance() const
Returns how many pixels the mouse must move for a drag to start This is usually 4 by default (QApplic...
virtual Vector< std::shared_ptr< Screen > > screens() const =0
Returns all available screens.
void installGlobalEventFilter(EventFilterInterface *)
Installs a global event filter Events will be forwarded to the specified EventFilterInterface.
virtual void onFloatingWindowCreated(Core::FloatingWindow *)
Called when a floating window is created. Overridden by flutter, so it can create a window.
bool isQtWidgets() const
Returns whether this platform is QtWidgets.
virtual void onMainWindowCreated(Core::MainWindow *)
Called when a main window is created. Overridden by flutter, so it can create a window Used by tests ...
static bool isInitialized()
Returns whether the Platform was already initialized.
static bool hasInstance()
Returns whether a Platform instance exists.
static DockRegistry * self()
implements functions specific to a particular platform A platform can be for example qtwidgets,...
implements functions specific to a particular platform A platform can be for example qtwidgets,...
static Platform * s_platform
Class to abstract QAction, so code still works with QtQuick and Flutter.
QtQuick::Platform * plat()
void DOCKS_EXPORT initFrontend(FrontendType)
Initializes the desired frontend This function should be called before using any docking....
char * data()
void resize(int size)
std::string toStdString() const const
Orientation

© 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