From 355c0c52d0148789d09f8ea146f068313d30d386 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Wed, 4 Sep 2024 15:31:11 -0700 Subject: [PATCH] Improve "driver messages" dialog Jamming a hundred errors into a MessageBox is not scalable and looks pretty dumb after a few. This makes it into a proper dialog that shows the first line of each and lets the user select each for better readability. Related to #11519 --- chirp/wxui/common.py | 54 ++++++++++++++++++++++++++++++++++++++------ chirp/wxui/main.py | 5 ++-- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/chirp/wxui/common.py b/chirp/wxui/common.py index 33cce0eec..e735a5612 100644 --- a/chirp/wxui/common.py +++ b/chirp/wxui/common.py @@ -22,7 +22,6 @@ import platform import shutil import tempfile -import textwrap import threading import wx @@ -711,8 +710,51 @@ def temporary_debug_log(): return dst +class MultiErrorDialog(wx.Dialog): + def __init__(self, *args, **kw): + super().__init__(*args, **kw) + vbox = wx.BoxSizer(wx.VERTICAL) + self.SetSizer(vbox) + + self.choices = [] + self.choice_box = wx.ListBox(self) + vbox.Add(self.choice_box, border=10, proportion=0, + flag=wx.EXPAND | wx.ALL) + + self.message = wx.TextCtrl(self) + self.message.SetEditable(False) + vbox.Add(self.message, border=10, proportion=1, + flag=wx.EXPAND | wx.ALL) + + buttons = self.CreateButtonSizer(wx.OK) + vbox.Add(buttons, border=10, flag=wx.ALL) + + self.Bind(wx.EVT_BUTTON, self._button) + self.choice_box.Bind(wx.EVT_LISTBOX, self._selected) + + self.SetMinSize((600, 400)) + self.Fit() + self.Center() + + def _button(self, event): + self.EndModal(wx.ID_OK) + + def select(self, index): + error = self.choices[index] + self.message.SetValue('%s in %s:\n%s' % ( + error.levelname, error.module, error.getMessage())) + + def _selected(self, event): + self.select(event.GetInt()) + + def set_errors(self, errors): + self.choices = errors + self.choice_box.Set([x.getMessage() for x in self.choices]) + self.select(0) + + @contextlib.contextmanager -def expose_logs(level, root, label, maxlen=128): +def expose_logs(level, root, label, maxlen=128, parent=None): if not isinstance(root, tuple): root = (root,) @@ -725,9 +767,7 @@ def expose_logs(level, root, label, maxlen=128): lines = list(itertools.chain.from_iterable(x.get_history() for x in histories)) if lines: - msg = os.linesep.join(textwrap.shorten(x.getMessage(), maxlen) - for x in lines) - d = wx.MessageDialog( - None, str(msg), label, - style=wx.OK | wx.ICON_INFORMATION) + d = MultiErrorDialog(parent) + d.SetTitle(label) + d.set_errors(lines) d.ShowModal() diff --git a/chirp/wxui/main.py b/chirp/wxui/main.py index 81b481fa3..54bb91a4f 100644 --- a/chirp/wxui/main.py +++ b/chirp/wxui/main.py @@ -551,7 +551,8 @@ def open_file(self, filename, exists=True, select=True, rclass=None): self.enable_bugreport() CSVRadio = directory.get_radio('Generic_CSV') label = _('Driver messages') - with common.expose_logs(logging.WARNING, 'chirp.drivers', label): + with common.expose_logs(logging.WARNING, 'chirp.drivers', label, + parent=self): if exists: if not os.path.exists(filename): raise FileNotFoundError( @@ -1356,7 +1357,7 @@ def _menu_import(self, event): r = d.ShowModal() if r == wx.ID_YES: with common.expose_logs(logging.WARNING, 'chirp.drivers', - _('Import messages')): + _('Import messages'), parent=self): radio = directory.get_radio_by_image(filename) self.current_editorset.current_editor.memedit_import_all(radio) elif r == wx.ID_NO: