Skip to content
Permalink
Browse files

[pyqgis-console] again more fixes and cleanup (for the editor)

  • Loading branch information
slarosa committed May 13, 2013
1 parent aa078df commit 41932dfcca05b0c177375e3a8ffa210255958c25
Showing with 79 additions and 93 deletions.
  1. +29 −56 python/console/console.py
  2. +49 −36 python/console/console_editor.py
  3. +1 −1 python/console/console_sci.py
@@ -539,7 +539,7 @@ def _textFindChanged(self):
def onClickGoToLine(self, item, column):
tabEditor = self.tabEditorWidget.currentWidget().newEditor
if item.text(1) == 'syntaxError':
check = self.tabEditorWidget.currentWidget().newEditor.syntaxCheck(fromContextMenu=False)
check = tabEditor.syntaxCheck(fromContextMenu=False)
if check and not tabEditor.isReadOnly():
self.tabEditorWidget.currentWidget().save()
return
@@ -589,47 +589,6 @@ def commentCode(self):
def uncommentCode(self):
self.tabEditorWidget.currentWidget().newEditor.commentEditorCode(False)

# def openScriptFile(self):
# settings = QSettings()
# lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
# scriptFile = QFileDialog.getOpenFileName(
# self, "Open File", lastDirPath, "Script file (*.py)")
# if scriptFile.isEmpty() == False:
# oF = open(scriptFile, 'r')
# listScriptFile = []
# for line in oF:
# if line != "\n":
# listScriptFile.append(line)
# self.shell.insertTextFromFile(listScriptFile)
#
# lastDirPath = QFileInfo(scriptFile).path()
# settings.setValue("pythonConsole/lastDirPath", QVariant(scriptFile))
#
#
# def saveScriptFile(self):
# scriptFile = QFileDialog()
# scriptFile.setDefaultSuffix(".py")
# fName = scriptFile.getSaveFileName(
# self, "Save file", QString(), "Script file (*.py)")
#
# if fName.isEmpty() == False:
# filename = str(fName)
# if not filename.endswith(".py"):
# fName += ".py"
# sF = open(fName,'w')
# listText = self.shellOut.getTextFromEditor()
# is_first_line = True
# for s in listText:
# if s[0:3] in (">>>", "..."):
# s.replace(">>> ", "").replace("... ", "")
# if is_first_line:
# is_first_line = False
# else:
# sF.write('\n')
# sF.write(s)
# sF.close()
# self.callWidgetMessageBar('Script was correctly saved.')

def openScriptFile(self):
settings = QSettings()
lastDirPath = settings.value("pythonConsole/lastDirPath").toString()
@@ -648,18 +607,18 @@ def openScriptFile(self):

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

def saveScriptFile(self):
tabWidget = self.tabEditorWidget.currentWidget()
try:
tabWidget.save()
except (IOError, OSError), e:
except (IOError, OSError), error:
errTr = QCoreApplication.translate("PythonConsole", "Save Error")
msgErrTr = QCoreApplication.translate("PythonConsole",
"Failed to save %1: %2").arg(tabWidget.path).arg(e)
QMessageBox.warning(self, errTr, msgErrTr)
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>%1</b> could not be saved. Error: %2') \
.arg(unicode(tabWidget.path)).arg(error.strerror)
self.callWidgetMessageBarEditor(msgText, 2, False)

def saveAsScriptFile(self):
tabWidget = self.tabEditorWidget.currentWidget()
@@ -668,17 +627,31 @@ def saveAsScriptFile(self):
return
if tabWidget.path is None:
pathFileName = self.tabEditorWidget.tabText(index) + '.py'
fileNone = True
else:
pathFileName = tabWidget.path
fileNone = False
saveAsFileTr = QCoreApplication.translate("PythonConsole", "Save File As")
filename = QFileDialog.getSaveFileName(self,
saveAsFileTr,
pathFileName, "Script file (*.py)")
if not filename.isEmpty():
#print tabWidget.path
self.tabListScript.remove(unicode(tabWidget.path))
tabWidget.path = filename
self.saveScriptFile()
try:
tabWidget.save(filename)
except (IOError, OSError), error:
errTr = QCoreApplication.translate("PythonConsole", "Save Error")
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>%1</b> could not be saved. Error: %2') \
.arg(unicode(tabWidget.path)).arg(error.strerror)
self.callWidgetMessageBarEditor(msgText, 2, False)
if fileNone:
tabWidget.path = None
else:
tabWidget.path = pathFileName
return

if not fileNone:
self.updateTabListScript(pathFileName, action='remove')

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

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

@@ -79,7 +79,7 @@ def __init__(self, parent=None):
super(Editor,self).__init__(parent)
self.parent = parent
## recent modification time
self.mtime = 0
self.lastModified = 0
self.opening = ['(', '{', '[', "'", '"']
self.closing = [')', '}', ']', "'", '"']

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

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

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

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

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

def save(self):
def save(self, fileName=None):
if fileName:
self.path = fileName
if self.path is None:
index = self.tw.currentIndex()
saveTr = QCoreApplication.translate('PythonConsole',
@@ -723,6 +727,13 @@ def save(self):
path = unicode(self.path)
overwrite = os.path.exists(path)
if overwrite:
try:
permis = os.stat(path).st_mode
#self.newEditor.lastModified = QFileInfo(path).lastModified()
os.chmod(path, permis)
except:
raise

temp_path = path + "~"
if os.path.exists(temp_path):
os.remove(temp_path)
@@ -732,13 +743,14 @@ def save(self):
f.write(self.newEditor.text())
if overwrite:
os.remove(temp_path)
if self.newEditor.isReadOnly():
self.newEditor.setReadOnly(False)
fN = path.split('/')[-1]
if not self.newEditor.isReadOnly():
self.tw.setTabTitle(self, fN)
self.tw.setTabTitle(self.tw.currentIndex(), fN)
self.tw.setTabToolTip(self.tw.currentIndex(), path)
self.newEditor.setModified(False)
self.pc.saveFileButton.setEnabled(False)
self.newEditor.mtime = os.stat(path).st_mtime
self.newEditor.lastModified = QFileInfo(path).lastModified()
self.pc.updateTabListScript(path, action='append')
self.tw.listObject(self)

@@ -865,23 +877,23 @@ def contextMenuEvent(self, e):
tabBar = self.tabBar()
self.idx = tabBar.tabAt(e.pos())
if self.widget(self.idx):
cW = self.currentWidget()
cW = self.widget(self.idx)
menu = QMenu(self)
menu.addSeparator()
newTabAction = menu.addAction("New Editor",
self.newTabEditor)
self.newTabEditor)
menu.addSeparator()
closeTabAction = menu.addAction("Close Tab",
cW.close)
closeAllTabAction = menu.addAction("Close All",
self.closeAll)
self.closeAll)
closeOthersTabAction = menu.addAction("Close Others",
self.closeOthers)
self.closeOthers)
menu.addSeparator()
saveAction = menu.addAction("Save",
cW.save)
cW.save)
saveAsAction = menu.addAction("Save As",
self.parent.saveAsScriptFile)
self.parent.saveAsScriptFile)
closeTabAction.setEnabled(False)
closeAllTabAction.setEnabled(False)
closeOthersTabAction.setEnabled(False)
@@ -929,7 +941,7 @@ def newTabEditor(self, tabName=None, filename=None):
except IOError, error:
IOErrorTr = QCoreApplication.translate('PythonConsole',
'The file %1 could not be opened. Error: %2\n') \
.arg(str(filename)).arg(error.strerror)
.arg(unicode(filename)).arg(error.strerror)
print '## Error: '
sys.stderr.write(IOErrorTr)
return
@@ -961,32 +973,33 @@ def closeTab(self, tab):
self.currentWidget().setFocus(Qt.TabFocusReason)

def setTabTitle(self, tab, title):
self.setTabText(self.indexOf(tab), title)
self.setTabText(tab, title)

def _removeTab(self, tab, tab2index=False):
if tab2index:
tab = self.indexOf(tab)
if self.widget(tab).newEditor.isModified():
tabWidget = self.widget(tab)
if tabWidget.newEditor.isModified():
txtSaveOnRemove = QCoreApplication.translate("PythonConsole",
"Python Console: Save File")
txtMsgSaveOnRemove = QCoreApplication.translate("PythonConsole",
"The file <b>'%1'</b> has been modified, save changes ?").arg(self.tabText(tab))
"The file <b>'%1'</b> has been modified, save changes ?") \
.arg(self.tabText(tab))
res = QMessageBox.question( self, txtSaveOnRemove,
txtMsgSaveOnRemove,
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel )
if res == QMessageBox.Save:
self.widget(tab).save()
tabWidget.save()
elif res == QMessageBox.Cancel:
return
else:
self.parent.updateTabListScript(self.widget(tab).path)
self.removeTab(tab)
if self.count() <= 1:
self.newTabEditor()
if tabWidget.path:
self.parent.updateTabListScript(tabWidget.path, action='remove')
self.removeTab(tab)
if self.count() < 1:
self.newTabEditor()
else:
if self.widget(tab).path is not None or \
self.widget(tab).path in self.restoreTabList:
self.parent.updateTabListScript(self.widget(tab).path)
if tabWidget.path:
self.parent.updateTabListScript(tabWidget.path, action='remove')
if self.count() <= 1:
self.removeTab(tab)
self.newTabEditor()
@@ -1005,7 +1018,7 @@ def closeCurrentWidget(self):
if currWidget:
currWidget.setFocus(Qt.TabFocusReason)
if currWidget.path in self.restoreTabList:
self.parent.updateTabListScript(currWidget.path)
self.parent.updateTabListScript(currWidget.path, action='remove')

def restoreTabs(self):
for script in self.restoreTabList:
@@ -1016,19 +1029,19 @@ def restoreTabs(self):
else:
errOnRestore = QCoreApplication.translate("PythonConsole",
"Unable to restore the file: \n%1\n") \
.arg(pathFile)
.arg(unicode(pathFile))
print '## Error: '
s = errOnRestore
sys.stderr.write(s)
self.parent.updateTabListScript(pathFile)
self.parent.updateTabListScript(pathFile, action='remove')
if self.count() < 1:
self.newTabEditor(filename=None)
self.topFrame.close()
self.enableToolBarEditor(True)
self.currentWidget().newEditor.setFocus(Qt.TabFocusReason)

def closeRestore(self):
self.parent.updateTabListScript('empty')
self.parent.updateTabListScript(None)
self.topFrame.close()
self.newTabEditor(filename=None)
self.enableToolBarEditor(True)
@@ -406,7 +406,7 @@ def contextMenuEvent(self, e):
pasteAction.setEnabled(False)
if self.hasSelectedText():
copyAction.setEnabled(True)
if QApplication.clipboard().text() != "":
if QApplication.clipboard().text():
pasteAction.setEnabled(True)
action = menu.exec_(self.mapToGlobal(e.pos()))

0 comments on commit 41932df

Please sign in to comment.
You can’t perform that action at this time.