1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "watchdelegatewidgets.h"
35 #include <QtGui/QDoubleValidator>
36 #include <QtCore/QDebug>
38 #include <utils/qtcassert.h>
45 // Basic watch line edit.
46 WatchLineEdit::WatchLineEdit(QWidget *parent)
50 QVariant WatchLineEdit::modelData() const
52 return QVariant(text());
55 void WatchLineEdit::setModelData(const QVariant &v)
58 qDebug("WatchLineEdit::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));
59 setText(v.toString());
62 /* ------ IntegerWatchLineEdit helpers:
63 * Integer validator using different number bases. */
64 class IntegerValidator : public QValidator
67 explicit IntegerValidator(QObject *parent);
68 virtual State validate(QString &, int &) const;
70 int base() const { return m_base; }
71 void setBase(int b) { m_base = b; }
72 bool isSigned() const { return m_signed; }
73 void setSigned(bool s) { m_signed = s; }
74 bool isBigInt() const { return m_bigInt; }
75 void setBigInt(bool b) { m_bigInt = b; }
77 static State validateEntry(const QString &s, int base, bool signedV, bool bigInt);
80 static inline bool isCharAcceptable(const QChar &c, int base);
87 IntegerValidator::IntegerValidator(QObject *parent) :
88 QValidator(parent), m_base(10), m_signed(true), m_bigInt(false)
92 // Check valid digits depending on base.
93 bool IntegerValidator::isCharAcceptable(const QChar &c, int base)
96 return base == 16 && c.toLower().toAscii() <= 'f';
99 const int digit = c.toAscii() - '0';
100 if (base == 8 && digit > 7)
102 if (base == 2 && digit > 1)
107 QValidator::State IntegerValidator::validate(QString &s, int &) const
109 return IntegerValidator::validateEntry(s, m_base, m_signed, m_bigInt);
112 QValidator::State IntegerValidator::validateEntry(const QString &s, int base, bool signedV, bool bigInt)
114 const int size = s.size();
116 return QValidator::Intermediate;
119 if (signedV && s.at(pos) == '-') {
122 return QValidator::Intermediate;
124 // Hexadecimal: '0x'?
125 if (base == 16 && pos + 2 <= size
126 && s.at(pos) == QLatin1Char('0') && s.at(pos + 1) == QLatin1Char('x')) {
129 return QValidator::Intermediate;
132 // Check characters past sign.
133 for (; pos < size; pos++)
134 if (!isCharAcceptable(s.at(pos), base))
135 return QValidator::Invalid;
136 // Check conversion unless big integer
138 return QValidator::Acceptable;
141 s.toLongLong(&ok, base);
143 s.toULongLong(&ok, base);
145 return ok ? QValidator::Acceptable : QValidator::Intermediate;
148 IntegerWatchLineEdit::IntegerWatchLineEdit(QWidget *parent) :
149 WatchLineEdit(parent),
150 m_validator(new IntegerValidator(this))
152 setValidator(m_validator);
155 bool IntegerWatchLineEdit::isUnsignedHexNumber(const QString &v)
157 return IntegerValidator::validateEntry(v, 16, false, true) == QValidator::Acceptable;
160 int IntegerWatchLineEdit::base() const
162 return m_validator->base();
165 void IntegerWatchLineEdit::setBase(int b)
167 QTC_ASSERT(b, return; )
168 m_validator->setBase(b);
171 bool IntegerWatchLineEdit::isSigned() const
173 return m_validator->isSigned();
176 void IntegerWatchLineEdit::setSigned(bool s)
178 m_validator->setSigned(s);
181 bool IntegerWatchLineEdit::isBigInt() const
183 return m_validator->isBigInt();
186 void IntegerWatchLineEdit::setBigInt(bool b)
188 m_validator->setBigInt(b);
191 QVariant IntegerWatchLineEdit::modelDataI() const
193 if (isBigInt()) // Big integer: Plain text
194 return QVariant(text());
197 const qint64 value = text().toLongLong(&ok, base());
199 return QVariant(value);
201 const quint64 value = text().toULongLong(&ok, base());
203 return QVariant(value);
208 QVariant IntegerWatchLineEdit::modelData() const
210 const QVariant data = modelDataI();
212 qDebug("IntegerLineEdit::modelData(): base=%d, signed=%d, bigint=%d returns %s '%s'",
213 base(), isSigned(), isBigInt(), data.typeName(), qPrintable(data.toString()));
217 void IntegerWatchLineEdit::setModelData(const QVariant &v)
220 qDebug(">IntegerLineEdit::setModelData(%s, '%s'): base=%d, signed=%d, bigint=%d",
221 v.typeName(), qPrintable(v.toString()),
222 base(), isSigned(), isBigInt());
225 case QVariant::LongLong: {
226 const qint64 iv = v.toLongLong();
228 setText(QString::number(iv, base()));
232 case QVariant::ULongLong: {
233 const quint64 iv = v.toULongLong();
235 setText(QString::number(iv, base()));
238 case QVariant::ByteArray:
239 setNumberText(QString::fromAscii(v.toByteArray()));
241 case QVariant::String:
242 setNumberText(v.toString());
245 qWarning("Invalid value (%s) passed to IntegerLineEdit::setModelData",
247 setText(QString(QLatin1Char('0')));
251 qDebug("<IntegerLineEdit::setModelData(): base=%d, signed=%d, bigint=%d",
252 base(), isSigned(), isBigInt());
255 void IntegerWatchLineEdit::setNumberText(const QString &t)
260 // ------------- FloatWatchLineEdit
261 FloatWatchLineEdit::FloatWatchLineEdit(QWidget *parent) :
262 WatchLineEdit(parent)
264 setValidator(new QDoubleValidator(this));
267 QVariant FloatWatchLineEdit::modelData() const
269 return QVariant(text().toDouble());
272 void FloatWatchLineEdit::setModelData(const QVariant &v)
275 qDebug("FloatWatchLineEdit::setModelData(%s, '%s')",
276 v.typeName(), qPrintable(v.toString()));
279 case QVariant::Double:
280 case QVariant::String:
281 setText(v.toString());
283 case QVariant::ByteArray:
284 setText(QString::fromAscii(v.toByteArray()));
287 qWarning("Invalid value (%s) passed to FloatWatchLineEdit::setModelData",
289 setText(QString::number(0.0));
294 WatchLineEdit *WatchLineEdit::create(QVariant::Type t, QWidget *parent)
300 case QVariant::LongLong:
301 case QVariant::ULongLong:
302 return new IntegerWatchLineEdit(parent);
304 case QVariant::Double:
305 return new FloatWatchLineEdit(parent);
309 return new WatchLineEdit(parent);
312 BooleanComboBox::BooleanComboBox(QWidget *parent) : QComboBox(parent)
315 items << QLatin1String("false") << QLatin1String("true");
319 QVariant BooleanComboBox::modelData() const
321 // As not to confuse debuggers with 'true', 'false', we return integers 1,0.
322 const int rc = currentIndex() == 1 ? 1 : 0;
326 void BooleanComboBox::setModelData(const QVariant &v)
329 qDebug("BooleanComboBox::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));
338 case QVariant::LongLong:
339 case QVariant::ULongLong:
340 value = v.toInt() != 0;
343 qWarning("Invalid value (%s) passed to BooleanComboBox::setModelData", v.typeName());
346 setCurrentIndex(value ? 1 : 0);
349 } // namespace Internal
350 } // namespace Debugger