QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskObjectTree.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_OBJECT_TREE_H
7#define QSK_OBJECT_TREE_H
8
9#include "QskControl.h"
10#include "QskWindow.h"
11
12#include <qvariant.h>
13
14namespace QskObjectTree
15{
16 class Visitor
17 {
18 public:
19 Visitor() = default;
20 virtual ~Visitor() = default;
21
22 virtual bool visitDown( QObject* object ) = 0;
23 virtual bool visitUp( const QObject* object ) = 0;
24
25 private:
26 Q_DISABLE_COPY( Visitor )
27 };
28
29 QSK_EXPORT QObjectList childNodes( const QObject* );
30 QSK_EXPORT QObject* parentNode( const QObject* );
31 QSK_EXPORT bool isRoot( const QObject* );
32
33 void traverseDown( QObject* object, Visitor& visitor );
34 void traverseUp( QObject* object, Visitor& visitor );
35
36 template< class T >
37 class ResolveVisitor : public Visitor
38 {
39 public:
40 ResolveVisitor( const char* propertyName )
41 : m_propertyName( propertyName )
42 {
43 }
44
45 inline const T& resolveValue() const
46 {
47 return m_value;
48 }
49
50 void setResolveValue( const T& value )
51 {
52 m_value = value;
53 }
54
55 bool visitDown( QObject* object ) override final
56 {
57 if ( auto control = qobject_cast< QskControl* >( object ) )
58 return setImplicitValue( control, m_value );
59
60 if ( auto window = qobject_cast< QskWindow* >( object ) )
61 return setImplicitValue( window, m_value );
62
63 return !setProperty( object, m_propertyName.constData(), m_value );
64 }
65
66 bool visitUp( const QObject* object ) override final
67 {
68 if ( isRoot( object ) )
69 return true;
70
71 if ( auto control = qobject_cast< const QskControl* >( object ) )
72 {
73 m_value = value( control );
74 return true;
75 }
76
77 if ( auto window = qobject_cast< const QskWindow* >( object ) )
78 {
79 m_value = value( window );
80 return true;
81 }
82
83 return getProperty( object, m_propertyName, m_value );
84 }
85
86 private:
87 inline bool getProperty( const QObject* object,
88 const char* name, T& value ) const
89 {
90 if ( !m_propertyName.isEmpty() )
91 {
92 const QVariant v = object->property( name );
93 if ( v.canConvert< T >() )
94 {
95 value = qvariant_cast< T >( v );
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 inline bool setProperty( QObject* object,
104 const char* name, const T& value ) const
105 {
106 T oldValue;
107 if ( !getProperty( object, name, oldValue ) || oldValue == value )
108 return false;
109
110 object->setProperty( name, value );
111 return true;
112 }
113
114 virtual bool setImplicitValue( QskControl*, const T& ) = 0;
115 virtual bool setImplicitValue( QskWindow*, const T& ) = 0;
116
117 virtual T value( const QskControl* ) const = 0;
118 virtual T value( const QskWindow* ) const = 0;
119
120 private:
121 const QByteArray m_propertyName;
122 T m_value;
123 };
124}
125
126#endif
Base class of all controls.
Definition QskControl.h:23