Skip to content

Commit 41932df

Browse files
committed
[pyqgis-console] again more fixes and cleanup (for the editor)
1 parent aa078df commit 41932df

File tree

3 files changed

+79
-93
lines changed

3 files changed

+79
-93
lines changed

python/console/console.py

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def _textFindChanged(self):
539539
def onClickGoToLine(self, item, column):
540540
tabEditor = self.tabEditorWidget.currentWidget().newEditor
541541
if item.text(1) == 'syntaxError':
542-
check = self.tabEditorWidget.currentWidget().newEditor.syntaxCheck(fromContextMenu=False)
542+
check = tabEditor.syntaxCheck(fromContextMenu=False)
543543
if check and not tabEditor.isReadOnly():
544544
self.tabEditorWidget.currentWidget().save()
545545
return
@@ -589,47 +589,6 @@ def commentCode(self):
589589
def uncommentCode(self):
590590
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)
591591

592-
# def openScriptFile(self):
593-
# settings = QSettings()
594-
# lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
595-
# scriptFile = QFileDialog.getOpenFileName(
596-
# self, "Open File", lastDirPath, "Script file (*.py)")
597-
# if scriptFile.isEmpty() == False:
598-
# oF = open(scriptFile, 'r')
599-
# listScriptFile = []
600-
# for line in oF:
601-
# if line != "\n":
602-
# listScriptFile.append(line)
603-
# self.shell.insertTextFromFile(listScriptFile)
604-
#
605-
# lastDirPath = QFileInfo(scriptFile).path()
606-
# settings.setValue("pythonConsole/lastDirPath", QVariant(scriptFile))
607-
#
608-
#
609-
# def saveScriptFile(self):
610-
# scriptFile = QFileDialog()
611-
# scriptFile.setDefaultSuffix(".py")
612-
# fName = scriptFile.getSaveFileName(
613-
# self, "Save file", QString(), "Script file (*.py)")
614-
#
615-
# if fName.isEmpty() == False:
616-
# filename = str(fName)
617-
# if not filename.endswith(".py"):
618-
# fName += ".py"
619-
# sF = open(fName,'w')
620-
# listText = self.shellOut.getTextFromEditor()
621-
# is_first_line = True
622-
# for s in listText:
623-
# if s[0:3] in (">>>", "..."):
624-
# s.replace(">>> ", "").replace("... ", "")
625-
# if is_first_line:
626-
# is_first_line = False
627-
# else:
628-
# sF.write('\n')
629-
# sF.write(s)
630-
# sF.close()
631-
# self.callWidgetMessageBar('Script was correctly saved.')
632-
633592
def openScriptFile(self):
634593
settings = QSettings()
635594
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
@@ -648,18 +607,18 @@ def openScriptFile(self):
648607

649608
lastDirPath = QFileInfo(filename).path()
650609
settings.setValue("pythonConsole/lastDirPath", QVariant(filename))
651-
self.tabListScript.append(filename)
652-
self.updateTabListScript(script=None)
610+
self.updateTabListScript(filename, action='append')
653611

654612
def saveScriptFile(self):
655613
tabWidget = self.tabEditorWidget.currentWidget()
656614
try:
657615
tabWidget.save()
658-
except (IOError, OSError), e:
616+
except (IOError, OSError), error:
659617
errTr = QCoreApplication.translate("PythonConsole", "Save Error")
660-
msgErrTr = QCoreApplication.translate("PythonConsole",
661-
"Failed to save %1: %2").arg(tabWidget.path).arg(e)
662-
QMessageBox.warning(self, errTr, msgErrTr)
618+
msgText = QCoreApplication.translate('PythonConsole',
619+
'The file <b>%1</b> could not be saved. Error: %2') \
620+
.arg(unicode(tabWidget.path)).arg(error.strerror)
621+
self.callWidgetMessageBarEditor(msgText, 2, False)
663622

664623
def saveAsScriptFile(self):
665624
tabWidget = self.tabEditorWidget.currentWidget()
@@ -668,17 +627,31 @@ def saveAsScriptFile(self):
668627
return
669628
if tabWidget.path is None:
670629
pathFileName = self.tabEditorWidget.tabText(index) + '.py'
630+
fileNone = True
671631
else:
672632
pathFileName = tabWidget.path
633+
fileNone = False
673634
saveAsFileTr = QCoreApplication.translate("PythonConsole", "Save File As")
674635
filename = QFileDialog.getSaveFileName(self,
675636
saveAsFileTr,
676637
pathFileName, "Script file (*.py)")
677638
if not filename.isEmpty():
678-
#print tabWidget.path
679-
self.tabListScript.remove(unicode(tabWidget.path))
680-
tabWidget.path = filename
681-
self.saveScriptFile()
639+
try:
640+
tabWidget.save(filename)
641+
except (IOError, OSError), error:
642+
errTr = QCoreApplication.translate("PythonConsole", "Save Error")
643+
msgText = QCoreApplication.translate('PythonConsole',
644+
'The file <b>%1</b> could not be saved. Error: %2') \
645+
.arg(unicode(tabWidget.path)).arg(error.strerror)
646+
self.callWidgetMessageBarEditor(msgText, 2, False)
647+
if fileNone:
648+
tabWidget.path = None
649+
else:
650+
tabWidget.path = pathFileName
651+
return
652+
653+
if not fileNone:
654+
self.updateTabListScript(pathFileName, action='remove')
682655

683656
def openHelp(self):
684657
self.helpDlg.show()
@@ -700,13 +673,13 @@ def callWidgetMessageBarEditor(self, text, level, timed):
700673

701674
def updateTabListScript(self, script, action=None):
702675
settings = QSettings()
703-
if script == 'empty':
704-
self.tabListScript = []
705-
if script is not None and not action and script != 'empty':
676+
if action == 'remove':
706677
self.tabListScript.remove(script)
707-
if action:
678+
elif action == 'append':
708679
if script not in self.tabListScript:
709680
self.tabListScript.append(script)
681+
else:
682+
self.tabListScript = []
710683
settings.setValue("pythonConsole/tabScripts",
711684
QVariant(self.tabListScript))
712685

python/console/console_editor.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, parent=None):
7979
super(Editor,self).__init__(parent)
8080
self.parent = parent
8181
## recent modification time
82-
self.mtime = 0
82+
self.lastModified = 0
8383
self.opening = ['(', '{', '[', "'", '"']
8484
self.closing = [')', '}', ']', "'", '"']
8585

@@ -349,7 +349,7 @@ def contextMenuEvent(self, e):
349349
undoAction.setEnabled(True)
350350
if self.isRedoAvailable():
351351
redoAction.setEnabled(True)
352-
if QApplication.clipboard().text() != "":
352+
if QApplication.clipboard().text():
353353
pasteAction.setEnabled(True)
354354
action = menu.exec_(self.mapToGlobal(e.pos()))
355355

@@ -502,7 +502,7 @@ def _runSubProcess(self, filename, tmp=False):
502502
except IOError, error:
503503
IOErrorTr = QCoreApplication.translate('PythonConsole',
504504
'Cannot execute file %1. Error: %2\n') \
505-
.arg(str(filename)).arg(error.strerror)
505+
.arg(unicode(filename)).arg(error.strerror)
506506
print '## Error: ' + IOErrorTr
507507
except:
508508
s = traceback.format_exc()
@@ -624,10 +624,10 @@ def focusInEvent(self, e):
624624
if not os.path.exists(pathfile):
625625
msgText = QCoreApplication.translate('PythonConsole',
626626
'The file <b>"%1"</b> has been deleted or is not accessible') \
627-
.arg(pathfile)
627+
.arg(unicode(pathfile))
628628
self.parent.pc.callWidgetMessageBarEditor(msgText, 2, False)
629629
return
630-
if pathfile and self.mtime != os.stat(pathfile).st_mtime:
630+
if pathfile and self.lastModified != QFileInfo(pathfile).lastModified():
631631
self.beginUndoAction()
632632
self.selectAll()
633633
#fileReplaced = self.selectedText()
@@ -643,16 +643,18 @@ def focusInEvent(self, e):
643643
self.endUndoAction()
644644

645645
self.parent.tw.listObject(self.parent.tw.currentWidget())
646-
self.mtime = os.stat(pathfile).st_mtime
646+
self.lastModified = QFileInfo(pathfile).lastModified()
647647
msgText = QCoreApplication.translate('PythonConsole',
648648
'The file <b>"%1"</b> has been changed and reloaded') \
649-
.arg(pathfile)
649+
.arg(unicode(pathfile))
650650
self.parent.pc.callWidgetMessageBarEditor(msgText, 1, False)
651651
QsciScintilla.focusInEvent(self, e)
652652

653653
def fileReadOnly(self):
654+
tabWidget = self.parent.tw.currentWidget()
654655
msgText = QCoreApplication.translate('PythonConsole',
655-
'Read only file, please save to different file first.')
656+
'The file <b>"%1"</b> is read only, please save to different file first.') \
657+
.arg(unicode(tabWidget.path))
656658
self.parent.pc.callWidgetMessageBarEditor(msgText, 1, False)
657659

658660
class EditorTab(QWidget):
@@ -700,10 +702,12 @@ def loadFile(self, filename, modified):
700702
self.newEditor.setReadOnly(self.readOnly)
701703
QApplication.restoreOverrideCursor()
702704
self.newEditor.setModified(modified)
703-
self.newEditor.mtime = os.stat(filename).st_mtime
705+
self.newEditor.lastModified = QFileInfo(filename).lastModified()
704706
self.newEditor.recolor()
705707

706-
def save(self):
708+
def save(self, fileName=None):
709+
if fileName:
710+
self.path = fileName
707711
if self.path is None:
708712
index = self.tw.currentIndex()
709713
saveTr = QCoreApplication.translate('PythonConsole',
@@ -723,6 +727,13 @@ def save(self):
723727
path = unicode(self.path)
724728
overwrite = os.path.exists(path)
725729
if overwrite:
730+
try:
731+
permis = os.stat(path).st_mode
732+
#self.newEditor.lastModified = QFileInfo(path).lastModified()
733+
os.chmod(path, permis)
734+
except:
735+
raise
736+
726737
temp_path = path + "~"
727738
if os.path.exists(temp_path):
728739
os.remove(temp_path)
@@ -732,13 +743,14 @@ def save(self):
732743
f.write(self.newEditor.text())
733744
if overwrite:
734745
os.remove(temp_path)
746+
if self.newEditor.isReadOnly():
747+
self.newEditor.setReadOnly(False)
735748
fN = path.split('/')[-1]
736-
if not self.newEditor.isReadOnly():
737-
self.tw.setTabTitle(self, fN)
749+
self.tw.setTabTitle(self.tw.currentIndex(), fN)
738750
self.tw.setTabToolTip(self.tw.currentIndex(), path)
739751
self.newEditor.setModified(False)
740752
self.pc.saveFileButton.setEnabled(False)
741-
self.newEditor.mtime = os.stat(path).st_mtime
753+
self.newEditor.lastModified = QFileInfo(path).lastModified()
742754
self.pc.updateTabListScript(path, action='append')
743755
self.tw.listObject(self)
744756

@@ -865,23 +877,23 @@ def contextMenuEvent(self, e):
865877
tabBar = self.tabBar()
866878
self.idx = tabBar.tabAt(e.pos())
867879
if self.widget(self.idx):
868-
cW = self.currentWidget()
880+
cW = self.widget(self.idx)
869881
menu = QMenu(self)
870882
menu.addSeparator()
871883
newTabAction = menu.addAction("New Editor",
872-
self.newTabEditor)
884+
self.newTabEditor)
873885
menu.addSeparator()
874886
closeTabAction = menu.addAction("Close Tab",
875887
cW.close)
876888
closeAllTabAction = menu.addAction("Close All",
877-
self.closeAll)
889+
self.closeAll)
878890
closeOthersTabAction = menu.addAction("Close Others",
879-
self.closeOthers)
891+
self.closeOthers)
880892
menu.addSeparator()
881893
saveAction = menu.addAction("Save",
882-
cW.save)
894+
cW.save)
883895
saveAsAction = menu.addAction("Save As",
884-
self.parent.saveAsScriptFile)
896+
self.parent.saveAsScriptFile)
885897
closeTabAction.setEnabled(False)
886898
closeAllTabAction.setEnabled(False)
887899
closeOthersTabAction.setEnabled(False)
@@ -929,7 +941,7 @@ def newTabEditor(self, tabName=None, filename=None):
929941
except IOError, error:
930942
IOErrorTr = QCoreApplication.translate('PythonConsole',
931943
'The file %1 could not be opened. Error: %2\n') \
932-
.arg(str(filename)).arg(error.strerror)
944+
.arg(unicode(filename)).arg(error.strerror)
933945
print '## Error: '
934946
sys.stderr.write(IOErrorTr)
935947
return
@@ -961,32 +973,33 @@ def closeTab(self, tab):
961973
self.currentWidget().setFocus(Qt.TabFocusReason)
962974

963975
def setTabTitle(self, tab, title):
964-
self.setTabText(self.indexOf(tab), title)
976+
self.setTabText(tab, title)
965977

966978
def _removeTab(self, tab, tab2index=False):
967979
if tab2index:
968980
tab = self.indexOf(tab)
969-
if self.widget(tab).newEditor.isModified():
981+
tabWidget = self.widget(tab)
982+
if tabWidget.newEditor.isModified():
970983
txtSaveOnRemove = QCoreApplication.translate("PythonConsole",
971984
"Python Console: Save File")
972985
txtMsgSaveOnRemove = QCoreApplication.translate("PythonConsole",
973-
"The file <b>'%1'</b> has been modified, save changes ?").arg(self.tabText(tab))
986+
"The file <b>'%1'</b> has been modified, save changes ?") \
987+
.arg(self.tabText(tab))
974988
res = QMessageBox.question( self, txtSaveOnRemove,
975989
txtMsgSaveOnRemove,
976990
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel )
977991
if res == QMessageBox.Save:
978-
self.widget(tab).save()
992+
tabWidget.save()
979993
elif res == QMessageBox.Cancel:
980994
return
981-
else:
982-
self.parent.updateTabListScript(self.widget(tab).path)
983-
self.removeTab(tab)
984-
if self.count() <= 1:
985-
self.newTabEditor()
995+
if tabWidget.path:
996+
self.parent.updateTabListScript(tabWidget.path, action='remove')
997+
self.removeTab(tab)
998+
if self.count() < 1:
999+
self.newTabEditor()
9861000
else:
987-
if self.widget(tab).path is not None or \
988-
self.widget(tab).path in self.restoreTabList:
989-
self.parent.updateTabListScript(self.widget(tab).path)
1001+
if tabWidget.path:
1002+
self.parent.updateTabListScript(tabWidget.path, action='remove')
9901003
if self.count() <= 1:
9911004
self.removeTab(tab)
9921005
self.newTabEditor()
@@ -1005,7 +1018,7 @@ def closeCurrentWidget(self):
10051018
if currWidget:
10061019
currWidget.setFocus(Qt.TabFocusReason)
10071020
if currWidget.path in self.restoreTabList:
1008-
self.parent.updateTabListScript(currWidget.path)
1021+
self.parent.updateTabListScript(currWidget.path, action='remove')
10091022

10101023
def restoreTabs(self):
10111024
for script in self.restoreTabList:
@@ -1016,19 +1029,19 @@ def restoreTabs(self):
10161029
else:
10171030
errOnRestore = QCoreApplication.translate("PythonConsole",
10181031
"Unable to restore the file: \n%1\n") \
1019-
.arg(pathFile)
1032+
.arg(unicode(pathFile))
10201033
print '## Error: '
10211034
s = errOnRestore
10221035
sys.stderr.write(s)
1023-
self.parent.updateTabListScript(pathFile)
1036+
self.parent.updateTabListScript(pathFile, action='remove')
10241037
if self.count() < 1:
10251038
self.newTabEditor(filename=None)
10261039
self.topFrame.close()
10271040
self.enableToolBarEditor(True)
10281041
self.currentWidget().newEditor.setFocus(Qt.TabFocusReason)
10291042

10301043
def closeRestore(self):
1031-
self.parent.updateTabListScript('empty')
1044+
self.parent.updateTabListScript(None)
10321045
self.topFrame.close()
10331046
self.newTabEditor(filename=None)
10341047
self.enableToolBarEditor(True)

python/console/console_sci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def contextMenuEvent(self, e):
406406
pasteAction.setEnabled(False)
407407
if self.hasSelectedText():
408408
copyAction.setEnabled(True)
409-
if QApplication.clipboard().text() != "":
409+
if QApplication.clipboard().text():
410410
pasteAction.setEnabled(True)
411411
action = menu.exec_(self.mapToGlobal(e.pos()))
412412

0 commit comments

Comments
 (0)