watchdelegatewidgets.cpp 9.45 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
16
17
18
19
20
21
22
23
24
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
**
**************************************************************************/

#include "watchdelegatewidgets.h"

#include <QtGui/QDoubleValidator>
#include <QtCore/QDebug>

#include <utils/qtcassert.h>

enum { debug = 0 };

namespace Debugger {
namespace Internal {

// Basic watch line edit.
47
48
49
WatchLineEdit::WatchLineEdit(QWidget *parent)
    : QLineEdit(parent)
{}
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

QVariant WatchLineEdit::modelData() const
{
    return QVariant(text());
}

void WatchLineEdit::setModelData(const QVariant &v)
{
    if (debug)
        qDebug("WatchLineEdit::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));
    setText(v.toString());
}

 /* ------ IntegerWatchLineEdit helpers:
  *        Integer validator using different number bases. */
65
66
class IntegerValidator : public QValidator
{
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
public:
    explicit IntegerValidator(QObject *parent);
    virtual State validate(QString &, int &) const;

    int base() const           { return m_base; }
    void setBase(int b)        { m_base = b; }
    bool isSigned() const      { return m_signed; }
    void setSigned(bool s)     { m_signed = s; }
    bool isBigInt() const      { return m_bigInt; }
    void setBigInt(bool b)     { m_bigInt = b; }

    static State validateEntry(const QString &s, int base, bool signedV, bool bigInt);

private:
    static inline bool isCharAcceptable(const QChar &c, int base);

    int m_base;
    bool m_signed;
    bool m_bigInt;
};

IntegerValidator::IntegerValidator(QObject *parent) :
    QValidator(parent), m_base(10), m_signed(true), m_bigInt(false)
{
}

// Check valid digits depending on base.
bool IntegerValidator::isCharAcceptable(const QChar &c, int base)
{
    if (c.isLetter())
        return base == 16 && c.toLower().toAscii() <= 'f';
    if (!c.isDigit())
        return false;
    const int digit = c.toAscii() - '0';
    if (base == 8 && digit > 7)
        return false;
    if (base == 2 && digit > 1)
        return false;
    return true;
}

QValidator::State IntegerValidator::validate(QString &s, int &) const
{
    return IntegerValidator::validateEntry(s, m_base, m_signed, m_bigInt);
}

QValidator::State IntegerValidator::validateEntry(const QString &s, int base, bool signedV, bool bigInt)
{
    const int size = s.size();
    if (!size)
        return QValidator::Intermediate;
    int pos = 0;
    // Skip sign.
    if (signedV && s.at(pos) == '-') {
        pos++;
        if (pos == size)
            return QValidator::Intermediate;
    }
    // Hexadecimal: '0x'?
    if (base == 16 && pos + 2 <= size
        && s.at(pos) == QLatin1Char('0') && s.at(pos + 1) == QLatin1Char('x')) {
        pos+= 2;
        if (pos == size)
            return QValidator::Intermediate;
    }

    // Check characters past sign.
    for (; pos < size; pos++)
        if (!isCharAcceptable(s.at(pos), base))
            return QValidator::Invalid;
    // Check conversion unless big integer
    if (bigInt)
        return QValidator::Acceptable;
    bool ok;
    if (signedV) {
        s.toLongLong(&ok, base);
    } else {
        s.toULongLong(&ok, base);
    }
    return ok ? QValidator::Acceptable : QValidator::Intermediate;
}

IntegerWatchLineEdit::IntegerWatchLineEdit(QWidget *parent) :
    WatchLineEdit(parent),
    m_validator(new IntegerValidator(this))
{
    setValidator(m_validator);
}

bool IntegerWatchLineEdit::isUnsignedHexNumber(const QString &v)
{
    return IntegerValidator::validateEntry(v, 16, false, true) == QValidator::Acceptable;
}

int IntegerWatchLineEdit::base() const
{
    return m_validator->base();
}

void IntegerWatchLineEdit::setBase(int b)
{
    QTC_ASSERT(b, return; )
    m_validator->setBase(b);
}

bool IntegerWatchLineEdit::isSigned() const
{
    return m_validator->isSigned();
}

void IntegerWatchLineEdit::setSigned(bool s)
{
    m_validator->setSigned(s);
}

bool IntegerWatchLineEdit::isBigInt() const
{
    return m_validator->isBigInt();
}

void IntegerWatchLineEdit::setBigInt(bool b)
{
    m_validator->setBigInt(b);
}

QVariant IntegerWatchLineEdit::modelDataI() const
{
    if (isBigInt()) // Big integer: Plain text
        return QVariant(text());
    bool ok;
    if (isSigned()) {
        const qint64 value = text().toLongLong(&ok, base());
        if (ok)
            return QVariant(value);
    } else {
        const quint64 value = text().toULongLong(&ok, base());
        if (ok)
            return QVariant(value);
    }
    return QVariant();
}

QVariant IntegerWatchLineEdit::modelData() const
{
    const QVariant data = modelDataI();
    if (debug)
        qDebug("IntegerLineEdit::modelData(): base=%d, signed=%d, bigint=%d returns %s '%s'",
               base(), isSigned(), isBigInt(), data.typeName(), qPrintable(data.toString()));
    return data;
}

void IntegerWatchLineEdit::setModelData(const QVariant &v)
{
    if (debug)
        qDebug(">IntegerLineEdit::setModelData(%s, '%s'): base=%d, signed=%d, bigint=%d",
               v.typeName(), qPrintable(v.toString()),
               base(), isSigned(), isBigInt());
    switch (v.type()) {
    case QVariant::Int:
    case QVariant::LongLong: {
        const qint64 iv = v.toLongLong();
        setSigned(true);
        setText(QString::number(iv, base()));
    }
        break;
    case QVariant::UInt:
    case QVariant::ULongLong: {
         const quint64 iv = v.toULongLong();
         setSigned(false);
         setText(QString::number(iv, base()));
        }
        break;
    case QVariant::ByteArray:
        setNumberText(QString::fromAscii(v.toByteArray()));
        break;
    case QVariant::String:
        setNumberText(v.toString());
        break;
    default:
        qWarning("Invalid value (%s) passed to IntegerLineEdit::setModelData",
                 v.typeName());
        setText(QString(QLatin1Char('0')));
        break;
    }
    if (debug)
        qDebug("<IntegerLineEdit::setModelData(): base=%d, signed=%d, bigint=%d",
               base(), isSigned(), isBigInt());
}

void IntegerWatchLineEdit::setNumberText(const QString &t)
{
    setText(t);
}

// ------------- FloatWatchLineEdit
FloatWatchLineEdit::FloatWatchLineEdit(QWidget *parent) :
    WatchLineEdit(parent)
{
    setValidator(new QDoubleValidator(this));
}

QVariant FloatWatchLineEdit::modelData() const
{
    return QVariant(text().toDouble());
}

void FloatWatchLineEdit::setModelData(const QVariant &v)
{
    if (debug)
        qDebug("FloatWatchLineEdit::setModelData(%s, '%s')",
               v.typeName(), qPrintable(v.toString()));
    switch (v.type()) {
        break;
    case QVariant::Double:
    case QVariant::String:
        setText(v.toString());
        break;
    case QVariant::ByteArray:
        setText(QString::fromAscii(v.toByteArray()));
        break;
    default:
        qWarning("Invalid value (%s) passed to FloatWatchLineEdit::setModelData",
                 v.typeName());
        setText(QString::number(0.0));
        break;
    }
}

WatchLineEdit *WatchLineEdit::create(QVariant::Type t, QWidget *parent)
{
    switch (t) {
    case QVariant::Bool:
    case QVariant::Int:
    case QVariant::UInt:
    case QVariant::LongLong:
    case QVariant::ULongLong:
        return new IntegerWatchLineEdit(parent);
        break;
    case QVariant::Double:
        return new FloatWatchLineEdit(parent);
    default:
        break;
    }
    return new WatchLineEdit(parent);
}

BooleanComboBox::BooleanComboBox(QWidget *parent) : QComboBox(parent)
{
    QStringList items;
    items << QLatin1String("false") << QLatin1String("true");
    addItems(items);
}

QVariant BooleanComboBox::modelData() const
{
    // As not to confuse debuggers with 'true', 'false', we return integers 1,0.
    const int rc = currentIndex() == 1 ? 1 : 0;
    return QVariant(rc);
}

void BooleanComboBox::setModelData(const QVariant &v)
{
    if (debug)
        qDebug("BooleanComboBox::setModelData(%s, '%s')", v.typeName(), qPrintable(v.toString()));

    bool value = false;
    switch (v.type()) {
    case QVariant::Bool:
        value = v.toBool();
        break;
    case QVariant::Int:
    case QVariant::UInt:
    case QVariant::LongLong:
    case QVariant::ULongLong:
        value = v.toInt() != 0;
        break;
    default:
        qWarning("Invalid value (%s) passed to BooleanComboBox::setModelData", v.typeName());
        break;
    }
    setCurrentIndex(value ? 1 : 0);
}

} // namespace Internal
} // namespace Debugger