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 "qscriptedit_p.h"
|
---|
43 | #include "qscriptsyntaxhighlighter_p.h"
|
---|
44 |
|
---|
45 | #include <QtGui/qpainter.h>
|
---|
46 | #include <QtGui/qicon.h>
|
---|
47 | #include <QtGui/qboxlayout.h>
|
---|
48 | #include <QtGui/qlabel.h>
|
---|
49 | #include <QtGui/qlineedit.h>
|
---|
50 | #include <QtGui/qmenu.h>
|
---|
51 | #include <QtGui/qaction.h>
|
---|
52 | #include <QtGui/qwidgetaction.h>
|
---|
53 | #include <QtCore/qdebug.h>
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | class QScriptEditExtraArea : public QWidget
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | QScriptEditExtraArea(QScriptEdit *edit)
|
---|
61 | : QWidget(edit)
|
---|
62 | {
|
---|
63 | setMouseTracking(true);
|
---|
64 | }
|
---|
65 |
|
---|
66 | QSize sizeHint() const {
|
---|
67 | return QSize(editor()->extraAreaWidth(), 0);
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected:
|
---|
71 | void paintEvent(QPaintEvent *event)
|
---|
72 | {
|
---|
73 | editor()->extraAreaPaintEvent(event);
|
---|
74 | }
|
---|
75 | void mousePressEvent(QMouseEvent *event)
|
---|
76 | {
|
---|
77 | editor()->extraAreaMouseEvent(event);
|
---|
78 | }
|
---|
79 | void mouseMoveEvent(QMouseEvent *event)
|
---|
80 | {
|
---|
81 | editor()->extraAreaMouseEvent(event);
|
---|
82 | }
|
---|
83 | void mouseReleaseEvent(QMouseEvent *event)
|
---|
84 | {
|
---|
85 | editor()->extraAreaMouseEvent(event);
|
---|
86 | }
|
---|
87 | bool event(QEvent *event)
|
---|
88 | {
|
---|
89 | if (editor()->extraAreaEvent(event))
|
---|
90 | return true;
|
---|
91 | return QWidget::event(event);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private:
|
---|
95 | QScriptEdit *editor() const
|
---|
96 | {
|
---|
97 | return qobject_cast<QScriptEdit*>(parent());
|
---|
98 | }
|
---|
99 | };
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | QScriptEdit::QScriptEdit(QWidget *parent)
|
---|
104 | : QPlainTextEdit(parent)
|
---|
105 | {
|
---|
106 | m_baseLineNumber = 1;
|
---|
107 | m_executionLineNumber = -1;
|
---|
108 |
|
---|
109 | m_extraArea = new QScriptEditExtraArea(this);
|
---|
110 |
|
---|
111 | QObject::connect(this, SIGNAL(blockCountChanged(int)),
|
---|
112 | this, SLOT(updateExtraAreaWidth()));
|
---|
113 | QObject::connect(this, SIGNAL(updateRequest(const QRect &, int)),
|
---|
114 | this, SLOT(updateExtraArea(const QRect &, int)));
|
---|
115 | QObject::connect(this, SIGNAL(cursorPositionChanged()),
|
---|
116 | this, SLOT(highlightCurrentLine()));
|
---|
117 |
|
---|
118 | updateExtraAreaWidth();
|
---|
119 |
|
---|
120 | #ifndef QT_NO_SYNTAXHIGHLIGHTER
|
---|
121 | (void) new QScriptSyntaxHighlighter(document());
|
---|
122 | #endif
|
---|
123 | }
|
---|
124 |
|
---|
125 | QScriptEdit::~QScriptEdit()
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 | int QScriptEdit::baseLineNumber() const
|
---|
130 | {
|
---|
131 | return m_baseLineNumber;
|
---|
132 | }
|
---|
133 |
|
---|
134 | void QScriptEdit::setBaseLineNumber(int base)
|
---|
135 | {
|
---|
136 | m_baseLineNumber = base;
|
---|
137 | m_extraArea->update();
|
---|
138 | }
|
---|
139 |
|
---|
140 | int QScriptEdit::executionLineNumber() const
|
---|
141 | {
|
---|
142 | return m_executionLineNumber;
|
---|
143 | }
|
---|
144 |
|
---|
145 | void QScriptEdit::setExecutionLineNumber(int lineNumber, bool error)
|
---|
146 | {
|
---|
147 | m_executionLineNumber = lineNumber;
|
---|
148 | m_executionLineNumberHasError = error;
|
---|
149 | m_extraArea->update();
|
---|
150 | updateExtraSelections();
|
---|
151 | gotoLine(lineNumber);
|
---|
152 | }
|
---|
153 |
|
---|
154 | void QScriptEdit::setExecutableLineNumbers(const QSet<int> &lineNumbers)
|
---|
155 | {
|
---|
156 | m_executableLineNumbers = lineNumbers;
|
---|
157 | }
|
---|
158 |
|
---|
159 | int QScriptEdit::currentLineNumber() const
|
---|
160 | {
|
---|
161 | return textCursor().blockNumber() + m_baseLineNumber;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void QScriptEdit::gotoLine(int lineNumber)
|
---|
165 | {
|
---|
166 | int blockNumber = lineNumber - m_baseLineNumber;
|
---|
167 | const QTextBlock &block = document()->findBlockByNumber(blockNumber);
|
---|
168 | if (block.isValid()) {
|
---|
169 | setTextCursor(QTextCursor(block));
|
---|
170 | centerCursor();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | void QScriptEdit::setBreakpoint(int lineNumber)
|
---|
175 | {
|
---|
176 | m_breakpoints[lineNumber] = BreakpointData();
|
---|
177 | m_extraArea->update();
|
---|
178 | }
|
---|
179 |
|
---|
180 | void QScriptEdit::setBreakpointEnabled(int lineNumber, bool enable)
|
---|
181 | {
|
---|
182 | m_breakpoints[lineNumber].enabled = enable;
|
---|
183 | m_extraArea->update();
|
---|
184 | }
|
---|
185 |
|
---|
186 | void QScriptEdit::deleteBreakpoint(int lineNumber)
|
---|
187 | {
|
---|
188 | m_breakpoints.remove(lineNumber);
|
---|
189 | m_extraArea->update();
|
---|
190 | }
|
---|
191 |
|
---|
192 | void QScriptEdit::paintEvent(QPaintEvent *e)
|
---|
193 | {
|
---|
194 | QPlainTextEdit::paintEvent(e);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void QScriptEdit::resizeEvent(QResizeEvent *e)
|
---|
198 | {
|
---|
199 | QPlainTextEdit::resizeEvent(e);
|
---|
200 |
|
---|
201 | QRect cr = contentsRect();
|
---|
202 | int x = isLeftToRight() ? cr.left() : cr.left() + cr.width() - extraAreaWidth();
|
---|
203 | m_extraArea->setGeometry(QRect(x, cr.top(), extraAreaWidth(), cr.height()));
|
---|
204 | }
|
---|
205 |
|
---|
206 | void QScriptEdit::updateExtraAreaWidth()
|
---|
207 | {
|
---|
208 | if (isLeftToRight())
|
---|
209 | setViewportMargins(extraAreaWidth(), 0, 0, 0);
|
---|
210 | else
|
---|
211 | setViewportMargins(0, 0, extraAreaWidth(), 0);
|
---|
212 | }
|
---|
213 |
|
---|
214 | void QScriptEdit::updateExtraArea(const QRect &rect, int dy)
|
---|
215 | {
|
---|
216 | if (dy)
|
---|
217 | m_extraArea->scroll(0, dy);
|
---|
218 | else
|
---|
219 | m_extraArea->update(0, rect.y(), m_extraArea->width(), rect.height());
|
---|
220 |
|
---|
221 | if (rect.contains(viewport()->rect()))
|
---|
222 | updateExtraAreaWidth();
|
---|
223 | }
|
---|
224 |
|
---|
225 | void QScriptEdit::highlightCurrentLine()
|
---|
226 | {
|
---|
227 | updateExtraSelections();
|
---|
228 | }
|
---|
229 |
|
---|
230 | void QScriptEdit::updateExtraSelections()
|
---|
231 | {
|
---|
232 | QList<QTextEdit::ExtraSelection> extraSelections;
|
---|
233 |
|
---|
234 | {
|
---|
235 | QTextEdit::ExtraSelection selection;
|
---|
236 | QColor lineColor = QColor(Qt::yellow).lighter(160);
|
---|
237 | selection.format.setBackground(lineColor);
|
---|
238 | selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
---|
239 | selection.cursor = textCursor();
|
---|
240 | selection.cursor.clearSelection();
|
---|
241 | extraSelections.append(selection);
|
---|
242 | }
|
---|
243 | if (m_executionLineNumber != -1) {
|
---|
244 | QTextEdit::ExtraSelection selection;
|
---|
245 | QColor lineColor;
|
---|
246 | if (m_executionLineNumberHasError)
|
---|
247 | lineColor = QColor(Qt::red);
|
---|
248 | else
|
---|
249 | lineColor = QColor(Qt::green).lighter(160);
|
---|
250 | selection.format.setBackground(lineColor);
|
---|
251 | selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
---|
252 | int blockNumber = m_executionLineNumber - m_baseLineNumber;
|
---|
253 | selection.cursor = QTextCursor(document()->findBlockByNumber(blockNumber));
|
---|
254 | selection.cursor.clearSelection();
|
---|
255 | extraSelections.append(selection);
|
---|
256 | }
|
---|
257 |
|
---|
258 | setExtraSelections(extraSelections);
|
---|
259 | }
|
---|
260 |
|
---|
261 | int QScriptEdit::extraAreaWidth() const
|
---|
262 | {
|
---|
263 | int space = 0;
|
---|
264 | const QFontMetrics fm(fontMetrics());
|
---|
265 |
|
---|
266 | int digits = 1;
|
---|
267 | int max = qMax(1, blockCount() + m_baseLineNumber);
|
---|
268 | while (max >= 10) {
|
---|
269 | max /= 10;
|
---|
270 | ++digits;
|
---|
271 | }
|
---|
272 | space += fm.width(QLatin1Char('9')) * digits;
|
---|
273 |
|
---|
274 | int markWidth = fm.lineSpacing();
|
---|
275 | space += markWidth;
|
---|
276 |
|
---|
277 | space += 4;
|
---|
278 |
|
---|
279 | return space;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void QScriptEdit::extraAreaPaintEvent(QPaintEvent *e)
|
---|
283 | {
|
---|
284 | QRect rect = e->rect();
|
---|
285 | QPalette pal = palette();
|
---|
286 | pal.setCurrentColorGroup(QPalette::Active);
|
---|
287 | QPainter painter(m_extraArea);
|
---|
288 | painter.fillRect(rect, Qt::lightGray);
|
---|
289 | const QFontMetrics fm(fontMetrics());
|
---|
290 |
|
---|
291 | int markWidth = fm.lineSpacing();
|
---|
292 | int extraAreaWidth = m_extraArea->width();
|
---|
293 |
|
---|
294 | QLinearGradient gradient(QPointF(extraAreaWidth - 10, 0), QPointF(extraAreaWidth, 0));
|
---|
295 | gradient.setColorAt(0, pal.color(QPalette::Background));
|
---|
296 | gradient.setColorAt(1, pal.color(QPalette::Base));
|
---|
297 | painter.fillRect(rect, gradient);
|
---|
298 |
|
---|
299 | QLinearGradient gradient2(QPointF(0, 0), QPointF(markWidth, 0));
|
---|
300 | gradient2.setColorAt(0, pal.color(QPalette::Dark));
|
---|
301 | gradient2.setColorAt(1, pal.color(QPalette::Background));
|
---|
302 | painter.fillRect(rect.intersected(QRect(rect.x(), rect.y(), markWidth, rect.height())), gradient2);
|
---|
303 |
|
---|
304 | painter.setPen(QPen(pal.color(QPalette::Background), 2));
|
---|
305 | if (isLeftToRight())
|
---|
306 | painter.drawLine(rect.x() + extraAreaWidth-1, rect.top(), rect.x() + extraAreaWidth-1, rect.bottom());
|
---|
307 | else
|
---|
308 | painter.drawLine(rect.x(), rect.top(), rect.x(), rect.bottom());
|
---|
309 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
310 |
|
---|
311 | QTextBlock block = firstVisibleBlock();
|
---|
312 | int blockNumber = block.blockNumber();
|
---|
313 | qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
|
---|
314 | qreal bottom = top + blockBoundingRect(block).height();
|
---|
315 |
|
---|
316 | QString imagesPath = QString::fromLatin1(":/qt/scripttools/debugging/images");
|
---|
317 | QString imageExt;
|
---|
318 | // SVGs don't work on all platforms, even when QT_NO_SVG is not defined, so disable SVG usage for now.
|
---|
319 | // #ifndef QT_NO_SVG
|
---|
320 | #if 0
|
---|
321 | imageExt = QString::fromLatin1("svg");
|
---|
322 | #else
|
---|
323 | imageExt = QString::fromLatin1("png");
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | while (block.isValid() && top <= rect.bottom()) {
|
---|
327 | if (block.isVisible() && bottom >= rect.top()) {
|
---|
328 |
|
---|
329 | int lineNumber = blockNumber + m_baseLineNumber;
|
---|
330 | if (m_breakpoints.contains(lineNumber)) {
|
---|
331 | int radius = fm.lineSpacing() - 1;
|
---|
332 | QRect r(rect.x(), (int)top, radius, radius);
|
---|
333 | QIcon icon(m_breakpoints[lineNumber].enabled
|
---|
334 | ? QString::fromLatin1("%0/breakpoint.%1").arg(imagesPath).arg(imageExt)
|
---|
335 | : QString::fromLatin1("%0/d_breakpoint.%1").arg(imagesPath).arg(imageExt));
|
---|
336 | icon.paint(&painter, r, Qt::AlignCenter);
|
---|
337 | }
|
---|
338 | if (m_executionLineNumber == lineNumber) {
|
---|
339 | int radius = fm.lineSpacing() - 1;
|
---|
340 | QRect r(rect.x(), (int)top, radius, radius);
|
---|
341 | QIcon icon(QString::fromLatin1("%0/location.%1").arg(imagesPath).arg(imageExt));
|
---|
342 | icon.paint(&painter, r, Qt::AlignCenter);
|
---|
343 | }
|
---|
344 |
|
---|
345 | #if QT_VERSION >= 0x040500
|
---|
346 | if (!m_executableLineNumbers.contains(lineNumber))
|
---|
347 | painter.setPen(pal.color(QPalette::Mid));
|
---|
348 | else
|
---|
349 | #endif
|
---|
350 | painter.setPen(QColor(Qt::darkCyan));
|
---|
351 | QString number = QString::number(lineNumber);
|
---|
352 | painter.drawText(rect.x() + markWidth, (int)top, rect.x() + extraAreaWidth - markWidth - 4,
|
---|
353 | fm.height(), Qt::AlignRight, number);
|
---|
354 | }
|
---|
355 |
|
---|
356 | block = block.next();
|
---|
357 | top = bottom;
|
---|
358 | bottom = top + blockBoundingRect(block).height();
|
---|
359 | ++blockNumber;
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | void QScriptEdit::extraAreaMouseEvent(QMouseEvent *e)
|
---|
364 | {
|
---|
365 | QTextCursor cursor = cursorForPosition(QPoint(0, e->pos().y()));
|
---|
366 | cursor.setPosition(cursor.block().position());
|
---|
367 |
|
---|
368 | QFontMetrics fm(font());
|
---|
369 | int markWidth = fm.lineSpacing();
|
---|
370 |
|
---|
371 | if (e->type() == QEvent::MouseMove && e->buttons() == 0) { // mouse tracking
|
---|
372 | bool hand = (e->pos().x() <= markWidth);
|
---|
373 | #if QT_VERSION >= 0x040500
|
---|
374 | int lineNumber = cursor.blockNumber() + m_baseLineNumber;
|
---|
375 | hand = hand && m_executableLineNumbers.contains(lineNumber);
|
---|
376 | #endif
|
---|
377 | #ifndef QT_NO_CURSOR
|
---|
378 | if (hand != (m_extraArea->cursor().shape() == Qt::PointingHandCursor))
|
---|
379 | m_extraArea->setCursor(hand ? Qt::PointingHandCursor : Qt::ArrowCursor);
|
---|
380 | #endif
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (e->type() == QEvent::MouseButtonPress) {
|
---|
384 | if (e->button() == Qt::LeftButton) {
|
---|
385 | #if QT_VERSION >= 0x040500
|
---|
386 | int lineNumber = cursor.blockNumber() + m_baseLineNumber;
|
---|
387 | bool executable = m_executableLineNumbers.contains(lineNumber);
|
---|
388 | #else
|
---|
389 | bool executable = true;
|
---|
390 | #endif
|
---|
391 | if ((e->pos().x() <= markWidth) && executable)
|
---|
392 | m_extraAreaToggleBlockNumber = cursor.blockNumber();
|
---|
393 | else
|
---|
394 | m_extraAreaToggleBlockNumber = -1;
|
---|
395 | }
|
---|
396 | } else if (e->type() == QEvent::MouseButtonRelease) {
|
---|
397 | if (e->button() == Qt::LeftButton) {
|
---|
398 | if ((m_extraAreaToggleBlockNumber != -1) && (e->pos().x() <= markWidth)) {
|
---|
399 | int lineNumber = m_extraAreaToggleBlockNumber + m_baseLineNumber;
|
---|
400 | bool on = !m_breakpoints.contains(lineNumber);
|
---|
401 | emit breakpointToggleRequest(lineNumber, on);
|
---|
402 | }
|
---|
403 | } else if (e->button() == Qt::RightButton) {
|
---|
404 | int lineNumber = cursor.blockNumber() + m_baseLineNumber;
|
---|
405 | #if QT_VERSION >= 0x040500
|
---|
406 | if (!m_executableLineNumbers.contains(lineNumber))
|
---|
407 | return;
|
---|
408 | #endif
|
---|
409 | bool has = m_breakpoints.contains(lineNumber);
|
---|
410 | QMenu *popup = new QMenu();
|
---|
411 | QAction *toggleAct = new QAction(QObject::tr("Toggle Breakpoint"), popup);
|
---|
412 | popup->addAction(toggleAct);
|
---|
413 | QAction *disableAct = new QAction(QObject::tr("Disable Breakpoint"), popup);
|
---|
414 | QAction *enableAct = new QAction(QObject::tr("Enable Breakpoint"), popup);
|
---|
415 | QWidget *conditionWidget = new QWidget();
|
---|
416 | {
|
---|
417 | QHBoxLayout *hbox = new QHBoxLayout(conditionWidget);
|
---|
418 | hbox->addWidget(new QLabel(QObject::tr("Breakpoint Condition:")));
|
---|
419 | hbox->addWidget(new QLineEdit());
|
---|
420 | }
|
---|
421 | // QWidgetAction *conditionAct = new QWidgetAction(popup);
|
---|
422 | // conditionAct->setDefaultWidget(conditionWidget);
|
---|
423 | if (has) {
|
---|
424 | popup->addSeparator();
|
---|
425 | popup->addAction(m_breakpoints[lineNumber].enabled ? disableAct : enableAct);
|
---|
426 | // popup->addAction(conditionAct);
|
---|
427 | }
|
---|
428 | QAction *ret = popup->exec(e->globalPos());
|
---|
429 | if (ret) {
|
---|
430 | if (ret == toggleAct) {
|
---|
431 | emit breakpointToggleRequest(lineNumber, !has);
|
---|
432 | } else if (ret == disableAct) {
|
---|
433 | emit breakpointEnableRequest(lineNumber, false);
|
---|
434 | } else if (ret == enableAct) {
|
---|
435 | emit breakpointEnableRequest(lineNumber, true);
|
---|
436 | }// else if (ret == conditionAct) {
|
---|
437 | //}
|
---|
438 | }
|
---|
439 | popup->deleteLater();
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | bool QScriptEdit::extraAreaEvent(QEvent *e)
|
---|
445 | {
|
---|
446 | if (e->type() == QEvent::ToolTip) {
|
---|
447 | // ### show the breakpoint's condition, if any
|
---|
448 | return true;
|
---|
449 | }
|
---|
450 | return false;
|
---|
451 | }
|
---|
452 |
|
---|
453 | QT_END_NAMESPACE
|
---|