Skip to content

Commit 9df95b1

Browse files
committedNov 2, 2012
Merge pull request #313 from slarosa/master
Automatically switch focus to the input area in console
2 parents 70fc8ca + 2425f92 commit 9df95b1

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed
 

‎python/console/console.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def activate(self):
7878
self.activateWindow()
7979
self.raise_()
8080
QDockWidget.setFocus(self)
81+
82+
def closeEvent(self, event):
83+
self.console.edit.writeHistoryFile()
84+
QWidget.closeEvent(self, event)
8185

8286
class PythonConsoleWidget(QWidget):
8387
def __init__(self, parent=None):
@@ -256,8 +260,8 @@ def __init__(self, parent=None):
256260
sM.setPopupMode(QToolButton.InstantPopup)
257261

258262
self.b.addWidget(self.toolBar)
259-
self.edit = PythonEdit()
260-
self.textEditOut = EditorOutput()
263+
self.edit = PythonEdit(self)
264+
self.textEditOut = EditorOutput(self)
261265

262266
self.setFocusProxy(self.edit)
263267

@@ -362,10 +366,6 @@ def prefChanged(self):
362366
self.edit.refreshLexerProperties()
363367
self.textEditOut.refreshLexerProperties()
364368

365-
def closeEvent(self, event):
366-
self.edit.writeHistoryFile()
367-
QWidget.closeEvent(self, event)
368-
369369
if __name__ == '__main__':
370370
a = QApplication(sys.argv)
371371
console = PythonConsoleWidget()

‎python/console/console_output.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@
2424
from PyQt4.Qsci import (QsciScintilla,
2525
QsciScintillaBase,
2626
QsciLexerPython)
27-
from console_sci import PythonEdit
27+
2828
import sys
2929

3030
class writeOut:
3131
def __init__(self, edit, out=None, style=None):
3232
"""
3333
This class allow to write stdout and stderr
3434
"""
35-
self.editor = edit
35+
self.outputArea = edit
3636
self.out = None
3737
self.style = style
3838

3939
def write(self, m):
4040
if self.style == "traceback":
41-
self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
42-
self.editor.append(m)
43-
self.editor.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
41+
self.outputArea.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
42+
self.outputArea.append(m)
43+
self.outputArea.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 1)
4444
else:
45-
self.editor.append(m)
45+
self.outputArea.append(m)
4646
self.move_cursor_to_end()
4747

4848
if self.out:
@@ -51,14 +51,14 @@ def write(self, m):
5151
def move_cursor_to_end(self):
5252
"""Move cursor to end of text"""
5353
line, index = self.get_end_pos()
54-
self.editor.setCursorPosition(line, index)
55-
self.editor.ensureCursorVisible()
56-
self.editor.ensureLineVisible(line)
54+
self.outputArea.setCursorPosition(line, index)
55+
self.outputArea.ensureCursorVisible()
56+
self.outputArea.ensureLineVisible(line)
5757

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

6363
def flush(self):
6464
pass
@@ -67,13 +67,15 @@ class EditorOutput(QsciScintilla):
6767
def __init__(self, parent=None):
6868
#QsciScintilla.__init__(self, parent)
6969
super(EditorOutput,self).__init__(parent)
70-
# Enable non-ascii chars for editor
70+
self.parent = parent
71+
self.edit = self.parent.edit
72+
73+
# Enable non-ascii chars for editor
7174
self.setUtf8(True)
7275

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

76-
self.edit = PythonEdit()
7779
self.setLexers()
7880
self.setReadOnly(True)
7981

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

104-
## Edge Mode : does not seems to work
106+
## Edge Mode
105107
#self.setEdgeMode(QsciScintilla.EdgeLine)
106108
#self.setEdgeColumn(80)
107109
#self.setEdgeColor(QColor("#FF0000"))
@@ -114,7 +116,7 @@ def __init__(self, parent=None):
114116
# Reimplemeted copy action to prevent paste prompt (>>>,...) in command view
115117
self.copyShortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_C), self)
116118
self.copyShortcut.activated.connect(self.copy)
117-
119+
118120
def refreshLexerProperties(self):
119121
self.setLexers()
120122

@@ -174,4 +176,15 @@ def enteredSelected(self):
174176
cmd = self.selectedText()
175177
self.edit.insertFromDropPaste(cmd)
176178
self.edit.entered()
177-
179+
180+
def keyPressEvent(self, e):
181+
# empty text indicates possible shortcut key sequence so stay in output
182+
txt = e.text()
183+
if txt.length() and txt >= " ":
184+
self.edit.append(txt)
185+
self.edit.move_cursor_to_end()
186+
self.edit.setFocus()
187+
e.ignore()
188+
else:
189+
# possible shortcut key sequence, accept it
190+
e.accept()

0 commit comments

Comments
 (0)
Please sign in to comment.