Skip to content

Commit 0175a80

Browse files
committed
[pyqgis-console] uses QFileInfo instead of os.path to check if file exists
- minor fixes and cleanup
1 parent 02ab027 commit 0175a80

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

python/console/console.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def __init__(self, parent=None):
9090
QWidget.__init__(self, parent)
9191
self.setWindowTitle(QCoreApplication.translate("PythonConsole", "Python Console"))
9292

93+
self.settings = QSettings()
94+
9395
self.options = optionsDialog(self)
9496
self.helpDlg = HelpDialog(self)
9597

@@ -242,7 +244,8 @@ def __init__(self, parent=None):
242244
objList = QCoreApplication.translate("PythonConsole", "Object Inspector")
243245
self.objectListButton = QAction(self)
244246
self.objectListButton.setCheckable(True)
245-
self.objectListButton.setEnabled(True)
247+
self.objectListButton.setEnabled(self.settings.value("pythonConsole/enableObjectInsp",
248+
False).toBool())
246249
self.objectListButton.setIcon(QgsApplication.getThemeIcon("console/iconClassBrowserConsole.png"))
247250
self.objectListButton.setMenuRole(QAction.PreferencesRole)
248251
self.objectListButton.setIconVisibleInMenu(True)
@@ -467,7 +470,7 @@ def __init__(self, parent=None):
467470
self.layoutFind.setContentsMargins(0, 0, 0, 0)
468471
self.lineEditFind = QgsFilterLineEdit()
469472
placeHolderTxt = QCoreApplication.translate("PythonConsole", "Enter text to find...")
470-
473+
471474
if pyqtconfig.Configuration().qt_version >= 0x40700:
472475
self.lineEditFind.setPlaceholderText(placeHolderTxt)
473476
else:
@@ -596,8 +599,7 @@ def uncommentCode(self):
596599
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)
597600

598601
def openScriptFile(self):
599-
settings = QSettings()
600-
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
602+
lastDirPath = self.settings.value("pythonConsole/lastDirPath").toString()
601603
openFileTr = QCoreApplication.translate("PythonConsole", "Open File")
602604
filename = QFileDialog.getOpenFileName(
603605
self, openFileTr, lastDirPath, "Script file (*.py)")
@@ -612,7 +614,7 @@ def openScriptFile(self):
612614
self.tabEditorWidget.newTabEditor(tabName, filename)
613615

614616
lastDirPath = QFileInfo(filename).path()
615-
settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
617+
self.settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
616618
self.updateTabListScript(filename, action='append')
617619

618620
def saveScriptFile(self):
@@ -678,33 +680,27 @@ def callWidgetMessageBarEditor(self, text, level, timed):
678680
self.tabEditorWidget.widgetMessageBar(iface, text, level, timed)
679681

680682
def updateTabListScript(self, script, action=None):
681-
settings = QSettings()
682683
if action == 'remove':
683684
self.tabListScript.remove(script)
684685
elif action == 'append':
685686
if script not in self.tabListScript:
686687
self.tabListScript.append(script)
687688
else:
688689
self.tabListScript = []
689-
settings.setValue("pythonConsole/tabScripts",
690+
self.settings.setValue("pythonConsole/tabScripts",
690691
QVariant(self.tabListScript))
691692

692693
def saveSettingsConsole(self):
693-
settings = QSettings()
694-
#settings.setValue("pythonConsole/geometry", self.saveGeometry())
695-
settings.setValue("pythonConsole/splitterObj", self.splitterObj.saveState())
696-
settings.setValue("pythonConsole/splitterEditor", self.splitterEditor.saveState())
694+
self.settings.setValue("pythonConsole/splitterObj", self.splitterObj.saveState())
695+
self.settings.setValue("pythonConsole/splitterEditor", self.splitterEditor.saveState())
697696

698697
self.shell.writeHistoryFile()
699698

700699
def restoreSettingsConsole(self):
701-
# List for tab script
702-
settings = QSettings()
703-
storedTabScripts = settings.value("pythonConsole/tabScripts")
700+
storedTabScripts = self.settings.value("pythonConsole/tabScripts")
704701
self.tabListScript = storedTabScripts.toList()
705-
#self.restoreGeometry(settings.value("pythonConsole/geometry").toByteArray())
706-
self.splitterEditor.restoreState(settings.value("pythonConsole/splitterEditor").toByteArray())
707-
self.splitterObj.restoreState(settings.value("pythonConsole/splitterObj").toByteArray())
702+
self.splitterEditor.restoreState(self.settings.value("pythonConsole/splitterEditor").toByteArray())
703+
self.splitterObj.restoreState(self.settings.value("pythonConsole/splitterObj").toByteArray())
708704

709705
if __name__ == '__main__':
710706
a = QApplication(sys.argv)

python/console/console_editor.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,13 @@ def contextMenuEvent(self, e):
266266
iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png")
267267
iconRunScript = QgsApplication.getThemeIcon("console/iconRunScriptConsole.png")
268268
iconCodePad = QgsApplication.getThemeIcon("console/iconCodepadConsole.png")
269-
#iconNewEditor = QgsApplication.getThemeIcon("console/iconTabEditorConsole.png")
270269
iconCommentEditor = QgsApplication.getThemeIcon("console/iconCommentEditorConsole.png")
271270
iconUncommentEditor = QgsApplication.getThemeIcon("console/iconUncommentEditorConsole.png")
272271
iconSettings = QgsApplication.getThemeIcon("console/iconSettingsConsole.png")
273272
iconFind = QgsApplication.getThemeIcon("console/iconSearchEditorConsole.png")
274273
iconSyntaxCk = QgsApplication.getThemeIcon("console/iconSyntaxErrorConsole.png")
275274
hideEditorAction = menu.addAction("Hide Editor",
276275
self.hideEditor)
277-
# menu.addSeparator()
278-
# newTabAction = menu.addAction(iconNewEditor,
279-
# "New Tab",
280-
# self.parent.newTab, 'Ctrl+T')
281-
# closeTabAction = menu.addAction("Close Tab",
282-
# self.parent.close, 'Ctrl+W')
283276
menu.addSeparator()
284277
syntaxCheck = menu.addAction(iconSyntaxCk, "Check Syntax",
285278
self.syntaxCheck, 'Ctrl+4')
@@ -333,11 +326,8 @@ def contextMenuEvent(self, e):
333326
runSelected.setEnabled(False)
334327
copyAction.setEnabled(False)
335328
selectAllAction.setEnabled(False)
336-
# closeTabAction.setEnabled(False)
337329
undoAction.setEnabled(False)
338330
redoAction.setEnabled(False)
339-
# if self.parent.tw.count() > 1:
340-
# closeTabAction.setEnabled(True)
341331
if self.hasSelectedText():
342332
runSelected.setEnabled(True)
343333
copyAction.setEnabled(True)
@@ -622,7 +612,7 @@ def keyPressEvent(self, e):
622612
def focusInEvent(self, e):
623613
pathfile = self.parent.path
624614
if pathfile:
625-
if not os.path.exists(pathfile):
615+
if not QFileInfo(pathfile).exists():
626616
msgText = QCoreApplication.translate('PythonConsole',
627617
'The file <b>"%1"</b> has been deleted or is not accessible') \
628618
.arg(unicode(pathfile))
@@ -672,7 +662,7 @@ def __init__(self, parent, parentConsole, filename, readOnly):
672662
self.newEditor = Editor(self)
673663
if filename:
674664
self.path = filename
675-
if os.path.exists(filename):
665+
if QFileInfo(filename).exists():
676666
self.loadFile(filename, False)
677667

678668
# Creates layout for message bar
@@ -726,7 +716,7 @@ def save(self, fileName=None):
726716
self.pc.callWidgetMessageBarEditor(msgText, 0, True)
727717
# Rename the original file, if it exists
728718
path = unicode(self.path)
729-
overwrite = os.path.exists(path)
719+
overwrite = QFileInfo(path).exists()
730720
if overwrite:
731721
try:
732722
permis = os.stat(path).st_mode
@@ -736,7 +726,7 @@ def save(self, fileName=None):
736726
raise
737727

738728
temp_path = path + "~"
739-
if os.path.exists(temp_path):
729+
if QFileInfo(temp_path).exists():
740730
os.remove(temp_path)
741731
os.rename(path, temp_path)
742732
# Save the new contents
@@ -1024,10 +1014,9 @@ def closeCurrentWidget(self):
10241014
self.parent.updateTabListScript(currWidget.path, action='remove')
10251015

10261016
def restoreTabs(self):
1027-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
10281017
for script in self.restoreTabList:
10291018
pathFile = unicode(script.toString())
1030-
if os.path.exists(pathFile):
1019+
if QFileInfo(pathFile).exists():
10311020
tabName = pathFile.split('/')[-1]
10321021
self.newTabEditor(tabName, pathFile)
10331022
else:
@@ -1038,7 +1027,6 @@ def restoreTabs(self):
10381027
s = errOnRestore
10391028
sys.stderr.write(s)
10401029
self.parent.updateTabListScript(pathFile, action='remove')
1041-
QApplication.restoreOverrideCursor()
10421030
if self.count() < 1:
10431031
self.newTabEditor(filename=None)
10441032
self.topFrame.close()

python/console/console_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def restoreSettings(self):
181181
settings = QSettings()
182182
self.spinBox.setValue(settings.value("pythonConsole/fontsize", 10).toInt()[0])
183183
self.spinBoxEditor.setValue(settings.value("pythonConsole/fontsizeEditor", 10).toInt()[0])
184-
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI").toBool())
184+
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True).toBool())
185185
itemTable = settings.value("pythonConsole/userAPI").toStringList()
186186
for i in range(len(itemTable)):
187187
self.tableWidget.insertRow(i)

python/console/console_settings.ui

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
<item row="2" column="0">
408408
<widget class="QGroupBox" name="groupBox_2">
409409
<property name="title">
410-
<string>API</string>
410+
<string>APIs</string>
411411
</property>
412412
<layout class="QGridLayout" name="gridLayout_3">
413413
<item row="0" column="0">

0 commit comments

Comments
 (0)