4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 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 "metapropertymodel.h"
25 #include "metaobjectrepository.h"
26 #include "metaobject.h"
29 using namespace GammaRay
;
31 MetaPropertyModel::MetaPropertyModel(QObject
*parent
):
32 QAbstractTableModel(parent
),
38 void MetaPropertyModel::setObject(void *object
, const QString
&typeName
)
42 m_metaObject
= MetaObjectRepository::instance()->metaObject(typeName
);
46 void MetaPropertyModel::setObject(QObject
*object
)
53 const QMetaObject
*mo
= object
->metaObject();
54 while (mo
&& !m_metaObject
) {
55 m_metaObject
= MetaObjectRepository::instance()->metaObject(mo
->className());
56 mo
= mo
->superClass();
65 int MetaPropertyModel::columnCount(const QModelIndex
&parent
) const
71 int MetaPropertyModel::rowCount(const QModelIndex
&parent
) const
73 if (parent
.isValid() || !m_metaObject
) {
76 return m_metaObject
->propertyCount();
79 QVariant
MetaPropertyModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
81 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
84 return tr("Property");
93 return QAbstractItemModel::headerData(section
, orientation
, role
);
96 QVariant
MetaPropertyModel::data(const QModelIndex
&index
, int role
) const
98 if (!m_metaObject
|| !index
.isValid()) {
102 MetaProperty
*property
= m_metaObject
->propertyAt(index
.row());
103 if (role
== Qt::DisplayRole
) {
104 switch (index
.column()) {
106 return property
->name();
108 return property
->typeName();
110 return property
->metaObject()->className();
114 if (index
.column() == 1) {
119 // TODO: cache this, to make this more robust against m_object becoming invalid
120 const QVariant value
= property
->value(m_metaObject
->castForPropertyAt(m_object
, index
.row()));
122 case Qt::DisplayRole
:
123 return Util::variantToString(value
);
124 case Qt::DecorationRole
:
125 return Util::decorationForVariant(value
);
133 bool MetaPropertyModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
135 if (index
.isValid() && index
.column() == 1 && m_metaObject
&& m_object
&& role
== Qt::EditRole
) {
136 MetaProperty
*property
= m_metaObject
->propertyAt(index
.row());
137 property
->setValue(m_metaObject
->castForPropertyAt(m_object
, index
.row()), value
);
140 return QAbstractItemModel::setData(index
, value
, role
);
143 Qt::ItemFlags
MetaPropertyModel::flags(const QModelIndex
&index
) const
145 const Qt::ItemFlags f
= QAbstractItemModel::flags(index
);
146 if (!index
.isValid() || index
.column() != 1 || !m_metaObject
|| !m_object
) {
150 MetaProperty
*property
= m_metaObject
->propertyAt(index
.row());
151 if (property
->isReadOnly()) {
154 return f
| Qt::ItemIsEditable
;
157 #include "metapropertymodel.moc"