2 objectstaticpropertymodel.cpp
4 This file is part of GammaRay, the Qt application inspection and
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 "objectstaticpropertymodel.h"
27 #include <QtCore/QMetaProperty>
29 using namespace GammaRay
;
31 ObjectStaticPropertyModel::ObjectStaticPropertyModel(QObject
*parent
)
32 : ObjectPropertyModel(parent
)
36 static QString
translateBool(bool value
)
38 return value
? QObject::tr("yes") : QObject::tr("no");
41 QVariant
ObjectStaticPropertyModel::data(const QModelIndex
&index
, int role
) const
43 if (!index
.isValid() || !m_obj
|| index
.row() < 0 ||
44 index
.row() >= m_obj
.data()->metaObject()->propertyCount()) {
48 const QMetaProperty prop
= m_obj
.data()->metaObject()->property(index
.row());
49 if (role
== Qt::DisplayRole
) {
50 if (index
.column() == 0) {
52 } else if (index
.column() == 1) {
53 // QMetaProperty::read sets QVariant::typeName to int for enums,
54 // so we need to handle that separately here
55 const QVariant value
= prop
.read(m_obj
.data());
56 const QString enumStr
= Util::enumToString(value
, prop
.typeName(), m_obj
.data());
57 if (!enumStr
.isEmpty()) {
60 return Util::variantToString(value
);
61 } else if (index
.column() == 2) {
62 return prop
.typeName();
63 } else if (index
.column() == 3) {
64 const QMetaObject
*mo
= m_obj
.data()->metaObject();
65 while (mo
->propertyOffset() > index
.row()) {
66 mo
= mo
->superClass();
68 return mo
->className();
70 } else if (role
== Qt::DecorationRole
) {
71 if (index
.column() == 1) {
72 return Util::decorationForVariant(prop
.read(m_obj
.data()));
74 } else if (role
== Qt::EditRole
) {
75 if (index
.column() == 1) {
76 return prop
.read(m_obj
.data());
78 } else if (role
== Qt::ToolTipRole
) {
79 const QString toolTip
=
80 tr("Constant: %1\nDesignable: %2\nFinal: %3\nResetable: %4\n"
81 "Has notification: %5\nScriptable: %6\nStored: %7\nUser: %8\nWritable: %9").
82 arg(translateBool(prop
.isConstant())).
83 arg(translateBool(prop
.isDesignable(m_obj
.data()))).
84 arg(translateBool(prop
.isFinal())).
85 arg(translateBool(prop
.isResettable())).
86 arg(translateBool(prop
.hasNotifySignal())).
87 arg(translateBool(prop
.isScriptable(m_obj
.data()))).
88 arg(translateBool(prop
.isStored(m_obj
.data()))).
89 arg(translateBool(prop
.isUser(m_obj
.data()))).
90 arg(translateBool(prop
.isWritable()));
97 bool ObjectStaticPropertyModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
99 if (index
.isValid() && m_obj
&& index
.column() == 1 && index
.row() >= 0 &&
100 index
.row() < m_obj
.data()->metaObject()->propertyCount() && role
== Qt::EditRole
) {
101 const QMetaProperty prop
= m_obj
.data()->metaObject()->property(index
.row());
102 return prop
.write(m_obj
.data(), value
);
104 return ObjectPropertyModel::setData(index
, value
, role
);
107 int ObjectStaticPropertyModel::columnCount(const QModelIndex
&parent
) const
109 if (parent
.isValid()) {
115 int ObjectStaticPropertyModel::rowCount(const QModelIndex
&parent
) const
117 if (!m_obj
|| parent
.isValid()) {
120 return m_obj
.data()->metaObject()->propertyCount();
123 Qt::ItemFlags
ObjectStaticPropertyModel::flags(const QModelIndex
&index
) const
125 const Qt::ItemFlags flags
= ObjectPropertyModel::flags(index
);
127 if (!index
.isValid() || !m_obj
|| index
.column() != 1 || index
.row() < 0 ||
128 index
.row() >= m_obj
.data()->metaObject()->propertyCount()) {
132 const QMetaProperty prop
= m_obj
.data()->metaObject()->property(index
.row());
133 if (prop
.isWritable()) {
134 return flags
| Qt::ItemIsEditable
;
139 #include "objectstaticpropertymodel.moc"