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 "qscriptdebugoutputwidget_p.h" |
41 | #include "qscriptdebugoutputwidgetinterface_p_p.h" |
42 | |
43 | #include <QtCore/qdebug.h> |
44 | #include <QtWidgets/qboxlayout.h> |
45 | #include <QtWidgets/qplaintextedit.h> |
46 | #include <QtWidgets/qscrollbar.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | namespace { |
51 | |
52 | class QScriptDebugOutputWidgetOutputEdit : public QPlainTextEdit |
53 | { |
54 | public: |
55 | QScriptDebugOutputWidgetOutputEdit(QWidget *parent = 0) |
56 | : QPlainTextEdit(parent) |
57 | { |
58 | setReadOnly(true); |
59 | // setFocusPolicy(Qt::NoFocus); |
60 | setMaximumBlockCount(2500); |
61 | } |
62 | |
63 | void scrollToBottom() |
64 | { |
65 | QScrollBar *bar = verticalScrollBar(); |
66 | bar->setValue(bar->maximum()); |
67 | } |
68 | |
69 | int charactersPerLine() const |
70 | { |
71 | QFontMetrics fm(font()); |
72 | return width() / fm.maxWidth(); |
73 | } |
74 | }; |
75 | |
76 | } // namespace |
77 | |
78 | class QScriptDebugOutputWidgetPrivate |
79 | : public QScriptDebugOutputWidgetInterfacePrivate |
80 | { |
81 | Q_DECLARE_PUBLIC(QScriptDebugOutputWidget) |
82 | public: |
83 | QScriptDebugOutputWidgetPrivate(); |
84 | ~QScriptDebugOutputWidgetPrivate(); |
85 | |
86 | QScriptDebugOutputWidgetOutputEdit *outputEdit; |
87 | }; |
88 | |
89 | QScriptDebugOutputWidgetPrivate::QScriptDebugOutputWidgetPrivate() |
90 | { |
91 | } |
92 | |
93 | QScriptDebugOutputWidgetPrivate::~QScriptDebugOutputWidgetPrivate() |
94 | { |
95 | } |
96 | |
97 | QScriptDebugOutputWidget::QScriptDebugOutputWidget(QWidget *parent) |
98 | : QScriptDebugOutputWidgetInterface(*new QScriptDebugOutputWidgetPrivate, parent, {}) |
99 | { |
100 | Q_D(QScriptDebugOutputWidget); |
101 | d->outputEdit = new QScriptDebugOutputWidgetOutputEdit(); |
102 | QVBoxLayout *vbox = new QVBoxLayout(this); |
103 | vbox->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
104 | vbox->setSpacing(0); |
105 | vbox->addWidget(d->outputEdit); |
106 | |
107 | #ifndef QT_NO_STYLE_STYLESHEET |
108 | QString sheet = QString::fromLatin1(str: "font-size: 14px; font-family: \"Monospace\";" ); |
109 | setStyleSheet(sheet); |
110 | #endif |
111 | } |
112 | |
113 | QScriptDebugOutputWidget::~QScriptDebugOutputWidget() |
114 | { |
115 | } |
116 | |
117 | void QScriptDebugOutputWidget::message( |
118 | QtMsgType type, const QString &text, const QString &fileName, |
119 | int lineNumber, int columnNumber, const QVariant &/*data*/) |
120 | { |
121 | // ### unify with QScriptDebuggerConsoleWidget::message() |
122 | Q_D(QScriptDebugOutputWidget); |
123 | QString msg; |
124 | if (!fileName.isEmpty() || (lineNumber != -1)) { |
125 | if (!fileName.isEmpty()) |
126 | msg.append(s: fileName); |
127 | else |
128 | msg.append(s: QLatin1String("<noname>" )); |
129 | if (lineNumber != -1) { |
130 | msg.append(c: QLatin1Char(':')); |
131 | msg.append(s: QString::number(lineNumber)); |
132 | if (columnNumber != -1) { |
133 | msg.append(c: QLatin1Char(':')); |
134 | msg.append(s: QString::number(columnNumber)); |
135 | } |
136 | } |
137 | msg.append(s: QLatin1String(": " )); |
138 | } |
139 | msg.append(s: text); |
140 | QTextCharFormat oldFmt = d->outputEdit->currentCharFormat(); |
141 | QTextCharFormat fmt(oldFmt); |
142 | if (type == QtCriticalMsg) { |
143 | fmt.setForeground(Qt::red); |
144 | d->outputEdit->setCurrentCharFormat(fmt); |
145 | } |
146 | d->outputEdit->appendPlainText(text: msg); |
147 | d->outputEdit->setCurrentCharFormat(oldFmt); |
148 | d->outputEdit->scrollToBottom(); |
149 | } |
150 | |
151 | void QScriptDebugOutputWidget::clear() |
152 | { |
153 | Q_D(QScriptDebugOutputWidget); |
154 | d->outputEdit->clear(); |
155 | } |
156 | |
157 | QT_END_NAMESPACE |
158 | |