1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtSCriptTools module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qscriptdebuggerlocalswidget_p.h"
|
---|
43 | #include "qscriptdebuggerlocalswidgetinterface_p_p.h"
|
---|
44 | #include "qscriptdebuggerlocalsmodel_p.h"
|
---|
45 | #include "qscriptcompletionproviderinterface_p.h"
|
---|
46 | #include "qscriptcompletiontaskinterface_p.h"
|
---|
47 |
|
---|
48 | #include <QtCore/qdebug.h>
|
---|
49 | #include <QtGui/qheaderview.h>
|
---|
50 | #include <QtGui/qcompleter.h>
|
---|
51 | #include <QtGui/qstringlistmodel.h>
|
---|
52 | #include <QtGui/qtreeview.h>
|
---|
53 | #include <QtGui/qboxlayout.h>
|
---|
54 | #include <QtGui/qsortfilterproxymodel.h>
|
---|
55 | #include <QtGui/qlineedit.h>
|
---|
56 | #include <QtGui/qstyleditemdelegate.h>
|
---|
57 | #include <QtGui/qevent.h>
|
---|
58 | #include <QtGui/qmessagebox.h>
|
---|
59 | #include <QtScript/qscriptengine.h>
|
---|
60 |
|
---|
61 | QT_BEGIN_NAMESPACE
|
---|
62 |
|
---|
63 | namespace {
|
---|
64 |
|
---|
65 | class CustomProxyModel : public QSortFilterProxyModel
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | CustomProxyModel(QObject *parent = 0)
|
---|
69 | : QSortFilterProxyModel(parent) {}
|
---|
70 |
|
---|
71 | bool hasChildren(const QModelIndex &parent) const
|
---|
72 | {
|
---|
73 | QModelIndex sourceParent = mapToSource(parent);
|
---|
74 | if (parent.isValid() && !sourceParent.isValid())
|
---|
75 | return false;
|
---|
76 | return sourceModel()->hasChildren(sourceParent);
|
---|
77 | }
|
---|
78 | };
|
---|
79 |
|
---|
80 | } // namespace
|
---|
81 |
|
---|
82 | class QScriptDebuggerLocalsWidgetPrivate
|
---|
83 | : public QScriptDebuggerLocalsWidgetInterfacePrivate
|
---|
84 | {
|
---|
85 | Q_DECLARE_PUBLIC(QScriptDebuggerLocalsWidget)
|
---|
86 | public:
|
---|
87 | QScriptDebuggerLocalsWidgetPrivate();
|
---|
88 | ~QScriptDebuggerLocalsWidgetPrivate();
|
---|
89 |
|
---|
90 | void complete(QLineEdit *le);
|
---|
91 |
|
---|
92 | // private slots
|
---|
93 | void _q_onCompletionTaskFinished();
|
---|
94 | void _q_insertCompletion(const QString &text);
|
---|
95 | void _q_expandIndex(const QModelIndex &index);
|
---|
96 |
|
---|
97 | QTreeView *view;
|
---|
98 | QPointer<QLineEdit> completingEditor;
|
---|
99 | QCompleter *completer;
|
---|
100 | CustomProxyModel *proxy;
|
---|
101 | };
|
---|
102 |
|
---|
103 | QScriptDebuggerLocalsWidgetPrivate::QScriptDebuggerLocalsWidgetPrivate()
|
---|
104 | {
|
---|
105 | completingEditor = 0;
|
---|
106 | completer = 0;
|
---|
107 | proxy = 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | QScriptDebuggerLocalsWidgetPrivate::~QScriptDebuggerLocalsWidgetPrivate()
|
---|
111 | {
|
---|
112 | }
|
---|
113 |
|
---|
114 | void QScriptDebuggerLocalsWidgetPrivate::complete(QLineEdit *le)
|
---|
115 | {
|
---|
116 | Q_Q(QScriptDebuggerLocalsWidget);
|
---|
117 | QScriptCompletionTaskInterface *task = 0;
|
---|
118 | // ### need to pass the current frame #
|
---|
119 | task = completionProvider->createCompletionTask(
|
---|
120 | le->text(), le->cursorPosition(),
|
---|
121 | q->localsModel()->frameIndex(), /*options=*/0);
|
---|
122 | QObject::connect(task, SIGNAL(finished()),
|
---|
123 | q, SLOT(_q_onCompletionTaskFinished()));
|
---|
124 | completingEditor = le;
|
---|
125 | task->start();
|
---|
126 | }
|
---|
127 |
|
---|
128 | void QScriptDebuggerLocalsWidgetPrivate::_q_onCompletionTaskFinished()
|
---|
129 | {
|
---|
130 | Q_Q(QScriptDebuggerLocalsWidget);
|
---|
131 | QScriptCompletionTaskInterface *task = 0;
|
---|
132 | task = qobject_cast<QScriptCompletionTaskInterface*>(q_func()->sender());
|
---|
133 | if (!completingEditor) {
|
---|
134 | task->deleteLater();
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (task->resultCount() == 1) {
|
---|
139 | // do the completion right away
|
---|
140 | QString completion = task->resultAt(0);
|
---|
141 | completion.append(task->appendix());
|
---|
142 | QString tmp = completingEditor->text();
|
---|
143 | tmp.remove(task->position(), task->length());
|
---|
144 | tmp.insert(task->position(), completion);
|
---|
145 | completingEditor->setText(tmp);
|
---|
146 | completingEditor = 0;
|
---|
147 | } else if (task->resultCount() > 1) {
|
---|
148 | // popup completion
|
---|
149 | if (!completer) {
|
---|
150 | completer = new QCompleter(q);
|
---|
151 | completer->setCompletionMode(QCompleter::PopupCompletion);
|
---|
152 | completer->setCaseSensitivity(Qt::CaseSensitive);
|
---|
153 | completer->setWrapAround(false);
|
---|
154 | QObject::connect(completer, SIGNAL(activated(QString)),
|
---|
155 | q, SLOT(_q_insertCompletion(QString)));
|
---|
156 | }
|
---|
157 | QStringListModel *model = qobject_cast<QStringListModel*>(completer->model());
|
---|
158 | if (!model) {
|
---|
159 | model = new QStringListModel(q);
|
---|
160 | completer->setModel(model);
|
---|
161 | }
|
---|
162 | QStringList strings;
|
---|
163 | for (int i = 0; i < task->resultCount(); ++i)
|
---|
164 | strings.append(task->resultAt(i));
|
---|
165 | model->setStringList(strings);
|
---|
166 | QString prefix = completingEditor->text().mid(task->position(), task->length());
|
---|
167 | completer->setCompletionPrefix(prefix);
|
---|
168 | completingEditor->setCompleter(completer);
|
---|
169 | // we want to handle the insertion ourselves
|
---|
170 | QObject::disconnect(completer, 0, completingEditor, 0);
|
---|
171 | completer->complete();
|
---|
172 | }
|
---|
173 | task->deleteLater();
|
---|
174 | }
|
---|
175 |
|
---|
176 | void QScriptDebuggerLocalsWidgetPrivate::_q_insertCompletion(const QString &text)
|
---|
177 | {
|
---|
178 | Q_ASSERT(completingEditor != 0);
|
---|
179 | QString tmp = completingEditor->text();
|
---|
180 | tmp.insert(completingEditor->cursorPosition(), text.mid(completer->completionPrefix().length()));
|
---|
181 | completingEditor->setText(tmp);
|
---|
182 | completingEditor = 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void QScriptDebuggerLocalsWidgetPrivate::_q_expandIndex(const QModelIndex &index)
|
---|
186 | {
|
---|
187 | view->expand(proxy->mapFromSource(index));
|
---|
188 | }
|
---|
189 |
|
---|
190 | class QScriptDebuggerLocalsItemDelegate
|
---|
191 | : public QStyledItemDelegate
|
---|
192 | {
|
---|
193 | Q_OBJECT
|
---|
194 | public:
|
---|
195 | QScriptDebuggerLocalsItemDelegate(QObject *parent = 0);
|
---|
196 |
|
---|
197 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
---|
198 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
|
---|
199 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
---|
200 |
|
---|
201 | bool eventFilter(QObject *watched, QEvent *event);
|
---|
202 |
|
---|
203 | private Q_SLOTS:
|
---|
204 | void validateInput(const QString &text)
|
---|
205 | {
|
---|
206 | QWidget *editor = qobject_cast<QWidget*>(sender());
|
---|
207 | QPalette pal = editor->palette();
|
---|
208 | QColor col;
|
---|
209 | #if QT_VERSION >= 0x040500
|
---|
210 | bool ok = (QScriptEngine::checkSyntax(text).state() == QScriptSyntaxCheckResult::Valid);
|
---|
211 | #else
|
---|
212 | bool ok = true;
|
---|
213 | #endif
|
---|
214 | if (ok) {
|
---|
215 | col = Qt::white;
|
---|
216 | } else {
|
---|
217 | QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(
|
---|
218 | text + QLatin1Char('\n'));
|
---|
219 | if (result.state() == QScriptSyntaxCheckResult::Intermediate)
|
---|
220 | col = QColor(255, 240, 192);
|
---|
221 | else
|
---|
222 | col = QColor(255, 102, 102);
|
---|
223 | }
|
---|
224 | pal.setColor(QPalette::Active, QPalette::Base, col);
|
---|
225 | editor->setPalette(pal);
|
---|
226 | }
|
---|
227 |
|
---|
228 | private:
|
---|
229 | static const QWidget *widget(const QStyleOptionViewItem &option)
|
---|
230 | {
|
---|
231 | if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option))
|
---|
232 | return v3->widget;
|
---|
233 | return 0;
|
---|
234 | }
|
---|
235 | };
|
---|
236 |
|
---|
237 | QScriptDebuggerLocalsItemDelegate::QScriptDebuggerLocalsItemDelegate(
|
---|
238 | QObject *parent)
|
---|
239 | : QStyledItemDelegate(parent)
|
---|
240 | {
|
---|
241 | }
|
---|
242 |
|
---|
243 | QWidget *QScriptDebuggerLocalsItemDelegate::createEditor(
|
---|
244 | QWidget *parent, const QStyleOptionViewItem &option,
|
---|
245 | const QModelIndex &index) const
|
---|
246 | {
|
---|
247 | QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index);
|
---|
248 | if (index.column() == 1) {
|
---|
249 | // value
|
---|
250 | QLineEdit *le = qobject_cast<QLineEdit*>(editor);
|
---|
251 | if (le) {
|
---|
252 | QObject::connect(le, SIGNAL(textEdited(QString)),
|
---|
253 | this, SLOT(validateInput(QString)));
|
---|
254 | }
|
---|
255 | }
|
---|
256 | return editor;
|
---|
257 | }
|
---|
258 |
|
---|
259 | bool QScriptDebuggerLocalsItemDelegate::eventFilter(QObject *watched, QEvent *event)
|
---|
260 | {
|
---|
261 | QLineEdit *le = qobject_cast<QLineEdit*>(watched);
|
---|
262 | if (!le)
|
---|
263 | return QStyledItemDelegate::eventFilter(watched, event);
|
---|
264 |
|
---|
265 | QScriptDebuggerLocalsWidget *localsWidget = qobject_cast<QScriptDebuggerLocalsWidget*>(parent());
|
---|
266 | Q_ASSERT(localsWidget != 0);
|
---|
267 | QScriptDebuggerLocalsWidgetPrivate *lvp =
|
---|
268 | reinterpret_cast<QScriptDebuggerLocalsWidgetPrivate*>(
|
---|
269 | QScriptDebuggerLocalsWidgetPrivate::get(localsWidget));
|
---|
270 |
|
---|
271 | if ((event->type() == QEvent::FocusIn) && lvp->completingEditor) {
|
---|
272 | // because QLineEdit insists on being difficult...
|
---|
273 | return true;
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (event->type() != QEvent::KeyPress)
|
---|
277 | return QStyledItemDelegate::eventFilter(watched, event);
|
---|
278 | QKeyEvent *ke = static_cast<QKeyEvent*>(event);
|
---|
279 | if ((ke->key() == Qt::Key_Enter) || (ke->key() == Qt::Key_Return)) {
|
---|
280 | #if QT_VERSION >= 0x040500
|
---|
281 | if (QScriptEngine::checkSyntax(le->text()).state() != QScriptSyntaxCheckResult::Valid) {
|
---|
282 | // ignore when script contains syntax error
|
---|
283 | return true;
|
---|
284 | }
|
---|
285 | #endif
|
---|
286 | }
|
---|
287 | if (ke->key() != Qt::Key_Tab)
|
---|
288 | return QStyledItemDelegate::eventFilter(watched, event);
|
---|
289 |
|
---|
290 | // trigger completion
|
---|
291 | lvp->complete(le);
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | void QScriptDebuggerLocalsItemDelegate::setModelData(
|
---|
296 | QWidget *editor, QAbstractItemModel *model,
|
---|
297 | const QModelIndex &index) const
|
---|
298 | {
|
---|
299 | #if QT_VERSION >= 0x040500
|
---|
300 | if (index.column() == 1) {
|
---|
301 | // check that the syntax is OK
|
---|
302 | QString expression = qobject_cast<QLineEdit*>(editor)->text();
|
---|
303 | if (QScriptEngine::checkSyntax(expression).state() != QScriptSyntaxCheckResult::Valid)
|
---|
304 | return;
|
---|
305 | }
|
---|
306 | #endif
|
---|
307 | QStyledItemDelegate::setModelData(editor, model, index);
|
---|
308 | }
|
---|
309 |
|
---|
310 | void QScriptDebuggerLocalsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
---|
311 | const QModelIndex &index) const
|
---|
312 | {
|
---|
313 | QStyledItemDelegate::paint(painter, option, index);
|
---|
314 | #if 0
|
---|
315 | QModelIndex parent = index.parent();
|
---|
316 | if (parent.isValid()) {
|
---|
317 | QStyledItemDelegate::paint(painter, option, index);
|
---|
318 | } else {
|
---|
319 | // this is a top-level item.
|
---|
320 | const QTreeView *view = qobject_cast<const QTreeView*>(widget(option));
|
---|
321 | Q_ASSERT(view != 0);
|
---|
322 |
|
---|
323 | QStyleOptionButton buttonOption;
|
---|
324 |
|
---|
325 | buttonOption.state = option.state;
|
---|
326 | #ifdef Q_WS_MAC
|
---|
327 | buttonOption.state |= QStyle::State_Raised;
|
---|
328 | #endif
|
---|
329 | buttonOption.state &= ~QStyle::State_HasFocus;
|
---|
330 |
|
---|
331 | buttonOption.rect = option.rect;
|
---|
332 | buttonOption.palette = option.palette;
|
---|
333 | buttonOption.features = QStyleOptionButton::None;
|
---|
334 | view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, view);
|
---|
335 |
|
---|
336 | QStyleOption branchOption;
|
---|
337 | static const int i = 9; // ### hardcoded in qcommonstyle.cpp
|
---|
338 | QRect r = option.rect;
|
---|
339 | branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
|
---|
340 | branchOption.palette = option.palette;
|
---|
341 | branchOption.state = QStyle::State_Children;
|
---|
342 |
|
---|
343 | if (view->isExpanded(index))
|
---|
344 | branchOption.state |= QStyle::State_Open;
|
---|
345 |
|
---|
346 | view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, view);
|
---|
347 |
|
---|
348 | // draw text
|
---|
349 | QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
|
---|
350 | QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
|
---|
351 | index.data(Qt::DisplayRole).toString());
|
---|
352 | view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
|
---|
353 | option.palette, view->isEnabled(), text);
|
---|
354 | }
|
---|
355 | #endif
|
---|
356 | }
|
---|
357 |
|
---|
358 | QScriptDebuggerLocalsWidget::QScriptDebuggerLocalsWidget(QWidget *parent)
|
---|
359 | : QScriptDebuggerLocalsWidgetInterface(*new QScriptDebuggerLocalsWidgetPrivate, parent, 0)
|
---|
360 | {
|
---|
361 | Q_D(QScriptDebuggerLocalsWidget);
|
---|
362 | d->view = new QTreeView();
|
---|
363 | d->view->setItemDelegate(new QScriptDebuggerLocalsItemDelegate(this));
|
---|
364 | d->view->setEditTriggers(QAbstractItemView::DoubleClicked);
|
---|
365 | // d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
---|
366 | d->view->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
|
---|
367 | d->view->setAlternatingRowColors(true);
|
---|
368 | d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
---|
369 | d->view->setSortingEnabled(true);
|
---|
370 | d->view->header()->setDefaultAlignment(Qt::AlignLeft);
|
---|
371 | // d->view->header()->setSortIndicatorShown(true);
|
---|
372 | // d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
|
---|
373 |
|
---|
374 | QVBoxLayout *vbox = new QVBoxLayout(this);
|
---|
375 | vbox->setMargin(0);
|
---|
376 | vbox->addWidget(d->view);
|
---|
377 | }
|
---|
378 |
|
---|
379 | QScriptDebuggerLocalsWidget::~QScriptDebuggerLocalsWidget()
|
---|
380 | {
|
---|
381 | }
|
---|
382 |
|
---|
383 | /*!
|
---|
384 | \reimp
|
---|
385 | */
|
---|
386 | QScriptDebuggerLocalsModel *QScriptDebuggerLocalsWidget::localsModel() const
|
---|
387 | {
|
---|
388 | Q_D(const QScriptDebuggerLocalsWidget);
|
---|
389 | if (!d->proxy)
|
---|
390 | return 0;
|
---|
391 | return qobject_cast<QScriptDebuggerLocalsModel*>(d->proxy->sourceModel());
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*!
|
---|
395 | \reimp
|
---|
396 | */
|
---|
397 | void QScriptDebuggerLocalsWidget::setLocalsModel(QScriptDebuggerLocalsModel *model)
|
---|
398 | {
|
---|
399 | Q_D(QScriptDebuggerLocalsWidget);
|
---|
400 | if (localsModel()) {
|
---|
401 | QObject::disconnect(localsModel(), 0, d->view, 0);
|
---|
402 | }
|
---|
403 | if (model) {
|
---|
404 | QObject::connect(model, SIGNAL(scopeObjectAvailable(QModelIndex)),
|
---|
405 | this, SLOT(_q_expandIndex(QModelIndex)));
|
---|
406 | }
|
---|
407 | if (!d->proxy) {
|
---|
408 | d->proxy = new CustomProxyModel(this);
|
---|
409 | d->view->sortByColumn(0, Qt::AscendingOrder);
|
---|
410 | }
|
---|
411 | d->proxy->setSourceModel(model);
|
---|
412 | d->view->setModel(d->proxy);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*!
|
---|
416 | \reimp
|
---|
417 | */
|
---|
418 | void QScriptDebuggerLocalsWidget::expand(const QModelIndex &index)
|
---|
419 | {
|
---|
420 | Q_D(QScriptDebuggerLocalsWidget);
|
---|
421 | d->view->expand(index);
|
---|
422 | d->view->setFirstColumnSpanned(index.row(), QModelIndex(), true);
|
---|
423 | }
|
---|
424 |
|
---|
425 | QT_END_NAMESPACE
|
---|
426 |
|
---|
427 | #include "qscriptdebuggerlocalswidget.moc"
|
---|
428 |
|
---|
429 | #include "moc_qscriptdebuggerlocalswidget_p.cpp"
|
---|