Skip to content

Commit

Permalink
Merge pull request #297 from slarosa/master
Browse files Browse the repository at this point in the history
[FEATURE] - Choose Fontfamily and API files for Python Console
  • Loading branch information
brushtyler committed Oct 19, 2012
2 parents 8ca2236 + 3fecd44 commit ee60974
Show file tree
Hide file tree
Showing 14 changed files with 566 additions and 51 deletions.
4 changes: 4 additions & 0 deletions i18n/qgis_it.ts
Expand Up @@ -6452,6 +6452,10 @@ Cambiare questa situazione prima, perché il plugin OSM non quale layer è la de
<source>Save to script file</source>
<translation>Salva file</translation>
</message>
<message>
<source>Settings</source>
<translation>Impostazioni</translation>
</message>
<message>
<source>Help</source>
<translation>Aiuto</translation>
Expand Down
Binary file added images/console/iconAboutConsole.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
8 changes: 7 additions & 1 deletion python/CMakeLists.txt
Expand Up @@ -149,7 +149,12 @@ ADD_CUSTOM_COMMAND(TARGET compile_python_files
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

FOREACH(file __init__.py utils.py console.py console_sci.py console_help.py)
FILE(GLOB UI_FILES *.ui)
PYQT4_WRAP_UI(PYUI_FILES ${UI_FILES})
ADD_CUSTOM_TARGET(console ALL DEPENDS ${PYUI_FILES})
INSTALL(FILES ${PYUI_FILES} DESTINATION ${QGIS_PYTHON_DIR})

FOREACH(file __init__.py utils.py console.py console_sci.py console_help.py console_settings.py)
ADD_CUSTOM_COMMAND(TARGET compile_python_files
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${file} ${QGIS_PYTHON_OUTPUT_DIRECTORY}
Expand All @@ -163,3 +168,4 @@ PYTHON_INSTALL(utils.py ${QGIS_PYTHON_DIR})
PYTHON_INSTALL(console.py ${QGIS_PYTHON_DIR})
PYTHON_INSTALL(console_sci.py ${QGIS_PYTHON_DIR})
PYTHON_INSTALL(console_help.py ${QGIS_PYTHON_DIR})
PYTHON_INSTALL(console_settings.py ${QGIS_PYTHON_DIR})
43 changes: 27 additions & 16 deletions python/console.py
Expand Up @@ -24,6 +24,7 @@
from qgis.utils import iface
from console_sci import PythonEdit
from console_help import HelpDialog
from console_settings import optionsDialog

import sys
import os
Expand Down Expand Up @@ -69,7 +70,7 @@ def get_and_clean_data(self):
def flush(self):
pass

sys.stdout = QgisOutputCatcher()
sys.stdout = QgisOutputCatcher()

class PythonConsole(QDockWidget):
def __init__(self, parent=None):
Expand Down Expand Up @@ -98,7 +99,8 @@ def __init__(self, parent=None):
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))

self.widgetButton = QWidget()

self.options = optionsDialog(self)

self.toolBar = QToolBar()
self.toolBar.setEnabled(True)
#self.toolBar.setFont(font)
Expand Down Expand Up @@ -130,13 +132,16 @@ def __init__(self, parent=None):
self.clearButton.setIconVisibleInMenu(True)
self.clearButton.setToolTip(clearBt)
self.clearButton.setText(clearBt)
## Action for paste snippets code
# self.currentLayerButton = QAction(parent)
# self.currentLayerButton.setCheckable(False)
# self.currentLayerButton.setEnabled(True)
# self.currentLayerButton.setIcon(QIcon("icon/iconTempConsole.png"))
# self.currentLayerButton.setMenuRole(QAction.PreferencesRole)
# self.currentLayerButton.setIconVisibleInMenu(True)
## Action for settings
optionsBt = QCoreApplication.translate("PythonConsole", "Settings")
self.optionsButton = QAction(parent)
self.optionsButton.setCheckable(False)
self.optionsButton.setEnabled(True)
self.optionsButton.setIcon(QIcon(":/images/console/iconSettingsConsole.png"))
self.optionsButton.setMenuRole(QAction.PreferencesRole)
self.optionsButton.setIconVisibleInMenu(True)
self.optionsButton.setToolTip(optionsBt)
self.optionsButton.setText(optionsBt)
## Action menu for class
actionClassBt = QCoreApplication.translate("PythonConsole", "Import Class")
self.actionClass = QAction(parent)
Expand Down Expand Up @@ -241,6 +246,7 @@ def __init__(self, parent=None):
self.toolBar.addAction(self.clearButton)
self.toolBar.addAction(self.actionClass)
self.toolBar.addAction(self.actionScript)
self.toolBar.addAction(self.optionsButton)
self.toolBar.addAction(self.helpButton)
self.toolBar.addAction(self.runButton)
## Menu Import Class
Expand Down Expand Up @@ -268,7 +274,7 @@ def __init__(self, parent=None):
self.e.addWidget(self.edit)

self.clearButton.triggered.connect(self.edit.clearConsole)
#self.currentLayerButton.triggered.connect(self.cLayer)
self.optionsButton.triggered.connect(self.openSettings)
self.loadIfaceButton.triggered.connect(self.iface)
self.loadSextanteButton.triggered.connect(self.sextante)
self.loadQtCoreButton.triggered.connect(self.qtCore)
Expand All @@ -277,9 +283,8 @@ def __init__(self, parent=None):
self.openFileButton.triggered.connect(self.openScriptFile)
self.saveFileButton.triggered.connect(self.saveScriptFile)
self.helpButton.triggered.connect(self.openHelp)

def cLayer(self):
self.edit.commandConsole('cLayer')
QObject.connect(self.options.buttonBox, SIGNAL("accepted()"),
self.prefChanged)

def sextante(self):
self.edit.commandConsole('sextante')
Expand All @@ -295,7 +300,7 @@ def qtGui(self):

def openScriptFile(self):
settings = QSettings()
lastDirPath = settings.value("/pythonConsole/lastDirPath").toString()
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
scriptFile = QFileDialog.getOpenFileName(
self, "Open File", lastDirPath, "Script file (*.py)")
if scriptFile.isEmpty() == False:
Expand All @@ -307,7 +312,7 @@ def openScriptFile(self):
self.edit.insertTextFromFile(listScriptFile)

lastDirPath = QFileInfo(scriptFile).path()
settings.setValue("/pythonConsole/lastDirPath", QVariant(scriptFile))
settings.setValue("pythonConsole/lastDirPath", QVariant(scriptFile))


def saveScriptFile(self):
Expand Down Expand Up @@ -336,12 +341,18 @@ def saveScriptFile(self):
def openHelp(self):
dlg = HelpDialog()
dlg.exec_()

def openSettings(self):
#options = optionsDialog()
self.options.exec_()

def prefChanged(self):
self.edit.refreshLexerProperties()

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


if __name__ == '__main__':
a = QApplication(sys.argv)
console = PythonConsoleWidget()
Expand Down
16 changes: 13 additions & 3 deletions python/console_help/help.htm
Expand Up @@ -34,7 +34,7 @@ <h2 id="headerTitle">Python Console for QGIS</h2>
</tr>
</table>
<p align="justify">
<span id="headerSubject">Python Console based on PyQScintilla2. (Developed by Salvatore Larosa)</span>
<span id="headerSubject">Python Console based on PyQScintilla2.</span>
<br><br>
<span id="headerSubjectMain">To access Quantum GIS environment from this console
use qgis.utils.iface object (instance of QgisInterface class).
Expand Down Expand Up @@ -64,16 +64,22 @@ <h4 id="features">Features</h4>
<br>
<li><span id="featuresB">CTRL+ALT+SPACE to view the command history list.</span></li>
<br>
<li><span id="featuresLoadAPI">Open Quantum GIS API documentation by typing <b>_api</b>.</span></li>
<br>
<li><span id="featuresLoadCookBook">Open PyQGIS Cookbook by typing <b>_pyqgis</b>.</span>
</li>
<br>
<li><span id="featuresC">Saves the 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</span>
</li>
<br>
<li><span id="featuresD">Clears the command history by typing <b>_clear</b>.<br>
This command clears the command history from file ~/.qgis/console_history.txt</span>
This command clears the temporary command history</span>
</li>
<br>
<li><span id="featuresE">Clears completely command history by typing <b>_clearAll</b>.<br>
This command clears completely the command history. It has an irreversible effect.</span>
This command clears completely the command history (both temporary and ~/.qgis/console_history.txt).
It has an irreversible effect.</span>
</li>
</ul>
</p>
Expand Down Expand Up @@ -120,6 +126,10 @@ <h4 id="toolbar">Toolbar</h4>
<td><img src="qrc:/images/console/iconSaveConsole.png" /></td>
<td><span id="toolbarScriptSave">Tool to save a python script</span></td>
</tr>
<tr>
<td><img src="qrc:/images/console/iconSettingsConsole.png" /></td>
<td colspan="2"><span id="toolbarSettings">Settings</span></td>
</tr>
<tr>
<td><img src="qrc:/images/console/iconHelpConsole.png" /></td>
<td colspan="2"><span id="toolbarHelp">Help</span></td>
Expand Down
2 changes: 1 addition & 1 deletion python/console_help/i18n/de_DE.properties
@@ -1,6 +1,6 @@
i18n_dict = {
"header.title" : "Python Console für QGIS",
"header.subject" : "Python Console auf PyQScintilla2 basierend (entwickelt von Salvatore Larosa)",
"header.subject" : "Python Console auf PyQScintilla2 basierend",
"header.subject.main" : "Das Objekt qgis.utils.iface (Instanz der Klasse QgisInterface) ermöglicht den Zugriff \
quf die Quantum GIS Umgebung von der Konsole aus. \
Über den defür bestimmten Knopf in der Werkzeugleiste \
Expand Down
5 changes: 4 additions & 1 deletion python/console_help/i18n/en_US.properties
@@ -1,6 +1,6 @@
i18n_dict = {
"header.title" : "Python Console for QGIS",
"header.subject" : "Python Console based on PyQScintilla2. (Developed by Salvatore Larosa)",
"header.subject" : "Python Console based on PyQScintilla2.",
"header.subject.main" : "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 \
Expand All @@ -15,6 +15,8 @@ i18n_dict = {
This command clears the command history from file ~/.qgis/console_history.txt",
"features.e" : "Clears completely command history by typing '_clearAll'. \
This command clears completely the command history. It has an irreversible effect.",
"features.api.doc" : "Open Quantum GIS API documentation by typing '_api'.",
"features.pyqgis.doc" : "Open PyQGIS Cookbook by typing '_pyqgis'.",
"toolbar" : "Toolbar",
"toolbar.title" : "The following is a description of the tools in the toolbar:",
"toolbar.clear" : "Tool to clear python console",
Expand All @@ -24,6 +26,7 @@ i18n_dict = {
"toolbar.qtgui" : "Tool to import PyQt4.QtGui class",
"toolbar.script.open" : "Tool to open a python script and load in console",
"toolbar.script.save" : "Tool to save a python script",
"toolbar.settings" : "Settings",
"toolbar.help" : "Help",
"toolbar.run" : "Run command (like Enter key pressed)",
"thanks" : "Acknowledgments",
Expand Down
5 changes: 4 additions & 1 deletion python/console_help/i18n/it_IT.properties
@@ -1,6 +1,6 @@
i18n_dict = {
"header.title" : "Console Python per QGIS",
"header.subject" : "Console Python basata su PyQScintilla2. (Sviluppata da Salvatore Larosa)",
"header.subject" : "Console Python basata su PyQScintilla2.",
"header.subject.main" : "Per accedere all'ambiente Quantum GIS da questa console \
usa l'oggetto qgis.utils.iface (istanza della classe QgisInterface). \
Per importare la classe QgisInterface puoi usare anche il bottone dedicato che si trova \
Expand All @@ -15,6 +15,8 @@ i18n_dict = {
La cronologia verrà cancellata dal file ~/.qgis/console_history.txt",
"features.e" : "Possibilità di cancellare completamente la cronologia dei comandi digitando '_clearAll'. \
La cronologia verrà cancellata sia dal file che dalla memoria temporanea.",
"features.api.doc" : "Apri la documentazione completa sulle API di QuantumGIS digitando '_api'.",
"features.pyqgis.doc" : "Apri il Cookbook PyQGIS digitando '_pyqgis'.",
"toolbar" : "Toolbar",
"toolbar.title" : "Di seguito la descrizione dei comandi disponibile nella toolbar:",
"toolbar.clear" : "Strumento per pulire la console",
Expand All @@ -24,6 +26,7 @@ i18n_dict = {
"toolbar.qtgui" : "Strumento per importare la classe PyQt4.QtGui",
"toolbar.script.open" : "Strumento per aprire un script python da eseguire in console",
"toolbar.script.save" : "Strumento per salvare uno script python sul disco",
"toolbar.settings" : "Impostazioni",
"toolbar.help" : "Aiuto",
"toolbar.run" : "Esegui comando (simile al tasto <Invio>)",
"thanks" : "Ringraziamenti",
Expand Down
2 changes: 1 addition & 1 deletion python/console_help/i18n/ru_RU.properties
@@ -1,6 +1,6 @@
i18n_dict = {
"header.title" : "Консоль Python для QGIS",
"header.subject" : "Консоль Python на основе PyQScintilla2. (Разработана Salvatore Larosa)",
"header.subject" : "Консоль Python на основе PyQScintilla2.",
"header.subject.main" : "Для доступа к окружению Quantum GIS из консоли \
используйте объект qgis.utils.iface (экземпляр класса QgisInterface). \
Также можно импортировать класс QgisInterface при помощи специальной \
Expand Down
3 changes: 3 additions & 0 deletions python/console_help/js/encoding.js
Expand Up @@ -10,6 +10,8 @@ $('span#featuresB').text($.i18n._('features.b'));
$('span#featuresC').text($.i18n._('features.c'));
$('span#featuresD').text($.i18n._('features.d'));
$('span#featuresE').text($.i18n._('features.e'));
$('span#featuresLoadAPI').text($.i18n._('features.api.doc'));
$('span#featuresLoadCookBook').text($.i18n._('features.pyqgis.doc'));
$('h4#toolbar').text($.i18n._('toolbar'));
$('span#toolbarTitle').text($.i18n._('toolbar.title'));
$('span#toolbarClear').text($.i18n._('toolbar.clear'));
Expand All @@ -20,6 +22,7 @@ $('span#toolbarQtGuiClass').text($.i18n._('toolbar.qtgui'));
$('span#toolbarScriptOpen').text($.i18n._('toolbar.script.open'));
$('span#toolbarScriptSave').text($.i18n._('toolbar.script.save'));
$('span#toolbarHelp').text($.i18n._('toolbar.help'));
$('span#toolbarSettings').text($.i18n._('toolbar.settings'));
$('span#toolbarRun').text($.i18n._('toolbar.run'));
$('h4#thanks').text($.i18n._('thanks'));
$('span#thanksText').text($.i18n._('thanks.text'));
71 changes: 44 additions & 27 deletions python/console_sci.py
Expand Up @@ -73,7 +73,7 @@ def __init__(self, parent=None):
# Set Python lexer
# Set style for Python comments (style number 1) to a fixed-width
# courier.
self.setLexers(True)
self.setLexers()

# Indentation
#self.setAutoIndent(True)
Expand Down Expand Up @@ -120,10 +120,11 @@ def showHistory(self):

def autoComplete(self):
self.autoCompleteFromAll()

def clearConsole(self):
"""Clear the contents of the console."""
self.setText('')
self.SendScintilla(QsciScintilla.SCI_CLEARALL)
#self.setText('')
self.insertInitText()
self.displayPrompt(False)
self.setFocus()
Expand Down Expand Up @@ -156,32 +157,39 @@ def commandConsole(self, command):
self.append('from PyQt4.QtGui import *')
self.move_cursor_to_end()
self.setFocus()
def setLexers(self, lexer):

def setLexers(self):
from qgis.core import QgsApplication
if lexer:
font = QFont()
font.setFamily('Mono') ## Courier New
font.setFixedPitch(True)
## check platform for font size
if sys.platform.startswith('darwin'):
font.setPointSize(13)
else:
font.setPointSize(10)
self.setFont(font)
self.setMarginsFont(font)
self.lexer = QsciLexerPython()
self.lexer.setDefaultFont(font)
self.lexer.setColor(Qt.red, 1)
self.lexer.setColor(Qt.darkGreen, 5)
self.lexer.setColor(Qt.darkBlue, 15)
self.lexer.setFont(font, 1)
self.lexer.setFont(font, 3)
self.lexer.setFont(font, 4)
self.api = QsciAPIs(self.lexer)

self.lexer = QsciLexerPython()
settings = QSettings()
loadFont = settings.value("pythonConsole/fontfamilytext", "Monospace").toString()
fontSize = settings.value("pythonConsole/fontsize", 10).toInt()[0]

font = QFont(loadFont)
font.setFixedPitch(True)
font.setPointSize(fontSize)

self.lexer.setDefaultFont(font)
self.lexer.setColor(Qt.red, 1)
self.lexer.setColor(Qt.darkGreen, 5)
self.lexer.setColor(Qt.darkBlue, 15)
self.lexer.setFont(font, 1)
self.lexer.setFont(font, 3)
self.lexer.setFont(font, 4)

self.api = QsciAPIs(self.lexer)
chekBoxAPI = settings.value( "pythonConsole/preloadAPI" ).toBool()
if chekBoxAPI:
self.api.loadPrepared( QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap" )
else:
apiPath = settings.value("pythonConsole/userAPI").toStringList()
for i in range(0, len(apiPath)):
self.api.load(QString(unicode(apiPath[i])))
self.api.prepare()
self.lexer.setAPIs(self.api)

self.setLexer(self.lexer)
self.setLexer(self.lexer)

## TODO: show completion list for file and directory

Expand Down Expand Up @@ -263,6 +271,9 @@ def new_prompt(self, prompt):
self.ensureCursorVisible()
self.ensureLineVisible(line)

def refreshLexerProperties(self):
self.setLexers()

# def check_selection(self):
# """
# Check if selected text is r/w,
Expand Down Expand Up @@ -484,9 +495,10 @@ def currentCommand(self):
return cmd

def runCommand(self, cmd):
import webbrowser
self.updateHistory(cmd)
self.SendScintilla(QsciScintilla.SCI_NEWLINE)
if cmd in ('_save', '_clear', '_clearAll'):
if cmd in ('_save', '_clear', '_clearAll', '_pyqgis', '_api'):
if cmd == '_save':
self.writeHistoryFile()
print QCoreApplication.translate("PythonConsole",
Expand All @@ -508,6 +520,11 @@ def runCommand(self, cmd):
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole",
"## History cleared successfully ##")
elif cmd == '_pyqgis':
webbrowser.open( "http://www.qgis.org/pyqgis-cookbook/" )
elif cmd == '_api':
webbrowser.open( "http://www.qgis.org/api/" )

output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
Expand Down

0 comments on commit ee60974

Please sign in to comment.