OSDN Git Service

31c8174618f1e7b976b2b0998df2e23e8565ed8e
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / registerhandler.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 "registerhandler.h"
35 #include "watchdelegatewidgets.h"
36
37
38 //////////////////////////////////////////////////////////////////
39 //
40 // RegisterHandler
41 //
42 //////////////////////////////////////////////////////////////////
43
44 namespace Debugger {
45 namespace Internal {
46
47 RegisterHandler::RegisterHandler()
48   : m_base(-1)
49 {
50     setNumberBase(16);
51 }
52
53 int RegisterHandler::rowCount(const QModelIndex &parent) const
54 {
55     return parent.isValid() ? 0 : m_registers.size();
56 }
57
58 int RegisterHandler::columnCount(const QModelIndex &parent) const
59 {
60     return parent.isValid() ? 0 : 2;
61 }
62
63 // Editor value: Preferably number, else string.
64 QVariant Register::editValue() const
65 {
66     bool ok = true;
67     // Try to convert to number?
68     const qulonglong v = value.toULongLong(&ok, 0); // Autodetect format
69     if (ok)
70         return QVariant(v);
71     return QVariant(value);
72 }
73
74 // Editor value: Preferably padded number, else padded string.
75 QString Register::displayValue(int base, int strlen) const
76 {
77     const QVariant editV = editValue();
78     if (editV.type() == QVariant::ULongLong)
79         return QString::fromAscii("%1").arg(editV.toULongLong(), strlen, base);
80     const QString stringValue = editV.toString();
81     if (stringValue.size() < strlen)
82         return QString(strlen - stringValue.size(), QLatin1Char(' ')) + value;
83     return stringValue;
84 }
85
86 QVariant RegisterHandler::data(const QModelIndex &index, int role) const
87 {
88     if (!index.isValid() || index.row() >= m_registers.size())
89         return QVariant();
90
91     const Register &reg = m_registers.at(index.row());
92
93     switch (role) {
94     case Qt::DisplayRole:
95         switch (index.column()) {
96         case 0: {
97             const QString padding = QLatin1String("  ");
98             return QVariant(padding + reg.name + padding);
99         }
100         case 1: // Display: Pad value for alignment
101             return reg.displayValue(m_base, m_strlen);
102         } // switch column
103     case Qt::EditRole: // Edit: Unpadded for editing
104         return reg.editValue();
105     case Qt::TextAlignmentRole:
106         return index.column() == 1 ? QVariant(Qt::AlignRight) : QVariant();
107     default:
108         break;
109     }
110     return QVariant();
111 }
112
113 QVariant RegisterHandler::headerData(int section, Qt::Orientation orientation,
114     int role) const
115 {
116     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
117         switch (section) {
118             case 0: return tr("Name");
119             case 1: return tr("Value (base %1)").arg(m_base);
120         };
121     }
122     return QVariant();
123 }
124
125 Qt::ItemFlags RegisterHandler::flags(const QModelIndex &idx) const
126 {
127     if (!idx.isValid())
128         return Qt::ItemFlags();
129
130     const Qt::ItemFlags notEditable = Qt::ItemIsSelectable|Qt::ItemIsEnabled;
131     // Can edit registers if they are hex numbers and not arrays.
132     if (idx.column() == 1
133         && IntegerWatchLineEdit::isUnsignedHexNumber(m_registers.at(idx.row()).value))
134         return notEditable | Qt::ItemIsEditable;
135     return notEditable;
136 }
137
138 void RegisterHandler::removeAll()
139 {
140     m_registers.clear();
141     reset();
142 }
143
144 bool RegisterHandler::isEmpty() const
145 {
146     return m_registers.isEmpty();
147 }
148
149 void RegisterHandler::setRegisters(const Registers &registers)
150 {
151     m_registers = registers;
152     calculateWidth();
153     reset();
154 }
155
156 void RegisterHandler::setAndMarkRegisters(const Registers &registers)
157 {
158     const Registers old = m_registers;
159     m_registers = registers;
160     for (int i = qMin(m_registers.size(), old.size()); --i >= 0; )
161         m_registers[i].changed = m_registers[i].value != old[i].value;
162     calculateWidth();
163     reset();
164 }
165
166 Registers RegisterHandler::registers() const
167 {
168     return m_registers;
169 }
170
171 void RegisterHandler::calculateWidth()
172 {
173     m_strlen = (m_base == 2 ? 64 : m_base == 8 ? 32 : m_base == 10 ? 26 : 16);
174 }
175
176 void RegisterHandler::setNumberBase(int base)
177 {
178     if (m_base != base) {
179         m_base = base;
180         calculateWidth();
181         emit reset();
182     }
183 }
184
185 } // namespace Internal
186 } // namespace Debugger