Allow MessagePopup to store data with the undo URL

This commit is contained in:
Kovid Goyal 2023-04-12 11:24:41 +05:30
parent 7a5176e1b4
commit 7fe6b7dfe5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 5 deletions

View File

@ -825,7 +825,7 @@ class Boss(QObject):
# }}} # }}}
# Global history {{{ # Global history {{{
def do_global_undo(self): def do_global_undo(self, *a):
container = self.global_undo.undo() container = self.global_undo.undo()
if container is not None: if container is not None:
set_current_container(container) set_current_container(container)

View File

@ -262,7 +262,7 @@ def install_new_plugins():
class MessagePopup(QLabel): class MessagePopup(QLabel):
undo_requested = pyqtSignal() undo_requested = pyqtSignal(object)
def __init__(self, parent): def __init__(self, parent):
QLabel.__init__(self, parent) QLabel.__init__(self, parent)
@ -294,12 +294,16 @@ class MessagePopup(QLabel):
def link_activated(self, link): def link_activated(self, link):
self.hide() self.hide()
if link.startswith('undo://'): if link.startswith('undo://'):
self.undo_requested.emit() import base64, json
data = base64.urlsafe_b64decode(link.rpartition('/')[-1])
self.undo_requested.emit(json.loads(data))
def __call__(self, text='Testing message popup', show_undo=True, timeout=5000, has_markup=False): def __call__(self, text='Testing message popup', show_undo=True, timeout=5000, has_markup=False):
text = '<p>' + (text if has_markup else prepare_string_for_xml(text)) text = '<p>' + (text if has_markup else prepare_string_for_xml(text))
if show_undo: if show_undo:
text += '\xa0\xa0<a style="text-decoration: none" href="undo://me.com">{}</a>'.format(_('Undo')) import base64, json
data = base64.urlsafe_b64encode(json.dumps(show_undo).encode('utf-8')).decode('ascii')
text += '\xa0\xa0<a style="text-decoration: none" href="undo://me.com/{}">{}</a>'.format(data, _('Undo'))
text += f'\xa0\xa0<a style="text-decoration: none; color: {self.color}" href="close://me.com">✖</a>' text += f'\xa0\xa0<a style="text-decoration: none; color: {self.color}" href="close://me.com">✖</a>'
self.setText(text) self.setText(text)
self.resize(self.sizeHint()) self.resize(self.sizeHint())
@ -317,7 +321,7 @@ class Main(MainWindow):
APP_NAME = _('Edit book') APP_NAME = _('Edit book')
STATE_VERSION = 0 STATE_VERSION = 0
undo_requested = pyqtSignal() undo_requested = pyqtSignal(object)
def __init__(self, opts, notify=None): def __init__(self, opts, notify=None):
MainWindow.__init__(self, opts, disable_automatic_gc=True) MainWindow.__init__(self, opts, disable_automatic_gc=True)