1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "registerhandler.h"
35 #include "watchdelegatewidgets.h"
38 //////////////////////////////////////////////////////////////////
42 //////////////////////////////////////////////////////////////////
47 RegisterHandler::RegisterHandler()
53 int RegisterHandler::rowCount(const QModelIndex &parent) const
55 return parent.isValid() ? 0 : m_registers.size();
58 int RegisterHandler::columnCount(const QModelIndex &parent) const
60 return parent.isValid() ? 0 : 2;
63 // Editor value: Preferably number, else string.
64 QVariant Register::editValue() const
67 // Try to convert to number?
68 const qulonglong v = value.toULongLong(&ok, 0); // Autodetect format
71 return QVariant(value);
74 // Editor value: Preferably padded number, else padded string.
75 QString Register::displayValue(int base, int strlen) const
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;
86 QVariant RegisterHandler::data(const QModelIndex &index, int role) const
88 if (!index.isValid() || index.row() >= m_registers.size())
91 const Register ® = m_registers.at(index.row());
95 switch (index.column()) {
97 const QString padding = QLatin1String(" ");
98 return QVariant(padding + reg.name + padding);
100 case 1: // Display: Pad value for alignment
101 return reg.displayValue(m_base, m_strlen);
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();
113 QVariant RegisterHandler::headerData(int section, Qt::Orientation orientation,
116 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
118 case 0: return tr("Name");
119 case 1: return tr("Value (base %1)").arg(m_base);
125 Qt::ItemFlags RegisterHandler::flags(const QModelIndex &idx) const
128 return Qt::ItemFlags();
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;
138 void RegisterHandler::removeAll()
144 bool RegisterHandler::isEmpty() const
146 return m_registers.isEmpty();
149 void RegisterHandler::setRegisters(const Registers ®isters)
151 m_registers = registers;
156 void RegisterHandler::setAndMarkRegisters(const Registers ®isters)
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;
166 Registers RegisterHandler::registers() const
171 void RegisterHandler::calculateWidth()
173 m_strlen = (m_base == 2 ? 64 : m_base == 8 ? 32 : m_base == 10 ? 26 : 16);
176 void RegisterHandler::setNumberBase(int base)
178 if (m_base != base) {
185 } // namespace Internal
186 } // namespace Debugger