Debian Codegraph @

src/scripttools/debugging/qscriptdebuggercodeview.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qscriptdebuggercodeview_p.h"
41 #include "qscriptdebuggercodeviewinterface_p_p.h"
42
43 #include "qscriptedit_p.h"
44
45 #include <QtWidgets/qboxlayout.h>
46 #include <QtGui/qtextobject.h>
47 #include <QtCore/qdebug.h>
48
49 QT_BEGIN_NAMESPACE
50
51 class QScriptDebuggerCodeViewPrivate
52 : public QScriptDebuggerCodeViewInterfacePrivate
53 {
54 Q_DECLARE_PUBLIC(QScriptDebuggerCodeView)
55 public:
56 QScriptDebuggerCodeViewPrivate();
57 ~QScriptDebuggerCodeViewPrivate();
58
59 QScriptEdit *editor;
60 };
61
62 QScriptDebuggerCodeViewPrivate::QScriptDebuggerCodeViewPrivate()
63 {
64 }
65
66 QScriptDebuggerCodeViewPrivate::~QScriptDebuggerCodeViewPrivate()
67 {
68 }
69
70 QScriptDebuggerCodeView::QScriptDebuggerCodeView(QWidget *parent)
71 : QScriptDebuggerCodeViewInterface(*new QScriptDebuggerCodeViewPrivate, parent, {})
72 {
73 Q_D(QScriptDebuggerCodeView);
74 d->editor = new QScriptEdit();
75 d->editor->setReadOnly(true);
76 d->editor->setBackgroundVisible(false);
77 QObject::connect(d->editor, SIGNAL(breakpointToggleRequest(int,bool)),
78 this, SIGNAL(breakpointToggleRequest(int,bool)));
79 QObject::connect(d->editor, SIGNAL(breakpointEnableRequest(int,bool)),
80 this, SIGNAL(breakpointEnableRequest(int,bool)));
81 QVBoxLayout *vbox = new QVBoxLayout(this);
82 vbox->setContentsMargins(0, 0, 0, 0);
83 vbox->addWidget(d->editor);
84 }
85
86 QScriptDebuggerCodeView::~QScriptDebuggerCodeView()
87 {
88 }
89
90 QString QScriptDebuggerCodeView::text() const
91 {
92 Q_D(const QScriptDebuggerCodeView);
93 return d->editor->toPlainText();
94 }
95
96 void QScriptDebuggerCodeView::setText(const QString &text)
97 {
98 Q_D(QScriptDebuggerCodeView);
99 d->editor->setPlainText(text);
100 }
101
102 int QScriptDebuggerCodeView::cursorLineNumber() const
103 {
104 Q_D(const QScriptDebuggerCodeView);
105 return d->editor->currentLineNumber();
106 }
107
108 void QScriptDebuggerCodeView::gotoLine(int lineNumber)
109 {
110 Q_D(QScriptDebuggerCodeView);
111 d->editor->gotoLine(lineNumber);
112 }
113
114 int QScriptDebuggerCodeView::find(const QString &exp, int options)
115 {
116 Q_D(QScriptDebuggerCodeView);
117 QPlainTextEdit *ed = (QPlainTextEdit*)d->editor;
118 QTextCursor cursor = ed->textCursor();
119 if (options & 0x100) {
120 // start searching from the beginning of selection
121 if (cursor.hasSelection()) {
122 int len = cursor.selectedText().length();
123 cursor.clearSelection();
124 cursor.setPosition(cursor.position() - len);
125 ed->setTextCursor(cursor);
126 }
127 options &= ~0x100;
128 }
129 int ret = 0;
130 if (ed->find(exp, QTextDocument::FindFlags(options))) {
131 ret |= 0x1;
132 } else {
133 QTextCursor curse = cursor;
134 curse.movePosition(QTextCursor::Start);
135 ed->setTextCursor(curse);
136 if (ed->find(exp, QTextDocument::FindFlags(options)))
137 ret |= 0x1 | 0x2;
138 else
139 ed->setTextCursor(cursor);
140 }
141 return ret;
142 }
143
144 void QScriptDebuggerCodeView::setExecutionLineNumber(int lineNumber, bool error)
145 {
146 Q_D(QScriptDebuggerCodeView);
147 d->editor->setExecutionLineNumber(lineNumber, error);
148 }
149
150 void QScriptDebuggerCodeView::setExecutableLineNumbers(const QSet<int> &lineNumbers)
151 {
152 Q_D(QScriptDebuggerCodeView);
153 d->editor->setExecutableLineNumbers(lineNumbers);
154 }
155
156 int QScriptDebuggerCodeView::baseLineNumber() const
157 {
158 Q_D(const QScriptDebuggerCodeView);
159 return d->editor->baseLineNumber();
160 }
161
162 void QScriptDebuggerCodeView::setBaseLineNumber(int lineNumber)
163 {
164 Q_D(QScriptDebuggerCodeView);
165 d->editor->setBaseLineNumber(lineNumber);
166 }
167
168 void QScriptDebuggerCodeView::setBreakpoint(int lineNumber)
169 {
170 Q_D(QScriptDebuggerCodeView);
171 d->editor->setBreakpoint(lineNumber);
172 }
173
174 void QScriptDebuggerCodeView::deleteBreakpoint(int lineNumber)
175 {
176 Q_D(QScriptDebuggerCodeView);
177 d->editor->deleteBreakpoint(lineNumber);
178 }
179
180 void QScriptDebuggerCodeView::setBreakpointEnabled(int lineNumber, bool enable)
181 {
182 Q_D(QScriptDebuggerCodeView);
183 d->editor->setBreakpointEnabled(lineNumber, enable);
184 }
185
186 namespace {
187
188 static bool isIdentChar(const QChar &ch)
189 {
190 static QChar underscore = QLatin1Char('_');
191 return ch.isLetter() || (ch == underscore);
192 }
193
194 } // namespace
195
196 /*!
197 \reimp
198 */
199 bool QScriptDebuggerCodeView::event(QEvent *e)
200 {
201 Q_D(QScriptDebuggerCodeView);
202 if (e->type() == QEvent::ToolTip) {
203 if (d->editor->executionLineNumber() == -1)
204 return false;
205 QHelpEvent *he = static_cast<QHelpEvent*>(e);
206 QPoint pt = he->pos();
207 pt.rx() -= d->editor->extraAreaWidth();
208 pt.ry() -= 8;
209 QTextCursor cursor = d->editor->cursorForPosition(pt);
210 QTextBlock block = cursor.block();
211 QString contents = block.text();
212 if (contents.isEmpty())
213 return false;
214 int linePosition = cursor.position() - block.position();
215 if (linePosition < 0)
216 linePosition = 0;
217
218 // ### generalize -- same as in completiontask
219
220 int pos = linePosition;
221 if ((pos > 0) && contents.at(pos-1).isNumber()) {
222 // tooltips for numbers is pointless
223 return false;
224 }
225
226 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
227 --pos;
228 if ((pos > 0) && ((contents.at(pos-1) == QLatin1Char('\''))
229 || (contents.at(pos-1) == QLatin1Char('\"')))) {
230 // ignore string literals
231 return false;
232 }
233 int pos2 = linePosition - 1;
234 while ((pos2+1 < contents.size()) && isIdentChar(contents.at(pos2+1)))
235 ++pos2;
236 QString ident = contents.mid(pos, pos2 - pos + 1);
237
238 QStringList path;
239 path.append(ident);
240 while ((pos > 0) && (contents.at(pos-1) == QLatin1Char('.'))) {
241 --pos;
242 pos2 = pos;
243 while ((pos > 0) && isIdentChar(contents.at(pos-1)))
244 --pos;
245 path.prepend(contents.mid(pos, pos2 - pos));
246 }
247
248 if (!path.isEmpty()) {
249 int lineNumber = cursor.blockNumber() + d->editor->baseLineNumber();
250 emit toolTipRequest(he->globalPos(), lineNumber, path);
251 }
252 }
253 return false;
254 }
255
256 QT_END_NAMESPACE
257
258 #include "moc_qscriptdebuggercodeview_p.cpp"