Install gammaray_probe.so to /usr/lib/gammaray
[gammaray-debian.git] / methodargumentmodel.cpp
blob6986ea1ae20081e417f52c279b4c1001d41f531d
1 /*
2 methodargumentmodel.cpp
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 #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()
40 if (m_data) {
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)
60 m_method = method;
61 m_arguments.clear();
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);
68 reset();
71 QVariant MethodArgumentModel::data(const QModelIndex &index, int role) const
73 if (!m_method.signature() || m_arguments.isEmpty() ||
74 index.row() < 0 ||
75 index.row() >= m_arguments.size()) {
76 return QVariant();
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()) {
84 case 0:
85 if (parameterName.isEmpty()) {
86 return tr("<unnamed> (%1)").arg(QString::fromLatin1(parameterType));
88 return parameterName;
89 case 1:
90 return value;
91 case 2:
92 return parameterType;
95 return QVariant();
98 int MethodArgumentModel::columnCount(const QModelIndex &parent) const
100 Q_UNUSED(parent);
101 return 3;
104 int MethodArgumentModel::rowCount(const QModelIndex &parent) const
106 if (parent.isValid()) {
107 return 0;
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;
116 return true;
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) {
124 switch (section) {
125 case 0:
126 return tr("Argument");
127 case 1:
128 return tr("Value");
129 case 2:
130 return tr("Type");
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;
142 return flags;
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));
151 return args;
154 #include "methodargumentmodel.moc"