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 #ifndef GAMMARAY_METAOBJECTMODEL_H
25 #define GAMMARAY_METAOBJECTMODEL_H
27 #include <qabstractitemmodel.h>
28 #include <QtCore/qsharedpointer.h>
29 #include <QMetaObject>
34 template <typename MetaThing
,
35 MetaThing (QMetaObject::*MetaAccessor
)(int) const,
36 int (QMetaObject::*MetaCount
)() const,
37 int (QMetaObject::*MetaOffset
)() const>
38 class MetaObjectModel
: public QAbstractItemModel
41 explicit MetaObjectModel(QObject
*parent
= 0) : QAbstractItemModel(parent
) {}
43 virtual void setObject(QObject
*object
)
49 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const
51 if (!index
.isValid() || !m_object
||
52 index
.row() < 0 || index
.row() >= rowCount(index
.parent())) {
56 const MetaThing metaThing
= (m_object
.data()->metaObject()->*MetaAccessor
)(index
.row());
57 if (index
.column() == columnCount(index
) - 1 && role
== Qt::DisplayRole
) {
58 const QMetaObject
*mo
= m_object
.data()->metaObject();
59 while ((mo
->*MetaOffset
)() > index
.row()) {
60 mo
= mo
->superClass();
62 return mo
->className();
64 return metaData(index
, metaThing
, role
);
67 QVariant
headerData(int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const
69 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
70 if (section
== columnCount() - 1) {
73 return columnHeader(section
);
75 return QAbstractItemModel::headerData(section
, orientation
, role
);
78 int rowCount(const QModelIndex
&parent
= QModelIndex()) const
80 if (!m_object
|| parent
.isValid()) {
83 return (m_object
.data()->metaObject()->*MetaCount
)();
86 QModelIndex
index(int row
, int column
, const QModelIndex
&parent
= QModelIndex()) const
88 if (row
>= 0 && row
< rowCount(parent
) && column
>= 0 &&
89 column
< columnCount(parent
) && !parent
.isValid()) {
90 return createIndex(row
, column
, -1);
95 QModelIndex
parent(const QModelIndex
&child
) const
102 virtual QVariant
metaData(const QModelIndex
&index
,
103 const MetaThing
&metaThing
, int role
) const = 0;
105 virtual QString
columnHeader(int index
) const = 0;
108 QPointer
<QObject
> m_object
;
113 #endif // GAMMARAY_METAOBJECTMODEL_H