Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #313 from slarosa/master
Automatically switch focus to the input area in console
  • Loading branch information
brushtyler committed Nov 2, 2012
2 parents 70fc8ca + 2425f92 commit 9df95b1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
12 changes: 6 additions & 6 deletions python/console/console.py
Expand Up @@ -78,6 +78,10 @@ def activate(self):
self.activateWindow()
self.raise_()
QDockWidget.setFocus(self)

def closeEvent(self, event):
self.console.edit.writeHistoryFile()
QWidget.closeEvent(self, event)

class PythonConsoleWidget(QWidget):
def __init__(self, parent=None):
Expand Down Expand Up @@ -256,8 +260,8 @@ def __init__(self, parent=None):
sM.setPopupMode(QToolButton.InstantPopup)

self.b.addWidget(self.toolBar)
self.edit = PythonEdit()
self.textEditOut = EditorOutput()
self.edit = PythonEdit(self)
self.textEditOut = EditorOutput(self)

self.setFocusProxy(self.edit)

Expand Down Expand Up @@ -362,10 +366,6 @@ def prefChanged(self):
self.edit.refreshLexerProperties()
self.textEditOut.refreshLexerProperties()

def closeEvent(self, event):
self.edit.writeHistoryFile()
QWidget.closeEvent(self, event)

if __name__ == '__main__':
a = QApplication(sys.argv)
console = PythonConsoleWidget()
Expand Down
45 changes: 29 additions & 16 deletions python/console/console_output.py
Expand Up @@ -24,25 +24,25 @@
from PyQt4.Qsci import (QsciScintilla,
QsciScintillaBase,
QsciLexerPython)
from console_sci import PythonEdit

import sys

class writeOut:
def __init__(self, edit, out=None, style=None):
"""
This class allow to write stdout and stderr
"""
self.editor = edit
self.outputArea = edit
self.out = None
self.style = style

def write(self, m):
if self.style == "traceback":
self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
self.editor.append(m)
self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
self.outputArea.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
self.outputArea.append(m)
self.outputArea.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
else:
self.editor.append(m)
self.outputArea.append(m)
self.move_cursor_to_end()

if self.out:
Expand All @@ -51,14 +51,14 @@ def write(self, m):
def move_cursor_to_end(self):
"""Move cursor to end of text"""
line, index = self.get_end_pos()
self.editor.setCursorPosition(line, index)
self.editor.ensureCursorVisible()
self.editor.ensureLineVisible(line)
self.outputArea.setCursorPosition(line, index)
self.outputArea.ensureCursorVisible()
self.outputArea.ensureLineVisible(line)

def get_end_pos(self):
"""Return (line, index) position of the last character"""
line = self.editor.lines() - 1
return (line, self.editor.text(line).length())
line = self.outputArea.lines() - 1
return (line, self.outputArea.text(line).length())

def flush(self):
pass
Expand All @@ -67,13 +67,15 @@ class EditorOutput(QsciScintilla):
def __init__(self, parent=None):
#QsciScintilla.__init__(self, parent)
super(EditorOutput,self).__init__(parent)
# Enable non-ascii chars for editor
self.parent = parent
self.edit = self.parent.edit

# Enable non-ascii chars for editor
self.setUtf8(True)

sys.stdout = writeOut(self, sys.stdout)
sys.stderr = writeOut(self, sys.stderr, "traceback")

self.edit = PythonEdit()
self.setLexers()
self.setReadOnly(True)

Expand Down Expand Up @@ -101,7 +103,7 @@ def __init__(self, parent=None):
#self.setFoldMarginColors(QColor("#99CC66"),QColor("#333300"))
#self.setWrapMode(QsciScintilla.WrapCharacter)

## Edge Mode : does not seems to work
## Edge Mode
#self.setEdgeMode(QsciScintilla.EdgeLine)
#self.setEdgeColumn(80)
#self.setEdgeColor(QColor("#FF0000"))
Expand All @@ -114,7 +116,7 @@ def __init__(self, parent=None):
# Reimplemeted copy action to prevent paste prompt (>>>,...) in command view
self.copyShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_C), self)
self.copyShortcut.activated.connect(self.copy)

def refreshLexerProperties(self):
self.setLexers()

Expand Down Expand Up @@ -174,4 +176,15 @@ def enteredSelected(self):
cmd = self.selectedText()
self.edit.insertFromDropPaste(cmd)
self.edit.entered()


def keyPressEvent(self, e):
# empty text indicates possible shortcut key sequence so stay in output
txt = e.text()
if txt.length() and txt >= " ":
self.edit.append(txt)
self.edit.move_cursor_to_end()
self.edit.setFocus()
e.ignore()
else:
# possible shortcut key sequence, accept it
e.accept()

0 comments on commit 9df95b1

Please sign in to comment.