Skip to content

Commit

Permalink
[pyqgis-console] warning the user if the file has been modified by an…
Browse files Browse the repository at this point in the history
…other process

- set fixed size for corner widget icons
- reorganized message levels for the message bar
- minor fixes
  • Loading branch information
slarosa committed May 1, 2013
1 parent b5042df commit 334f43e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
4 changes: 2 additions & 2 deletions python/console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ def prefChanged(self):
def callWidgetMessageBar(self, text):
self.shellOut.widgetMessageBar(iface, text)

def callWidgetMessageBarEditor(self, text):
self.tabEditorWidget.widgetMessageBar(iface, text)
def callWidgetMessageBarEditor(self, text, level, timed):
self.tabEditorWidget.widgetMessageBar(iface, text, level, timed)

def updateTabListScript(self, script, action=None):
if script != '':
Expand Down
106 changes: 78 additions & 28 deletions python/console/console_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class Editor(QsciScintilla):
def __init__(self, parent=None):
super(Editor,self).__init__(parent)
self.parent = parent

## recent modification time
self.mtime = 0
self.settings = QSettings()

# Enable non-ascii chars for editor
Expand Down Expand Up @@ -167,6 +168,7 @@ def __init__(self, parent=None):
self.uncommentScut = QShortcut(QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_3), self)
self.uncommentScut.setContext(Qt.WidgetShortcut)
self.uncommentScut.activated.connect(self.parent.pc.uncommentCode)
self.modificationChanged.connect(self.parent.modified)

def settingsEditor(self):
# Set Python lexer
Expand Down Expand Up @@ -320,7 +322,7 @@ def contextMenuEvent(self, e):
closeTabAction.setEnabled(False)
undoAction.setEnabled(False)
redoAction.setEnabled(False)
if self.parent.mw.count() > 1:
if self.parent.tw.count() > 1:
closeTabAction.setEnabled(True)
if self.hasSelectedText():
runSelected.setEnabled(True)
Expand Down Expand Up @@ -370,10 +372,10 @@ def codepad(self):
if link:
QApplication.clipboard().setText(link)
msgText = QCoreApplication.translate('PythonConsole', 'URL copied to clipboard.')
self.parent.pc.callWidgetMessageBarEditor(msgText)
self.parent.pc.callWidgetMessageBarEditor(msgText, 0, True)
except urllib2.URLError, e:
msgText = QCoreApplication.translate('PythonConsole', 'Connection error: ')
self.parent.pc.callWidgetMessageBarEditor(msgText + str(e.args))
self.parent.pc.callWidgetMessageBarEditor(msgText + str(e.args), 0, True)

def hideEditor(self):
self.parent.pc.splitterObj.hide()
Expand Down Expand Up @@ -464,7 +466,7 @@ def _runSubProcess(self, filename, tmp=False):
def runScriptCode(self):
autoSave = self.settings.value("pythonConsole/autoSaveScript").toBool()

tabWidget = self.parent.mw.currentWidget()
tabWidget = self.parent.tw.currentWidget()
filename = tabWidget.path

msgEditorBlank = QCoreApplication.translate('PythonConsole',
Expand All @@ -474,12 +476,13 @@ def runScriptCode(self):
if not autoSave:
if filename is None:
if not self.isModified():
self.parent.pc.callWidgetMessageBarEditor(msgEditorBlank)
self.parent.pc.callWidgetMessageBarEditor(msgEditorBlank, 0, True)
return
else:
self.parent.pc.callWidgetMessageBarEditor(msgEditorUnsaved)
self.parent.pc.callWidgetMessageBarEditor(msgEditorUnsaved, 0, True)
return
if self.isModified():
self.parent.pc.callWidgetMessageBarEditor(msgEditorUnsaved)
self.parent.pc.callWidgetMessageBarEditor(msgEditorUnsaved, 0, True)
return
else:
self._runSubProcess(filename)
Expand Down Expand Up @@ -509,24 +512,48 @@ def goToLine(self, objName, linenr):
self.ensureLineVisible(linenr)
self.setFocus()

def focusInEvent(self, e):
pathfile = self.parent.path
if pathfile:
if not os.path.exists(pathfile): return
if pathfile and self.mtime != os.stat(pathfile).st_mtime:
self.beginUndoAction()
self.selectAll()
#fileReplaced = self.selectedText()
self.removeSelectedText()
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
file = open(pathfile, "r").readlines()
except IOError, error:
print 'The file %s could not be opened. Error: %s' % (pathfile, error.strerror)
for line in reversed(file):
self.insert(line)
QApplication.restoreOverrideCursor()
self.setModified(True)
self.endUndoAction()

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

QsciScintilla.focusInEvent(self, e)

class EditorTab(QWidget):
def __init__(self, parent, parentConsole, filename, *args):
QWidget.__init__(self, parent=None, *args)
self.mw = parent
self.tw = parent
self.pc = parentConsole
self.path = None

self.fileExcuteList = {}
self.fileExcuteList = dict()

self.newEditor = Editor(self)
self.newEditor.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.newEditor.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.newEditor.modificationChanged.connect(self.modified)
if filename:
self.newEditor.setText(open(unicode(filename), "r").read())
self.newEditor.setModified(False)
self.path = filename
if os.path.exists(filename):
self.loadFile(filename, False)

# Creates layout for message bar
self.layout = QGridLayout(self.newEditor)
Expand All @@ -545,11 +572,25 @@ def __init__(self, parent, parentConsole, filename, *args):

self.keyFilter = KeyFilter(parent, self)
self.setEventFilter(self.keyFilter)

def loadFile(self, filename, modified):
try:
fn = open(unicode(filename), "rb")
except IOError, error:
print 'The file <b>%s</b> could not be opened. Error: %s' % (filename, error.strerror)
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
txt = fn.read()
fn.close()
self.newEditor.setText(txt)
QApplication.restoreOverrideCursor()
self.newEditor.setModified(modified)
self.newEditor.mtime = os.stat(filename).st_mtime
self.newEditor.recolor()

def save(self):
if self.path is None:
self.path = str(QFileDialog().getSaveFileName(self,
"Save file",
"Python Console: Save file",
"*.py",
"Script file (*.py)"))
# If the user didn't select a file, abort the save operation
Expand All @@ -558,7 +599,7 @@ def save(self):
return
msgText = QCoreApplication.translate('PythonConsole',
'Script was correctly saved.')
self.pc.callWidgetMessageBarEditor(msgText)
self.pc.callWidgetMessageBarEditor(msgText, 0, True)
# Rename the original file, if it exists
path = unicode(self.path)
overwrite = os.path.exists(path)
Expand All @@ -573,23 +614,24 @@ def save(self):
if overwrite:
os.remove(temp_path)
fN = path.split('/')[-1]
self.mw.setTabTitle(self, fN)
self.mw.setTabToolTip(self.mw.currentIndex(), path)
self.tw.setTabTitle(self, fN)
self.tw.setTabToolTip(self.tw.currentIndex(), path)
self.newEditor.setModified(False)
self.newEditor.mtime = os.stat(path).st_mtime
self.pc.updateTabListScript(path, action='append')
self.mw.listObject(self)
self.tw.listObject(self)

def modified(self, modified):
self.mw.tabModified(self, modified)
self.tw.tabModified(self, modified)

def close(self):
self.mw._removeTab(self, tab2index=True)
self.tw._removeTab(self, tab2index=True)

def setEventFilter(self, filter):
self.newEditor.installEventFilter(filter)

def newTab(self):
self.mw.newTabEditor()
self.tw.newTabEditor()

class EditorTabWidget(QTabWidget):
def __init__(self, parent):
Expand All @@ -616,6 +658,7 @@ def __init__(self, parent):
"Restore tabs")
self.restoreTabsButton.setToolTip(toolTipRestore)
self.restoreTabsButton.setIcon(QgsApplication.getThemeIcon("console/iconRestoreTabsConsole.png"))
self.restoreTabsButton.setIconSize(QSize(24, 24))
self.restoreTabsButton.setAutoRaise(True)
self.restoreTabsButton.setCursor(Qt.PointingHandCursor)
self.restoreTabsButton.setStyleSheet('QToolButton:hover{border: none } \
Expand Down Expand Up @@ -670,6 +713,7 @@ def __init__(self, parent):
self.fileTabButton = QToolButton(self)
self.fileTabButton.setToolTip('List all tabs')
self.fileTabButton.setIcon(QgsApplication.getThemeIcon("console/iconFileTabsMenuConsole.png"))
self.fileTabButton.setIconSize(QSize(24, 24))
self.fileTabButton.setAutoRaise(True)
self.fileTabButton.setPopupMode(QToolButton.InstantPopup)
self.fileTabButton.setMenu(self.fileTabMenu)
Expand All @@ -683,6 +727,7 @@ def __init__(self, parent):
self.newTabButton.setToolTip('New Tab')
self.newTabButton.setAutoRaise(True)
self.newTabButton.setIcon(QgsApplication.getThemeIcon("console/iconNewTabEditorConsole.png"))
self.newTabButton.setIconSize(QSize(24, 24))
self.setCornerWidget(self.newTabButton, Qt.TopLeftCorner)
self.connect(self.newTabButton, SIGNAL('clicked()'), self.newTabEditor)

Expand Down Expand Up @@ -736,8 +781,8 @@ def _removeTab(self, tab, tab2index=False):
if tab2index:
tab = self.indexOf(tab)
if self.widget(tab).newEditor.isModified():
res = QMessageBox.question( self, 'Save Script',
'The script "%s" has been modified, save changes ?'
res = QMessageBox.question( self, 'Python Console: Save File',
'The file <b>"%s"</b> has been modified, save changes ?'
% self.tabText(tab),
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel )
if res == QMessageBox.Save:
Expand All @@ -755,7 +800,7 @@ def _removeTab(self, tab, tab2index=False):
self.removeTab(tab)
else:
self.removeTab(tab)
self.currentWidget().setFocus(Qt.TabFocusReason)
self.currentWidget().newEditor.setFocus(Qt.TabFocusReason)

def buttonClosePressed(self):
self.closeCurrentWidget()
Expand Down Expand Up @@ -787,6 +832,7 @@ def restoreTabs(self):
self.newTabEditor(filename=None)
self.topFrame.close()
self.enableToolBarEditor(True)
self.currentWidget().newEditor.setFocus(Qt.TabFocusReason)

def closeRestore(self):
self.parent.updateTabListScript('empty')
Expand Down Expand Up @@ -884,7 +930,11 @@ def changeLastDirPath(self, tab):
tabWidget = self.widget(tab)
self.settings.setValue("pythonConsole/lastDirPath", QVariant(tabWidget.path))

def widgetMessageBar(self, iface, text):
timeout = iface.messageTimeout()
def widgetMessageBar(self, iface, text, level, timed=True):
messageLevel = [QgsMessageBar.INFO, QgsMessageBar.WARNING, QgsMessageBar.CRITICAL]
if timed:
timeout = iface.messageTimeout()
else:
timeout = 0
currWidget = self.currentWidget()
currWidget.infoBar.pushMessage(text, QgsMessageBar.INFO, timeout)
currWidget.infoBar.pushMessage(text, messageLevel[level], timeout)

0 comments on commit 334f43e

Please sign in to comment.