Updated d/copyright
[gammaray-debian.git] / metaobjectmodel.h
blob22da8473d49f5e6bead0ddf29fa65871ca738f79
1 /*
2 metaobjectmodel.h
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 #ifndef GAMMARAY_METAOBJECTMODEL_H
25 #define GAMMARAY_METAOBJECTMODEL_H
27 #include <qabstractitemmodel.h>
28 #include <QtCore/qsharedpointer.h>
29 #include <QMetaObject>
30 #include <QPointer>
32 namespace GammaRay {
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
40 public:
41 explicit MetaObjectModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
43 virtual void setObject(QObject *object)
45 m_object = object;
46 reset();
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())) {
53 return QVariant();
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) {
71 return tr("Class");
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()) {
81 return 0;
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);
92 return QModelIndex();
95 QModelIndex parent(const QModelIndex &child) const
97 Q_UNUSED(child);
98 return QModelIndex();
101 protected:
102 virtual QVariant metaData(const QModelIndex &index,
103 const MetaThing &metaThing, int role) const = 0;
105 virtual QString columnHeader(int index) const = 0;
107 protected:
108 QPointer<QObject> m_object;
113 #endif // GAMMARAY_METAOBJECTMODEL_H