src/scripttools/debugging/qscriptdebuggerlocalswidget.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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         bool ok = (QScriptEngine::checkSyntax(text).state() == QScriptSyntaxCheckResult::Valid);
       
   210         if (ok) {
       
   211             col = Qt::white;
       
   212         } else {
       
   213             QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(
       
   214                 text + QLatin1Char('\n'));
       
   215             if (result.state() == QScriptSyntaxCheckResult::Intermediate)
       
   216                 col = QColor(255, 240, 192);
       
   217             else
       
   218                 col = QColor(255, 102, 102);
       
   219         }
       
   220         pal.setColor(QPalette::Active, QPalette::Base, col);
       
   221         editor->setPalette(pal);
       
   222     }
       
   223 
       
   224 private:
       
   225     static const QWidget *widget(const QStyleOptionViewItem &option)
       
   226     {
       
   227         if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option))
       
   228             return v3->widget;
       
   229         return 0;
       
   230     }
       
   231 };
       
   232 
       
   233 QScriptDebuggerLocalsItemDelegate::QScriptDebuggerLocalsItemDelegate(
       
   234     QObject *parent)
       
   235     : QStyledItemDelegate(parent)
       
   236 {
       
   237 }
       
   238 
       
   239 QWidget *QScriptDebuggerLocalsItemDelegate::createEditor(
       
   240     QWidget *parent, const QStyleOptionViewItem &option,
       
   241     const QModelIndex &index) const
       
   242 {
       
   243     QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index);
       
   244     if (index.column() == 1) {
       
   245         // value
       
   246         QLineEdit *le = qobject_cast<QLineEdit*>(editor);
       
   247         if (le) {
       
   248             QObject::connect(le, SIGNAL(textEdited(QString)),
       
   249                              this, SLOT(validateInput(QString)));
       
   250         }
       
   251     }
       
   252     return editor;
       
   253 }
       
   254 
       
   255 bool QScriptDebuggerLocalsItemDelegate::eventFilter(QObject *watched, QEvent *event)
       
   256 {
       
   257     QLineEdit *le = qobject_cast<QLineEdit*>(watched);
       
   258     if (!le)
       
   259         return QStyledItemDelegate::eventFilter(watched, event);
       
   260 
       
   261     QScriptDebuggerLocalsWidget *localsWidget = qobject_cast<QScriptDebuggerLocalsWidget*>(parent());
       
   262     Q_ASSERT(localsWidget != 0);
       
   263     QScriptDebuggerLocalsWidgetPrivate *lvp =
       
   264         reinterpret_cast<QScriptDebuggerLocalsWidgetPrivate*>(
       
   265             QScriptDebuggerLocalsWidgetPrivate::get(localsWidget));
       
   266 
       
   267     if ((event->type() == QEvent::FocusIn) && lvp->completingEditor) {
       
   268         // because QLineEdit insists on being difficult...
       
   269         return true;
       
   270     }
       
   271 
       
   272     if (event->type() != QEvent::KeyPress)
       
   273         return QStyledItemDelegate::eventFilter(watched, event);
       
   274     QKeyEvent *ke = static_cast<QKeyEvent*>(event);
       
   275     if ((ke->key() == Qt::Key_Enter) || (ke->key() == Qt::Key_Return)) {
       
   276         if (QScriptEngine::checkSyntax(le->text()).state() != QScriptSyntaxCheckResult::Valid) {
       
   277             // ignore when script contains syntax error
       
   278             return true;
       
   279         }
       
   280     }
       
   281     if (ke->key() != Qt::Key_Tab)
       
   282         return QStyledItemDelegate::eventFilter(watched, event);
       
   283 
       
   284     // trigger completion
       
   285     lvp->complete(le);
       
   286     return true;
       
   287 }
       
   288 
       
   289 void QScriptDebuggerLocalsItemDelegate::setModelData(
       
   290     QWidget *editor, QAbstractItemModel *model,
       
   291     const QModelIndex &index) const
       
   292 {
       
   293     if (index.column() == 1) {
       
   294         // check that the syntax is OK
       
   295         QString expression = qobject_cast<QLineEdit*>(editor)->text();
       
   296         if (QScriptEngine::checkSyntax(expression).state() != QScriptSyntaxCheckResult::Valid)
       
   297             return;
       
   298     }
       
   299     QStyledItemDelegate::setModelData(editor, model, index);
       
   300 }
       
   301 
       
   302 void QScriptDebuggerLocalsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
       
   303                                               const QModelIndex &index) const
       
   304 {
       
   305     QStyledItemDelegate::paint(painter, option, index);
       
   306 #if 0
       
   307     QModelIndex parent = index.parent();
       
   308     if (parent.isValid()) {
       
   309         QStyledItemDelegate::paint(painter, option, index);
       
   310     } else {
       
   311         // this is a top-level item.
       
   312         const QTreeView *view = qobject_cast<const QTreeView*>(widget(option));
       
   313         Q_ASSERT(view != 0);
       
   314 
       
   315         QStyleOptionButton buttonOption;
       
   316 
       
   317         buttonOption.state = option.state;
       
   318 #ifdef Q_WS_MAC
       
   319         buttonOption.state |= QStyle::State_Raised;
       
   320 #endif
       
   321         buttonOption.state &= ~QStyle::State_HasFocus;
       
   322 
       
   323         buttonOption.rect = option.rect;
       
   324         buttonOption.palette = option.palette;
       
   325         buttonOption.features = QStyleOptionButton::None;
       
   326         view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, view);
       
   327 
       
   328         QStyleOption branchOption;
       
   329         static const int i = 9; // ### hardcoded in qcommonstyle.cpp
       
   330         QRect r = option.rect;
       
   331         branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
       
   332         branchOption.palette = option.palette;
       
   333         branchOption.state = QStyle::State_Children;
       
   334 
       
   335         if (view->isExpanded(index))
       
   336             branchOption.state |= QStyle::State_Open;
       
   337 
       
   338         view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, view);
       
   339 
       
   340         // draw text
       
   341         QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
       
   342         QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle, 
       
   343             index.data(Qt::DisplayRole).toString());
       
   344         view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
       
   345                                     option.palette, view->isEnabled(), text);
       
   346     }
       
   347 #endif
       
   348 }
       
   349 
       
   350 QScriptDebuggerLocalsWidget::QScriptDebuggerLocalsWidget(QWidget *parent)
       
   351     : QScriptDebuggerLocalsWidgetInterface(*new QScriptDebuggerLocalsWidgetPrivate, parent, 0)
       
   352 {
       
   353     Q_D(QScriptDebuggerLocalsWidget);
       
   354     d->view = new QTreeView();
       
   355     d->view->setItemDelegate(new QScriptDebuggerLocalsItemDelegate(this));
       
   356     d->view->setEditTriggers(QAbstractItemView::DoubleClicked);
       
   357 //    d->view->setEditTriggers(QAbstractItemView::NoEditTriggers);
       
   358     d->view->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
       
   359     d->view->setAlternatingRowColors(true);
       
   360     d->view->setSelectionBehavior(QAbstractItemView::SelectRows);
       
   361     d->view->setSortingEnabled(true);
       
   362     d->view->header()->setDefaultAlignment(Qt::AlignLeft);
       
   363 //    d->view->header()->setSortIndicatorShown(true);
       
   364 //    d->view->header()->setResizeMode(QHeaderView::ResizeToContents);
       
   365 
       
   366     QVBoxLayout *vbox = new QVBoxLayout(this);
       
   367     vbox->setMargin(0);
       
   368     vbox->addWidget(d->view);
       
   369 }
       
   370 
       
   371 QScriptDebuggerLocalsWidget::~QScriptDebuggerLocalsWidget()
       
   372 {
       
   373 }
       
   374 
       
   375 /*!
       
   376   \reimp
       
   377 */
       
   378 QScriptDebuggerLocalsModel *QScriptDebuggerLocalsWidget::localsModel() const
       
   379 {
       
   380     Q_D(const QScriptDebuggerLocalsWidget);
       
   381     if (!d->proxy)
       
   382         return 0;
       
   383     return qobject_cast<QScriptDebuggerLocalsModel*>(d->proxy->sourceModel());
       
   384 }
       
   385 
       
   386 /*!
       
   387   \reimp
       
   388 */
       
   389 void QScriptDebuggerLocalsWidget::setLocalsModel(QScriptDebuggerLocalsModel *model)
       
   390 {
       
   391     Q_D(QScriptDebuggerLocalsWidget);
       
   392     if (localsModel()) {
       
   393         QObject::disconnect(localsModel(), 0, d->view, 0);
       
   394     }
       
   395     if (model) {
       
   396         QObject::connect(model, SIGNAL(scopeObjectAvailable(QModelIndex)),
       
   397                          this, SLOT(_q_expandIndex(QModelIndex)));
       
   398     }
       
   399     if (!d->proxy) {
       
   400         d->proxy = new CustomProxyModel(this);
       
   401         d->view->sortByColumn(0, Qt::AscendingOrder);
       
   402     }
       
   403     d->proxy->setSourceModel(model);
       
   404     d->view->setModel(d->proxy);
       
   405 }
       
   406 
       
   407 /*!
       
   408   \reimp
       
   409 */
       
   410 void QScriptDebuggerLocalsWidget::expand(const QModelIndex &index)
       
   411 {
       
   412     Q_D(QScriptDebuggerLocalsWidget);
       
   413     d->view->expand(index);
       
   414     d->view->setFirstColumnSpanned(index.row(), QModelIndex(), true);
       
   415 }
       
   416 
       
   417 QT_END_NAMESPACE
       
   418 
       
   419 #include "qscriptdebuggerlocalswidget.moc"
       
   420 
       
   421 #include "moc_qscriptdebuggerlocalswidget_p.cpp"