Skip to content
Permalink
Browse files

Merge pull request #247 from slarosa/master

[FEATURE] Added support to save History in Python Console
  • Loading branch information
timlinux committed Sep 25, 2012
2 parents ded943d + a0566c7 commit ba327b83d8d84b2789d6ff9d77939382040c8ae5
Showing with 112 additions and 30 deletions.
  1. +38 −2 i18n/qgis_it.ts
  2. +22 −14 python/console.py
  3. +47 −13 python/console_sci.py
  4. +5 −1 python/helpConsole/help.htm
@@ -6253,13 +6253,49 @@ Cambiare questa situazione prima, perché il plugin OSM non quale layer è la de
<source>Python Console</source>
<translation>Console python</translation>
</message>
<message>
<source>Clear console</source>
<translation>Pulisci console</translation>
</message>
<message>
<source>Import iface class</source>
<translation>Importa la classe iface</translation>
</message>
<message>
<source>Import sextante class</source>
<translation>Importa la classe Sextante</translation>
</message>
<message>
<source>Open script file</source>
<translation>Apri file</translation>
</message>
<message>
<source>Save to script file</source>
<translation>Salva file</translation>
</message>
<message>
<source>Help</source>
<translation>Aiuto</translation>
</message>
<message>
<source>Run command</source>
<translation>Esegui comando</translation>
</message>
<message>
<source>## History saved successfully ##</source>
<translation>## Storia comandi salvata con successo ##</translation>
</message>
<message>
<source>## History cleared successfully ##</source>
<translation>## Storia comandi cancellata con successo ##</translation>
</message>
<message>
<source>## To access Quantum GIS environment from this console
## use qgis.utils.iface object (instance of QgisInterface class).
## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.

</source>
<translation>## Per accedere all&apos;ambiente Quantum GIS da questa console
## usa l&apos;oggetto qgis.utils.iface (istanza della classe QgisInterface).
## usa l&apos;oggetto qgis.utils.iface (istanza della classe QgisInterface). Consulta l'aiuto per ottenere più informazioni.

</translation>
</message>
@@ -97,14 +97,15 @@ def __init__(self, parent=None):
self.b.setMargin(0)

## Action for Clear button
clearBt = QCoreApplication.translate("PythonConsole", "Clear console")
self.clearButton = QAction(parent)
self.clearButton.setCheckable(False)
self.clearButton.setEnabled(True)
self.clearButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconClearConsole.png"))
self.clearButton.setMenuRole(QAction.PreferencesRole)
self.clearButton.setIconVisibleInMenu(True)
self.clearButton.setToolTip('Clear console')
self.clearButton.setText('Clear console')
self.clearButton.setToolTip(clearBt)
self.clearButton.setText(clearBt)
## Action for paste snippets code
# self.currentLayerButton = QAction(parent)
# self.currentLayerButton.setCheckable(False)
@@ -113,59 +114,65 @@ def __init__(self, parent=None):
# self.currentLayerButton.setMenuRole(QAction.PreferencesRole)
# self.currentLayerButton.setIconVisibleInMenu(True)
## Import Sextante class
loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import sextante class")
self.loadSextanteButton = QAction(parent)
self.loadSextanteButton.setCheckable(False)
self.loadSextanteButton.setEnabled(True)
self.loadSextanteButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSextanteConsole.png"))
self.loadSextanteButton.setMenuRole(QAction.PreferencesRole)
self.loadSextanteButton.setIconVisibleInMenu(True)
self.loadSextanteButton.setToolTip('Import sextante class')
self.loadSextanteButton.setText('Import sextante class')
self.loadSextanteButton.setToolTip(loadSextanteBt)
self.loadSextanteButton.setText(loadSextanteBt)
## Import QgisInterface class
loadIfaceBt = QCoreApplication.translate("PythonConsole", "Import iface class")
self.loadIfaceButton = QAction(parent)
self.loadIfaceButton.setCheckable(False)
self.loadIfaceButton.setEnabled(True)
self.loadIfaceButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconTempConsole.png"))
self.loadIfaceButton.setMenuRole(QAction.PreferencesRole)
self.loadIfaceButton.setIconVisibleInMenu(True)
self.loadIfaceButton.setToolTip('Import iface class')
self.loadIfaceButton.setText('Import iface class')
self.loadIfaceButton.setToolTip(loadIfaceBt)
self.loadIfaceButton.setText(loadIfaceBt)
## Action for Open File
openFileBt = QCoreApplication.translate("PythonConsole", "Open script file")
self.openFileButton = QAction(parent)
self.openFileButton.setCheckable(False)
self.openFileButton.setEnabled(True)
self.openFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconOpenConsole.png"))
self.openFileButton.setMenuRole(QAction.PreferencesRole)
self.openFileButton.setIconVisibleInMenu(True)
self.openFileButton.setToolTip('Open script file')
self.openFileButton.setText('Open script file')
self.openFileButton.setToolTip(openFileBt)
self.openFileButton.setText(openFileBt)
## Action for Save File
saveFileBt = QCoreApplication.translate("PythonConsole", "Save to script file")
self.saveFileButton = QAction(parent)
self.saveFileButton.setCheckable(False)
self.saveFileButton.setEnabled(True)
self.saveFileButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconSaveConsole.png"))
self.saveFileButton.setMenuRole(QAction.PreferencesRole)
self.saveFileButton.setIconVisibleInMenu(True)
self.saveFileButton.setToolTip('Save to script file')
self.saveFileButton.setText('Save to script file')
self.saveFileButton.setToolTip(saveFileBt)
self.saveFileButton.setText(saveFileBt)
## Action for Run script
runBt = QCoreApplication.translate("PythonConsole", "Run command")
self.runButton = QAction(parent)
self.runButton.setCheckable(False)
self.runButton.setEnabled(True)
self.runButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconRunConsole.png"))
self.runButton.setMenuRole(QAction.PreferencesRole)
self.runButton.setIconVisibleInMenu(True)
self.runButton.setToolTip('Run command')
self.runButton.setText('Run')
self.runButton.setToolTip(runBt)
self.runButton.setText(runBt)
## Help action
helpBt = QCoreApplication.translate("PythonConsole", "Help")
self.helpButton = QAction(parent)
self.helpButton.setCheckable(False)
self.helpButton.setEnabled(True)
self.helpButton.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconHelpConsole.png"))
self.helpButton.setMenuRole(QAction.PreferencesRole)
self.helpButton.setIconVisibleInMenu(True)
self.helpButton.setToolTip('Help')
self.helpButton.setText('Help')
self.helpButton.setToolTip(helpBt)
self.helpButton.setText(helpBt)

self.toolBar.addAction(self.clearButton)
#self.toolBar.addAction(self.currentLayerButton)
@@ -253,6 +260,7 @@ def openHelp(self):
dlg.exec_()

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


@@ -32,6 +32,7 @@
import code

_init_commands = ["from qgis.core import *", "import qgis.utils"]
_historyFile = os.path.join(str(QDir.homePath()),".qgis","console_history.txt")

class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
def __init__(self, parent=None):
@@ -59,6 +60,8 @@ def __init__(self, parent=None):

self.history = QStringList()
self.historyIndex = 0
# Read history command file
self.readHistoryFile()

# Brace matching: enable for a brace immediately before or after
# the current position
@@ -206,7 +209,7 @@ def setLexers(self, lexer):
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).\n\n")
"## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.\n\n")
initText = self.setText(txtInit)

def getCurrentPos(self):
@@ -311,6 +314,27 @@ def updateHistory(self, command):
self.history.append(command)
self.historyIndex = len(self.history)

def writeHistoryFile(self):
wH = open(_historyFile, 'w')
for s in self.history:
wH.write(s + '\n')
wH.close()

def readHistoryFile(self):
fileExist = QFile.exists(_historyFile)
if fileExist:
rH = open(_historyFile, 'r')
for line in rH:
if line != "\n":
l = line.rstrip('\n')
self.updateHistory(l)
else:
return

def clearHistoryFile(self):
cH = open(_historyFile, 'w')
cH.close()

def showPrevious(self):
if self.historyIndex < len(self.history) and not self.history.isEmpty():
line, pos = self.getCurLine()
@@ -475,18 +499,28 @@ def currentCommand(self):
def runCommand(self, cmd):
self.updateHistory(cmd)
self.SendScintilla(QsciScintilla.SCI_NEWLINE)
self.buffer.append(cmd)
src = "\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)
if cmd in ('_save', '_clear'):
if cmd == '_save':
self.writeHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History saved successfully ##")
elif cmd == '_clear':
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History cleared successfully ##")
output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
self.displayPrompt(False)
else:
self.buffer.append(cmd)
src = "\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)
@@ -22,7 +22,7 @@ <h2>Python Console for QGIS</h2>
</tr>
</table>
<p align="justify">
Now you can use auto-completion for syntax in console!!!
Now you can use auto-completion and highlighting for syntax in console!!!
<br>
(Thanks to Larry Shaffer who provided the API files)
<br><br>
@@ -31,6 +31,10 @@ <h2>Python Console for QGIS</h2>
To import the class QgisInterface can also use the dedicated
button on the toolbar on the left.
<br><br>
To save history commands type '<b>_save</b>' in console or close this widget.
<br><br>
To clear history commands type '<b>_clear</b>' in console.
<br><br>
The following is a description of the tools in the toolbar:
</p>
<table width="100%" bordercolor="#000" border="1">

0 comments on commit ba327b8

Please sign in to comment.
You can’t perform that action at this time.