d/control: Recommend gdb
[gammaray-debian.git] / toolmodel.cpp
blobbb0f5e90d87df95ba62de3dde0ae11bfbeb2656d
1 /*
2 toolmodel.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 "config-gammaray.h"
25 #include "toolmodel.h"
26 #include "toolfactory.h"
28 #include "tools/codecbrowser/codecbrowser.h"
29 #include "tools/connectioninspector/connectioninspector.h"
30 #include "tools/fontbrowser/fontbrowser.h"
31 #include "tools/localeinspector/localeinspector.h"
32 #include "tools/metatypebrowser/metatypebrowser.h"
33 #include "tools/modelinspector/modelinspector.h"
34 #include "tools/objectinspector/objectinspector.h"
35 #include "tools/resourcebrowser/resourcebrowser.h"
36 #include "tools/sceneinspector/sceneinspector.h"
37 #include "tools/selectionmodelinspector/selectionmodelinspector.h"
38 #include "tools/textdocumentinspector/textdocumentinspector.h"
39 #include "tools/widgetinspector/widgetinspector.h"
40 #include "tools/messagehandler/messagehandler.h"
41 #ifdef BUILD_TIMER_PLUGIN
42 #include "tools/timertop/timertop.h"
43 #endif
45 #include "pluginmanager.h"
46 #include "probe.h"
47 #include "readorwritelocker.h"
49 #include <QCoreApplication>
50 #include <QDebug>
51 #include <QDir>
52 #include <QLibrary>
53 #include <QThread>
55 using namespace GammaRay;
57 ToolModel::ToolModel(QObject *parent): QAbstractListModel(parent)
59 // built-in tools
60 m_tools.push_back(new ObjectInspectorFactory(this));
61 m_tools.push_back(new WidgetInspectorFactory(this));
62 m_tools.push_back(new ModelInspector(this));
63 m_tools.push_back(new SceneInspectorFactory(this));
64 m_tools.push_back(new ConnectionInspectorFactory(this));
65 m_tools.push_back(new ResourceBrowserFactory(this));
66 m_tools.push_back(new MetaTypeBrowserFactory(this));
67 m_tools.push_back(new SelectionModelInspectorFactory(this));
68 m_tools.push_back(new FontBrowserFactory(this));
69 m_tools.push_back(new CodecBrowserFactory(this));
70 m_tools.push_back(new TextDocumentInspectorFactory(this));
71 m_tools.push_back(new MessageHandlerFactory(this));
72 m_tools.push_back(new LocaleInspectorFactory(this));
73 #ifdef BUILD_TIMER_PLUGIN
74 m_tools.push_back(new TimerTopFactory(this));
75 #endif
77 Q_FOREACH (ToolFactory *factory, PluginManager::instance()->plugins()) {
78 m_tools.push_back(factory);
81 // everything but the object inspector is inactive initially
82 for (int i = 1; i < m_tools.size(); ++i) {
83 m_inactiveTools.insert(m_tools.at(i));
87 QVariant ToolModel::headerData(int section, Qt::Orientation orientation, int role) const
89 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
90 switch (section) {
91 case 0:
92 return tr("Probe");
93 default:
94 return tr("N/A");
97 return QAbstractItemModel::headerData(section, orientation, role);
100 QVariant ToolModel::data(const QModelIndex &index, int role) const
102 if (!index.isValid()) {
103 return QVariant();
106 ToolFactory *toolIface = m_tools.at(index.row());
107 if (role == Qt::DisplayRole) {
108 return toolIface->name();
109 } else if (role == ToolFactoryRole) {
110 return QVariant::fromValue(toolIface);
111 } else if (role == ToolWidgetRole) {
112 return QVariant::fromValue(m_toolWidgets.value(toolIface));
113 } else if (role == ToolIdRole) {
114 return toolIface->id();
116 return QVariant();
119 bool ToolModel::setData(const QModelIndex &index, const QVariant &value, int role)
121 if (index.isValid() && role == Qt::EditRole) {
122 ToolFactory *toolIface = m_tools.at(index.row());
123 m_toolWidgets.insert(toolIface, value.value<QWidget*>());
124 return true;
126 return QAbstractItemModel::setData(index, value, role);
129 int ToolModel::rowCount(const QModelIndex &parent) const
131 if (parent.isValid()) {
132 return 0;
134 return m_tools.size();
137 Qt::ItemFlags ToolModel::flags(const QModelIndex &index) const
139 Qt::ItemFlags flags = QAbstractItemModel::flags(index);
140 if (index.isValid()) {
141 ToolFactory *toolIface = m_tools.at(index.row());
142 if (m_inactiveTools.contains(toolIface)) {
143 flags &= ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
146 return flags;
149 void ToolModel::objectAdded(QObject *obj)
151 // delay to main thread if required
152 QMetaObject::invokeMethod(this, "objectAddedMainThread",
153 Qt::AutoConnection, Q_ARG(QObject *, obj));
156 void ToolModel::objectAddedMainThread(QObject *obj)
158 ReadOrWriteLocker lock(Probe::instance()->objectLock());
160 if (Probe::instance()->isValidObject(obj)) {
161 objectAdded(obj->metaObject());
165 void ToolModel::objectAdded(const QMetaObject *mo)
167 Q_ASSERT(thread() == QThread::currentThread());
168 foreach (ToolFactory *factory, m_inactiveTools) {
169 if (factory->supportedTypes().contains(mo->className())) {
170 m_inactiveTools.remove(factory);
171 factory->init(Probe::instance());
172 emit dataChanged(index(0, 0), index(rowCount() - 1, 0));
175 if (mo->superClass()) {
176 objectAdded(mo->superClass());
180 #include "toolmodel.moc"