42 changes: 21 additions & 21 deletions python/console/console_sci.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, parent=None):
for line in _init_commands:
self.runsource(line)

self.history = QStringList()
self.history = []
self.historyIndex = 0
# Read history command file
self.readHistoryFile()
Expand Down Expand Up @@ -114,10 +114,10 @@ def __init__(self, parent=None):
def settingsShell(self):
# Set Python lexer
self.setLexers()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2).toInt()[0]
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2)
self.setAutoCompletionThreshold(threshold)
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI').toString()
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True).toBool()
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True)
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.setAutoCompletionSource(self.AcsDocument)
Expand All @@ -135,8 +135,8 @@ def showHistory(self):
self.historyDlg.activateWindow()

def autoCompleteKeyBinding(self):
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource").toString()
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled").toBool()
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource")
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled")
if autoCompEnabled:
if radioButtonSource == 'fromDoc':
self.autoCompleteFromDocument()
Expand All @@ -149,7 +149,7 @@ def commandConsole(self, command):
if not self.is_cursor_on_last_line():
self.move_cursor_to_end()
line, pos = self.getCursorPosition()
selCmdLenght = self.text(line).length()
selCmdLenght = len(self.text(line))
self.setSelection(line, 4, line, selCmdLenght)
self.removeSelectedText()
if command == "sextante":
Expand All @@ -168,8 +168,8 @@ def commandConsole(self, command):
def setLexers(self):
self.lexer = QsciLexerPython()

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

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

self.api = QsciAPIs(self.lexer)
chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True).toBool()
chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True)
if chekBoxAPI:
self.api.loadPrepared( QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap" )
else:
apiPath = self.settings.value("pythonConsole/userAPI").toStringList()
apiPath = self.settings.value("pythonConsole/userAPI")
for i in range(0, len(apiPath)):
self.api.load(QString(unicode(apiPath[i])))
self.api.prepare()
Expand Down Expand Up @@ -223,7 +223,7 @@ def getTextLength(self):
def get_end_pos(self):
"""Return (line, index) position of the last character"""
line = self.lines() - 1
return (line, self.text(line).length())
return (line, len(self.text(line)))

def is_cursor_at_end(self):
"""Return True if cursor is at the end of text"""
Expand Down Expand Up @@ -262,7 +262,7 @@ def displayPrompt(self, more=False):
self.move_cursor_to_end()

def updateHistory(self, command):
if isinstance(command, QStringList):
if isinstance(command, list):
for line in command:
self.history.append(line)
elif not command == "":
Expand Down Expand Up @@ -320,9 +320,9 @@ def clearHistorySession(self):
self.clearHistory(True)

def showPrevious(self):
if self.historyIndex < len(self.history) and not self.history.isEmpty():
if self.historyIndex < len(self.history) and not self.history:
line, pos = self.getCursorPosition()
selCmdLenght = self.text(line).length()
selCmdLenght = len(self.text(line))
self.setSelection(line, 4, line, selCmdLenght)
self.removeSelectedText()
self.historyIndex += 1
Expand All @@ -335,9 +335,9 @@ def showPrevious(self):
#self.SendScintilla(QsciScintilla.SCI_DELETEBACK)

def showNext(self):
if self.historyIndex > 0 and not self.history.isEmpty():
if self.historyIndex > 0 and not self.history:
line, pos = self.getCursorPosition()
selCmdLenght = self.text(line).length()
selCmdLenght = len(self.text(line))
self.setSelection(line, 4, line, selCmdLenght)
self.removeSelectedText()
self.historyIndex -= 1
Expand Down Expand Up @@ -406,7 +406,7 @@ def keyPressEvent(self, e):
self.showNext()
## TODO: press event for auto-completion file directory
else:
if self.settings.value("pythonConsole/autoCloseBracket", True).toBool():
if self.settings.value("pythonConsole/autoCloseBracket", True):
t = unicode(e.text())
## Close bracket automatically
if t in self.opening:
Expand Down Expand Up @@ -521,9 +521,9 @@ def entered(self):

def currentCommand(self):
linenr, index = self.getCursorPosition()
txtLength = self.text(linenr).length()
txtLength = len(self.text(linenr))
string = self.text()
cmdLine = string.right(txtLength - 4)
cmdLine = string[4:]
cmd = unicode(cmdLine)
return cmd

Expand Down Expand Up @@ -578,7 +578,7 @@ def __init__(self, parent):
self.reloadHistory.clicked.connect(self._reloadHistory)

def _runHistory(self, item):
cmd = item.data(Qt.DisplayRole).toString()
cmd = item.data(Qt.DisplayRole)
self.parent.runCommand(unicode(cmd))

def _reloadHistory(self):
Expand Down
26 changes: 13 additions & 13 deletions python/console/console_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def enableDisable(self, value):

def loadAPIFile(self):
settings = QSettings()
lastDirPath = settings.value("pythonConsole/lastDirAPIPath").toString()
lastDirPath = settings.value("pythonConsole/lastDirAPIPath")
fileAPI = QFileDialog.getOpenFileName(
self, "Open API File", lastDirPath, "API file (*.api)")
if fileAPI:
Expand Down Expand Up @@ -142,29 +142,29 @@ def saveSettings(self):

def restoreSettings(self):
settings = QSettings()
self.spinBox.setValue(settings.value("pythonConsole/fontsize", 10).toInt()[0])
self.spinBoxEditor.setValue(settings.value("pythonConsole/fontsizeEditor", 10).toInt()[0])
self.spinBox.setValue(settings.value("pythonConsole/fontsize", 10))
self.spinBoxEditor.setValue(settings.value("pythonConsole/fontsizeEditor", 10))
self.fontComboBox.setCurrentFont(QFont(settings.value("pythonConsole/fontfamilytext",
"Monospace").toString()))
"Monospace")))
self.fontComboBoxEditor.setCurrentFont(QFont(settings.value("pythonConsole/fontfamilytextEditor",
"Monospace").toString()))
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True).toBool())
itemTable = settings.value("pythonConsole/userAPI").toStringList()
"Monospace")))
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True, type=bool))
itemTable = settings.value("pythonConsole/userAPI")
for i in range(len(itemTable)):
self.tableWidget.insertRow(i)
self.tableWidget.setColumnCount(2)
pathSplit = itemTable[i].split("/")
apiName = pathSplit[-1][0:-4]
self.tableWidget.setItem(i, 0, QTableWidgetItem(apiName))
self.tableWidget.setItem(i, 1, QTableWidgetItem(itemTable[i]))
self.autoSaveScript.setChecked(settings.value("pythonConsole/autoSaveScript", False).toBool())
self.autoSaveScript.setChecked(settings.value("pythonConsole/autoSaveScript", False, type=bool))

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

self.enableObjectInspector.setChecked(settings.value("pythonConsole/enableObjectInsp", False).toBool())
self.autoCloseBracketEditor.setChecked(settings.value("pythonConsole/autoCloseBracketEditor", True).toBool())
self.autoCloseBracket.setChecked(settings.value("pythonConsole/autoCloseBracket", True).toBool())
self.enableObjectInspector.setChecked(settings.value("pythonConsole/enableObjectInsp", False, type=bool))
self.autoCloseBracketEditor.setChecked(settings.value("pythonConsole/autoCloseBracketEditor", True, type=bool))
self.autoCloseBracket.setChecked(settings.value("pythonConsole/autoCloseBracket", True, type=bool))

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
self.autoCompFromDoc.setChecked(True)
Expand Down
16 changes: 8 additions & 8 deletions python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"""

from PyQt4.QtCore import QCoreApplication, QLocale, QString
from PyQt4.QtCore import QCoreApplication, QLocale
from qgis.core import QGis, QgsExpression, QgsMessageLog
from string import Template
import sys
Expand Down Expand Up @@ -182,8 +182,8 @@ def loadPlugin(packageName):
__import__(packageName)
return True
except:
msgTemplate = QCoreApplication.translate("Python", "Couldn't load plugin '%1' from ['%2']")
msg = msgTemplate.arg(packageName).arg("', '".join(sys.path))
msgTemplate = QCoreApplication.translate("Python", "Couldn't load plugin '%s' from ['%s']")
msg = msgTemplate % (packageName, "', '".join(sys.path))
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
return False

Expand All @@ -196,14 +196,14 @@ def startPlugin(packageName):

package = sys.modules[packageName]

errMsg = QCoreApplication.translate("Python", "Couldn't load plugin %1" ).arg(packageName)
errMsg = QCoreApplication.translate("Python", "Couldn't load plugin %s" ) % packageName

# create an instance of the plugin
try:
plugins[packageName] = package.classFactory(iface)
except:
_unloadPluginModules(packageName)
msg = QCoreApplication.translate("Python", "%1 due an error when calling its classFactory() method").arg(errMsg)
msg = QCoreApplication.translate("Python", "%s due an error when calling its classFactory() method") % errMsg
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
return False

Expand All @@ -213,7 +213,7 @@ def startPlugin(packageName):
except:
del plugins[packageName]
_unloadPluginModules(packageName)
msg = QCoreApplication.translate("Python", "%1 due an error when calling its initGui() method" ).arg( errMsg )
msg = QCoreApplication.translate("Python", "%s due an error when calling its initGui() method" ) % errMsg
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
return False

Expand Down Expand Up @@ -255,7 +255,7 @@ def unloadPlugin(packageName):
_unloadPluginModules(packageName)
return True
except Exception, e:
msg = QCoreApplication.translate("Python", "Error while unloading plugin %1").arg(packageName)
msg = QCoreApplication.translate("Python", "Error while unloading plugin %s") % packageName
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
return False

Expand Down Expand Up @@ -418,7 +418,7 @@ def add(values, *args):
helptemplate = Template("""<h3>$name function</h3><br>$doc""")
class QgsExpressionFunction(QgsExpression.Function):
def __init__(self, name, args, group, helptext=''):
QgsExpression.Function.__init__(self, name, args, group, QString(helptext))
QgsExpression.Function.__init__(self, name, args, group, helptext)

def func(self, values, feature, parent):
pass
Expand Down
15 changes: 14 additions & 1 deletion src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,26 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
runString( "sys.path = [" + newpaths.join( "," ) + "] + sys.path" );

// import SIP
if ( !runString( "from sip import wrapinstance, unwrapinstance",
if ( !runString( "import sip",
QObject::tr( "Couldn't load SIP module." ) + "\n" + QObject::tr( "Python support will be disabled." ) ) )
{
exitPython();
return;
}

// set PyQt4 api versions
QStringList apiV2classes;
apiV2classes << "QDate" << "QDateTime" << "QString" << "QTextStream" << "QTime" << "QUrl" << "QVariant";
foreach ( const QString& clsName, apiV2classes )
{
if ( !runString( QString( "sip.setapi('%1', 2)" ).arg( clsName ),
QObject::tr( "Couldn't set SIP API versions.") + "\n" + QObject::tr( "Python support will be disabled." ) ) )
{
exitPython();
return;
}
}

// import Qt bindings
if ( !runString( "from PyQt4 import QtCore, QtGui",
QObject::tr( "Couldn't load PyQt4." ) + "\n" + QObject::tr( "Python support will be disabled." ) ) )
Expand Down