Imported Upstream version 1.1.0
[gammaray-debian.git] / metapropertymodel.cpp
blob36b03293e503be0d5ad022dc7e147637d02e8c09
1 /*
2 metapropertymodel.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
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"
27 #include "util.h"
29 using namespace GammaRay;
31 MetaPropertyModel::MetaPropertyModel(QObject *parent):
32 QAbstractTableModel(parent),
33 m_metaObject(0),
34 m_object(0)
38 void MetaPropertyModel::setObject(void *object, const QString &typeName)
40 beginResetModel();
41 m_object = object;
42 m_metaObject = MetaObjectRepository::instance()->metaObject(typeName);
43 endResetModel();
46 void MetaPropertyModel::setObject(QObject *object)
48 beginResetModel();
49 m_object = 0;
50 m_metaObject = 0;
52 if (object) {
53 const QMetaObject *mo = object->metaObject();
54 while (mo && !m_metaObject) {
55 m_metaObject = MetaObjectRepository::instance()->metaObject(mo->className());
56 mo = mo->superClass();
58 if (m_metaObject) {
59 m_object = object;
62 endResetModel();
65 int MetaPropertyModel::columnCount(const QModelIndex &parent) const
67 Q_UNUSED(parent);
68 return 4;
71 int MetaPropertyModel::rowCount(const QModelIndex &parent) const
73 if (parent.isValid() || !m_metaObject) {
74 return 0;
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) {
82 switch (section) {
83 case 0:
84 return tr("Property");
85 case 1:
86 return tr("Value");
87 case 2:
88 return tr("Type");
89 case 3:
90 return tr("Class");
93 return QAbstractItemModel::headerData(section, orientation, role);
96 QVariant MetaPropertyModel::data(const QModelIndex &index, int role) const
98 if (!m_metaObject || !index.isValid()) {
99 return QVariant();
102 MetaProperty *property = m_metaObject->propertyAt(index.row());
103 if (role == Qt::DisplayRole) {
104 switch (index.column()) {
105 case 0:
106 return property->name();
107 case 2:
108 return property->typeName();
109 case 3:
110 return property->metaObject()->className();
114 if (index.column() == 1) {
115 if (!m_object) {
116 return QVariant();
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()));
121 switch (role) {
122 case Qt::DisplayRole:
123 return Util::variantToString(value);
124 case Qt::DecorationRole:
125 return Util::decorationForVariant(value);
126 case Qt::EditRole:
127 return value;
130 return QVariant();
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);
138 return true;
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) {
147 return f;
150 MetaProperty *property = m_metaObject->propertyAt(index.row());
151 if (property->isReadOnly()) {
152 return f;
154 return f | Qt::ItemIsEditable;
157 #include "metapropertymodel.moc"