1 /*
2 methodargumentmodel.cpp
3
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
6
7 Copyright (C) 2010-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
9
10 Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11 accordance with GammaRay Commercial License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #include "methodargumentmodel.h"
30
31 using namespace GammaRay;
32
MethodArgumentModel(QObject * parent)33 MethodArgumentModel::MethodArgumentModel(QObject *parent)
34 : QAbstractTableModel(parent)
35 {
36 }
37
setMethod(const QMetaMethod & method)38 void MethodArgumentModel::setMethod(const QMetaMethod &method)
39 {
40 beginResetModel();
41 m_method = method;
42 m_arguments.clear();
43 m_arguments.resize(method.parameterTypes().size());
44 for (int i = 0; i < m_arguments.size(); ++i) {
45 const QByteArray typeName = method.parameterTypes().at(i);
46 const QVariant::Type variantType = QVariant::nameToType(typeName);
47 m_arguments[i] = QVariant(variantType);
48 }
49 endResetModel();
50 }
51
data(const QModelIndex & index,int role) const52 QVariant MethodArgumentModel::data(const QModelIndex &index, int role) const
53 {
54 if (m_method.methodSignature().isEmpty()
55 || m_arguments.isEmpty()
56 || index.row() < 0
57 || index.row() >= m_arguments.size())
58 return QVariant();
59
60 if (role == Qt::DisplayRole || role == Qt::EditRole) {
61 const QVariant value = m_arguments.at(index.row());
62 const QByteArray parameterName = m_method.parameterNames().at(index.row());
63 const QByteArray parameterType = m_method.parameterTypes().at(index.row());
64 switch (index.column()) {
65 case 0:
66 if (parameterName.isEmpty())
67 return tr("<unnamed> (%1)").arg(QString::fromLatin1(parameterType));
68 return parameterName;
69 case 1:
70 return value;
71 case 2:
72 return parameterType;
73 }
74 }
75 return QVariant();
76 }
77
columnCount(const QModelIndex & parent) const78 int MethodArgumentModel::columnCount(const QModelIndex &parent) const
79 {
80 Q_UNUSED(parent);
81 return 3;
82 }
83
rowCount(const QModelIndex & parent) const84 int MethodArgumentModel::rowCount(const QModelIndex &parent) const
85 {
86 if (parent.isValid())
87 return 0;
88 return m_arguments.size();
89 }
90
setData(const QModelIndex & index,const QVariant & value,int role)91 bool MethodArgumentModel::setData(const QModelIndex &index, const QVariant &value, int role)
92 {
93 if (index.row() >= 0 && index.row() < m_arguments.size() && role == Qt::EditRole) {
94 m_arguments[index.row()] = value;
95 emit dataChanged(index, index);
96 return true;
97 }
98 return QAbstractItemModel::setData(index, value, role);
99 }
100
headerData(int section,Qt::Orientation orientation,int role) const101 QVariant MethodArgumentModel::headerData(int section, Qt::Orientation orientation, int role) const
102 {
103 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
104 switch (section) {
105 case 0:
106 return tr("Argument");
107 case 1:
108 return tr("Value");
109 case 2:
110 return tr("Type");
111 }
112 }
113 return QAbstractItemModel::headerData(section, orientation, role);
114 }
115
flags(const QModelIndex & index) const116 Qt::ItemFlags MethodArgumentModel::flags(const QModelIndex &index) const
117 {
118 const Qt::ItemFlags flags = QAbstractTableModel::flags(index);
119 if (index.column() == 1)
120 return flags | Qt::ItemIsEditable;
121 return flags;
122 }
123
arguments() const124 QVector<MethodArgument> MethodArgumentModel::arguments() const
125 {
126 QVector<MethodArgument> args(10);
127 for (int i = 0; i < rowCount(); ++i)
128 args[i] = MethodArgument(m_arguments.at(i));
129 return args;
130 }
131