1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com>
5 * Copyright (C) 2018 Alex Tsui
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 #include "pyconsole.h"
22 #include "pyinterpreter.h"
23
24 NEXTPNR_NAMESPACE_BEGIN
25
26 const QColor PythonConsole::NORMAL_COLOR = QColor::fromRgbF(0, 0, 0);
27 const QColor PythonConsole::ERROR_COLOR = QColor::fromRgbF(1.0, 0, 0);
28 const QColor PythonConsole::OUTPUT_COLOR = QColor::fromRgbF(0, 0, 1.0);
29
PythonConsole(QWidget * parent)30 PythonConsole::PythonConsole(QWidget *parent) : QTextEdit(parent) {}
31
parseEvent(const ParseMessage & message)32 void PythonConsole::parseEvent(const ParseMessage &message)
33 {
34 // handle invalid user input
35 if (message.errorCode) {
36 setTextColor(ERROR_COLOR);
37 append(message.message.c_str());
38
39 setTextColor(NORMAL_COLOR);
40 append("");
41 return;
42 }
43 // interpret valid user input
44 int errorCode = 0;
45 std::string res;
46 if (message.message.size())
47 res = pyinterpreter_execute(message.message, &errorCode);
48 if (errorCode) {
49 setTextColor(ERROR_COLOR);
50 } else {
51 setTextColor(OUTPUT_COLOR);
52 }
53
54 if (res.size()) {
55 append(res.c_str());
56 }
57 setTextColor(NORMAL_COLOR);
58 append("");
59 moveCursorToEnd();
60 }
61
displayString(QString text)62 void PythonConsole::displayString(QString text)
63 {
64 QTextCursor cursor = textCursor();
65 cursor.movePosition(QTextCursor::End);
66 setTextColor(NORMAL_COLOR);
67 cursor.insertText(text);
68 cursor.movePosition(QTextCursor::EndOfLine);
69 moveCursorToEnd();
70 }
71
moveCursorToEnd()72 void PythonConsole::moveCursorToEnd()
73 {
74 QTextCursor cursor = textCursor();
75 cursor.movePosition(QTextCursor::End);
76 setTextCursor(cursor);
77 }
78
execute_python(std::string filename)79 void PythonConsole::execute_python(std::string filename)
80 {
81 int errorCode = 0;
82 std::string res;
83 res = pyinterpreter_execute_file(filename.c_str(), &errorCode);
84 if (res.size()) {
85 if (errorCode) {
86 setTextColor(ERROR_COLOR);
87 } else {
88 setTextColor(OUTPUT_COLOR);
89 }
90 append(res.c_str());
91 setTextColor(NORMAL_COLOR);
92 moveCursorToEnd();
93 }
94 }
95
96 NEXTPNR_NAMESPACE_END
97