1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "watchdelegatewidgets.h"
27
28 #include <QDoubleValidator>
29 #include <QDebug>
30
31 #include <utils/qtcassert.h>
32
33 enum { debug = 0 };
34
35 namespace Debugger {
36 namespace Internal {
37
38 // Basic watch line edit.
WatchLineEdit(QWidget * parent)39 WatchLineEdit::WatchLineEdit(QWidget *parent)
40 : QLineEdit(parent)
41 {}
42
modelData() const43 QVariant WatchLineEdit::modelData() const
44 {
45 return QVariant(text());
46 }
47
setModelData(const QVariant & v)48 void WatchLineEdit::setModelData(const QVariant &v)
49 {
50 if (debug)
51 qDebug("WatchLineEdit::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));
52 setText(v.toString());
53 }
54
55 /* ------ IntegerWatchLineEdit helpers:
56 * Integer validator using different number bases. */
57 class IntegerValidator : public QValidator
58 {
59 public:
60 explicit IntegerValidator(QObject *parent);
61 State validate(QString &, int &) const override;
62
base() const63 int base() const { return m_base; }
setBase(int b)64 void setBase(int b) { m_base = b; }
isSigned() const65 bool isSigned() const { return m_signed; }
setSigned(bool s)66 void setSigned(bool s) { m_signed = s; }
isBigInt() const67 bool isBigInt() const { return m_bigInt; }
setBigInt(bool b)68 void setBigInt(bool b) { m_bigInt = b; }
69
70 static State validateEntry(const QString &s, int base, bool signedV, bool bigInt);
71
72 private:
73 static inline bool isCharAcceptable(const QChar &c, int base);
74
75 int m_base;
76 bool m_signed;
77 bool m_bigInt;
78 };
79
IntegerValidator(QObject * parent)80 IntegerValidator::IntegerValidator(QObject *parent) :
81 QValidator(parent), m_base(10), m_signed(true), m_bigInt(false)
82 {
83 }
84
85 // Check valid digits depending on base.
isCharAcceptable(const QChar & c,int base)86 bool IntegerValidator::isCharAcceptable(const QChar &c, int base)
87 {
88 if (c.isLetter())
89 return base == 16 && c.toLower().toLatin1() <= 'f';
90 if (!c.isDigit())
91 return false;
92 const int digit = c.toLatin1() - '0';
93 if (base == 8 && digit > 7)
94 return false;
95 if (base == 2 && digit > 1)
96 return false;
97 return true;
98 }
99
validate(QString & s,int &) const100 QValidator::State IntegerValidator::validate(QString &s, int &) const
101 {
102 return IntegerValidator::validateEntry(s, m_base, m_signed, m_bigInt);
103 }
104
validateEntry(const QString & s,int base,bool signedV,bool bigInt)105 QValidator::State IntegerValidator::validateEntry(const QString &s, int base, bool signedV, bool bigInt)
106 {
107 const int size = s.size();
108 if (!size)
109 return QValidator::Intermediate;
110 int pos = 0;
111 // Skip sign.
112 if (signedV && s.at(pos) == '-') {
113 pos++;
114 if (pos == size)
115 return QValidator::Intermediate;
116 }
117 // Hexadecimal: '0x'?
118 if (base == 16 && pos + 2 <= size
119 && s.at(pos) == '0' && s.at(pos + 1) == 'x') {
120 pos+= 2;
121 if (pos == size)
122 return QValidator::Intermediate;
123 }
124
125 // Check characters past sign.
126 for (; pos < size; pos++)
127 if (!isCharAcceptable(s.at(pos), base))
128 return QValidator::Invalid;
129 // Check conversion unless big integer
130 if (bigInt)
131 return QValidator::Acceptable;
132 bool ok;
133 if (signedV)
134 s.toLongLong(&ok, base);
135 else
136 s.toULongLong(&ok, base);
137 return ok ? QValidator::Acceptable : QValidator::Intermediate;
138 }
139
IntegerWatchLineEdit(QWidget * parent)140 IntegerWatchLineEdit::IntegerWatchLineEdit(QWidget *parent) :
141 WatchLineEdit(parent),
142 m_validator(new IntegerValidator(this))
143 {
144 setValidator(m_validator);
145 }
146
isUnsignedHexNumber(const QString & v)147 bool IntegerWatchLineEdit::isUnsignedHexNumber(const QString &v)
148 {
149 return IntegerValidator::validateEntry(v, 16, false, true) == QValidator::Acceptable;
150 }
151
base() const152 int IntegerWatchLineEdit::base() const
153 {
154 return m_validator->base();
155 }
156
setBase(int b)157 void IntegerWatchLineEdit::setBase(int b)
158 {
159 QTC_ASSERT(b, return);
160 m_validator->setBase(b);
161 }
162
isSigned() const163 bool IntegerWatchLineEdit::isSigned() const
164 {
165 return m_validator->isSigned();
166 }
167
setSigned(bool s)168 void IntegerWatchLineEdit::setSigned(bool s)
169 {
170 m_validator->setSigned(s);
171 }
172
isBigInt() const173 bool IntegerWatchLineEdit::isBigInt() const
174 {
175 return m_validator->isBigInt();
176 }
177
setBigInt(bool b)178 void IntegerWatchLineEdit::setBigInt(bool b)
179 {
180 m_validator->setBigInt(b);
181 }
182
modelDataI() const183 QVariant IntegerWatchLineEdit::modelDataI() const
184 {
185 if (isBigInt()) // Big integer: Plain text
186 return QVariant(text());
187 bool ok;
188 if (isSigned()) {
189 const qint64 value = text().toLongLong(&ok, base());
190 if (ok)
191 return QVariant(value);
192 } else {
193 const quint64 value = text().toULongLong(&ok, base());
194 if (ok)
195 return QVariant(value);
196 }
197 return QVariant();
198 }
199
modelData() const200 QVariant IntegerWatchLineEdit::modelData() const
201 {
202 const QVariant data = modelDataI();
203 if (debug)
204 qDebug("IntegerLineEdit::modelData(): base=%d, signed=%d, bigint=%d returns %s '%s'",
205 base(), isSigned(), isBigInt(), data.typeName(), qPrintable(data.toString()));
206 return data;
207 }
208
setModelData(const QVariant & v)209 void IntegerWatchLineEdit::setModelData(const QVariant &v)
210 {
211 if (debug)
212 qDebug(">IntegerLineEdit::setModelData(%s, '%s'): base=%d, signed=%d, bigint=%d",
213 v.typeName(), qPrintable(v.toString()),
214 base(), isSigned(), isBigInt());
215 switch (v.type()) {
216 case QVariant::Int:
217 case QVariant::LongLong: {
218 const qint64 iv = v.toLongLong();
219 setSigned(true);
220 setText(QString::number(iv, base()));
221 }
222 break;
223 case QVariant::UInt:
224 case QVariant::ULongLong: {
225 const quint64 iv = v.toULongLong();
226 setSigned(false);
227 setText(QString::number(iv, base()));
228 }
229 break;
230 case QVariant::ByteArray:
231 setNumberText(QString::fromLatin1(v.toByteArray()));
232 break;
233 case QVariant::String:
234 setNumberText(v.toString());
235 break;
236 default:
237 qWarning("Invalid value (%s) passed to IntegerLineEdit::setModelData",
238 v.typeName());
239 setText(QString('0'));
240 break;
241 }
242 if (debug)
243 qDebug("<IntegerLineEdit::setModelData(): base=%d, signed=%d, bigint=%d",
244 base(), isSigned(), isBigInt());
245 }
246
setNumberText(const QString & t)247 void IntegerWatchLineEdit::setNumberText(const QString &t)
248 {
249 setText(t);
250 }
251
252 // ------------- FloatWatchLineEdit
FloatWatchLineEdit(QWidget * parent)253 FloatWatchLineEdit::FloatWatchLineEdit(QWidget *parent) :
254 WatchLineEdit(parent)
255 {
256 setValidator(new QDoubleValidator(this));
257 }
258
modelData() const259 QVariant FloatWatchLineEdit::modelData() const
260 {
261 return QVariant(text().toDouble());
262 }
263
setModelData(const QVariant & v)264 void FloatWatchLineEdit::setModelData(const QVariant &v)
265 {
266 if (debug)
267 qDebug("FloatWatchLineEdit::setModelData(%s, '%s')",
268 v.typeName(), qPrintable(v.toString()));
269 switch (v.type()) {
270 case QVariant::Double:
271 case QVariant::String:
272 setText(v.toString());
273 break;
274 case QVariant::ByteArray:
275 setText(QString::fromLatin1(v.toByteArray()));
276 break;
277 default:
278 qWarning("Invalid value (%s) passed to FloatWatchLineEdit::setModelData",
279 v.typeName());
280 setText(QString::number(0.0));
281 break;
282 }
283 }
284
create(QVariant::Type t,QWidget * parent)285 WatchLineEdit *WatchLineEdit::create(QVariant::Type t, QWidget *parent)
286 {
287 switch (t) {
288 case QVariant::Bool:
289 case QVariant::Int:
290 case QVariant::UInt:
291 case QVariant::LongLong:
292 case QVariant::ULongLong:
293 return new IntegerWatchLineEdit(parent);
294 break;
295 case QVariant::Double:
296 return new FloatWatchLineEdit(parent);
297 default:
298 break;
299 }
300 return new WatchLineEdit(parent);
301 }
302
BooleanComboBox(QWidget * parent)303 BooleanComboBox::BooleanComboBox(QWidget *parent) : QComboBox(parent)
304 {
305 QStringList items;
306 items << "false" << "true";
307 addItems(items);
308 }
309
modelData() const310 QVariant BooleanComboBox::modelData() const
311 {
312 // As not to confuse debuggers with 'true', 'false', we return integers 1,0.
313 const int rc = currentIndex() == 1 ? 1 : 0;
314 return QVariant(rc);
315 }
316
setModelData(const QVariant & v)317 void BooleanComboBox::setModelData(const QVariant &v)
318 {
319 if (debug)
320 qDebug("BooleanComboBox::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));
321
322 bool value = false;
323 switch (v.type()) {
324 case QVariant::Bool:
325 value = v.toBool();
326 break;
327 case QVariant::Int:
328 case QVariant::UInt:
329 case QVariant::LongLong:
330 case QVariant::ULongLong:
331 value = v.toInt() != 0;
332 break;
333 default:
334 qWarning("Invalid value (%s) passed to BooleanComboBox::setModelData", v.typeName());
335 break;
336 }
337 setCurrentIndex(value ? 1 : 0);
338 }
339
340 } // namespace Internal
341 } // namespace Debugger
342