source: branches/4.5.1/src/scripttools/debugging/qscriptdebuggercodeview.cpp@ 559

Last change on this file since 559 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.0 KB
Line 
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 "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
51QT_BEGIN_NAMESPACE
52
53class QScriptDebuggerCodeViewPrivate
54 : public QScriptDebuggerCodeViewInterfacePrivate
55{
56 Q_DECLARE_PUBLIC(QScriptDebuggerCodeView)
57public:
58 QScriptDebuggerCodeViewPrivate();
59 ~QScriptDebuggerCodeViewPrivate();
60
61 QScriptEdit *editor;
62};
63
64QScriptDebuggerCodeViewPrivate::QScriptDebuggerCodeViewPrivate()
65{
66}
67
68QScriptDebuggerCodeViewPrivate::~QScriptDebuggerCodeViewPrivate()
69{
70}
71
72QScriptDebuggerCodeView::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
88QScriptDebuggerCodeView::~QScriptDebuggerCodeView()
89{
90}
91
92QString QScriptDebuggerCodeView::text() const
93{
94 Q_D(const QScriptDebuggerCodeView);
95 return d->editor->toPlainText();
96}
97
98void QScriptDebuggerCodeView::setText(const QString &text)
99{
100 Q_D(QScriptDebuggerCodeView);
101 d->editor->setPlainText(text);
102}
103
104int QScriptDebuggerCodeView::cursorLineNumber() const
105{
106 Q_D(const QScriptDebuggerCodeView);
107 return d->editor->currentLineNumber();
108}
109
110void QScriptDebuggerCodeView::gotoLine(int lineNumber)
111{
112 Q_D(QScriptDebuggerCodeView);
113 d->editor->gotoLine(lineNumber);
114}
115
116int 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
146void QScriptDebuggerCodeView::setExecutionLineNumber(int lineNumber, bool error)
147{
148 Q_D(QScriptDebuggerCodeView);
149 d->editor->setExecutionLineNumber(lineNumber, error);
150}
151
152void QScriptDebuggerCodeView::setExecutableLineNumbers(const QSet<int> &lineNumbers)
153{
154 Q_D(QScriptDebuggerCodeView);
155 d->editor->setExecutableLineNumbers(lineNumbers);
156}
157
158int QScriptDebuggerCodeView::baseLineNumber() const
159{
160 Q_D(const QScriptDebuggerCodeView);
161 return d->editor->baseLineNumber();
162}
163
164void QScriptDebuggerCodeView::setBaseLineNumber(int lineNumber)
165{
166 Q_D(QScriptDebuggerCodeView);
167 d->editor->setBaseLineNumber(lineNumber);
168}
169
170void QScriptDebuggerCodeView::setBreakpoint(int lineNumber)
171{
172 Q_D(QScriptDebuggerCodeView);
173 d->editor->setBreakpoint(lineNumber);
174}
175
176void QScriptDebuggerCodeView::deleteBreakpoint(int lineNumber)
177{
178 Q_D(QScriptDebuggerCodeView);
179 d->editor->deleteBreakpoint(lineNumber);
180}
181
182void QScriptDebuggerCodeView::setBreakpointEnabled(int lineNumber, bool enable)
183{
184 Q_D(QScriptDebuggerCodeView);
185 d->editor->setBreakpointEnabled(lineNumber, enable);
186}
187
188namespace {
189
190static 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*/
201bool 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 linePosition -= 3;
218 if (linePosition < 0)
219 linePosition = 0;
220
221 // ### generalize -- same as in completiontask
222
223 int pos = linePosition;
224 if ((pos > 0) && contents.at(pos-1).isNumber()) {
225 // tooltips for numbers is pointless
226 return false;
227 }
228
229 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
230 --pos;
231 if ((pos > 0) && ((contents.at(pos-1) == QLatin1Char('\''))
232 || (contents.at(pos-1) == QLatin1Char('\"')))) {
233 // ignore string literals
234 return false;
235 }
236 int pos2 = linePosition;
237 while ((pos2 < contents.size()-1) && isIdentChar(contents.at(pos2+1)))
238 ++pos2;
239 QString ident = contents.mid(pos, pos2 - pos + 1);
240
241 QStringList path;
242 path.append(ident);
243 while ((pos > 0) && (contents.at(pos-1) == QLatin1Char('.'))) {
244 --pos;
245 pos2 = pos;
246 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
247 --pos;
248 path.prepend(contents.mid(pos, pos2 - pos));
249 }
250
251 if (!path.isEmpty()) {
252 int lineNumber = cursor.blockNumber() + d->editor->baseLineNumber();
253 emit toolTipRequest(he->globalPos(), lineNumber, path);
254 }
255 }
256 return false;
257}
258
259QT_END_NAMESPACE
260
261#include "moc_qscriptdebuggercodeview_p.cpp"
Note: See TracBrowser for help on using the repository browser.