src/scripttools/debugging/qscriptdebuggercodeview.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 "qscriptdebuggercodeview_p.h"
       
    43 #include "qscriptdebuggercodeviewinterface_p_p.h"
       
    44 
       
    45 #include "qscriptedit_p.h"
       
    46 
       
    47 #include <QtGui/qboxlayout.h>
       
    48 #include <QtGui/qtextobject.h>
       
    49 #include <QtCore/qdebug.h>
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 class QScriptDebuggerCodeViewPrivate
       
    54     : public QScriptDebuggerCodeViewInterfacePrivate
       
    55 {
       
    56     Q_DECLARE_PUBLIC(QScriptDebuggerCodeView)
       
    57 public:
       
    58     QScriptDebuggerCodeViewPrivate();
       
    59     ~QScriptDebuggerCodeViewPrivate();
       
    60 
       
    61     QScriptEdit *editor;
       
    62 };
       
    63 
       
    64 QScriptDebuggerCodeViewPrivate::QScriptDebuggerCodeViewPrivate()
       
    65 {
       
    66 }
       
    67 
       
    68 QScriptDebuggerCodeViewPrivate::~QScriptDebuggerCodeViewPrivate()
       
    69 {
       
    70 }
       
    71 
       
    72 QScriptDebuggerCodeView::QScriptDebuggerCodeView(QWidget *parent)
       
    73     : QScriptDebuggerCodeViewInterface(*new QScriptDebuggerCodeViewPrivate, parent, 0)
       
    74 {
       
    75     Q_D(QScriptDebuggerCodeView);
       
    76     d->editor = new QScriptEdit();
       
    77     d->editor->setReadOnly(true);
       
    78     d->editor->setBackgroundVisible(false);
       
    79     QObject::connect(d->editor, SIGNAL(breakpointToggleRequest(int,bool)),
       
    80                      this, SIGNAL(breakpointToggleRequest(int,bool)));
       
    81     QObject::connect(d->editor, SIGNAL(breakpointEnableRequest(int,bool)),
       
    82                      this, SIGNAL(breakpointEnableRequest(int,bool)));
       
    83     QVBoxLayout *vbox = new QVBoxLayout(this);
       
    84     vbox->setMargin(0);
       
    85     vbox->addWidget(d->editor);
       
    86 }
       
    87 
       
    88 QScriptDebuggerCodeView::~QScriptDebuggerCodeView()
       
    89 {
       
    90 }
       
    91 
       
    92 QString QScriptDebuggerCodeView::text() const
       
    93 {
       
    94     Q_D(const QScriptDebuggerCodeView);
       
    95     return d->editor->toPlainText();
       
    96 }
       
    97 
       
    98 void QScriptDebuggerCodeView::setText(const QString &text)
       
    99 {
       
   100     Q_D(QScriptDebuggerCodeView);
       
   101     d->editor->setPlainText(text);
       
   102 }
       
   103 
       
   104 int QScriptDebuggerCodeView::cursorLineNumber() const
       
   105 {
       
   106     Q_D(const QScriptDebuggerCodeView);
       
   107     return d->editor->currentLineNumber();
       
   108 }
       
   109 
       
   110 void QScriptDebuggerCodeView::gotoLine(int lineNumber)
       
   111 {
       
   112     Q_D(QScriptDebuggerCodeView);
       
   113     d->editor->gotoLine(lineNumber);
       
   114 }
       
   115 
       
   116 int QScriptDebuggerCodeView::find(const QString &exp, int options)
       
   117 {
       
   118     Q_D(QScriptDebuggerCodeView);
       
   119     QPlainTextEdit *ed = (QPlainTextEdit*)d->editor;
       
   120     QTextCursor cursor = ed->textCursor();
       
   121     if (options & 0x100) {
       
   122         // start searching from the beginning of selection
       
   123         if (cursor.hasSelection()) {
       
   124             int len = cursor.selectedText().length();
       
   125             cursor.clearSelection();
       
   126             cursor.setPosition(cursor.position() - len);
       
   127             ed->setTextCursor(cursor);
       
   128         }
       
   129         options &= ~0x100;
       
   130     }
       
   131     int ret = 0;
       
   132     if (ed->find(exp, QTextDocument::FindFlags(options))) {
       
   133         ret |= 0x1;
       
   134     } else {
       
   135         QTextCursor curse = cursor;
       
   136         curse.movePosition(QTextCursor::Start);
       
   137         ed->setTextCursor(curse);
       
   138         if (ed->find(exp, QTextDocument::FindFlags(options)))
       
   139             ret |= 0x1 | 0x2;
       
   140         else
       
   141             ed->setTextCursor(cursor);
       
   142     }
       
   143     return ret;
       
   144 }
       
   145 
       
   146 void QScriptDebuggerCodeView::setExecutionLineNumber(int lineNumber, bool error)
       
   147 {
       
   148     Q_D(QScriptDebuggerCodeView);
       
   149     d->editor->setExecutionLineNumber(lineNumber, error);
       
   150 }
       
   151 
       
   152 void QScriptDebuggerCodeView::setExecutableLineNumbers(const QSet<int> &lineNumbers)
       
   153 {
       
   154     Q_D(QScriptDebuggerCodeView);
       
   155     d->editor->setExecutableLineNumbers(lineNumbers);
       
   156 }
       
   157 
       
   158 int QScriptDebuggerCodeView::baseLineNumber() const
       
   159 {
       
   160     Q_D(const QScriptDebuggerCodeView);
       
   161     return d->editor->baseLineNumber();
       
   162 }
       
   163 
       
   164 void QScriptDebuggerCodeView::setBaseLineNumber(int lineNumber)
       
   165 {
       
   166     Q_D(QScriptDebuggerCodeView);
       
   167     d->editor->setBaseLineNumber(lineNumber);
       
   168 }
       
   169 
       
   170 void QScriptDebuggerCodeView::setBreakpoint(int lineNumber)
       
   171 {
       
   172     Q_D(QScriptDebuggerCodeView);
       
   173     d->editor->setBreakpoint(lineNumber);
       
   174 }
       
   175 
       
   176 void QScriptDebuggerCodeView::deleteBreakpoint(int lineNumber)
       
   177 {
       
   178     Q_D(QScriptDebuggerCodeView);
       
   179     d->editor->deleteBreakpoint(lineNumber);
       
   180 }
       
   181 
       
   182 void QScriptDebuggerCodeView::setBreakpointEnabled(int lineNumber, bool enable)
       
   183 {
       
   184     Q_D(QScriptDebuggerCodeView);
       
   185     d->editor->setBreakpointEnabled(lineNumber, enable);
       
   186 }
       
   187 
       
   188 namespace {
       
   189 
       
   190 static bool isIdentChar(const QChar &ch)
       
   191 {
       
   192     static QChar underscore = QLatin1Char('_');
       
   193     return ch.isLetter() || (ch == underscore);
       
   194 }
       
   195 
       
   196 } // namespace
       
   197 
       
   198 /*!
       
   199   \reimp
       
   200 */
       
   201 bool QScriptDebuggerCodeView::event(QEvent *e)
       
   202 {
       
   203     Q_D(QScriptDebuggerCodeView);
       
   204     if (e->type() == QEvent::ToolTip) {
       
   205         if (d->editor->executionLineNumber() == -1)
       
   206             return false;
       
   207         QHelpEvent *he = static_cast<QHelpEvent*>(e);
       
   208         QPoint pt = he->pos();
       
   209         pt.rx() -= d->editor->extraAreaWidth();
       
   210         pt.ry() -= 8;
       
   211         QTextCursor cursor = d->editor->cursorForPosition(pt);
       
   212         QTextBlock block = cursor.block();
       
   213         QString contents = block.text();
       
   214         if (contents.isEmpty())
       
   215             return false;
       
   216         int linePosition = cursor.position() - block.position();
       
   217         if (linePosition < 0)
       
   218             linePosition = 0;
       
   219 
       
   220         // ### generalize -- same as in completiontask
       
   221 
       
   222         int pos = linePosition;
       
   223         if ((pos > 0) && contents.at(pos-1).isNumber()) {
       
   224             // tooltips for numbers is pointless
       
   225             return false;
       
   226         }
       
   227 
       
   228         while ((pos > 0) && isIdentChar(contents.at(pos-1)))
       
   229             --pos;
       
   230         if ((pos > 0) && ((contents.at(pos-1) == QLatin1Char('\''))
       
   231                           || (contents.at(pos-1) == QLatin1Char('\"')))) {
       
   232             // ignore string literals
       
   233             return false;
       
   234         }
       
   235         int pos2 = linePosition - 1;
       
   236         while ((pos2 < contents.size()-1) && isIdentChar(contents.at(pos2+1)))
       
   237             ++pos2;
       
   238         QString ident = contents.mid(pos, pos2 - pos + 1);
       
   239 
       
   240         QStringList path;
       
   241         path.append(ident);
       
   242         while ((pos > 0) && (contents.at(pos-1) == QLatin1Char('.'))) {
       
   243             --pos;
       
   244             pos2 = pos;
       
   245             while ((pos > 0) && isIdentChar(contents.at(pos-1)))
       
   246                 --pos;
       
   247             path.prepend(contents.mid(pos, pos2 - pos));
       
   248         }
       
   249 
       
   250         if (!path.isEmpty()) {
       
   251             int lineNumber = cursor.blockNumber() + d->editor->baseLineNumber();
       
   252             emit toolTipRequest(he->globalPos(), lineNumber, path);
       
   253         }
       
   254     }
       
   255     return false;
       
   256 }
       
   257 
       
   258 QT_END_NAMESPACE
       
   259 
       
   260 #include "moc_qscriptdebuggercodeview_p.cpp"