2 methodargumentmodel.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 "methodargumentmodel.h"
25 #include <QtCore/qsharedpointer.h>
27 using namespace GammaRay
;
29 // TODO: this should be implicitly shared to avoid m_data double deletion
30 SafeArgument::SafeArgument() : m_data(0)
34 SafeArgument::SafeArgument(const QVariant
&v
) : m_value(v
), m_name(v
.typeName()), m_data(0)
38 SafeArgument::~SafeArgument()
41 QMetaType::destroy(m_value
.type(), m_data
);
45 SafeArgument::operator QGenericArgument() const
47 if (m_value
.isValid()) {
48 m_data
= QMetaType::construct(m_value
.type(), m_value
.constData());
49 return QGenericArgument(m_name
.data(), m_data
);
51 return QGenericArgument();
54 MethodArgumentModel::MethodArgumentModel(QObject
*parent
) : QAbstractTableModel(parent
)
58 void MethodArgumentModel::setMethod(const QMetaMethod
&method
)
62 m_arguments
.resize(method
.parameterTypes().size());
63 for (int i
= 0; i
< m_arguments
.size(); ++i
) {
64 const QByteArray typeName
= method
.parameterTypes().at(i
);
65 const QVariant::Type variantType
= QVariant::nameToType(typeName
);
66 m_arguments
[i
] = QVariant(variantType
);
71 QVariant
MethodArgumentModel::data(const QModelIndex
&index
, int role
) const
73 if (!m_method
.signature() || m_arguments
.isEmpty() ||
75 index
.row() >= m_arguments
.size()) {
79 if (role
== Qt::DisplayRole
|| role
== Qt::EditRole
) {
80 const QVariant value
= m_arguments
.at(index
.row());
81 const QByteArray parameterName
= m_method
.parameterNames().at(index
.row());
82 const QByteArray parameterType
= m_method
.parameterTypes().at(index
.row());
83 switch (index
.column()) {
85 if (parameterName
.isEmpty()) {
86 return tr("<unnamed> (%1)").arg(QString::fromLatin1(parameterType
));
98 int MethodArgumentModel::columnCount(const QModelIndex
&parent
) const
104 int MethodArgumentModel::rowCount(const QModelIndex
&parent
) const
106 if (parent
.isValid()) {
109 return m_arguments
.size();
112 bool MethodArgumentModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
114 if (index
.row() >= 0 && index
.row() < m_arguments
.size() && role
== Qt::EditRole
) {
115 m_arguments
[index
.row()] = value
;
118 return QAbstractItemModel::setData(index
, value
, role
);
121 QVariant
MethodArgumentModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
123 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
126 return tr("Argument");
133 return QAbstractItemModel::headerData(section
, orientation
, role
);
136 Qt::ItemFlags
MethodArgumentModel::flags(const QModelIndex
&index
) const
138 const Qt::ItemFlags flags
= QAbstractItemModel::flags(index
);
139 if (index
.column() == 1) {
140 return flags
| Qt::ItemIsEditable
;
145 QVector
<SafeArgument
> MethodArgumentModel::arguments() const
147 QVector
<SafeArgument
> args(10);
148 for (int i
= 0; i
< rowCount(); ++i
) {
149 args
[i
] = SafeArgument(m_arguments
.at(i
));
154 #include "methodargumentmodel.moc"