1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtSCriptTools module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** 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 either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qscriptdebuggerlocalswidget_p.h"
#include "qscriptdebuggerlocalswidgetinterface_p_p.h"
#include "qscriptdebuggerlocalsmodel_p.h"
#include "qscriptcompletionproviderinterface_p.h"
#include "qscriptcompletiontaskinterface_p.h"
#include <QtCore/qdebug.h>
#include <QtGui/qheaderview.h>
#include <QtGui/qcompleter.h>
#include <QtGui/qstringlistmodel.h>
#include <QtGui/qtreeview.h>
#include <QtGui/qboxlayout.h>
#include <QtGui/qsortfilterproxymodel.h>
#include <QtGui/qlineedit.h>
#include <QtGui/qstyleditemdelegate.h>
#include <QtGui/qevent.h>
#include <QtGui/qmessagebox.h>
#include <QtScript/qscriptengine.h>
QT_BEGIN_NAMESPACE
namespace {
class CustomProxyModel : public QSortFilterProxyModel
{
public:
CustomProxyModel(QObject *parent = 0)
: QSortFilterProxyModel(parent) {}
bool hasChildren(const QModelIndex &parent) const
{
QModelIndex sourceParent = mapToSource(parent);
if (parent.isValid() && !sourceParent.isValid())
return false;
return sourceModel()->hasChildren(sourceParent);
}
};
} // namespace
class QScriptDebuggerLocalsWidgetPrivate
: public QScriptDebuggerLocalsWidgetInterfacePrivate
{
Q_DECLARE_PUBLIC(QScriptDebuggerLocalsWidget)
public:
QScriptDebuggerLocalsWidgetPrivate();
~QScriptDebuggerLocalsWidgetPrivate();
void complete(QLineEdit *le);
// private slots
void _q_onCompletionTaskFinished();
void _q_insertCompletion(const QString &text);
void _q_expandIndex(const QModelIndex &index);
QTreeView *view;
QPointer<QLineEdit> completingEditor;
QCompleter *completer;
CustomProxyModel *proxy;
};
QScriptDebuggerLocalsWidgetPrivate::QScriptDebuggerLocalsWidgetPrivate()
{
completingEditor = 0;
completer = 0;
proxy = 0;
}
QScriptDebuggerLocalsWidgetPrivate::~QScriptDebuggerLocalsWidgetPrivate()
{
}
void QScriptDebuggerLocalsWidgetPrivate::complete(QLineEdit *le)
{
Q_Q(QScriptDebuggerLocalsWidget);
QScriptCompletionTaskInterface *task = 0;
// ### need to pass the current frame #
task = completionProvider->createCompletionTask(
le->text(), le->cursorPosition(),
q->localsModel()->frameIndex(), /*options=*/0);
QObject::connect(task, SIGNAL(finished()),
q, SLOT(_q_onCompletionTaskFinished()));
completingEditor = le;
task->start();
}
void QScriptDebuggerLocalsWidgetPrivate::_q_onCompletionTaskFinished()
{
Q_Q(QScriptDebuggerLocalsWidget);
QScriptCompletionTaskInterface *task = 0;
task = qobject_cast<QScriptCompletionTaskInterface*>(q_func()->sender());
if (!completingEditor) {
task->deleteLater();
return;
}
if (task->resultCount() == 1) {
// do the completion right away
QString completion = task->resultAt(0);
completion.append(task->appendix());
QString tmp = completingEditor->text();
tmp.remove(task->position(), task->length());
tmp.insert(task->position(), completion);
completingEditor->setText(tmp);
completingEditor = 0;
} else if (task->resultCount() > 1) {
// popup completion
if (!completer) {
completer = new QCompleter(q);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseSensitive);
completer->setWrapAround(false);
QObject::connect(completer, SIGNAL(activated(QString)),
q, SLOT(_q_insertCompletion(QString)));
}
QStringListModel *model = qobject_cast<QStringListModel*>(completer->model());
if (!model) {
model = new QStringListModel(q);
completer->setModel(model);
}
QStringList strings;
for (int i = 0; i < task->resultCount(); ++i)
strings.append(task->resultAt(i));
model->setStringList(strings);
QString prefix = completingEditor->text().mid(task->position(), task->length());
completer->setCompletionPrefix(prefix);
completingEditor->setCompleter(completer);
// we want to handle the insertion ourselves
QObject::disconnect(completer, 0, completingEditor, 0);
completer->complete();
}
task->deleteLater();
}
void QScriptDebuggerLocalsWidgetPrivate::_q_insertCompletion(const QString &text)
{
Q_ASSERT(completingEditor != 0);
QString tmp = completingEditor->text();
tmp.insert(completingEditor->cursorPosition(), text.mid(completer->completionPrefix().length()));
completingEditor->setText(tmp);
completingEditor = 0;
}
void QScriptDebuggerLocalsWidgetPrivate::_q_expandIndex(const QModelIndex &index)
{
view->expand(proxy->mapFromSource(index));
}
class QScriptDebuggerLocalsItemDelegate
: public QStyledItemDelegate
{
Q_OBJECT
public:
QScriptDebuggerLocalsItemDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool eventFilter(QObject *watched, QEvent *event);
private Q_SLOTS:
void validateInput(const QString &text)
{
QWidget *editor = qobject_cast<QWidget*>(sender());
QPalette pal = editor->palette();
QColor col;
#if QT_VERSION >= 0x040500
bool ok = (QScriptEngine::checkSyntax(text).state() == QScriptSyntaxCheckResult::Valid);
#else
bool ok = true;
#endif
if (ok) {
col = Qt::white;
} else {
QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(
text + QLatin1Char('\n'));
if (result.state() == QScriptSyntaxCheckResult::Intermediate)
col = QColor(255, 240, 192);
else
col = QColor(255, 102, 102);
}
pal.setColor(QPalette::Active, QPalette::Base, col);
editor->setPalette(pal);
}
private:
static const QWidget *widget(const QStyleOptionViewItem &option)
{
if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option))
return v3->widget;
return 0;
}
};
QScriptDebuggerLocalsItemDelegate::QScriptDebuggerLocalsItemDelegate(
QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *QScriptDebuggerLocalsItemDelegate::createEditor(
QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index);
if (index.column() == 1) {
// value
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
if (le) {
QObject::connect(le, SIGNAL(textEdited(QString)),
this, SLOT(validateInput(QString)));
}
}
return editor;
}
bool QScriptDebuggerLocalsItemDelegate::eventFilter(QObject *watched, QEvent *event)
{
QLineEdit *le = qobject_cast<QLineEdit*>(watched);
if (!le)
return QStyledItemDelegate::eventFilter(watched, event);
QScriptDebuggerLocalsWidget *localsWidget = qobject_cast<QScriptDebuggerLocalsWidget*>(parent());
Q_ASSERT(localsWidget != 0);
QScriptDebuggerLocalsWidgetPrivate *lvp =
reinterpret_cast<QScriptDebuggerLocalsWidgetPrivate*>(
QScriptDebuggerLocalsWidgetPrivate::get(localsWidget));
if ((event->type() == QEvent::FocusIn) && lvp->completingEditor) {
// because QLineEdit insists on being difficult...
return true;
}
if (event->type() != QEvent::KeyPress)
return QStyledItemDelegate::eventFilter(watched, event);
QKeyEvent *ke = static_cast<QKeyEvent*>(event);
if ((ke->key() == Qt::Key_Enter) || (ke->key() == Qt::Key_Return)) {
#if QT_VERSION >= 0x040500
if (QScriptEngine::checkSyntax(le->text()).state() != QScriptSyntaxCheckResult::Valid) {
// ignore when script contains syntax error
return true;
}
#endif
}
if (ke->key() != Qt::Key_Tab)
return QStyledItemDelegate::eventFilter(watched, event);
// trigger completion
lvp->complete(le);
return true;
}
void QScriptDebuggerLocalsItemDelegate::setModelData(
QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
#if QT_VERSION >= 0x040500
if (index.column() == 1) {
// check that the syntax is OK
QString expression = qobject_cast<QLineEdit*>(editor)->text();
if (QScriptEngine::checkSyntax(expression).state() != QScriptSyntaxCheckResult::Valid)
return;
}
#endif
QStyledItemDelegate::setModelData(editor, model, index);
}
void QScriptDebuggerLocalsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
#if 0
QModelIndex parent = index.parent();
if (parent.isValid()) {
QStyledItemDelegate::paint(painter, option, index);
} else {
// this is a top-level item.
const QTreeView *view = qobject_cast<const QTreeView*>(widget(option));
Q_ASSERT(view != 0);
QStyleOptionButton buttonOption;
buttonOption.state = option.state;
#ifdef Q_WS_MAC
buttonOption.state |= QStyle::State_Raised;
#endif
buttonOption.state &= ~QStyle::State_HasFocus;
buttonOption.rect = option.rect;
buttonOption.palette = option.palette;
buttonOption.features = QStyleOptionButton::None;
view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, view);
QStyleOption branchOption;
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
QRect r = option.rect;
branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
branchOption.palette = option.palette;
branchOption.state = QStyle::State_Children;
if (view->isExpanded(index))
branchOption.state |= QStyle::State_Open;
view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, view);
// draw text
QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
index.data(Qt::DisplayRole).toString());
view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
option.palette, view->isEnabled(), text);
}
#endif
}
QScriptDebuggerLocalsWidget::QScriptDebuggerLocalsWidget(QWidget *parent)
: QScriptDebuggerLocalsWidgetInterface(*new QScriptDebuggerLocalsWidgetPrivate, parent, 0)
{
Q_D(QScriptDebuggerLocalsWidget);
d->view = new QTreeView();
d->view->setItemDelegate(new QScriptDebuggerLocalsItemDelegate(this));
d->view->setEditTriggers(QAbstractItemView::DoubleClicked);
// d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
d->view->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
d->view->setAlternatingRowColors(true);
d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
d->view->setSortingEnabled(true);
d->view->header()->setDefaultAlignment(Qt::AlignLeft);
// d->view->header()->setSortIndicatorShown(true);
// d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->setMargin(0);
vbox->addWidget(d->view);
}
QScriptDebuggerLocalsWidget::~QScriptDebuggerLocalsWidget()
{
}
/*!
\reimp
*/
QScriptDebuggerLocalsModel *QScriptDebuggerLocalsWidget::localsModel() const
{
Q_D(const QScriptDebuggerLocalsWidget);
if (!d->proxy)
return 0;
return qobject_cast<QScriptDebuggerLocalsModel*>(d->proxy->sourceModel());
}
/*!
\reimp
*/
void QScriptDebuggerLocalsWidget::setLocalsModel(QScriptDebuggerLocalsModel *model)
{
Q_D(QScriptDebuggerLocalsWidget);
if (localsModel()) {
QObject::disconnect(localsModel(), 0, d->view, 0);
}
if (model) {
QObject::connect(model, SIGNAL(scopeObjectAvailable(QModelIndex)),
this, SLOT(_q_expandIndex(QModelIndex)));
}
if (!d->proxy) {
d->proxy = new CustomProxyModel(this);
d->view->sortByColumn(0, Qt::AscendingOrder);
}
d->proxy->setSourceModel(model);
d->view->setModel(d->proxy);
}
/*!
\reimp
*/
void QScriptDebuggerLocalsWidget::expand(const QModelIndex &index)
{
Q_D(QScriptDebuggerLocalsWidget);
d->view->expand(index);
d->view->setFirstColumnSpanned(index.row(), QModelIndex(), true);
}
QT_END_NAMESPACE
#include "qscriptdebuggerlocalswidget.moc"
#include "moc_qscriptdebuggerlocalswidget_p.cpp"
|