53 changes: 38 additions & 15 deletions python/console/console_sci.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def __init__(self, parent=None):

self.parent = parent

self.settings = QSettings()

# Enable non-ascii chars for editor
self.setUtf8(True)

Expand Down Expand Up @@ -74,8 +76,7 @@ def __init__(self, parent=None):
# Set Python lexer
self.setLexers()

self.setAutoCompletionThreshold(2)
self.setAutoCompletionSource(self.AcsAPIs)
self.settingsShell()

# Don't want to see the horizontal scrollbar at all
# Use raw message to Scintilla here (all messages are documented
Expand Down Expand Up @@ -103,16 +104,41 @@ def __init__(self, parent=None):
self.newShortcutCAS = QShortcut(QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_Space), self)
self.newShortcutCSS.setContext(Qt.WidgetShortcut)
self.newShortcutCAS.setContext(Qt.WidgetShortcut)
self.newShortcutCAS.activated.connect(self.autoComplete)
self.newShortcutCAS.activated.connect(self.autoCompleteKeyBinding)
self.newShortcutCSS.activated.connect(self.showHistory)
self.connect(self, SIGNAL('userListActivated(int, const QString)'),
self.completion_list_selected)

def settingsShell(self):
self.setLexers()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2).toInt()[0]
self.setAutoCompletionThreshold(threshold)
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI').toString()
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True).toBool()
self.setAutoCompletionThreshold(threshold)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.setAutoCompletionSource(self.AcsDocument)
elif radioButtonSource == 'fromAPI':
self.setAutoCompletionSource(self.AcsAPIs)
elif radioButtonSource == 'fromDocAPI':
self.setAutoCompletionSource(self.AcsAll)
else:
self.setAutoCompletionSource(self.AcsNone)

def showHistory(self):
self.showUserList(1, QStringList(self.history))

def autoComplete(self):
self.autoCompleteFromAll()
def autoCompleteKeyBinding(self):
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource").toString()
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled").toBool()
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.autoCompleteFromDocument()
elif radioButtonSource == 'fromAPI':
self.autoCompleteFromAPIs()
elif radioButtonSource == 'fromDocAPI':
self.autoCompleteFromAll()

def commandConsole(self, command):
if not self.is_cursor_on_last_line():
Expand All @@ -136,9 +162,9 @@ def commandConsole(self, command):

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

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

font = QFont(loadFont)
font.setFixedPitch(True)
Expand All @@ -157,11 +183,11 @@ def setLexers(self):
self.lexer.setFont(font, 4)

self.api = QsciAPIs(self.lexer)
chekBoxAPI = settings.value("pythonConsole/preloadAPI", True).toBool()
chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True).toBool()
if chekBoxAPI:
self.api.loadPrepared( QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap" )
else:
apiPath = settings.value("pythonConsole/userAPI").toStringList()
apiPath = self.settings.value("pythonConsole/userAPI").toStringList()
for i in range(0, len(apiPath)):
self.api.load(QString(unicode(apiPath[i])))
self.api.prepare()
Expand Down Expand Up @@ -242,9 +268,6 @@ def new_prompt(self, prompt):
self.ensureCursorVisible()
self.ensureLineVisible(line)

def refreshLexerProperties(self):
self.setLexers()

def displayPrompt(self, more=False):
self.append("... ") if more else self.append(">>> ")
self.move_cursor_to_end()
Expand Down Expand Up @@ -461,7 +484,7 @@ def currentCommand(self):
return cmd

def runCommand(self, cmd):
self.write_stdout(cmd)
self.writeCMD(cmd)
import webbrowser
self.updateHistory(cmd)
line, pos = self.getCursorPosition()
Expand Down Expand Up @@ -502,7 +525,7 @@ def runCommand(self, cmd):
def write(self, txt):
sys.stderr.write(txt)

def write_stdout(self, txt):
def writeCMD(self, txt):
if len(txt) > 0:
getCmdString = self.text()
prompt = getCmdString[0:4]
Expand Down
126 changes: 96 additions & 30 deletions python/console/console_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,29 @@ class optionsDialog(QDialog, Ui_SettingsDialogPythonConsole):
def __init__(self, parent):
QDialog.__init__(self, parent)
self.setWindowTitle(QCoreApplication.translate("SettingsDialogPythonConsole", "Settings Python Console"))
#self.iface = iface
self.parent = parent
self.setupUi(self)
#self.show()

self.listPath = []

self.restoreSettings()
self.initialCheck()
self.autoCompletionOptions()
self.fontConfig()

self.lineEdit.setReadOnly(True)

self.addAPIpath.setIcon(QIcon(":/images/themes/default/symbologyAdd.png"))
self.addAPIpath.setToolTip(QCoreApplication.translate("PythonConsole", "Add API path"))
self.removeAPIpath.setIcon(QIcon(":/images/themes/default/symbologyRemove.png"))
self.removeAPIpath.setToolTip(QCoreApplication.translate("PythonConsole", "Remove API path"))

self.connect( self.preloadAPI,
SIGNAL("stateChanged(int)"), self.initialCheck)
self.connect(self.browseButton,
SIGNAL("clicked()"), self.loadAPIFile)
self.connect( self.autoCompleteEnabled,
SIGNAL("stateChanged(int)"), self.autoCompletionOptions)
self.connect( self.autoCompleteEnabledEditor,
SIGNAL("stateChanged(int)"), self.autoCompletionOptions)
self.connect(self.addAPIpath,
SIGNAL("clicked()"), self.addAPI)
SIGNAL("clicked()"), self.loadAPIFile)
self.connect(self.removeAPIpath,
SIGNAL("clicked()"), self.removeAPI)

Expand All @@ -63,20 +62,41 @@ def initialCheck(self):

def enableDisable(self, value):
self.tableWidget.setEnabled(value)
self.lineEdit.setEnabled(value)
self.browseButton.setEnabled(value)
self.addAPIpath.setEnabled(value)
self.removeAPIpath.setEnabled(value)

def autoCompletionOptions(self):
if self.autoCompleteEnabled.isChecked():
self.enableDisableAutoCompleteOptions(True)
else:
self.enableDisableAutoCompleteOptions(False)
if self.autoCompleteEnabledEditor.isChecked():
self.enableDisableAutoCompleteOptions(True, editor='editor')
else:
self.enableDisableAutoCompleteOptions(False, editor='editor')

def enableDisableAutoCompleteOptions(self, value, editor=None):
if editor:
self.autoCompFromAPIEditor.setEnabled(value)
self.autoCompFromDocAPIEditor.setEnabled(value)
self.autoCompFromDocEditor.setEnabled(value)
self.autoCompThresholdEditor.setEnabled(value)
else:
self.autoCompFromAPI.setEnabled(value)
self.autoCompFromDocAPI.setEnabled(value)
self.autoCompFromDoc.setEnabled(value)
self.autoCompThreshold.setEnabled(value)

def loadAPIFile(self):
settings = QSettings()
lastDirPath = settings.value("pythonConsole/lastDirAPIPath").toString()
fileAPI = QFileDialog.getOpenFileName(
self, "Open API File", lastDirPath, "API file (*.api)")
self.lineEdit.setText(fileAPI)
if fileAPI:
self.addAPI(fileAPI)

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

def accept(self):
if not self.preloadAPI.isChecked():
Expand All @@ -88,15 +108,12 @@ def accept(self):
self.listPath = []
QDialog.accept( self )

def addAPI(self):
if self.lineEdit.text() == "":
return
path = self.lineEdit.text()
def addAPI(self, pathAPI):
count = self.tableWidget.rowCount()
self.tableWidget.setColumnCount(2)
self.tableWidget.insertRow(count)
pathItem = QTableWidgetItem(path)
pathSplit = path.split("/")
pathItem = QTableWidgetItem(pathAPI)
pathSplit = pathAPI.split("/")
apiName = pathSplit[-1][0:-4]
apiNameItem = QTableWidgetItem(apiName)
self.tableWidget.setItem(count, 0, apiNameItem)
Expand All @@ -105,40 +122,69 @@ def addAPI(self):
self.tableWidget.horizontalHeader().setResizeMode(0, QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().show()
self.tableWidget.horizontalHeader().setResizeMode(1, QHeaderView.Stretch)
#self.tableWidget.resizeRowsToContents()
self.lineEdit.clear()

def removeAPI(self):
listItemSel = self.tableWidget.selectedIndexes()
#row = self.tableWidget.currentRow()
for indx in listItemSel:
self.tableWidget.removeRow(indx.row())
listItemSel = self.tableWidget.selectionModel().selectedRows()
for index in reversed(listItemSel):
self.tableWidget.removeRow(index.row())

def fontConfig(self):
#fontFamily = ['Courier','Monospace','Aurulent Sans','Bitstream Vera Serif']
#for i in range(0, len(fontFamily)):
#self.comboBox.addItem(fontFamily[i])
settings = QSettings()
self.fontComboBox.setCurrentIndex(settings.value("pythonConsole/fontfamilyindex").toInt()[0])
self.fontComboBoxEditor.setCurrentIndex(settings.value("pythonConsole/fontfamilyindexEditor").toInt()[0])

def saveSettings(self):
settings = QSettings()
settings.setValue("pythonConsole/preloadAPI", QVariant(self.preloadAPI.isChecked()))
settings.setValue("pythonConsole/autoSaveScript", QVariant(self.autoSaveScript.isChecked()))
fontFamilyIndex = self.fontComboBox.currentIndex()
settings.setValue("pythonConsole/fontfamilyindex", QVariant(fontFamilyIndex))
fontFamilyText = self.fontComboBox.currentText()
settings.setValue("pythonConsole/fontfamilytext", QVariant(fontFamilyText))

fontFamilyIndexEditor = self.fontComboBoxEditor.currentIndex()
settings.setValue("pythonConsole/fontfamilyindexEditor", QVariant(fontFamilyIndexEditor))
fontFamilyTextEditor = self.fontComboBoxEditor.currentText()
settings.setValue("pythonConsole/fontfamilytextEditor", QVariant(fontFamilyTextEditor))

fontSize = self.spinBox.value()
fontSizeEditor = self.spinBoxEditor.value()

for i in range(0, self.tableWidget.rowCount()):
text = self.tableWidget.item(i, 1).text()
self.listPath.append(text)
settings.setValue("pythonConsole/fontsize", QVariant(fontSize))
settings.setValue("pythonConsole/fontsizeEditor", QVariant(fontSizeEditor))
settings.setValue("pythonConsole/userAPI", QVariant(self.listPath))

settings.setValue("pythonConsole/autoCompThreshold", QVariant(self.autoCompThreshold.value()))
settings.setValue("pythonConsole/autoCompThresholdEditor", QVariant(self.autoCompThresholdEditor.value()))

if self.autoCompFromAPIEditor.isChecked():
settings.setValue("pythonConsole/autoCompleteSourceEditor", QVariant('fromAPI'))
elif self.autoCompFromDocEditor.isChecked():
settings.setValue("pythonConsole/autoCompleteSourceEditor", QVariant('fromDoc'))
elif self.autoCompFromDocAPIEditor.isChecked():
settings.setValue("pythonConsole/autoCompleteSourceEditor", QVariant('fromDocAPI'))

if self.autoCompFromAPI.isChecked():
settings.setValue("pythonConsole/autoCompleteSource", QVariant('fromAPI'))
elif self.autoCompFromDoc.isChecked():
settings.setValue("pythonConsole/autoCompleteSource", QVariant('fromDoc'))
elif self.autoCompFromDocAPI.isChecked():
settings.setValue("pythonConsole/autoCompleteSource", QVariant('fromDocAPI'))

settings.setValue("pythonConsole/autoCompleteEnabledEditor", QVariant(self.autoCompleteEnabledEditor.isChecked()))
settings.setValue("pythonConsole/autoCompleteEnabled", QVariant(self.autoCompleteEnabled.isChecked()))

def restoreSettings(self):
settings = QSettings()
self.spinBox.setValue(settings.value("pythonConsole/fontsize").toInt()[0])
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True).toBool())
self.spinBox.setValue(settings.value("pythonConsole/fontsize", 10).toInt()[0])
self.spinBoxEditor.setValue(settings.value("pythonConsole/fontsizeEditor", 10).toInt()[0])
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI").toBool())
itemTable = settings.value("pythonConsole/userAPI").toStringList()
for i in range(len(itemTable)):
self.tableWidget.insertRow(i)
Expand All @@ -151,7 +197,27 @@ def restoreSettings(self):
self.tableWidget.horizontalHeader().setResizeMode(0, QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().show()
self.tableWidget.horizontalHeader().setResizeMode(1, QHeaderView.Stretch)
#self.comboBox.setCurrentIndex(settings.value("pythonConsole/fontfamilyindex").toInt()[0])

def reject( self ):
QDialog.reject( self )
self.autoSaveScript.setChecked(settings.value("pythonConsole/autoSaveScript", False).toBool())

self.autoCompThreshold.setValue(settings.value("pythonConsole/autoCompThreshold", 2).toInt()[0])
self.autoCompThresholdEditor.setValue(settings.value("pythonConsole/autoCompThresholdEditor", 2).toInt()[0])

self.autoCompleteEnabledEditor.setChecked(settings.value("pythonConsole/autoCompleteEnabledEditor", True).toBool())
self.autoCompleteEnabled.setChecked(settings.value("pythonConsole/autoCompleteEnabled", True).toBool())

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
self.autoCompFromDoc.setChecked(True)
elif settings.value("pythonConsole/autoCompleteSource") == 'fromAPI':
self.autoCompFromAPI.setChecked(True)
elif settings.value("pythonConsole/autoCompleteSource") == 'fromDocAPI':
self.autoCompFromDocAPI.setChecked(True)

if settings.value("pythonConsole/autoCompleteSourceEditor") == 'fromDoc':
self.autoCompFromDocEditor.setChecked(True)
elif settings.value("pythonConsole/autoCompleteSourceEditor") == 'fromAPI':
self.autoCompFromAPIEditor.setChecked(True)
elif settings.value("pythonConsole/autoCompleteSourceEditor") == 'fromDocAPI':
self.autoCompFromDocAPIEditor.setChecked(True)

def reject(self):
QDialog.reject(self)
662 changes: 460 additions & 202 deletions python/console/console_settings.ui

Large diffs are not rendered by default.