qskinny/src/controls/QskObjectTree.cpp

97 lines
2.4 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskObjectTree.h"
2018-07-19 14:10:48 +02:00
#include <qguiapplication.h>
2018-08-03 08:15:28 +02:00
#include <qquickitem.h>
#include <qquickwindow.h>
2017-07-21 18:21:34 +02:00
bool QskObjectTree::isRoot( const QObject* object )
{
return object == QGuiApplication::instance();
}
QObjectList QskObjectTree::childNodes( const QObject* object )
{
QObjectList children;
if ( object == nullptr )
{
2017-10-30 12:06:19 +01:00
const auto windows = QGuiApplication::topLevelWindows();
for ( auto window : windows )
2017-07-21 18:21:34 +02:00
children += window;
}
else if ( object->isWindowType() )
{
const auto childObjects = object->children();
for ( auto child : childObjects )
2017-07-21 18:21:34 +02:00
{
if ( child->isWindowType() )
children += child;
}
if ( auto w = qobject_cast< const QQuickWindow* >( object ) )
{
// For some reason the window is not the parent of its contentItem()
children += w->contentItem();
2017-07-21 18:21:34 +02:00
}
}
else if ( auto item = qobject_cast< const QQuickItem* >( object ) )
2017-07-21 18:21:34 +02:00
{
const auto childItems = item->childItems();
for ( auto child : childItems )
2017-07-21 18:21:34 +02:00
children += child;
}
return children;
}
QObject* QskObjectTree::parentNode( const QObject* object )
{
if ( object == nullptr )
return nullptr;
if ( object->isWindowType() )
{
if ( object->parent() == nullptr )
2017-07-21 18:21:34 +02:00
return QGuiApplication::instance();
}
if ( auto item = qobject_cast< const QQuickItem* >( object ) )
2017-07-21 18:21:34 +02:00
{
if ( item->parentItem() )
return item->parentItem();
return item->window();
}
return object->parent();
}
void QskObjectTree::traverseDown( QObject* object, Visitor& visitor )
{
const auto children = childNodes( object );
for ( QObject* child : children )
{
const bool done = visitor.visitDown( child );
if ( !done )
traverseDown( child, visitor );
}
}
void QskObjectTree::traverseUp( QObject* object, Visitor& visitor )
{
QObject* parent = parentNode( object );
if ( parent )
{
const bool done = visitor.visitUp( parent );
if ( !done )
traverseUp( parent, visitor );
}
}