Debian Codegraph @

src/console.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "console.h"
24
25 #include <QDebug>
26 #include <QtWidgets>
27
28
29 Console::Console(QWidget *parent) : QTextEdit(parent)
30 {
31 error = false;
32 errorLine = false;
33 setReadOnly(true);
34 m_warningColor = QColor("orange");
35 rxerr.setPattern("^\\s*error:\\.+line\\ ");
36 }
37
38 Console::~Console()
39 {
40 disconnect(this, 0,0,0);
41 }
42
43 void Console::appendMessage(QString msg)
44 {
45 QMutexLocker locker(&consoleLock);
46 logMessage(msg);
47
48 /*
49 // Filter unnecessary messages
50 if (msg.startsWith("libsndfile-1")
51 || msg.startsWith("UnifiedCSD: ")
52 || msg.startsWith("orchname: ")
53 || msg.startsWith("scorename: ")) {
54 return;
55 }
56 */
57
58 setTextColor(m_textColor);
59 // if "unexpected token error", remove this newline, otherwise line number stays
60 // in next messageLine
61 if ( msg.contains("(token") )
62 msg.remove("\n");
63 messageLine.append(msg);
64
65 // if (msg.contains("\n")) {
66 if(msg.lastIndexOf(QChar('\n')) >= 0) {
67 // line finished, analyze it now
68 if(rxerr.match(messageLine).hasMatch()) {
69 errorTexts.append(messageLine);
70 errorTexts.last().remove("\n");
71
72 QStringList parts = messageLine.split("line "); // get the line number
73 QString lnr = parts.last().remove(">>>");
74 lnr = lnr.remove(":");
75 lnr = lnr.trimmed();
76 errorLines.append(lnr.toInt());
77 qDebug() << "error line appended --- " << lnr.toInt();
78 }
79 else if (messageLine.contains("Line:", Qt::CaseSensitive)) {
80 // as in type erroris in csound6 like 'Line: 54 Loc: 1'
81 errorTexts.append("Error");
82 QStringList parts = messageLine.split(" "); // get the line number
83 QString lnr = parts[1]; // 2nd element in the array
84 errorLines.append(lnr.toInt());
85 qDebug() << "error line appended --- " << lnr.toInt();
86 }
87 else if (messageLine.startsWith("B ")
88 || messageLine.contains("rtevent", Qt::CaseInsensitive)
89 || messageLine.contains("evaluated", Qt::CaseInsensitive)) {
90 setTextColor(QColor("#4040FF"));
91 }
92 else if (messageLine.contains("overall samples out of range")
93 || messageLine.contains("disabled")
94 || messageLine.contains("error", Qt::CaseInsensitive)
95 || messageLine.contains("Found:")
96 || messageLine.contains("Line:")) { // any error
97 setTextColor(m_errorColor);
98 }
99 else if (messageLine.contains("warning", Qt::CaseInsensitive)) {
100 setTextColor(m_warningColor);
101 }
102 else if(messageLine.contains("sread: unexpected char")) {
103 // score error
104 setTextColor(m_errorColor);
105 }
106 insertPlainText(messageLine);
107 setTextColor(m_textColor);
108 moveCursor(QTextCursor::End);
109 messageLine.clear();
110 }
111 }
112
113 void Console::setDefaultFont(QFont font)
114 {
115 document()->setDefaultFont(font);
116 }
117
118 void Console::setColors(QColor textColor, QColor bgColor)
119 {
120 // Sets the colors of the csound console. Colors for warnings
121 // and errors are set depending on these colors (is it dark
122 // foreground on light background or the other way around)
123
124 // before it was setPalette, but that does not work runtime.
125 auto sheet = QString("QTextEdit { color: %1; background-color: %2 }"
126 ).arg(textColor.name(), bgColor.name());
127 this->setStyleSheet(sheet);
128 m_textColor = textColor;
129 m_bgColor = bgColor;
130 if(m_textColor.lightness() < m_bgColor.lightness()) {
131 // dark text on light background
132 m_warningColor = QColor("#AC7F00");
133 m_errorColor = QColor("#AC0000");
134 }
135 else {
136 // dark background
137 m_warningColor = QColor("orange");
138 m_errorColor = QColor("#FF4040");
139 }
140 }
141
142 void Console::reset()
143 {
144 clear();
145 errorLines.clear();
146 errorTexts.clear();
147 error = false;
148 }
149
150 void Console::scrollToEnd()
151 {
152 moveCursor(QTextCursor::End);
153 }
154
155 void Console::setKeyRepeatMode(bool repeat)
156 {
157 m_repeatKeys = repeat;
158 }
159
160 void Console::contextMenuEvent(QContextMenuEvent *event)
161 {
162 QMenu *menu = createStandardContextMenu();
163 menu->addAction("Clear", this, SLOT(reset()));
164 menu->exec(event->globalPos());
165 delete menu;
166 }
167
168 void Console::keyPressEvent(QKeyEvent *event)
169 {
170 if (!event->isAutoRepeat() || m_repeatKeys) {
171 QString keyText = event->text();
172 if (!keyText.isEmpty()) {
173 appendMessage(keyText);
174 emit keyPressed(static_cast<int>(keyText[0].toLatin1()));
175 } else {
176 emit keyPressed(event->key());
177 }
178 }
179 }
180
181 void Console::keyReleaseEvent(QKeyEvent *event)
182 {
183 if (!event->isAutoRepeat() || m_repeatKeys) {
184 QString keyText = event->text();
185 if (!keyText.isEmpty()) {
186 appendMessage(keyText);
187 emit keyReleased(static_cast<int>(keyText[0].toLatin1()));
188 } else {
189 emit keyReleased(event->key());
190 }
191 }
192 }
193
194 // ---------------------------------------------------------------
195
196 DockConsole::DockConsole(QWidget * parent): QDockWidget(parent)
197 {
198 setWindowTitle(tr("Output Console"));
199 // text = new Console(parent);
200 // text->setReadOnly(true);
201 // text->setContextMenuPolicy(Qt::NoContextMenu);
202 // text->document()->setDefaultFont(QFont("Courier", 10));
203 // setWidget(text);
204 // QStackedLayout *l = new QStackedLayout(this);
205 // l->addWidget(text);
206 // setLayout(l);
207 }
208
209 DockConsole::~DockConsole()
210 {;}
211
212 void DockConsole::copy()
213 {
214 qDebug() ;
215 static_cast<Console *>(widget())->copy();
216 }
217
218 bool DockConsole::widgetHasFocus()
219 {
220 return widget()->hasFocus();
221 }
222
223 void DockConsole::appendMessage(QString msg)
224 {
225 static_cast<Console *>(widget())->appendMessage(msg);
226 // static_cast<Console *>(widget())->scrollToEnd();
227 }
228
229
230 void DockConsole::closeEvent(QCloseEvent * /*event*/)
231 {
232 emit Close(false);
233 }
234
235 // ---------------------------------------------------------------
236
237 void ConsoleWidget::setWidgetGeometry(int /*x*/,int /*y*/,int width,int height)
238 {
239 setGeometry(QRect(0,0,width, height));
240 }