QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskObjectTree.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskObjectTree.h"
7
8#include <qguiapplication.h>
9#include <qquickitem.h>
10#include <qquickwindow.h>
11
12bool QskObjectTree::isRoot( const QObject* object )
13{
14 return object == QGuiApplication::instance();
15}
16
17QObjectList QskObjectTree::childNodes( const QObject* object )
18{
19 QObjectList children;
20
21 if ( object == nullptr )
22 {
23 const auto windows = QGuiApplication::topLevelWindows();
24 children.reserve( windows.count() );
25
26 for ( auto window : windows )
27 children += window;
28 }
29 else if ( object->isWindowType() )
30 {
31 const auto childObjects = object->children();
32
33 for ( auto child : childObjects )
34 {
35 if ( child->isWindowType() )
36 children += child;
37 }
38
39 if ( auto w = qobject_cast< const QQuickWindow* >( object ) )
40 {
41 // For some reason the window is not the parent of its contentItem()
42 children += w->contentItem();
43 }
44 }
45 else if ( auto item = qobject_cast< const QQuickItem* >( object ) )
46 {
47 const auto childItems = item->childItems();
48 children.reserve( childItems.count() );
49
50 for ( auto child : childItems )
51 children += child;
52 }
53
54 return children;
55}
56
57QObject* QskObjectTree::parentNode( const QObject* object )
58{
59 if ( object == nullptr )
60 return nullptr;
61
62 if ( object->isWindowType() )
63 {
64 if ( object->parent() == nullptr )
65 return QGuiApplication::instance();
66 }
67
68 if ( auto item = qobject_cast< const QQuickItem* >( object ) )
69 {
70 if ( item->parentItem() )
71 return item->parentItem();
72
73 return item->window();
74 }
75
76 return object->parent();
77}
78
79void QskObjectTree::traverseDown( QObject* object, Visitor& visitor )
80{
81 const auto children = childNodes( object );
82 for ( QObject* child : children )
83 {
84 const bool done = visitor.visitDown( child );
85 if ( !done )
86 traverseDown( child, visitor );
87 }
88}
89
90void QskObjectTree::traverseUp( QObject* object, Visitor& visitor )
91{
92 QObject* parent = parentNode( object );
93 if ( parent )
94 {
95 const bool done = visitor.visitUp( parent );
96 if ( !done )
97 traverseUp( parent, visitor );
98 }
99}