Merge pull request #248 from slarosa/master

Several fixes and added for python console
This commit is contained in:
Tim Sutton 2012-09-27 14:19:35 -07:00
commit e6c591fe20
8 changed files with 190 additions and 92 deletions

View File

@ -6253,6 +6253,14 @@ Cambiare questa situazione prima, perché il plugin OSM non quale layer è la de
<source>Python Console</source>
<translation>Console python</translation>
</message>
<message>
<source>Import Class</source>
<translation>Importa Modulo</translation>
</message>
<message>
<source>Manage Script</source>
<translation>Gestione Script</translation>
</message>
<message>
<source>Clear console</source>
<translation>Pulisci console</translation>
@ -6281,6 +6289,12 @@ Cambiare questa situazione prima, perché il plugin OSM non quale layer è la de
<source>Run command</source>
<translation>Esegui comando</translation>
</message>
<message>
<source>Are you sure you want to completely
delete the command history ?</source>
<translation>Sei sicuro di voler concellare completamente
la cronologia dei comandi ?</translation>
</message>
<message>
<source>## History saved successfully ##</source>
<translation>## Storia comandi salvata con successo ##</translation>

View File

@ -113,6 +113,26 @@ class PythonConsole(QDockWidget):
# self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png"))
# self.currentLayerButton.setMenuRole(QAction.PreferencesRole)
# self.currentLayerButton.setIconVisibleInMenu(True)
## Action menu for class
actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class")
self.actionClass = QAction(parent)
self.actionClass.setCheckable(False)
self.actionClass.setEnabled(True)
self.actionClass.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconClassConsole.png"))
self.actionClass.setMenuRole(QAction.PreferencesRole)
self.actionClass.setIconVisibleInMenu(True)
self.actionClass.setToolTip(actionClassBt)
self.actionClass.setText(actionClassBt)
## Action menu Open/Save script
actionScriptBt = QCoreApplication.translate("PythonConsole", "Manage Script")
self.actionScript = QAction(parent)
self.actionScript.setCheckable(False)
self.actionScript.setEnabled(True)
self.actionScript.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconTempConsole.png"))
self.actionScript.setMenuRole(QAction.PreferencesRole)
self.actionScript.setIconVisibleInMenu(True)
self.actionScript.setToolTip(actionScriptBt)
self.actionScript.setText(actionScriptBt)
## Import Sextante class
loadSextanteBt = QCoreApplication.translate("PythonConsole", "Import sextante class")
self.loadSextanteButton = QAction(parent)
@ -128,7 +148,7 @@ class PythonConsole(QDockWidget):
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.setIcon(QIcon(os.path.dirname(__file__) + "/iconConsole/iconIfaceConsole.png"))
self.loadIfaceButton.setMenuRole(QAction.PreferencesRole)
self.loadIfaceButton.setIconVisibleInMenu(True)
self.loadIfaceButton.setToolTip(loadIfaceBt)
@ -175,13 +195,24 @@ class PythonConsole(QDockWidget):
self.helpButton.setText(helpBt)
self.toolBar.addAction(self.clearButton)
#self.toolBar.addAction(self.currentLayerButton)
self.toolBar.addAction(self.loadIfaceButton)
self.toolBar.addAction(self.loadSextanteButton)
self.toolBar.addAction(self.openFileButton)
self.toolBar.addAction(self.saveFileButton)
self.toolBar.addAction(self.actionClass)
self.toolBar.addAction(self.actionScript)
self.toolBar.addAction(self.helpButton)
self.toolBar.addAction(self.runButton)
## Menu Import Class
self.classMenu = QMenu(self)
self.classMenu.addAction(self.loadIfaceButton)
self.classMenu.addAction(self.loadSextanteButton)
cM = self.toolBar.widgetForAction(self.actionClass)
cM.setMenu(self.classMenu)
cM.setPopupMode(QToolButton.InstantPopup)
## Menu Manage Script
self.scriptMenu = QMenu(self)
self.scriptMenu.addAction(self.openFileButton)
self.scriptMenu.addAction(self.saveFileButton)
sM = self.toolBar.widgetForAction(self.actionScript)
sM.setMenu(self.scriptMenu)
sM.setPopupMode(QToolButton.InstantPopup)
self.b.addWidget(self.toolBar)
self.edit = PythonEdit()

View File

@ -97,6 +97,7 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
# not too small
#self.setMinimumSize(500, 300)
self.setMinimumHeight(125)
self.SendScintilla(QsciScintilla.SCI_SETWRAPMODE, 1)
self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER)
@ -114,7 +115,12 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
self.newShortcutCS = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_Space), self)
self.newShortcutCAS = QShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_Space), self)
self.newShortcutCS.activated.connect(self.autoComplete)
self.newShortcutCAS.activated.connect(self.autoComplete)
self.newShortcutCAS.activated.connect(self.showHistory)
self.connect(self, SIGNAL('userListActivated(int, const QString)'),
self.completion_list_selected)
def showHistory(self):
self.showUserList(1, QStringList(self.history))
def autoComplete(self):
self.autoCompleteFromAll()
@ -161,8 +167,6 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
self.setMarginsFont(font)
self.lexer = QsciLexerPython()
self.lexer.setDefaultFont(font)
#self.lexer.setDefaultFont(QFont('Mono', 10, 0, False))
#self.lexer.setDefaultColor(Qt.darkGray)
self.lexer.setColor(Qt.red, 1)
self.lexer.setColor(Qt.darkGreen, 5)
self.lexer.setColor(Qt.darkBlue, 15)
@ -171,44 +175,25 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
self.lexer.setFont(font, 4)
self.api = QsciAPIs(self.lexer)
self.api.loadPrepared(QString(os.path.dirname(__file__) + "/api/pyqgis_master.pap"))
# self.api.load(os.path.dirname(__file__) + "/api/PyQGIS_1.8.api")
# self.api.load(os.path.dirname(__file__) + "/api/osgeo_gdal-ogr_1.9.1-1.api")
# self.api.load("qgis.networkanalysis.api")
# self.api.load("qgis.gui.api")
# self.api.load("qgis.core.api")
# self.api.load("qgis.analysis.api")
# self.api.prepare()
# self.lexer.setAPIs(self.api)
self.setLexer(self.lexer)
## TODO: show completion list for file and directory
# def show_completion_list(self, completions, text):
# """Private method to display the possible completions"""
# if len(completions) == 0:
# return
# if len(completions) > 1:
# self.showUserList(1, QStringList(sorted(completions)))
# self.completion_chars = 1
# else:
# txt = completions[0]
# if text != "":
# txt = txt.replace(text, "")
# self.insert(txt)
# self.completion_chars = 0
#
# def show_file_completion(self):
# """Display a completion list for files and directories"""
# cwd = os.getcwdu()
# self.show_completion_list(self.listdir_fullpath('/'), cwd)
#
# def listdir_fullpath(self, d):
# return [os.path.join(d, f) for f in os.listdir(d)]
## TODO: show completion list for file and directory
def completion_list_selected(self, id, txt):
if id == 1:
txt = unicode(txt)
# get current cursor position
line, pos = self.getCurLine()
selCmd= self.text(line).length()
# select typed text
self.setSelection(line, 4, line, selCmd)
self.removeSelectedText()
self.insert(txt)
def insertInitText(self):
#self.setLexers(False)
txtInit = QCoreApplication.translate("PythonConsole","## To access Quantum GIS environment from this console\n"
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")
initText = self.setText(txtInit)
@ -380,6 +365,38 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
elif e.modifiers() & Qt.ControlModifier:
if e.key() == Qt.Key_V:
self.paste()
elif e.key() == Qt.Key_C:
self.copy()
elif e.key() == Qt.Key_X:
self.cut()
elif e.key() == Qt.Key_Left:
e.accept()
if e.modifiers() & Qt.ShiftModifier:
if index > 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDLEFTEXTEND)
else:
self.SendScintilla(QsciScintilla.SCI_CHARLEFTEXTEND)
else:
if index > 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDLEFT)
else:
self.SendScintilla(QsciScintilla.SCI_CHARLEFT)
elif e.key() == Qt.Key_Right:
e.accept()
if e.modifiers() & Qt.ShiftModifier:
if index >= 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDRIGHTEXTEND)
else:
self.SendScintilla(QsciScintilla.SCI_CHARRIGHTEXTEND)
else:
if index >= 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDRIGHT)
else:
self.SendScintilla(QsciScintilla.SCI_CHARRIGHT)
elif e.key() == Qt.Key_Backspace:
curPos, pos = self.getCursorPosition()
line = self.lines() -1
@ -401,39 +418,7 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
self.showPrevious()
elif e.key() == Qt.Key_Up and not self.isListActive():
self.showNext()
elif e.key() == Qt.Key_Left:
e.accept()
if e.modifiers() & Qt.ShiftModifier:
if index > 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDLEFTEXTEND)
else:
self.SendScintilla(QsciScintilla.SCI_CHARLEFTEXTEND)
else:
if index > 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDLEFT)
else:
self.SendScintilla(QsciScintilla.SCI_CHARLEFT)
elif e.key() == Qt.Key_Right:
e.accept()
if e.modifiers() & Qt.ShiftModifier:
if index >= 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDRIGHTEXTEND)
else:
self.SendScintilla(QsciScintilla.SCI_CHARRIGHTEXTEND)
else:
if index >= 4:
if e.modifiers() & Qt.ControlModifier:
self.SendScintilla(QsciScintilla.SCI_WORDRIGHT)
else:
self.SendScintilla(QsciScintilla.SCI_CHARRIGHT)
## TODO: press event for auto-completion file directory
#elif e.key() == Qt.Key_Tab:
#self.show_file_completion()
#else:
#self.on_new_line()
else:
QsciScintilla.keyPressEvent(self, e)
@ -499,13 +484,28 @@ class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
def runCommand(self, cmd):
self.updateHistory(cmd)
self.SendScintilla(QsciScintilla.SCI_NEWLINE)
if cmd in ('_save', '_clear'):
if cmd in ('_save', '_clear', '_clearAll'):
if cmd == '_save':
self.writeHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History saved successfully ##")
print QCoreApplication.translate("PythonConsole",
"## History saved successfully ##")
elif cmd == '_clear':
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History cleared successfully ##")
print QCoreApplication.translate("PythonConsole",
"## History cleared successfully ##")
elif cmd == '_clearAll':
res = QMessageBox.question(self, "Python Console",
QCoreApplication.translate("PythonConsole",
"Are you sure you want to completely\n"
"delete the command history ?"),
QMessageBox.Yes | QMessageBox.No)
if res == QMessageBox.No:
self.SendScintilla(QsciScintilla.SCI_DELETEBACK)
return
self.history = QStringList()
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole",
"## History cleared successfully ##")
output = sys.stdout.get_and_clean_data()
if output:
self.append(output)

View File

@ -8,10 +8,14 @@
/*font-family:Verdana,Geneva,sans-serif;*/
font-size : 12px;
}
#header{
background: #c6c6c6;
width: 100%;
}
</style>
</head>
<body>
<table>
<table id="header">
<tr>
<td>
<img src="../iconConsole/imgHelpDialog.png" />
@ -22,28 +26,64 @@
</tr>
</table>
<p align="justify">
Now you can use auto-completion and highlighting for syntax in console!!!
<br>
(Thanks to Larry Shaffer who provided the API files)
Python Console based on pyScintilla. (Developed by Salvatore Larosa)
<br><br>
To access Quantum GIS environment from this console
use qgis.utils.iface object (instance of QgisInterface class).
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">
<table id="header">
<tr>
<td>
<h4>Features</h4>
</td>
</tr>
</table>
<p align="justify">
<ul>
<li>Auto-completion and highlighting syntax for the following APIs:
<ol>
<li>Python</li>
<li>PyQGIS-master</li>
<li>PyQt4</li>
<li>QScintilla2</li>
<li>osgeo-gdal-ogr</li>
</ol>
</li>
<br>
<li>CTRL+SPACE to view the auto-completion list.</li>
<br>
<li>CTRL+ALT+SPACE to view the command history list.</li>
<br>
<li>Saves command history by typing <b>_save</b> or closing the widget.<br>
This command saves the history command in the file ~/.qgis/console_history.txt
</li>
<br>
<li>Clears command history by typing <b>_clear</b>.<br>
This command clears the history command from file ~/.qgis/console_history.txt
</li>
<br>
<li>Clears completely command history by typing <b>_clearAll</b>.<br>
This command clears completely the history command. It has an irreversible effect.
</li>
</ul>
</p>
<table id="header">
<tr>
<td>
<h4>Toolbar</h4>
</td>
</tr>
</table>
<p>The following is a description of the tools in the toolbar:</p>
<table width="100%">
<tr>
<td><img src="../iconConsole/iconClearConsole.png" /></td>
<td>Tool to clear python console</td>
</tr>
<tr>
<td><img src="../iconConsole/iconTempConsole.png" /></td>
<td><img src="../iconConsole/iconIfaceConsole.png" /></td>
<td>Tool to import iface class</td>
</tr>
<tr>
@ -67,5 +107,16 @@
<td>Run command (like Enter key pressed)</td>
</tr>
</table>
<br>
<table id="header">
<tr>
<td>
<h4>Acknowledgments</h4>
</td>
</tr>
</table>
<p>
Thanks to Larry Shaffer who provided the API files.
</p>
</body>
</html>

View File

@ -7,6 +7,8 @@ iconSextanteConsole.png
iconSaveConsole.png
iconHelpConsole.png
imgHelpDialog.png
iconIfaceConsole.png
iconClassConsole.png
)
FILE(GLOB ICON_FILES *.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB