OSDN Git Service

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