KDDockWidgets API Documentation 2.0
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)
55{
56 QtCommon::Platform_qt::maybeSetOffscreenQPA(argc, argv);
57 return new QGuiApplication(argc, argv);
58}
59
60}
61
62Platform::Platform(int &argc, char **argv)
63 : Platform_qt(createCoreApplication(argc, argv))
64 , m_qquickHelpers(new QtQuickHelpers())
65{
66 init();
67}
68
69void Platform::tests_initPlatform_impl()
70{
71 Platform_qt::tests_initPlatform_impl();
72
73 QQuickStyle::setStyle(QStringLiteral("Material")); // so we don't load KDE plugins
74 plat()->setQmlEngine(new QQmlEngine());
75}
76
77void Platform::tests_deinitPlatform_impl()
78{
79 delete m_qmlEngine;
80 auto windows = qGuiApp->topLevelWindows();
81 while (!windows.isEmpty()) {
82 delete windows.first();
83 windows = qGuiApp->topLevelWindows();
84 }
85
86 Platform_qt::tests_deinitPlatform_impl();
87}
88
89Core::View *Platform::tests_createView(Core::CreateViewOptions opts, Core::View *parent)
90{
91 auto parentItem = parent ? QtQuick::asQQuickItem(parent) : nullptr;
92 auto newItem = new TestView(opts, parentItem);
93
94 if (!parentItem && opts.createWindow) {
95 auto view = new QQuickView(m_qmlEngine, nullptr);
96 view->resize(QSize(800, 800));
97
98 newItem->QQuickItem::setParentItem(view->contentItem());
99 newItem->QQuickItem::setParent(view->contentItem());
100 if (opts.isVisible)
101 newItem->QtQuick::View::setVisible(true);
102
103 QTest::qWait(100); // the root object gets sized delayed
104 }
105
106 return newItem;
107}
108
109Core::View *Platform::tests_createFocusableView(Core::CreateViewOptions opts, Core::View *parent)
110{
111 auto view = tests_createView(opts, parent);
112 view->setFocusPolicy(Qt::StrongFocus);
113
114 return view;
115}
116
117Core::View *Platform::tests_createNonClosableView(Core::View *parent)
118{
119 Core::CreateViewOptions opts;
120 opts.isVisible = true;
121 auto view = tests_createView(opts, parent);
122 view->d->closeRequested.connect([](QCloseEvent *ev) { ev->ignore(); });
123
124 return view;
125}
126
127Core::MainWindow *Platform::createMainWindow(const QString &uniqueName,
128 Core::CreateViewOptions viewOpts,
129 MainWindowOptions options, Core::View *parent,
130 Qt::WindowFlags flags) const
131{
132 QQuickItem *parentItem = QtQuick::asQQuickItem(parent);
133
134 if (!parentItem) {
135 auto view = new QQuickView(m_qmlEngine, nullptr);
136 view->resize(viewOpts.size);
137
138 view->setResizeMode(QQuickView::SizeRootObjectToView);
139 view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
140
141 if (viewOpts.isVisible)
142 view->show();
143
144 parentItem = view->rootObject();
145 Platform::instance()->tests_wait(100); // the root object gets sized delayed
146 }
147
148 auto view = new QtQuick::MainWindow(uniqueName, options, parentItem, flags);
149
150 return view->mainWindow();
151}
152
153std::shared_ptr<Core::Window> Platform::tests_createWindow()
154{
155 Core::CreateViewOptions viewOpts;
156 viewOpts.isVisible = true;
157 static int id = 0;
158 id++;
159 auto mainWindow = createMainWindow(QStringLiteral("testWindow-%1").arg(id), viewOpts);
160 return mainWindow->view()->window();
161}
162
163
164#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