KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtquick/views/ViewWrapper.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 "ViewWrapper_p.h"
14#include "qtquick/views/View.h"
15#include "core/View_p.h"
16
19#include "qtquick/views/Group.h"
22#include "core/layouting/Item_p.h"
23// #include "qtquick/views/SideBar.h"
24#include "qtquick/views/Stack.h"
29
30#include "../Window_p.h"
31
32#include "kddockwidgets/core/DropArea.h"
33#include "kddockwidgets/core/MDILayout.h"
34// #include "views/MDIArea.h"
35
36#include <QtQuick/private/qquickitem_p.h>
37#include <QDebug>
38
39using namespace KDDockWidgets;
40using namespace KDDockWidgets::QtQuick;
41
42namespace KDDockWidgets {
43static Core::Controller *controllerForItem(QQuickItem *item)
44{
45 // KDDW deals in views, but sometimes we might get a native type like QWidget, for example if
46 // you call someview->window(). This function let's us retrieve the actual controller of the
47 // stray QWidget.
48
49 for (int i = int(Core::ViewType::FIRST); i <= int(::Core::ViewType::LAST); i *= 2) {
50 // Using a for+switch pattern so that compiler reminds us if new enumerators are added to
51 // enum
52 switch (Core::ViewType(i)) {
54 if (auto view = qobject_cast<Group *>(item))
55 return view->controller();
56 break;
58 if (auto view = qobject_cast<TitleBar *>(item))
59 return view->controller();
60 break;
62 if (auto view = qobject_cast<TabBar *>(item))
63 return view->controller();
64 break;
66 if (auto view = qobject_cast<Stack *>(item))
67 return view->controller();
68 break;
70 if (auto view = qobject_cast<FloatingWindow *>(item))
71 return view->controller();
72 break;
74 if (auto view = qobject_cast<Separator *>(item))
75 return view->controller();
76 break;
78 if (auto view = qobject_cast<QtQuick::DockWidget *>(item))
79 return view->controller();
80 break;
82 if (auto view = qobject_cast<DropArea *>(item))
83 return view->controller();
84 break;
86 if (auto view = qobject_cast<MDILayout *>(item))
87 return view->controller();
88 break;
90 // Not implemented for QtQuick yet
91 break;
93 if (auto view = qobject_cast<MainWindow *>(item))
94 return view->controller();
95 break;
101 // skip internal types
102 continue;
103 }
104 }
105
106 return nullptr;
107}
108}
109
110ViewWrapper::ViewWrapper(QObject *item)
111 : ViewWrapper(qobject_cast<QQuickItem *>(item))
112{
113}
114
115ViewWrapper::ViewWrapper(QQuickItem *item)
116 : QtCommon::ViewWrapper(controllerForItem(item), item)
117 , m_item(item)
118{
119}
120
121QRect ViewWrapper::geometry() const
122{
123 if (isRootView()) {
124 if (QWindow *w = m_item->window()) {
125 return w->geometry();
126 }
127 }
128
129 return QRect(QPointF(m_item->x(), m_item->y()).toPoint(), m_item->size().toSize());
130}
131
132QPoint ViewWrapper::mapToGlobal(QPoint localPt) const
133{
134 return m_item->mapToGlobal(localPt).toPoint();
135}
136
137QPoint ViewWrapper::mapFromGlobal(QPoint globalPt) const
138{
139 return m_item->mapFromGlobal(globalPt).toPoint();
140}
141
142void ViewWrapper::setGeometry(QRect rect)
143{
144 setSize(rect.width(), rect.height());
145 ViewWrapper::move(rect.topLeft().x(), rect.topLeft().y());
146}
147
148std::shared_ptr<Core::View> ViewWrapper::childViewAt(QPoint p) const
149{
150 auto child = m_item->childAt(p.x(), p.y());
151 return child ? QtQuick::View::asQQuickWrapper(child) : nullptr;
152}
153
154std::shared_ptr<Core::Window> ViewWrapper::window() const
155{
156 if (QWindow *w = m_item->window()) {
157 auto windowqtquick = new Window(w);
158 return std::shared_ptr<Core::Window>(windowqtquick);
159 }
160
161 return {};
162}
163
164bool ViewWrapper::isRootView() const
165{
166 return QtQuick::View::isRootView(m_item);
167}
168
169void ViewWrapper::setVisible(bool is)
170{
171 if (isRootView()) {
172 if (QWindow *w = m_item->window()) {
173 if (is && !w->isVisible()) {
174 w->show();
175 } else if (!is && w->isVisible()) {
176 w->hide();
177 }
178 }
179 }
180
181 m_item->setVisible(is);
182}
183
184bool ViewWrapper::isVisible() const
185{
186 if (QWindow *w = m_item->window()) {
187 if (!w->isVisible())
188 return false;
189 }
190
191 return m_item->isVisible();
192}
193
194bool ViewWrapper::isExplicitlyHidden() const
195{
196 auto priv = QQuickItemPrivate::get(m_item);
197 return !priv->explicitVisible;
198}
199
200void ViewWrapper::move(int x, int y)
201{
202 if (isRootView()) {
203 if (QWindow *w = m_item->window()) {
204 w->setPosition(x, y);
205 return;
206 }
207 }
208
209 m_item->setX(x);
210 m_item->setY(y);
211 enableAttribute(Qt::WA_Moved);
212}
213
214void ViewWrapper::activateWindow()
215{
216 if (QWindow *w = m_item->window())
217 w->requestActivate();
218}
219
220bool ViewWrapper::isMaximized() const
221{
222 if (QWindow *w = m_item->window())
223 return w->windowStates() & Qt::WindowMaximized;
224
225 return false;
226}
227
228bool ViewWrapper::isMinimized() const
229{
230 if (QWindow *w = m_item->window())
231 return w->windowStates() & Qt::WindowMinimized;
232
233 return false;
234}
235
236QSize ViewWrapper::maxSizeHint() const
237{
238 if (auto view = unwrap()) {
239 return view->maxSizeHint();
240 } else {
241 const QSize max = m_item->property("kddockwidgets_max_size").toSize();
242 return max.isEmpty() ? Core::Item::hardcodedMaximumSize
243 : max.boundedTo(Core::Item::hardcodedMaximumSize);
244 }
245}
246
247void ViewWrapper::setSize(int w, int h)
248{
249 if (isRootView()) {
250 if (QWindow *window = m_item->window()) {
251 QRect windowGeo = window->geometry();
252 windowGeo.setSize(QSize(w, h));
253 window->setGeometry(windowGeo);
254 }
255 }
256
257 m_item->setSize(QSizeF(w, h));
258}
259
260bool ViewWrapper::is(Core::ViewType t) const
261{
262 if (t == Core::ViewType::ViewWrapper)
263 return true;
264
265 switch (t) {
266
267 case Core::ViewType::Frame:
268 return qobject_cast<Group *>(m_item);
269 case Core::ViewType::TitleBar:
270 return qobject_cast<TitleBar *>(m_item);
271 case Core::ViewType::TabBar:
272 return qobject_cast<TabBar *>(m_item);
273 case Core::ViewType::Stack:
274 return qobject_cast<Stack *>(m_item);
275 case Core::ViewType::FloatingWindow:
276 return qobject_cast<FloatingWindow *>(m_item);
277 case Core::ViewType::Separator:
278 return qobject_cast<Separator *>(m_item);
279 case Core::ViewType::DockWidget:
280 return qobject_cast<QtQuick::DockWidget *>(m_item);
281 case Core::ViewType::SideBar:
282 return false; // QtQuick doesn't support sidebar yet
283 // return qobject_cast<SideBar *>(m_item);
284 case Core::ViewType::MainWindow:
285 return qobject_cast<QtQuick::MainWindow *>(m_item);
286 case Core::ViewType::DropArea:
287 return qobject_cast<DropArea *>(m_item);
288 case Core::ViewType::MDILayout:
289 return qobject_cast<MDILayout *>(m_item);
290 case Core::ViewType::RubberBand:
291 return qobject_cast<RubberBand *>(m_item);
292 case Core::ViewType::LayoutItem:
293 case Core::ViewType::None:
294 case Core::ViewType::DropAreaIndicatorOverlay:
295 qWarning() << Q_FUNC_INFO << "These are framework internals that are not wrapped";
296 return false;
297 case Core::ViewType::ViewWrapper:
298 return true;
299 }
300
301 qWarning() << Q_FUNC_INFO << "Unknown type" << static_cast<int>(t);
302 return false;
303}
304
305std::shared_ptr<Core::View> ViewWrapper::rootView() const
306{
307 if (Core::Window::Ptr window = this->window())
308 return window->rootView();
309
310 qWarning() << Q_FUNC_INFO << "No window present";
311 return {};
312}
313
314std::shared_ptr<Core::View> ViewWrapper::parentView() const
315{
316 return QtQuick::View::parentViewFor(m_item);
317}
318
319void ViewWrapper::grabMouse()
320{
321 m_item->grabMouse();
322}
323
324void ViewWrapper::releaseMouse()
325{
326 m_item->ungrabMouse();
327}
328
329Qt::FocusPolicy ViewWrapper::focusPolicy() const
330{
331 if (auto view = unwrap()) {
332 return view->focusPolicy();
333 } else {
334 // The DockWidget's user guest widget is not a View, it's a QQuickItem
335 // QQuickItem don't have focus policy and are focusable
336 return Qt::StrongFocus;
337 }
338}
339
340void ViewWrapper::setFocus(Qt::FocusReason reason)
341{
342 m_item->QQuickItem::setFocus(true, reason);
343 m_item->forceActiveFocus(reason);
344}
345
346void ViewWrapper::setFocusPolicy(Qt::FocusPolicy policy)
347{
348 if (auto view = unwrap()) {
349 view->setFocusPolicy(policy);
350 } else {
351 qWarning() << Q_FUNC_INFO << "Not implemented for QtQuick";
352 }
353}
354
355bool ViewWrapper::hasFocus() const
356{
357 return m_item->hasActiveFocus();
358}
359
360QString ViewWrapper::viewName() const
361{
362 return m_item->objectName();
363}
364
365bool ViewWrapper::isNull() const
366{
367 return m_item.data() == nullptr;
368}
369
370void ViewWrapper::setWindowTitle(const QString &title)
371{
372 if (QWindow *w = m_item->window())
373 w->setTitle(title);
374}
375
376QPoint ViewWrapper::mapTo(View *parent, QPoint pos) const
377{
378 if (!parent)
379 return {};
380
381 auto parentItem = asQQuickItem(parent);
382 return parentItem->mapFromGlobal(m_item->mapToGlobal(pos)).toPoint();
383}
384
385bool ViewWrapper::hasAttribute(Qt::WidgetAttribute attr) const
386{
387 if (auto view = unwrap()) {
388 // Only real views have min size
389 return view->hasAttribute(attr);
390 } else {
391 qFatal("not implemented");
392 return false;
393 }
394}
395
396void ViewWrapper::setCursor(Qt::CursorShape shape)
397{
398 m_item->QQuickItem::setCursor(shape);
399}
400
401QSize ViewWrapper::minSize() const
402{
403 if (auto view = unwrap()) {
404 // Only real views have min size
405 return view->minSize();
406 } else {
407 const QSize min = m_item->property("kddockwidgets_min_size").toSize();
408 return min.expandedTo(Core::Item::hardcodedMinimumSize);
409 }
410}
411
412QVector<std::shared_ptr<Core::View>> ViewWrapper::childViews() const
413{
415 const auto childItems = m_item->childItems();
416 result.reserve(childItems.size());
417 for (QQuickItem *child : childItems) {
418 result << QtQuick::View::asQQuickWrapper(child);
419 }
420
421 return result;
422}
423
424void ViewWrapper::setParent(View *parent)
425{
426 if (auto view = unwrap()) {
427 view->setParent(parent);
428 } else {
429 auto parentItem = QtQuick::asQQuickItem(parent);
430 m_item->QQuickItem::setParent(parentItem);
431 m_item->QQuickItem::setParentItem(parentItem);
432 }
433
434 m_item->setVisible(false);
435}
436
437bool ViewWrapper::close()
438{
439 return QtQuick::View::close(m_item);
440}
441
442Core::View *ViewWrapper::unwrap()
443{
444 return qobject_cast<QtQuick::View *>(m_item);
445}
446
447const Core::View *ViewWrapper::unwrap() const
448{
449 return qobject_cast<const QtQuick::View *>(m_item);
450}
451
452void ViewWrapper::raiseAndActivate()
453{
455}
456
457/*static*/
458std::shared_ptr<Core::View> ViewWrapper::create(QObject *item)
459{
460 return create(qobject_cast<QQuickItem *>(item));
461}
462
463/*static*/
464std::shared_ptr<Core::View> ViewWrapper::create(QQuickItem *item)
465{
466 if (!item)
467 return {};
468
469 auto wrapper = new ViewWrapper(item);
470 auto sharedptr = std::shared_ptr<View>(wrapper);
471 wrapper->d->m_thisWeakPtr = sharedptr;
472
473 return sharedptr;
474}
bool isRootView() const override final
static std::shared_ptr< Core::View > asQQuickWrapper(QQuickItem *item)
static std::shared_ptr< Core::View > parentViewFor(const QQuickItem *)
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.
static Core::Controller * controllerForItem(QQuickItem *item)
int x() const const
int y() const const
QPoint toPoint() const const
int height() const const
void setSize(const QSize &size)
QPoint topLeft() const const
int width() const const
QSize boundedTo(const QSize &otherSize) const const
QSize expandedTo(const QSize &otherSize) const const
bool isEmpty() const const
QChar * data()
CursorShape
FocusPolicy
FocusReason
WA_Moved
WindowMaximized
Represents a dock widget.
The QQuickItem counter part of TabWidgetQuick. Handles GUI while TabWidget handles state.
Implements a QTabWidget derived class with support for docking and undocking KDockWidget::DockWidget ...
void reserve(int size)

© 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