1
/*  This file is part of the KDE project.
2
3
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5
This library is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 2.1 or 3 of the License.
8
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Lesser General Public License for more details.
13
14
You should have received a copy of the GNU Lesser General Public License
15
along with this library.  If not, see <http://www.gnu.org/licenses/>.
16
17
*/
18
19
#ifndef OBJECTDUMP_H
20
#define OBJECTDUMP_H
21
22
#include <QObject>
23
#include <QList>
24
#include <QByteArray>
25
#include <QScopedPointer>
26
27
QT_BEGIN_NAMESPACE
28
29
namespace ObjectDump
30
{
31
32
/**
33
 * Abstract base for annotator classes invoked by QVisitor.
34
 */
35
class QAnnotator : public QObject
36
{
37
    Q_OBJECT
38
public:
39
    virtual ~QAnnotator();
40
    virtual QList<QByteArray> annotation(const QObject& object) = 0;
41
};
42
43
/**
44
 * Annotator which replicates QObject::dumpObjectTree functionality.
45
 */
46
class QAnnotatorBasic : public QAnnotator
47
{
48
    Q_OBJECT
49
public:
50
    QList<QByteArray> annotation(const QObject& object);
51
};
52
53
/**
54
 * Annotator which returns widget information.
55
 */
56
class QAnnotatorWidget : public QAnnotator
57
{
58
    Q_OBJECT
59
public:
60
    QList<QByteArray> annotation(const QObject& object);
61
};
62
63
64
class QDumperPrivate;
65
66
/**
67
 * Class used to dump information about individual QObjects.
68
 */
69
class QDumper : public QObject
70
{
71
    Q_OBJECT
72
    Q_DECLARE_PRIVATE(QDumper)
73
74
public:
75
    QDumper();
76
    ~QDumper();
77
78
    /**
79
     * Specify a prefix, to be printed on each line of output.
80
     */
81
    void setPrefix(const QString& prefix);
82
83
    /**
84
     * Takes ownership of annotator.
85
     */
86
    void addAnnotator(QAnnotator* annotator);
87
88
    /**
89
     * Invoke each annotator on the object and write to debug output.
90
     */
91
    void dumpObject(const QObject& object);
92
93
private:
94
    QScopedPointer<QDumperPrivate> d_ptr;
95
96
};
97
98
99
class QVisitorPrivate;
100
101
/**
102
 * Visitor class which dumps information about nodes in the object tree.
103
 */
104
class QVisitor : public QObject
105
{
106
    Q_OBJECT
107
    Q_DECLARE_PRIVATE(QVisitor)
108
109
public:
110
    QVisitor();
111
    ~QVisitor();
112
113
    /**
114
     * Specify a prefix, to be printed on each line of output.
115
     */
116
    void setPrefix(const QString& prefix);
117
118
    /**
119
     * Set number of spaces by which each level of the tree is indented.
120
     */
121
    void setIndent(unsigned indent);
122
123
    /**
124
     * Called by the visitor algorithm before starting the visit.
125
     */
126
    void visitPrepare();
127
128
    /**
129
     * Called by the visitor algorithm as each node is visited.
130
     */
131
    void visitNode(const QObject& object);
132
133
    /**
134
     * Called by the visitor algorithm when the visit is complete.
135
     */
136
    void visitComplete();
137
138
    /**
139
     * Takes ownership of annotator.
140
     */
141
    void addAnnotator(QAnnotator* annotator);
142
143
private:
144
    QScopedPointer<QVisitorPrivate> d_ptr;
145
146
};
147
148
149
//-----------------------------------------------------------------------------
150
// Utility functions
151
//-----------------------------------------------------------------------------
152
153
void addDefaultAnnotators(QDumper& dumper);
154
void addDefaultAnnotators(QVisitor& visitor);
155
156
void dumpTreeFromRoot(const QObject& root, QVisitor& visitor);
157
void dumpTreeFromLeaf(const QObject& leaf, QVisitor& visitor);
158
void dumpAncestors(const QObject& leaf, QVisitor& visitor);
159
160
} // namespace ObjectDump
161
162
QT_END_NAMESPACE
163
164
#endif