KDDockWidgets API Documentation 2.1
Loading...
Searching...
No Matches
qtquick/TestHelpers.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 "kddockwidgets/KDDockWidgets.h"
13#include "Platform.h"
14#include "views/View.h"
15#include "kddockwidgets/qtquick/views/MainWindow.h"
16#include "kddockwidgets/core/MainWindow.h"
17#include "Helpers_p.h"
18#include "core/View_p.h"
19#include "core/Logging_p.h"
20
21#include <QGuiApplication>
22#include <QQmlEngine>
23#include <QQuickStyle>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QtTest/QTest>
27
28using namespace KDDockWidgets;
29using namespace KDDockWidgets::QtQuick;
30
31#ifdef DOCKS_TESTING_METHODS
32
33namespace KDDockWidgets::QtQuick {
34
35class TestView : public QtQuick::View
36{
37public:
38 explicit TestView(Core::CreateViewOptions opts, QQuickItem *parent)
39 : QtQuick::View(nullptr, Core::ViewType::None, parent)
40 , m_opts(opts)
41 {
42 setMinimumSize(opts.minSize);
43 setMaximumSize(opts.maxSize);
44 }
45
46 ~TestView();
47
48private:
49 Core::CreateViewOptions m_opts;
50};
51
52TestView::~TestView() = default;
53
54inline QCoreApplication *createCoreApplication(int &argc, char **argv, bool defaultToOffscreenQPA)
55{
56 if (defaultToOffscreenQPA)
57 QtCommon::Platform_qt::maybeSetOffscreenQPA(argc, argv);
58
59 return new QGuiApplication(argc, argv);
60}
61
62}
63
64Platform::Platform(int &argc, char **argv, bool defaultToOffscreenQPA)
65 : Platform_qt(createCoreApplication(argc, argv, defaultToOffscreenQPA))
66 , m_qquickHelpers(new QtQuickHelpers())
67{
68 init();
69}
70
71void Platform::tests_initPlatform_impl()
72{
73 Platform_qt::tests_initPlatform_impl();
74
75 QQuickStyle::setStyle(QStringLiteral("Material")); // so we don't load KDE plugins
76 plat()->setQmlEngine(new QQmlEngine());
77}
78
79void Platform::tests_deinitPlatform_impl()
80{
81 delete m_qmlEngine;
82 auto windows = qGuiApp->topLevelWindows();
83 while (!windows.isEmpty()) {
84 delete windows.first();
85 windows = qGuiApp->topLevelWindows();
86 }
87
88 Platform_qt::tests_deinitPlatform_impl();
89}
90
91Core::View *Platform::tests_createView(Core::CreateViewOptions opts, Core::View *parent)
92{
93 auto parentItem = parent ? QtQuick::asQQuickItem(parent) : nullptr;
94 auto newItem = new TestView(opts, parentItem);
95
96 if (!parentItem && opts.createWindow) {
97 auto view = new QQuickView(m_qmlEngine, nullptr);
98 view->resize(QSize(800, 800));
99
100 newItem->QQuickItem::setParentItem(view->contentItem());
101 newItem->QQuickItem::setParent(view->contentItem());
102 if (opts.isVisible)
103 newItem->QtQuick::View::setVisible(true);
104
105 QTest::qWait(100); // the root object gets sized delayed
106 }
107
108 return newItem;
109}
110
111Core::View *Platform::tests_createFocusableView(Core::CreateViewOptions opts, Core::View *parent)
112{
113 auto view = tests_createView(opts, parent);
114 view->setFocusPolicy(Qt::StrongFocus);
115
116 return view;
117}
118
119Core::View *Platform::tests_createNonClosableView(Core::View *parent)
120{
121 Core::CreateViewOptions opts;
122 opts.isVisible = true;
123 auto view = tests_createView(opts, parent);
124 view->d->closeRequested.connect([](QCloseEvent *ev) { ev->ignore(); });
125
126 return view;
127}
128
129Core::MainWindow *Platform::createMainWindow(const QString &uniqueName,
130 Core::CreateViewOptions viewOpts,
131 MainWindowOptions options, Core::View *parent,
132 Qt::WindowFlags flags) const
133{
134 QQuickItem *parentItem = QtQuick::asQQuickItem(parent);
135
136 if (!parentItem) {
137 auto view = new QQuickView(m_qmlEngine, nullptr);
138 view->resize(viewOpts.size);
139
140 view->setResizeMode(QQuickView::SizeRootObjectToView);
141 view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
142
143 if (viewOpts.isVisible)
144 view->show();
145
146 parentItem = view->rootObject();
147 Platform::instance()->tests_wait(100); // the root object gets sized delayed
148 }
149
150 auto view = new QtQuick::MainWindow(uniqueName, options, parentItem, flags);
151
152 return view->mainWindow();
153}
154
155std::shared_ptr<Core::Window> Platform::tests_createWindow()
156{
157 Core::CreateViewOptions viewOpts;
158 viewOpts.isVisible = true;
159 static int id = 0;
160 id++;
161 auto mainWindow = createMainWindow(QStringLiteral("testWindow-%1").arg(id), viewOpts);
162 return mainWindow->view()->window();
163}
164
165
166#endif
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
virtual bool isVisible() const =0
QVector< std::shared_ptr< Core::Window > > windows() const override
Returns all windows.
A docking area for dock widgets Named MainWindow as it's the QtWidgets/QMainWindow counterpart....
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
QQuickItem * asQQuickItem(Core::View *view)
Class to abstract QAction, so code still works with QtQuick and Flutter.
@ None
Just use the defaults.
QtQuick::Platform * plat()
void ignore()
StrongFocus
typedef WindowFlags

© 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