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
21#ifdef KDDW_FRONTEND_QTWIDGETS
22#include "qtwidgets/Platform.h"
23#endif
24
25#ifdef KDDW_FRONTEND_QTQUICK
26#include "qtquick/Platform.h"
27#endif
28
29#ifdef KDDW_FRONTEND_FLUTTER
30#include "flutter/Platform.h"
31#endif
32
33#include "Config.h"
34#include "core/layouting/Item_p.h"
35#include "core/Screen_p.h"
36
37#include "QtCompat_p.h"
38
39#include "kdbindings/signal.h"
40
41#include <iostream>
42#include <fstream>
43#include <cstdlib>
44
45using namespace KDDockWidgets;
46using namespace KDDockWidgets::Core;
47
48static Platform *s_platform = nullptr;
49
51
53 : d(new Private())
54{
55 assert(!s_platform);
56 s_platform = this;
57
58 Item::setDumpScreenInfoFunc([] {
59 const auto screens = Platform::instance()->screens();
60 for (const auto &screen : screens) {
61 std::cerr << "Screen: " << screen->geometry() << "; " << screen->availableGeometry()
62 << "; drp=" << screen->devicePixelRatio() << "\n";
63 }
64 });
65}
66
68{
69 Item::setDumpScreenInfoFunc(nullptr);
70 s_platform = nullptr;
71 delete d;
72}
73
74Platform::Private::Private()
75{
79 Core::Item::setCreateSeparatorFunc([](Core::LayoutingHost *host, Qt::Orientation orientation, Core::ItemBoxContainer *container) -> Core::LayoutingSeparator * {
80 return (new Core::Separator(host, orientation, container))->asLayoutingSeparator();
81 });
82}
83
85{
86 if (!s_platform) {
87 static bool guard = false;
88 if (guard)
89 return nullptr;
90 guard = true;
91
92 // For convenience, if there's only 1 frontend supported then don't
93 // require the user to call initFrontend(), just do it here.
94 const auto types = Platform::frontendTypes();
95 if (types.size() == 1)
97 guard = false;
98 }
99
100 return s_platform;
101}
102
104{
105 return false;
106}
107
109{
110 return strcmp(name(), "qtwidgets") == 0;
111}
112
114{
115 return strcmp(name(), "qtquick") == 0;
116}
117
118bool Platform::isQt() const
119{
120 static const bool is = isQtWidgets() || isQtQuick();
121 return is;
122}
123
125{
126 const int userRequestedDistance = Config::self().startDragDistance();
127 if (userRequestedDistance > -1)
128 return userRequestedDistance;
129
130 return startDragDistance_impl();
131}
132
134{
135 // Override this method in derived classes for some different value if needed
136 return 4;
137}
138
140std::vector<KDDockWidgets::FrontendType> Platform::frontendTypes()
141{
142 std::vector<KDDockWidgets::FrontendType> types;
143
144#ifdef DOCKS_DEVELOPER_MODE
145 // During development it's useful to quickly run tests only on the frontend we're developing.
146 // The developer can set, for example, KDDW_TEST_FRONTEND=2 to run only the QtQuick tests
147 bool ok = false;
148 const int frontendId = envVarIntValue("KDDW_TEST_FRONTEND", /*by-ref*/ ok);
149 if (ok) {
150 types.push_back(FrontendType(frontendId));
151 return types;
152 }
153
154#endif
155
156#ifdef KDDW_FRONTEND_QTQUICK
157 types.push_back(FrontendType::QtQuick);
158#endif
159
160#ifdef KDDW_FRONTEND_QTWIDGETS
161 types.push_back(FrontendType::QtWidgets);
162#endif
163
164#ifdef KDDW_FRONTEND_FLUTTER
165 types.push_back(FrontendType::Flutter);
166#endif
167
168 return types;
169}
170
171#ifdef DOCKS_TESTING_METHODS
172
173void Platform::pauseForDebugger()
174{
175}
176
177Platform::WarningObserver::~WarningObserver() = default;
178
179/*static */
180bool Platform::isInitialized()
181{
182 return s_platform != nullptr;
183}
184
185#endif
186
187#ifdef DOCKS_DEVELOPER_MODE
188/*static*/
189void Platform::tests_initPlatform(int &argc, char **argv, KDDockWidgets::FrontendType type)
190{
191 if (Platform::isInitialized())
192 return;
193
194 Platform *platform = nullptr;
195
196 switch (type) {
198#ifdef KDDW_FRONTEND_QTWIDGETS
199 platform = new QtWidgets::Platform(argc, argv);
200#endif
201 break;
203#ifdef KDDW_FRONTEND_QTQUICK
204 platform = new QtQuick::Platform(argc, argv);
205#endif
206 break;
208#ifdef KDDW_FRONTEND_FLUTTER
209 platform = nullptr;
210 KDDW_UNUSED(argc);
211 KDDW_UNUSED(argv);
212#endif
213 break;
214 }
215
216 if (!platform) {
217 KDDW_ERROR("Could not initialize platform for type={}. KDDockWidgets was built without support for it");
218 std::abort();
219 return;
220 }
221
224
226 Platform::instance()->tests_initPlatform_impl();
227}
228
229/*static */
230void Platform::tests_deinitPlatform()
231{
232 auto plat = Platform::instance();
233 plat->d->m_inDestruction = true;
234
235 plat->tests_deinitPlatform_impl();
236 delete plat;
237}
238#endif
239
241{
242 d->m_globalEventFilters.push_back(filter);
243}
244
246{
247 d->m_globalEventFilters.erase(
248 std::remove(d->m_globalEventFilters.begin(), d->m_globalEventFilters.end(), filter),
249 d->m_globalEventFilters.end());
250}
251
255
259
263
267
268QByteArray Platform::readFile(const QString &fileName, bool &ok) const
269{
270 ok = true;
271
272 std::ifstream file(fileName.toStdString(), std::ios::binary);
273 if (!file.is_open()) {
274 KDDW_ERROR("Failed to open {}", fileName);
275 ok = false;
276 return {};
277 }
278
279 QByteArray data;
280
281 file.seekg(0, std::ios::end);
282 std::streampos fileSize = file.tellg();
283 file.seekg(0, std::ios::beg);
284
285 data.resize(int(fileSize));
286
287 file.read(data.data(), fileSize);
288 file.close();
289
290 return data;
291}
292
294{
295 return false;
296}
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:87
int startDragDistance() const
Returns the value set by setStartDragDistance() Returns -1 if setStartDragDistance() wasn't call,...
Definition Config.cpp:372
void setViewFactory(Core::ViewFactory *)
Sets the ViewFactory.
Definition Config.cpp:155
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 ...
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