Skip to content

Commit

Permalink
Use embedded messageBar in showMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 18, 2023
1 parent f2c23ad commit d798fd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
8 changes: 4 additions & 4 deletions python/console/console.py
Expand Up @@ -702,7 +702,7 @@ def saveScriptFile(self):
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>{0}</b> could not be saved. Error: {1}').format(tabWidget.path,
error.strerror)
self.callWidgetMessageBarEditor(msgText, 2, False)
self.callWidgetMessageBarEditor(msgText, Qgis.Critical)

def saveAsScriptFile(self, index=None):
tabWidget = self.tabEditorWidget.currentWidget()
Expand All @@ -729,7 +729,7 @@ def saveAsScriptFile(self, index=None):
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>{0}</b> could not be saved. Error: {1}').format(tabWidget.path,
error.strerror)
self.callWidgetMessageBarEditor(msgText, 2, False)
self.callWidgetMessageBarEditor(msgText, Qgis.Critical)
if fileNone:
tabWidget.path = None
else:
Expand Down Expand Up @@ -763,8 +763,8 @@ def updateSettings(self):
def callWidgetMessageBar(self, text):
self.shellOut.widgetMessageBar(iface, text)

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

def updateTabListScript(self, script, action=None):
if action == 'remove':
Expand Down
33 changes: 17 additions & 16 deletions python/console/console_editor.py
Expand Up @@ -52,7 +52,7 @@
QTreeWidgetItem,
QWidget,
)
from qgis.utils import OverrideCursor
from qgis.utils import OverrideCursor, iface


class Editor(QgsCodeEditorPython):
Expand Down Expand Up @@ -218,7 +218,7 @@ def findText(self, forward, showMessage=True, findFirst=False):
if showMessage:
msgText = QCoreApplication.translate('PythonConsole',
'<b>"{0}"</b> was not found.').format(text)
self.pythonconsole.callWidgetMessageBarEditor(msgText, 0, True)
self.parent.showMessage(msgText)
else:
styleError = ''
self.pythonconsole.lineEditFind.setStyleSheet(styleError)
Expand All @@ -243,7 +243,7 @@ def shareOnGist(self, is_public):
if not ACCESS_TOKEN:
msg_text = QCoreApplication.translate(
'PythonConsole', 'GitHub personal access token must be generated (see Console Options)')
self.pythonconsole.callWidgetMessageBarEditor(msg_text, 1, True)
self.parent.showMessage(msg_text, Qgis.Warning, 5)
return

URL = "https://api.github.com/gists"
Expand All @@ -270,10 +270,10 @@ def shareOnGist(self, is_public):
link = _json.object()['html_url'].toString()
QApplication.clipboard().setText(link)
msg = QCoreApplication.translate('PythonConsole', 'URL copied to clipboard.')
self.pythonconsole.callWidgetMessageBarEditor(msg, 0, True)
self.parent.showMessage(msg)
else:
msg = QCoreApplication.translate('PythonConsole', 'Connection error: ')
self.pythonconsole.callWidgetMessageBarEditor(msg + request.erroMessage(), 0, True)
self.parent.showMessage(msg + request.erroMessage(), Qgis.Warning, 5)

def hideEditor(self):
self.pythonconsole.splitterObj.hide()
Expand Down Expand Up @@ -314,7 +314,7 @@ def runScriptCode(self):
'Hey, type something to run!')
if filename is None:
if not self.isModified():
self.pythonconsole.callWidgetMessageBarEditor(msgEditorBlank, 0, True)
self.parent.showMessage(msgEditorBlank)
return

deleteTempFile = False
Expand Down Expand Up @@ -376,7 +376,7 @@ def focusInEvent(self, e):
if not QFileInfo(self.path).exists():
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>"{0}"</b> has been deleted or is not accessible').format(self.path)
self.pythonconsole.callWidgetMessageBarEditor(msgText, 2, False)
self.parent.showMessage(msgText, Qgis.Critical)
return
if self.path and self.lastModified != QFileInfo(self.path).lastModified():
self.beginUndoAction()
Expand All @@ -394,7 +394,7 @@ def fileReadOnly(self):
tabWidget = self.tabwidget.currentWidget()
msgText = QCoreApplication.translate('PythonConsole',
'The file <b>"{0}"</b> is read only, please save to different file first.').format(tabWidget.path)
self.pythonconsole.callWidgetMessageBarEditor(msgText, 1, False)
self.parent.showMessage(msgText, Qgis.Warning)

def loadFile(self, filename, readOnly=False):
self.lastModified = QFileInfo(filename).lastModified()
Expand Down Expand Up @@ -430,7 +430,7 @@ def save(self, filename=None):

msgText = QCoreApplication.translate('PythonConsole',
'Script was correctly saved.')
self.pythonconsole.callWidgetMessageBarEditor(msgText, 0, True)
self.parent.showMessage(msgText)

# Save the new contents
# Need to use newline='' to avoid adding extra \r characters on Windows
Expand Down Expand Up @@ -491,6 +491,9 @@ def keyPressEvent(self, e):

super().keyPressEvent(e)

def showMessage(self, title, text, level):
self.parent.showMessage(text, level, title=title)


class EditorTab(QWidget):

Expand Down Expand Up @@ -540,6 +543,9 @@ def __setattr__(self, name, value):
except AttributeError:
return setattr(self._editor, name, value)

def showMessage(self, text, level=Qgis.Info, timeout=-1, title=""):
self.infoBar.pushMessage(title, text, level, timeout)


class EditorTabWidget(QTabWidget):

Expand Down Expand Up @@ -922,11 +928,6 @@ def changeLastDirPath(self, tab):
if tabWidget and tabWidget.path:
self.settings.setValue("pythonConsole/lastDirPath", tabWidget.path)

def widgetMessageBar(self, iface, text, level, timed=True):
messageLevel = [Qgis.Info, Qgis.Warning, Qgis.Critical]
if timed:
timeout = iface.messageTimeout()
else:
timeout = 0
def showMessage(self, text, level=Qgis.Info, timeout=-1, title=""):
currWidget = self.currentWidget()
currWidget.infoBar.pushMessage(text, messageLevel[level], timeout)
currWidget.showMessage(text, level, timeout, title)

0 comments on commit d798fd0

Please sign in to comment.