Skip to content

Commit

Permalink
Follow up f15b4e1
Browse files Browse the repository at this point in the history
- add contextual menu to output area (copy and enter selected entry)
- auto-entered for entry in import class menu
- cleaning code
- more info see http://hub.qgis.org/issues/6558
  • Loading branch information
slarosa committed Oct 27, 2012
1 parent dd570c0 commit 9fb1c11
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
22 changes: 0 additions & 22 deletions python/console.py
Expand Up @@ -79,7 +79,6 @@ def activate(self):
self.raise_()
QDockWidget.setFocus(self)


class PythonConsoleWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
Expand All @@ -88,30 +87,20 @@ def __init__(self, parent=None):
#self.widgetEditors = QWidget()

self.options = optionsDialog(self)
#self.textEdit = QTextEdit()

self.toolBar = QToolBar()
self.toolBar.setEnabled(True)
#self.toolBar.setFont(font)
self.toolBar.setFocusPolicy(Qt.NoFocus)
self.toolBar.setContextMenuPolicy(Qt.DefaultContextMenu)
self.toolBar.setLayoutDirection(Qt.LeftToRight)
self.toolBar.setIconSize(QSize(24, 24))
self.toolBar.setOrientation(Qt.Vertical)
self.toolBar.setMovable(True)
self.toolBar.setFloatable(True)
#self.toolBar.setAllowedAreas(Qt.LeftToolBarArea)
#self.toolBar.setAllowedAreas(Qt.RightToolBarArea)
#self.toolBar.setObjectName(_fromUtf8("toolMappa"))

#self.gridLayout = QGridLayout(self)

self.b = QGridLayout(self.widgetButton)
#self.e = QGridLayout(self.widgetEditors)
self.f = QGridLayout(self)

#self.e.setMargin(0)
#self.e.setSpacing(0)
self.f.setMargin(0)
self.f.setSpacing(0)
self.b.setMargin(0)
Expand Down Expand Up @@ -264,10 +253,6 @@ def __init__(self, parent=None):
self.b.addWidget(self.toolBar)
self.edit = PythonEdit()
self.textEditOut = EditorOutput()

#self.textEdit = PythonEditOutput()
#self.textEdit.setReadOnly(True)
#self.textEdit.setMinimumHeight(80)

self.setFocusProxy(self.edit)

Expand All @@ -276,8 +261,6 @@ def __init__(self, parent=None):
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widgetButton.sizePolicy().hasHeightForWidth())
self.widgetButton.setSizePolicy(sizePolicy)
#self.e.addWidget(self.textEdit)
#self.e.addWidget(self.edit)

self.consoleFrame = QFrame(self)
self.consoleFrame.setObjectName("consoleFrame")
Expand All @@ -296,13 +279,8 @@ def __init__(self, parent=None):
self.edit.setMaximumSize(QSize(16777215, 32))
self.consoleLayout.addWidget(self.edit)


self.f.addWidget(self.widgetButton, 0, 0)
self.f.addWidget(self.consoleFrame, 0, 1)
#self.f.addWidget(self.widgetEditors)

#self.f.setStretchFactor(self.widgetEditors, 1)


self.clearButton.triggered.connect(self.textEditOut.clearConsole)
self.optionsButton.triggered.connect(self.openSettings)
Expand Down
30 changes: 28 additions & 2 deletions python/console_output.py
Expand Up @@ -24,7 +24,7 @@
from PyQt4.Qsci import (QsciScintilla,
QsciScintillaBase,
QsciLexerPython)

from console_sci import PythonEdit
import sys

class writeOut:
Expand Down Expand Up @@ -69,10 +69,11 @@ def __init__(self, parent=None):
super(EditorOutput,self).__init__(parent)
# 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 @@ -139,3 +140,28 @@ def getTextFromEditor(self):
def clearConsole(self):
#self.SendScintilla(QsciScintilla.SCI_CLEARALL)
self.setText('')

def contextMenuEvent(self, e):
menu = QMenu(self)
runAction = menu.addAction("Enter Selected")
copyAction = menu.addAction("Copy CTRL+C")
runAction.setEnabled(False)
if self.hasSelectedText():
runAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))
if action == runAction:
cmd = self.selectedText()
self.edit.insertFromDropPaste(cmd)
self.edit.entered()
if action == copyAction:
self.copy()

def copy(self):
"""Copy text to clipboard... or keyboard interrupt"""
if self.hasSelectedText():
text = unicode(self.selectedText())
text = text.replace('>>> ', '').replace('... ', '').strip() # removing prompts
QApplication.clipboard().setText(text)
else:
self.emit(SIGNAL("keyboard_interrupt()"))

22 changes: 6 additions & 16 deletions python/console_sci.py
Expand Up @@ -127,14 +127,6 @@ def showHistory(self):

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 @@ -146,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 Down Expand Up @@ -479,10 +468,11 @@ def insertFromDropPaste(self, textDP):
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):
Expand Down Expand Up @@ -567,6 +557,6 @@ def write(self, txt):

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

0 comments on commit 9fb1c11

Please sign in to comment.