Skip to content

Commit

Permalink
Merge pull request #3955 from t20100/silx-view-no-error-messagebox
Browse files Browse the repository at this point in the history
silx view: Stopped displaying a message box for each error
  • Loading branch information
t20100 authored Oct 25, 2023
2 parents df8c9da + 9516630 commit 3666465
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/silx/app/view/Viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# ############################################################################*/
"""Browse a data file with a GUI"""

from __future__ import annotations

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "15/01/2019"
Expand All @@ -30,6 +32,8 @@
import os
import logging
import functools
import traceback
from types import TracebackType
from typing import Optional

import silx.io.nxdata
Expand Down Expand Up @@ -64,6 +68,8 @@ def __init__(self, parent=None, settings=None):
silxIcon = icons.getQIcon("silx")
self.setWindowIcon(silxIcon)

self.__error = ""

self.__context = self.createApplicationContext(settings)
self.__context.restoreLibrarySettings()

Expand Down Expand Up @@ -743,6 +749,14 @@ def createMenus(self):
helpMenu.addAction(self._aboutAction)
helpMenu.addAction(self._documentationAction)

self.__errorButton = qt.QToolButton(self)
self.__errorButton.setIcon(
self.style().standardIcon(qt.QStyle.SP_MessageBoxWarning))
self.__errorButton.setToolTip("An error occured!\nClick to display last error\nor check messages in the console")
self.__errorButton.setVisible(False)
self.__errorButton.clicked.connect(self.__errorButtonClicked)
self.menuBar().setCornerWidget(self.__errorButton)

def open(self):
dialog = self.createFileDialog()
if self.__dialogState is None:
Expand Down Expand Up @@ -950,3 +964,31 @@ def customContextMenu(self, event):
action = qt.QAction("Synchronize %s" % obj.local_filename, event.source())
action.triggered.connect(lambda: self.__synchronizeH5pyObject(h5))
menu.addAction(action)

def __errorButtonClicked(self):
button = qt.QMessageBox.warning(
self,
"Error",
self.getError(),
qt.QMessageBox.Reset | qt.QMessageBox.Close,
qt.QMessageBox.Close,
)
if button == qt.QMessageBox.Reset:
self.setError("")

def getError(self) -> str:
"""Returns error information string"""
return self.__error

def setError(self, error: str):
"""Set error information string"""
if error == self.__error:
return

self.__error = error
self.__errorButton.setVisible(error != "")

def setErrorFromException(self, type_: type[BaseException], value: BaseException, trace: TracebackType):
"""Set information about the last exception that occured"""
formattedTrace = '\n'.join(traceback.format_tb(trace))
self.setError(f"{type_.__name__}:\n{value}\n\n{formattedTrace}")
12 changes: 11 additions & 1 deletion src/silx/app/view/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import os
import signal
import sys
import traceback
from silx.app.utils import parseutils


Expand Down Expand Up @@ -136,7 +137,6 @@ def sigintHandler(*args):
qt.QApplication.quit()

signal.signal(signal.SIGINT, sigintHandler)
sys.excepthook = qt.exceptionHandler

timer = qt.QTimer()
timer.start(500)
Expand All @@ -155,6 +155,16 @@ def sigintHandler(*args):
window = createWindow(parent=None, settings=settings)
window.setAttribute(qt.Qt.WA_DeleteOnClose, True)

def exceptHook(type_, value, trace):
_logger.error("An error occured in silx view:")
_logger.error("%s %s %s", type_, value, ''.join(traceback.format_tb(trace)))
try:
window.setErrorFromException(type_, value, trace)
except Exception:
pass
sys.excepthook = exceptHook


if options.use_opengl_plot:
# It have to be done after the settings (after the Viewer creation)
silx.config.DEFAULT_PLOT_BACKEND = "opengl"
Expand Down

0 comments on commit 3666465

Please sign in to comment.