Skip to content

Commit

Permalink
[pyqgis-console] uses QFileInfo instead of os.path to check if file e…
Browse files Browse the repository at this point in the history
…xists

- minor fixes and cleanup
  • Loading branch information
slarosa committed May 15, 2013
1 parent 02ab027 commit 0175a80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
30 changes: 13 additions & 17 deletions python/console/console.py
Expand Up @@ -90,6 +90,8 @@ def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))

self.settings = QSettings()

self.options = optionsDialog(self)
self.helpDlg = HelpDialog(self)

Expand Down Expand Up @@ -242,7 +244,8 @@ def __init__(self, parent=None):
objList = QCoreApplication.translate("PythonConsole", "Object Inspector")
self.objectListButton = QAction(self)
self.objectListButton.setCheckable(True)
self.objectListButton.setEnabled(True)
self.objectListButton.setEnabled(self.settings.value("pythonConsole/enableObjectInsp",
False).toBool())
self.objectListButton.setIcon(QgsApplication.getThemeIcon("console/iconClassBrowserConsole.png"))
self.objectListButton.setMenuRole(QAction.PreferencesRole)
self.objectListButton.setIconVisibleInMenu(True)
Expand Down Expand Up @@ -467,7 +470,7 @@ def __init__(self, parent=None):
self.layoutFind.setContentsMargins(0, 0, 0, 0)
self.lineEditFind = QgsFilterLineEdit()
placeHolderTxt = QCoreApplication.translate("PythonConsole", "Enter text to find...")

if pyqtconfig.Configuration().qt_version >= 0x40700:
self.lineEditFind.setPlaceholderText(placeHolderTxt)
else:
Expand Down Expand Up @@ -596,8 +599,7 @@ def uncommentCode(self):
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)

def openScriptFile(self):
settings = QSettings()
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
lastDirPath = self.settings.value("pythonConsole/lastDirPath").toString()
openFileTr = QCoreApplication.translate("PythonConsole", "Open File")
filename = QFileDialog.getOpenFileName(
self, openFileTr, lastDirPath, "Script file (*.py)")
Expand All @@ -612,7 +614,7 @@ def openScriptFile(self):
self.tabEditorWidget.newTabEditor(tabName, filename)

lastDirPath = QFileInfo(filename).path()
settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
self.settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
self.updateTabListScript(filename, action='append')

def saveScriptFile(self):
Expand Down Expand Up @@ -678,33 +680,27 @@ def callWidgetMessageBarEditor(self, text, level, timed):
self.tabEditorWidget.widgetMessageBar(iface, text, level, timed)

def updateTabListScript(self, script, action=None):
settings = QSettings()
if action == 'remove':
self.tabListScript.remove(script)
elif action == 'append':
if script not in self.tabListScript:
self.tabListScript.append(script)
else:
self.tabListScript = []
settings.setValue("pythonConsole/tabScripts",
self.settings.setValue("pythonConsole/tabScripts",
QVariant(self.tabListScript))

def saveSettingsConsole(self):
settings = QSettings()
#settings.setValue("pythonConsole/geometry", self.saveGeometry())
settings.setValue("pythonConsole/splitterObj", self.splitterObj.saveState())
settings.setValue("pythonConsole/splitterEditor", self.splitterEditor.saveState())
self.settings.setValue("pythonConsole/splitterObj", self.splitterObj.saveState())
self.settings.setValue("pythonConsole/splitterEditor", self.splitterEditor.saveState())

self.shell.writeHistoryFile()

def restoreSettingsConsole(self):
# List for tab script
settings = QSettings()
storedTabScripts = settings.value("pythonConsole/tabScripts")
storedTabScripts = self.settings.value("pythonConsole/tabScripts")
self.tabListScript = storedTabScripts.toList()
#self.restoreGeometry(settings.value("pythonConsole/geometry").toByteArray())
self.splitterEditor.restoreState(settings.value("pythonConsole/splitterEditor").toByteArray())
self.splitterObj.restoreState(settings.value("pythonConsole/splitterObj").toByteArray())
self.splitterEditor.restoreState(self.settings.value("pythonConsole/splitterEditor").toByteArray())
self.splitterObj.restoreState(self.settings.value("pythonConsole/splitterObj").toByteArray())

if __name__ == '__main__':
a = QApplication(sys.argv)
Expand Down
22 changes: 5 additions & 17 deletions python/console/console_editor.py
Expand Up @@ -266,20 +266,13 @@ def contextMenuEvent(self, e):
iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png")
iconRunScript = QgsApplication.getThemeIcon("console/iconRunScriptConsole.png")
iconCodePad = QgsApplication.getThemeIcon("console/iconCodepadConsole.png")
#iconNewEditor = QgsApplication.getThemeIcon("console/iconTabEditorConsole.png")
iconCommentEditor = QgsApplication.getThemeIcon("console/iconCommentEditorConsole.png")
iconUncommentEditor = QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.png")
iconSettings = QgsApplication.getThemeIcon("console/iconSettingsConsole.png")
iconFind = QgsApplication.getThemeIcon("console/iconSearchEditorConsole.png")
iconSyntaxCk = QgsApplication.getThemeIcon("console/iconSyntaxErrorConsole.png")
hideEditorAction = menu.addAction("Hide Editor",
self.hideEditor)
# menu.addSeparator()
# newTabAction = menu.addAction(iconNewEditor,
# "New Tab",
# self.parent.newTab, 'Ctrl+T')
# closeTabAction = menu.addAction("Close Tab",
# self.parent.close, 'Ctrl+W')
menu.addSeparator()
syntaxCheck = menu.addAction(iconSyntaxCk, "Check Syntax",
self.syntaxCheck, 'Ctrl+4')
Expand Down Expand Up @@ -333,11 +326,8 @@ def contextMenuEvent(self, e):
runSelected.setEnabled(False)
copyAction.setEnabled(False)
selectAllAction.setEnabled(False)
# closeTabAction.setEnabled(False)
undoAction.setEnabled(False)
redoAction.setEnabled(False)
# if self.parent.tw.count() > 1:
# closeTabAction.setEnabled(True)
if self.hasSelectedText():
runSelected.setEnabled(True)
copyAction.setEnabled(True)
Expand Down Expand Up @@ -622,7 +612,7 @@ def keyPressEvent(self, e):
def focusInEvent(self, e):
pathfile = self.parent.path
if pathfile:
if not os.path.exists(pathfile):
if not QFileInfo(pathfile).exists():
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>"%1"</b> has been deleted or is not accessible') \
.arg(unicode(pathfile))
Expand Down Expand Up @@ -672,7 +662,7 @@ def __init__(self, parent, parentConsole, filename, readOnly):
self.newEditor = Editor(self)
if filename:
self.path = filename
if os.path.exists(filename):
if QFileInfo(filename).exists():
self.loadFile(filename, False)

# Creates layout for message bar
Expand Down Expand Up @@ -726,7 +716,7 @@ def save(self, fileName=None):
self.pc.callWidgetMessageBarEditor(msgText, 0, True)
# Rename the original file, if it exists
path = unicode(self.path)
overwrite = os.path.exists(path)
overwrite = QFileInfo(path).exists()
if overwrite:
try:
permis = os.stat(path).st_mode
Expand All @@ -736,7 +726,7 @@ def save(self, fileName=None):
raise

temp_path = path + "~"
if os.path.exists(temp_path):
if QFileInfo(temp_path).exists():
os.remove(temp_path)
os.rename(path, temp_path)
# Save the new contents
Expand Down Expand Up @@ -1024,10 +1014,9 @@ def closeCurrentWidget(self):
self.parent.updateTabListScript(currWidget.path, action='remove')

def restoreTabs(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
for script in self.restoreTabList:
pathFile = unicode(script.toString())
if os.path.exists(pathFile):
if QFileInfo(pathFile).exists():
tabName = pathFile.split('/')[-1]
self.newTabEditor(tabName, pathFile)
else:
Expand All @@ -1038,7 +1027,6 @@ def restoreTabs(self):
s = errOnRestore
sys.stderr.write(s)
self.parent.updateTabListScript(pathFile, action='remove')
QApplication.restoreOverrideCursor()
if self.count() < 1:
self.newTabEditor(filename=None)
self.topFrame.close()
Expand Down
2 changes: 1 addition & 1 deletion python/console/console_settings.py
Expand Up @@ -181,7 +181,7 @@ 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.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI").toBool())
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True).toBool())
itemTable = settings.value("pythonConsole/userAPI").toStringList()
for i in range(len(itemTable)):
self.tableWidget.insertRow(i)
Expand Down
2 changes: 1 addition & 1 deletion python/console/console_settings.ui
Expand Up @@ -407,7 +407,7 @@
<item row="2" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>API</string>
<string>APIs</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
Expand Down

0 comments on commit 0175a80

Please sign in to comment.