Qt 4.8
qscriptdebuggercodeview.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
44 
45 #include "qscriptedit_p.h"
46 
47 #include <QtGui/qboxlayout.h>
48 #include <QtGui/qtextobject.h>
49 #include <QtCore/qdebug.h>
50 
52 
55 {
57 public:
60 
62 };
63 
65 {
66 }
67 
69 {
70 }
71 
74 {
76  d->editor = new QScriptEdit();
77  d->editor->setReadOnly(true);
78  d->editor->setBackgroundVisible(false);
80  this, SIGNAL(breakpointToggleRequest(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 
89 {
90 }
91 
93 {
95  return d->editor->toPlainText();
96 }
97 
99 {
101  d->editor->setPlainText(text);
102 }
103 
105 {
107  return d->editor->currentLineNumber();
108 }
109 
111 {
113  d->editor->gotoLine(lineNumber);
114 }
115 
116 int QScriptDebuggerCodeView::find(const QString &exp, int options)
117 {
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;
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 
147 {
149  d->editor->setExecutionLineNumber(lineNumber, error);
150 }
151 
153 {
155  d->editor->setExecutableLineNumbers(lineNumbers);
156 }
157 
159 {
161  return d->editor->baseLineNumber();
162 }
163 
165 {
167  d->editor->setBaseLineNumber(lineNumber);
168 }
169 
171 {
173  d->editor->setBreakpoint(lineNumber);
174 }
175 
177 {
179  d->editor->deleteBreakpoint(lineNumber);
180 }
181 
182 void QScriptDebuggerCodeView::setBreakpointEnabled(int lineNumber, bool enable)
183 {
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 
202 {
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+1 < contents.size()) && 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 
259 
260 #include "moc_qscriptdebuggercodeview_p.cpp"
QPoint pos() const
double d
Definition: qnumeric_p.h:62
bool find(const QString &exp, QTextDocument::FindFlags options=0)
Finds the next occurrence of the string, exp, using the given options.
void setBreakpoint(int lineNumber)
QString text() const
Returns the block&#39;s contents as plain text.
bool isLetter() const
Returns true if the character is a letter (Letter_* categories); otherwise returns false...
Definition: qchar.cpp:653
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QCursor cursor() const
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
bool event(QEvent *e)
Reimplemented Function
#define error(msg)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setBreakpointEnabled(int lineNumber, bool enable)
int & ry()
Returns a reference to the y coordinate of this point.
Definition: qpoint.h:143
QTextCursor textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor.
void setBaseLineNumber(int lineNumber)
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=0)
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment...
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
int position() const
Returns the index of the block&#39;s first character within the document.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setExecutableLineNumbers(const QSet< int > &lineNumbers)
int blockNumber() const
Returns the number of the block the cursor is in, or 0 if the cursor is invalid.
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int & rx()
Returns a reference to the x coordinate of this point.
Definition: qpoint.h:140
The QTextBlock class provides a container for text fragments in a QTextDocument.
Definition: qtextobject.h:199
The QPlainTextEdit class provides a widget that is used to edit and display plain text...
int position() const
Returns the absolute position of the cursor within the document.
void setExecutionLineNumber(int lineNumber, bool error)
QTextBlock block() const
Returns the block that contains the cursor.
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void breakpointToggleRequest(int lineNumber, bool on)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QScriptDebuggerCodeView(QWidget *parent=0)
const QPoint & globalPos() const
Returns the mouse cursor position when the event was generated in global coordinates.
Definition: qevent.h:598
int find(const QString &exp, int options=0)
bool movePosition(MoveOperation op, MoveMode=MoveAnchor, int n=1)
Moves the cursor by performing the given operation n times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false.
void setTextCursor(const QTextCursor &cursor)
Sets the visible cursor.
QObject * parent
Definition: qobject.h:92
void toolTipRequest(const QPoint &pos, int lineNumber, const QStringList &path)
void setText(const QString &text)
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:149
void breakpointEnableRequest(int lineNumber, bool enable)
void setMargin(int)
Definition: qlayout.cpp:464
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool isNumber() const
Returns true if the character is a number (Number_* categories, not just 0-9); otherwise returns fals...
Definition: qchar.cpp:669
void deleteBreakpoint(int lineNumber)
The QHelpEvent class provides an event that is used to request helpful information about a particular...
Definition: qevent.h:586
const QPoint & pos() const
Returns the mouse cursor position when the event was generated, relative to the widget to which the e...
Definition: qevent.h:597