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 "objectlistmodel.h"
26 #include "readorwritelocker.h"
31 using namespace GammaRay
;
33 ObjectListModel::ObjectListModel(QObject
*parent
)
34 : ObjectModelBase
< QAbstractTableModel
>(parent
),
35 m_lock(QReadWriteLock::Recursive
)
39 QVariant
ObjectListModel::data(const QModelIndex
&index
, int role
) const
41 ReadOrWriteLocker
lock(&m_lock
);
42 if (index
.row() >= 0 && index
.row() < m_objects
.size()) {
43 QObject
*obj
= m_objects
.at(index
.row());
45 return dataForObject(obj
, index
, role
);
51 int ObjectListModel::columnCount(const QModelIndex
&parent
) const
53 if (parent
.isValid()) {
56 return ObjectModelBase
<QAbstractTableModel
>::columnCount(parent
);
59 int ObjectListModel::rowCount(const QModelIndex
&parent
) const
61 if (parent
.isValid()) {
65 ReadOrWriteLocker
lock(&m_lock
);
66 return m_objects
.size();
69 void ObjectListModel::objectAdded(QObject
*obj
)
71 // when called from background, delay into foreground, otherwise call directly
72 QMetaObject::invokeMethod(this, "objectAddedMainThread", Qt::AutoConnection
,
73 Q_ARG(QObject
*, obj
));
76 void ObjectListModel::objectAddedMainThread(QObject
*obj
)
78 ReadOrWriteLocker
objectLock(Probe::instance()->objectLock());
79 if (!Probe::instance()->isValidObject(obj
)) {
83 QWriteLocker
lock(&m_lock
);
84 if (m_objects
.contains(obj
)) {
88 beginInsertRows(QModelIndex(), m_objects
.size(), m_objects
.size());
93 void ObjectListModel::objectRemoved(QObject
*obj
)
95 if (thread() != QThread::currentThread()) {
97 QWriteLocker
lock(&m_lock
);
98 const int index
= m_objects
.indexOf(obj
);
100 m_objects
[index
] = 0;
104 // when called from background, delay into foreground, otherwise call directly
105 QMetaObject::invokeMethod(this, "objectRemovedMainThread", Qt::AutoConnection
,
106 Q_ARG(QObject
*, obj
));
109 void ObjectListModel::objectRemovedMainThread(QObject
*obj
)
111 QWriteLocker
lock(&m_lock
);
113 for (int i
= 0; i
< m_objects
.size(); ++i
) {
114 if (!m_objects
.at(i
) || m_objects
.at(i
) == obj
) {
115 beginRemoveRows(QModelIndex(), i
, i
);
122 #include "objectlistmodel.moc"