from PyQt5.QtCore import QThread, QObject
from PyQt5.QtGui import QTextCursor, QIcon
from AlphaHooks.widgets.console.interpreters import PythonInterpreter
[docs]class PythonDisplay(QObject):
def __init__(self, ui, config, parent=None, **kwargs):
"""
Loads python configurations for console_log. A new thread is
spawned to which the interpreter is moved. This is done to
increase the responsiveness of the main user interface.
:param ui: used to access 'main.ui' methods
:param config: used to configure classes
"""
super(PythonDisplay, self).__init__(parent)
self.ui = ui
self.config = config
# Prompts
self.ps1 = '>>>'
self.ps2 = '...'
for key, val in kwargs.items():
if key == 'ps1':
self.ps1 = val
if key == 'ps2':
self.ps2 = val
self.ui.console_prompt.setText(self.ps1)
# Widget Controls
self.cursor = self.ui.console_log.textCursor()
self.scrollbar = self.ui.console_log.verticalScrollBar()
# Threads
self.thread = QThread()
self.thread.start()
self.interpreter = PythonInterpreter(self.config)
self.interpreter.moveToThread(self.thread)
# States
self.running = False
# Slots
self.ui.console_input.returnPressed.connect(self.send_console_input)
self.ui.interpreter_run.clicked.connect(self.send_console_source)
self.interpreter.stream_buffer.output.connect(self.send_console_log)
self.interpreter.running.connect(self.change_running_state)
self.interpreter.error.connect(self.send_console_log)
self.interpreter.multi_line.connect(self.prompt)
[docs] def prompt(self, multi_line):
"""
Checks and displays the current prompt in console_prompt.
:param multi_line: Can be True or False for the
defined prompts
"""
if not multi_line:
self.ui.console_prompt.setText(self.ps1)
else:
self.ui.console_prompt.setText(self.ps2)
[docs] def send_console_source(self):
"""
Check if the interpreter is running. If it isn't
take the source from the editor and push it to
the interpreter.
"""
if self.running is False:
source = self.ui.code_editor.text()
self.interpreter.push_source.emit(source)
[docs] def send_console_log(self, string):
"""
Insert the output into console_log and automatically scroll the
scrollbar.
:param string: output from the interpreter
"""
# Move cursor
self.cursor.movePosition(QTextCursor.End)
self.ui.console_log.setTextCursor(self.cursor)
# Insert text
self.ui.console_log.insertPlainText(string)
# Move scrollbar
self.scrollbar.setValue(self.scrollbar.maximum())
[docs] def change_running_state(self, state):
if state is True:
self.ui.interpreter_run.setIcon(QIcon(":/icons/interpreter_stop"))
else:
self.ui.interpreter_run.setIcon(QIcon(":/icons/interpreter_run"))
self.running = state
[docs] def stop_running(self):
self.thread.quit()
self.thread.wait()