Skip to content
Permalink
Browse files

Fix annoying blocking Python error dialog

  • Loading branch information
NathanW2 committed Sep 28, 2015
1 parent c7961b1 commit 6cf2dd0a4c9fa5b29e89bd1274ea7166005e5492
@@ -168,6 +168,8 @@ class QgisInterface : QObject
/** Return the message bar of the main app */
virtual QgsMessageBar * messageBar() = 0;

virtual void openMessageLog() = 0;

/** Adds a widget to the user input tool bar.*/
virtual void addUserInputWidget( QWidget* widget ) = 0;

@@ -29,7 +29,9 @@
"""

from PyQt4.QtCore import QCoreApplication, QLocale
from PyQt4.QtGui import QPushButton
from qgis.core import QGis, QgsExpression, QgsMessageLog, qgsfunction
from qgis.gui import QgsMessageBar

import sys
import traceback
@@ -60,7 +62,7 @@ def showWarning(message, category, filename, lineno, file=None, line=None):
warnings.showwarning = showWarning


def showException(type, value, tb, msg):
def showException(type, value, tb, msg, messagebar=False):
lst = traceback.format_exception(type, value, tb)
if msg is None:
msg = QCoreApplication.translate('Python', 'An error has occured while executing Python code:')
@@ -77,14 +79,27 @@ def showException(type, value, tb, msg):

from qgis.core import QgsMessageOutput

msg = QgsMessageOutput.createMessageOutput()
msg.setTitle(QCoreApplication.translate('Python', 'Python error'))
msg.setMessage(txt, QgsMessageOutput.MessageHtml)
msg.showMessage()
title = QCoreApplication.translate('Python', 'Python error')
logmessage = ''
for s in lst:
logmessage += s.decode('utf-8', 'replace')

QgsMessageLog.logMessage(logmessage, title)

if messagebar and iface:
widget = iface.messageBar().createMessage(title, msg + " See message log (Python Error) for more details.")
button = QPushButton("View message log", pressed=iface.openMessageLog)
widget.layout().addWidget(button)
iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)
else:
msg = QgsMessageOutput.createMessageOutput()
msg.setTitle()
msg.setMessage(txt, QgsMessageOutput.MessageHtml)
msg.showMessage()


def qgis_excepthook(type, value, tb):
showException(type, value, tb, None)
showException(type, value, tb, None, messagebar=True)


def installErrorHook():
@@ -197,9 +212,9 @@ def loadPlugin(packageName):
__import__(packageName)
return True
except:
msgTemplate = QCoreApplication.translate("Python", "Couldn't load plugin '%s' from ['%s']")
msg = msgTemplate % (packageName, "', '".join(sys.path))
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
msgTemplate = QCoreApplication.translate("Python", "Couldn't load plugin '%s'")
msg = msgTemplate % packageName
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
return False


@@ -223,7 +238,7 @@ def startPlugin(packageName):
except:
_unloadPluginModules(packageName)
msg = QCoreApplication.translate("Python", "%s due to an error when calling its classFactory() method") % errMsg
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
return False

# initGui
@@ -233,7 +248,7 @@ def startPlugin(packageName):
del plugins[packageName]
_unloadPluginModules(packageName)
msg = QCoreApplication.translate("Python", "%s due to an error when calling its initGui() method") % errMsg
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
return False

# add to active plugins
@@ -260,7 +275,7 @@ def canUninstallPlugin(packageName):
return bool(metadata.canBeUninstalled())
except:
msg = "Error calling " + packageName + ".canBeUninstalled"
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
return True


@@ -281,7 +296,7 @@ def unloadPlugin(packageName):
return True
except Exception as e:
msg = QCoreApplication.translate("Python", "Error while unloading plugin %s") % packageName
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
return False


@@ -2577,6 +2577,11 @@ void QgisApp::toggleLogMessageIcon( bool hasLogMessage )
}
}

void QgisApp::openMessageLog()
{
mMessageButton->toggle();
}

void QgisApp::addUserInputWidget( QWidget *widget )
{
mUserInputDockWidget->addUserInputWidget( widget );
@@ -8421,9 +8426,6 @@ void QgisApp::closeProject()
QgsPythonRunner::run( "qgis.utils.unloadProjectMacros();" );
}

// remove any message widgets from the message bar
mInfoBar->clearWidgets();

mTrustedMacros = false;

setFilterLegendByMapEnabled( false );
@@ -201,6 +201,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
/** Return the messageBar object which allows displaying unobtrusive messages to the user.*/
QgsMessageBar *messageBar();

/** Open the message log dock widget **/
void openMessageLog();

/** Adds a widget to the user input tool bar.*/
void addUserInputWidget( QWidget* widget );

@@ -326,6 +326,12 @@ QgsMessageBar * QgisAppInterface::messageBar()
return qgis->messageBar();
}

void QgisAppInterface::openMessageLog()
{
qgis->openMessageLog();
}


void QgisAppInterface::addUserInputWidget( QWidget *widget )
{
qgis->addUserInputWidget( widget );
@@ -184,6 +184,9 @@ class APP_EXPORT QgisAppInterface : public QgisInterface

QgsMessageBar * messageBar() override;

/** Open the message log dock widget **/
void openMessageLog() override;

/** Adds a widget to the user input tool bar.*/
void addUserInputWidget( QWidget* widget ) override;

@@ -219,6 +219,9 @@ class GUI_EXPORT QgisInterface : public QObject
/** Return the message bar of the main app */
virtual QgsMessageBar * messageBar() = 0;

/** Open the message log dock widget **/
virtual void openMessageLog() = 0;

/** Adds a widget to the user input tool bar.*/
virtual void addUserInputWidget( QWidget* widget ) = 0;

0 comments on commit 6cf2dd0

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