Skip to content

Commit

Permalink
Fix annoying blocking Python error dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Sep 28, 2015
1 parent c7961b1 commit 6cf2dd0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
2 changes: 2 additions & 0 deletions python/gui/qgisinterface.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 28 additions & 13 deletions python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:')
Expand All @@ -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():
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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


Expand All @@ -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


Expand Down
8 changes: 5 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,11 @@ void QgisApp::toggleLogMessageIcon( bool hasLogMessage )
}
}

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

void QgisApp::addUserInputWidget( QWidget *widget )
{
mUserInputDockWidget->addUserInputWidget( widget );
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisappinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ QgsMessageBar * QgisAppInterface::messageBar()
return qgis->messageBar();
}

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


void QgisAppInterface::addUserInputWidget( QWidget *widget )
{
qgis->addUserInputWidget( widget );
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisappinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgisinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6cf2dd0

Please sign in to comment.