Install gammaray_probe.so to /usr/lib/gammaray
[gammaray-debian.git] / util.cpp
blobffca50ad07d33674b045f1839281fca03ac592ef
1 /*
2 util.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "util.h"
26 #include <QtCore/qobject.h>
27 #include <QtCore/QStringList>
28 #include <qsize.h>
29 #include <qpoint.h>
30 #include <qrect.h>
31 #include <qsizepolicy.h>
32 #include <qmetaobject.h>
33 #include <QtGui/qtextformat.h>
35 using namespace GammaRay;
37 QString Util::displayString(const QObject *object)
39 if (!object) {
40 return "QObject(0x0)";
42 if (object->objectName().isEmpty()) {
43 return QString::fromLatin1("%1 (%2)").
44 arg(addressToString(object)).
45 arg(object->metaObject()->className());
47 return object->objectName();
50 static QString sizePolicyToString(QSizePolicy::Policy policy)
52 const int index = QSizePolicy::staticMetaObject.indexOfEnumerator("Policy");
53 const QMetaEnum metaEnum = QSizePolicy::staticMetaObject.enumerator(index);
54 return QString::fromLatin1(metaEnum.valueToKey(policy));
57 QString GammaRay::Util::variantToString(const QVariant &value)
59 switch (value.type()) {
60 case QVariant::Point:
61 return QString::fromLatin1("%1x%2").
62 arg(value.toPoint().x()).
63 arg(value.toPoint().y());
65 case QVariant::PointF:
66 return QString::fromLatin1("%1x%2").
67 arg(value.toPointF().x()).
68 arg(value.toPointF().y());
70 case QVariant::Rect:
71 return QString::fromLatin1("%1x%2 %3x%4").
72 arg(value.toRect().x()).
73 arg(value.toRect().y()).
74 arg(value.toRect().width()).
75 arg(value.toRect().height());
77 case QVariant::RectF:
78 return QString::fromLatin1("%1x%2 %3x%4").
79 arg(value.toRectF().x()).
80 arg(value.toRectF().y()).
81 arg(value.toRectF().width()).
82 arg(value.toRectF().height());
84 case QVariant::Size:
85 return QString::fromLatin1("%1x%2").
86 arg(value.toSize().width()).
87 arg(value.toSize().height());
89 case QVariant::SizeF:
90 return QString::fromLatin1("%1x%2").
91 arg(value.toSizeF().width()).
92 arg(value.toSizeF().height());
94 case QVariant::SizePolicy:
95 return QString::fromLatin1("%1 x %2").
96 arg(sizePolicyToString(value.value<QSizePolicy>().horizontalPolicy())).
97 arg(sizePolicyToString(value.value<QSizePolicy>().verticalPolicy()));
98 case QVariant::StringList:
99 return value.toStringList().join(", ");
100 default:
101 break;
104 // types with dynamic type ids
105 if (value.type() == qMetaTypeId<QTextLength>()) {
106 const QTextLength l = value.value<QTextLength>();
107 QString typeStr;
108 switch (l.type()) {
109 case QTextLength::VariableLength:
110 typeStr = QObject::tr("variable");
111 break;
112 case QTextLength::FixedLength:
113 typeStr = QObject::tr("fixed");
114 break;
115 case QTextLength::PercentageLength:
116 typeStr = QObject::tr("percentage");
117 break;
119 return QString::fromLatin1("%1 (%2)").arg(l.rawValue()).arg(typeStr);
122 return value.toString();
125 QString Util::addressToString(const void *p)
127 return (QLatin1String("0x") + QString::number(reinterpret_cast<qlonglong>(p), 16));
130 bool Util::descendantOf(QObject *ascendant, QObject *obj)
132 QObject *parent = obj->parent();
133 if (!parent) {
134 return false;
136 if (parent == ascendant) {
137 return true;
139 return descendantOf(ascendant, parent);