Make it possible to cancel a message.question

This commit is contained in:
Florian Bruhin 2014-06-12 17:49:36 +02:00
parent dc0b025055
commit c91dced99f
3 changed files with 10 additions and 11 deletions

View File

@ -88,13 +88,15 @@ def alert(message):
instance().question.emit(q, True) instance().question.emit(q, True)
def question(message, mode, handler, default=None): def question(message, mode, handler, cancelled_handler=None, default=None):
"""Ask an async question in the statusbar. """Ask an async question in the statusbar.
Args: Args:
message: The message to display to the user. message: The message to display to the user.
mode: A PromptMode. mode: A PromptMode.
handler: The function to get called with the answer as argument. handler: The function to get called with the answer as argument.
cancelled_handler: The function to get called when the prompt was
cancelled by the user, or None.
default: The default value to display. default: The default value to display.
""" """
q = Question() q = Question()
@ -102,6 +104,8 @@ def question(message, mode, handler, default=None):
q.mode = mode q.mode = mode
q.default = default q.default = default
q.answered.connect(lambda: handler(q.answer)) q.answered.connect(lambda: handler(q.answer))
if cancelled_handler is not None:
q.cancelled.connect(cancelled_handler)
instance().question.emit(q, True) instance().question.emit(q, True)

View File

@ -255,6 +255,7 @@ class Question(QObject):
answered: Emitted when the question has been answered by the user. answered: Emitted when the question has been answered by the user.
This is emitted from qutebrowser.widgets.statusbar._prompt so This is emitted from qutebrowser.widgets.statusbar._prompt so
it can be emitted after the mode is left. it can be emitted after the mode is left.
cancelled: Emitted when the question has been cancelled by the user.
answered_yes: Convienience signal emitted when a yesno question was answered_yes: Convienience signal emitted when a yesno question was
answered with yes. answered with yes.
answered_no: Convienience signal emitted when a yesno question was answered_no: Convienience signal emitted when a yesno question was
@ -262,6 +263,7 @@ class Question(QObject):
""" """
answered = pyqtSignal() answered = pyqtSignal()
cancelled = pyqtSignal()
answered_yes = pyqtSignal() answered_yes = pyqtSignal()
answered_no = pyqtSignal() answered_no = pyqtSignal()

View File

@ -41,12 +41,10 @@ class Prompt(QWidget):
Signals: Signals:
show_prompt: Emitted when the prompt widget wants to be shown. show_prompt: Emitted when the prompt widget wants to be shown.
hide_prompt: Emitted when the prompt widget wants to be hidden. hide_prompt: Emitted when the prompt widget wants to be hidden.
cancelled: Emitted when the prompt was cancelled by the user.
""" """
show_prompt = pyqtSignal() show_prompt = pyqtSignal()
hide_prompt = pyqtSignal() hide_prompt = pyqtSignal()
cancelled = pyqtSignal()
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
@ -65,19 +63,14 @@ class Prompt(QWidget):
self._hbox.addWidget(self._input) self._hbox.addWidget(self._input)
def on_mode_left(self, mode): def on_mode_left(self, mode):
"""Clear and reset input when the mode was left. """Clear and reset input when the mode was left."""
Emit:
cancelled: Emitted when the mode was forcibly left by the user
without answering the question.
"""
if mode in ('prompt', 'yesno'): if mode in ('prompt', 'yesno'):
self._txt.setText('') self._txt.setText('')
self._input.clear() self._input.clear()
self._input.setEchoMode(QLineEdit.Normal) self._input.setEchoMode(QLineEdit.Normal)
self.hide_prompt.emit() self.hide_prompt.emit()
if self.question.answer is None: if self.question.answer is None:
self.cancelled.emit() self.question.cancelled.emit()
@cmdutils.register(instance='mainwindow.status.prompt', hide=True, @cmdutils.register(instance='mainwindow.status.prompt', hide=True,
modes=['prompt']) modes=['prompt'])
@ -206,6 +199,6 @@ class Prompt(QWidget):
The answer to the question. No, it's not always 42. The answer to the question. No, it's not always 42.
""" """
self.question.answered.connect(self.loop.quit) self.question.answered.connect(self.loop.quit)
self.cancelled.connect(self.loop.quit) self.question.cancelled.connect(self.loop.quit)
self.loop.exec_() self.loop.exec_()
return self.question.answer return self.question.answer