Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions qtutils/outputbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ def __init__(self, container, scrollback_lines=1000,
self.output_textedit.setWordWrapMode(QTextOption.WrapMode.WrapAnywhere)
set_auto_scroll_to_end(self.output_textedit.verticalScrollBar())
self.output_textedit.setMaximumBlockCount(scrollback_lines)

self.output_textedit.setContextMenuPolicy(Qt.CustomContextMenu)
self.output_textedit.customContextMenuRequested.connect(self._show_context_menu)

self.shortcut = QShortcut(QKeySequence("Return"), self.output_textedit)
self.shortcut.activated.connect(self.add_whitespace)

self.shortcut = QShortcut(QKeySequence("CTRL+L"), self.output_textedit)
self.shortcut.activated.connect(self.jump_to_end)

if zmq_context is None:
zmq_context = zmq.Context.instance()
self.zmq_context = zmq_context
Expand Down Expand Up @@ -225,6 +233,26 @@ def output(self, text, red=False):
bold = False
self.write(text, color=color, bold=bold)

def _show_context_menu(self, pos):
menu = self.output_textedit.createStandardContextMenu()
menu.addSeparator()
clear_action = menu.addAction('Clear Output')
chosen = menu.exec(self.output_textedit.mapToGlobal(pos))
if chosen is clear_action:
self.clear_output()

@inmain_decorator(False)
def clear_output(self):
"""Clear all text currently displayed in the output box by flushing
queue and reset line-position."""
while True:
try:
self._text_queue.get_nowait()
except queue.Empty:
break
self.output_textedit.clear()
self.linepos = self.LINE_NEW

def mainloop(self, socket):
while True:
messages = []
Expand Down Expand Up @@ -322,6 +350,29 @@ def add_text(self):
cursor.movePosition(QTextCursor.MoveOperation.PreviousCharacter, n=charsprinted)
cursor.movePosition(QTextCursor.MoveOperation.End, QTextCursor.MoveMode.KeepAnchor)
cursor.setCharFormat(charformats(charformat_repr))

def add_whitespace(self):
"""Blackspace actually? Jumps to end of the outputbox and adds an empty line."""

self.output_textedit.moveCursor(QTextCursor.End)
self._text_queue.put(("\n", "default"))
self.add_text()

def jump_to_end(self):
"""Jumps to the end of outputbox and sets the last line as the top"""

# Stores the last block
lastBlock = self.output_textedit.document().lastBlock()
lastLineText = lastBlock.text()

# Clears
self.output_textedit.clear()

# Then tosses that block back to outputbox
self._text_queue.put((lastLineText, "default"))
self.add_text()

self.output_textedit.moveCursor(QTextCursor.End)

def shutdown(self):
"""Stop the mainloop. Further writing to the OutputBox will be ignored. It is
Expand All @@ -346,7 +397,7 @@ def isatty(self):
return False

def flush(self):
pass
self.clear_output()


if __name__ == '__main__':
Expand Down