Skip to content

Commit

Permalink
automatically switch focus to the input area in console
Browse files Browse the repository at this point in the history
- any keystroke switch focus when in the output area
- moved closeEvent method to DockWidget (since 0330202 it has not worked anymore)
  • Loading branch information
slarosa committed Nov 2, 2012
1 parent 6abe861 commit 2425f92
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.activateWindow()
self.raise_() self.raise_()
QDockWidget.setFocus(self) QDockWidget.setFocus(self)

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


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


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


self.setFocusProxy(self.edit) self.setFocusProxy(self.edit)


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


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

if __name__ == '__main__': if __name__ == '__main__':
a = QApplication(sys.argv) a = QApplication(sys.argv)
console = PythonConsoleWidget() 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, from PyQt4.Qsci import (QsciScintilla,
QsciScintillaBase, QsciScintillaBase,
QsciLexerPython) QsciLexerPython)
from console_sci import PythonEdit
import sys import sys


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


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


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


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


def flush(self): def flush(self):
pass pass
Expand All @@ -67,13 +67,15 @@ class EditorOutput(QsciScintilla):
def __init__(self, parent=None): def __init__(self, parent=None):
#QsciScintilla.__init__(self, parent) #QsciScintilla.__init__(self, parent)
super(EditorOutput,self).__init__(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) self.setUtf8(True)


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


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


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


## Edge Mode : does not seems to work ## Edge Mode
#self.setEdgeMode(QsciScintilla.EdgeLine) #self.setEdgeMode(QsciScintilla.EdgeLine)
#self.setEdgeColumn(80) #self.setEdgeColumn(80)
#self.setEdgeColor(QColor("#FF0000")) #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 # Reimplemeted copy action to prevent paste prompt (>>>,...) in command view
self.copyShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_C), self) self.copyShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_C), self)
self.copyShortcut.activated.connect(self.copy) self.copyShortcut.activated.connect(self.copy)

def refreshLexerProperties(self): def refreshLexerProperties(self):
self.setLexers() self.setLexers()


Expand Down Expand Up @@ -174,4 +176,15 @@ def enteredSelected(self):
cmd = self.selectedText() cmd = self.selectedText()
self.edit.insertFromDropPaste(cmd) self.edit.insertFromDropPaste(cmd)
self.edit.entered() 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 2425f92

Please sign in to comment.