source: trunk/src/scripttools/debugging/qscriptdebuggerlocalswidget.cpp@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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