OSDN Git Service

3e1c24ecd5fdab1acf1cdf6d9f31c5f2fc39d173
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / moduleshandler.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "moduleshandler.h"
35
36 #include <utils/qtcassert.h>
37
38 #include <QtCore/QDebug>
39 #include <QtGui/QSortFilterProxyModel>
40
41
42 //////////////////////////////////////////////////////////////////
43 //
44 // ModulesModel
45 //
46 //////////////////////////////////////////////////////////////////
47
48 namespace Debugger {
49 namespace Internal {
50
51 ModulesModel::ModulesModel(ModulesHandler *parent)
52   : QAbstractItemModel(parent)
53 {}
54
55 QVariant ModulesModel::headerData(int section,
56     Qt::Orientation orientation, int role) const
57 {
58     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
59         static QString headers[] = {
60             tr("Module name") + "        ",
61             tr("Module path") + "        ",
62             tr("Symbols read") + "        ",
63             tr("Symbols type") + "        ",
64             tr("Start address") + "        ",
65             tr("End address") + "        "
66         };
67         return headers[section];
68     }
69     return QVariant();
70 }
71
72 QVariant ModulesModel::data(const QModelIndex &index, int role) const
73 {
74     int row = index.row();
75     if (row < 0 || row >= m_modules.size())
76         return QVariant();
77
78     const Module &module = m_modules.at(row);
79
80     switch (index.column()) {
81         case 0:
82             if (role == Qt::DisplayRole)
83                 return module.moduleName;
84             // FIXME: add icons
85             //if (role == Qt::DecorationRole)
86             //    return module.symbolsRead ? icon2 : icon;
87             break;
88         case 1:
89             if (role == Qt::DisplayRole)
90                 return module.modulePath;
91             break;
92         case 2:
93             if (role == Qt::DisplayRole)
94                 switch (module.symbolsRead) {
95                     case Module::UnknownReadState: return tr("unknown");
96                     case Module::ReadFailed: return tr("no");
97                     case Module::ReadOk: return tr("yes");
98                 }
99             break;
100         case 3:
101             if (role == Qt::DisplayRole)
102                 switch (module.symbolsType) {
103                     case Module::UnknownType: return tr("unknown");
104                     case Module::PlainSymbols: return tr("plain");
105                     case Module::FastSymbols: return tr("fast");
106                 }
107             break;
108         case 4:
109             if (role == Qt::DisplayRole)
110                 return QString(QLatin1String("0x")
111                             + QString::number(module.startAddress, 16));
112             break;
113         case 5:
114             if (role == Qt::DisplayRole) {
115                 if (module.endAddress)
116                     return QString(QLatin1String("0x")
117                                 + QString::number(module.endAddress, 16));
118                 //: End address of loaded module
119                 return tr("<unknown>", "address");
120             }
121             break;
122     }
123     return QVariant();
124 }
125
126 void ModulesModel::addModule(const Module &m)
127 {
128     beginInsertRows(QModelIndex(), m_modules.size(), m_modules.size());
129     m_modules.push_back(m);
130     endInsertRows();
131 }
132
133 void ModulesModel::setModules(const Modules &m)
134 {
135     m_modules = m;
136     reset();
137 }
138
139 void ModulesModel::clearModel()
140 {
141     if (!m_modules.isEmpty()) {
142         m_modules.clear();
143         reset();
144     }
145 }
146
147 int ModulesModel::indexOfModule(const QString &name) const
148 {
149     // Recent modules are more likely to be unloaded first.
150     for (int i = m_modules.size() - 1; i >= 0; i--)
151         if (m_modules.at(i).moduleName == name)
152             return i;
153     return -1;
154 }
155
156 void ModulesModel::removeModule(const QString &moduleName)
157 {
158     const int index = indexOfModule(moduleName);
159     QTC_ASSERT(index != -1, return);
160     beginRemoveRows(QModelIndex(), index, index);
161     m_modules.remove(index);
162     endRemoveRows();
163 }
164
165 void ModulesModel::updateModule(const QString &moduleName, const Module &module)
166 {
167     const int index = indexOfModule(moduleName);
168     QTC_ASSERT(index != -1, return);
169     m_modules[index] = module;
170     reset();
171 }
172
173 //////////////////////////////////////////////////////////////////
174 //
175 // ModulesHandler
176 //
177 //////////////////////////////////////////////////////////////////
178
179 ModulesHandler::ModulesHandler()
180 {
181     m_model = new ModulesModel(this);
182     m_proxyModel = new QSortFilterProxyModel(this);
183     m_proxyModel->setSourceModel(m_model);
184 }
185
186 QAbstractItemModel *ModulesHandler::model() const
187 {
188     return m_proxyModel;
189 }
190
191 void ModulesHandler::removeAll()
192 {
193     m_model->clearModel();
194 }
195
196 void ModulesHandler::addModule(const Module &module)
197 {
198     m_model->addModule(module);
199 }
200
201 void ModulesHandler::removeModule(const QString &moduleName)
202 {
203     m_model->removeModule(moduleName);
204 }
205
206 void ModulesHandler::updateModule(const QString &moduleName, const Module &module)
207 {
208     m_model->updateModule(moduleName, module);
209 }
210
211 void ModulesHandler::setModules(const Modules &modules)
212 {
213     m_model->setModules(modules);
214 }
215
216 Modules ModulesHandler::modules() const
217 {
218     return m_model->modules();
219 }
220
221 } // namespace Internal
222 } // namespace Debugger
223