88 changes: 49 additions & 39 deletions python/console_sci.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, parent=None):

self.buffer = []

self.insertInitText()
#self.insertInitText()
self.displayPrompt(False)

for line in _init_commands:
Expand All @@ -69,8 +69,8 @@ def __init__(self, parent=None):
#self.selectToMatchingBrace()

# Current line visible with special background color
self.setCaretLineVisible(True)
self.setCaretLineBackgroundColor(QColor("#ffe4e4"))
#self.setCaretLineVisible(True)
#self.setCaretLineBackgroundColor(QColor("#ffe4e4"))
self.setCaretWidth(2)

# Set Python lexer
Expand All @@ -93,12 +93,14 @@ def __init__(self, parent=None):
# Use raw message to Scintilla here (all messages are documented
# here: http://www.scintilla.org/ScintillaDoc.html)
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
self.SendScintilla(QsciScintilla.SCI_SETVSCROLLBAR, 0)


# not too small
#self.setMinimumSize(500, 300)
self.setMinimumHeight(125)
self.setMinimumHeight(32)

self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 1)
self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 2)
self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER)

## Disable command key
Expand All @@ -117,20 +119,14 @@ def __init__(self, parent=None):
self.newShortcutCAS.activated.connect(self.showHistory)
self.connect(self, SIGNAL('userListActivated(int, const QString)'),
self.completion_list_selected)

self.createStandardContextMenu()

def showHistory(self):
self.showUserList(1, QStringList(self.history))

def autoComplete(self):
self.autoCompleteFromAll()

def clearConsole(self):
"""Clear the contents of the console."""
self.SendScintilla(QsciScintilla.SCI_CLEARALL)
#self.setText('')
self.insertInitText()
self.displayPrompt(False)
self.setFocus()

def commandConsole(self, command):
if not self.is_cursor_on_last_line():
Expand All @@ -142,23 +138,20 @@ def commandConsole(self, command):
if command == "iface":
"""Import QgisInterface class"""
self.append('from qgis.utils import iface')
self.move_cursor_to_end()
elif command == "sextante":
"""Import Sextante class"""
self.append('from sextante.core.Sextante import Sextante')
self.move_cursor_to_end()
elif command == "cLayer":
"""Retrieve current Layer from map camvas"""
self.append('cLayer = iface.mapCanvas().currentLayer()')
self.move_cursor_to_end()
elif command == "qtCore":
"""Import QtCore class"""
self.append('from PyQt4.QtCore import *')
self.move_cursor_to_end()
elif command == "qtGui":
"""Import QtGui class"""
self.append('from PyQt4.QtGui import *')
self.move_cursor_to_end()
self.entered()
self.move_cursor_to_end()
self.setFocus()

def setLexers(self):
Expand All @@ -172,6 +165,10 @@ def setLexers(self):
font = QFont(loadFont)
font.setFixedPitch(True)
font.setPointSize(fontSize)
font.setStyleHint(QFont.TypeWriter)
font.setStretch(QFont.SemiCondensed)
font.setLetterSpacing(QFont.PercentageSpacing, 87.0)
font.setBold(False)

self.lexer.setDefaultFont(font)
self.lexer.setColor(Qt.red, 1)
Expand Down Expand Up @@ -209,9 +206,9 @@ def completion_list_selected(self, id, txt):

def insertInitText(self):
#self.setLexers(False)
txtInit = QCoreApplication.translate("PythonConsole",
"## To access Quantum GIS environment from this console\n"
"## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.\n\n")
txtInit = QCoreApplication.translate("PythonConsole", "## Interactive Python Console for Quantum GIS\n\n")
#"## To access Quantum GIS environment from this console\n"
#"## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.\n\n")
initText = self.setText(txtInit)

def getText(self):
Expand Down Expand Up @@ -413,6 +410,16 @@ def keyPressEvent(self, e):
## TODO: press event for auto-completion file directory
else:
QsciScintilla.keyPressEvent(self, e)

def contextMenuEvent(self, e):
menu = QMenu(self)
copyAction = menu.addAction("Copy CTRL+C")
pasteAction = menu.addAction("Paste CTRL+V")
action = menu.exec_(self.mapToGlobal(e.pos()))
if action == copyAction:
self.copy()
elif action == pasteAction:
self.paste()

def mousePressEvent(self, e):
"""
Expand Down Expand Up @@ -458,18 +465,20 @@ def dropEvent(self, e):
def insertFromDropPaste(self, textDP):
pasteList = textDP.split("\n")
for line in pasteList[:-1]:
line.replace(">>> ", "").replace("... ", "")
self.insert(line)
self.move_cursor_to_end()
#self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
self.runCommand(unicode(self.currentCommand()))
if pasteList[-1] != "":
self.insert(unicode(pasteList[-1]))
line = pasteList[-1]
line.replace(">>> ", "").replace("... ", "")
self.insert(unicode(line))
self.move_cursor_to_end()

def getTextFromEditor(self):
text = self.text()
textList = text.split("\n")
return textList
# def getTextFromEditor(self):
# text = self.text()
# textList = text.split("\n")
# return textList

def insertTextFromFile(self, listOpenFile):
for line in listOpenFile[:-1]:
Expand All @@ -483,7 +492,7 @@ def insertTextFromFile(self, listOpenFile):

def entered(self):
self.move_cursor_to_end()
self.runCommand( unicode(self.currentCommand()) )
self.runCommand( unicode(self.currentCommand()) )
self.setFocus()
self.move_cursor_to_end()
#self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER)
Expand All @@ -498,9 +507,14 @@ def currentCommand(self):
return cmd

def runCommand(self, cmd):
self.write_stdout(cmd)
import webbrowser
self.updateHistory(cmd)
self.SendScintilla(QsciScintilla.SCI_NEWLINE)
line, pos = self.getCursorPosition()
selCmdLenght = self.text(line).length()
self.setSelection(line, 0, line, selCmdLenght)
self.removeSelectedText()
#self.SendScintilla(QsciScintilla.SCI_NEWLINE)
if cmd in ('_save', '_clear', '_clearAll', '_pyqgis', '_api'):
if cmd == '_save':
self.writeHistoryFile()
Expand Down Expand Up @@ -528,25 +542,21 @@ def runCommand(self, cmd):
elif cmd == '_api':
webbrowser.open( "http://www.qgis.org/api/" )

output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
self.displayPrompt(False)
else:
self.buffer.append(cmd)
src = u"\n".join(self.buffer)
more = self.runsource(src, "<input>")
if not more:
self.buffer = []
output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
self.move_cursor_to_end()
self.displayPrompt(more)

def write(self, txt):
self.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(txt), 1)
self.append(txt)
self.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(txt), 1)

sys.stderr.write(txt)

def write_stdout(self, txt):
if len(txt) > 0:
getCmdString = self.text()
prompt = getCmdString[0:4]
sys.stdout.write(prompt+txt+'\n')