#-------------------------------------------------------------------------- #-------------------------------------------------------------------------- # # QGISLite - An Open Source GIS tool based on the QGIS core libraries # and written in Python # # Copyright (C) 2008 Aaron Racicot, Z-Pulley Inc. # # EMAIL: aaronr (at) z-pulley.com # WEB : http://www.reprojected.com # TRAC : http://trac.reprojected.com/qgislite # SVN : http://svn.reprojected.com/qgislite # #-------------------------------------------------------------------------- #-------------------------------------------------------------------------- # # licensed under the terms of GNU GPL 2 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # #-------------------------------------------------------------------------- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgislitepy_ui import Ui_QGISLitePythonWin # General system includes import sys from code import * class MyLineEdit(QLineEdit): def __init__(self, *args): QLineEdit.__init__(self, *args) def event(self, event): if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Up): self.emit(SIGNAL("updownPressed(PyQt_PyObject)"),Qt.Key_Up) return True if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Down): self.emit(SIGNAL("updownPressed(PyQt_PyObject)"),Qt.Key_Down) return True return QLineEdit.event(self, event) class QGISLitePythonInterpGui(QDialog, Ui_QGISLitePythonWin): def __init__(self, parent, fl): QDialog.__init__(self, parent, fl) self.setupUi(self) self.parent = parent self.pythonline = MyLineEdit(self.commandWidget) self.pythonline.setEnabled(True) self.pythonline.setObjectName("pythonline") self.hboxlayout.addWidget(self.pythonline) self.console = QGISLitePythonShell(self,self.pythonline,parent.__dict__) self.gridlayout.addWidget(self.console) def on_pbnFinished_released(self): self.close() class QGISLitePythonShell(QTextBrowser): class Output: def __init__( self, writefunc ): self.writefunc = writefunc def write( self, line ): if line != "\n": map( self.writefunc, line.split("\n") ) def __init__( self, parent, cmdline, localdict={} ): QTextBrowser.__init__( self, parent ) self.history = [] self.pointer = len(self.history) self.cmdline = cmdline QObject.connect(self.cmdline, SIGNAL("returnPressed()"), self.returnPressed ) QObject.connect(self.cmdline, SIGNAL("updownPressed(PyQt_PyObject)"), self.updownPressed ) self.setFont(QFont("Fixed",10)) self.console = InteractiveConsole( localdict ) self.cmdline = cmdline sys.ps1 = ">>> " sys.ps2 = "... " self.append("---- QGISLite Python ----") self.append( "Python %s on %s\n" % ( sys.version, sys.platform) ) self.append("--------") self.more, self.prompt = 0, sys.ps1 self.output = QGISLitePythonShell.Output(self.writeResult) self.stdout = sys.stdout self.stderr = sys.stderr def writeResult(self,result): if result == "": return self.append(result) def processInput(self,line): self.append( sys.ps1 + line ) sys.stdout, sys.stderr = self.output, self.output self.more = self.console.push(line) sys.stdout, sys.stderr = self.stdout, self.stderr def returnPressed(self): line = str(self.cmdline.text()) self.history.append(line) self.pointer = len(self.history) self.cmdline.clear() self.processInput( line ) self.append(QString("--------")) def updownPressed(self,updownkey): if updownkey == Qt.Key_Down and len(self.history) > 0: self.cmdline.clear() if self.pointer < (len(self.history) - 1): self.pointer = self.pointer + 1 else: self.pointer = 0 self.cmdline.setText(self.history[self.pointer]) elif updownkey ==Qt.Key_Up and len(self.history) > 0: self.cmdline.clear() if self.pointer > 0: self.pointer = self.pointer - 1 else: self.pointer = len(self.history) - 1 self.cmdline.setText(self.history[self.pointer])