Use object registry for Prompt in Prompter.
This commit is contained in:
parent
8cb6ba01e3
commit
507354c8d1
@ -55,6 +55,7 @@ class Prompt(QWidget):
|
|||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
objreg.register('prompt', self)
|
||||||
self._hbox = QHBoxLayout(self)
|
self._hbox = QHBoxLayout(self)
|
||||||
self._hbox.setContentsMargins(0, 0, 0, 0)
|
self._hbox.setContentsMargins(0, 0, 0, 0)
|
||||||
self._hbox.setSpacing(5)
|
self._hbox.setSpacing(5)
|
||||||
|
@ -60,7 +60,6 @@ class Prompter:
|
|||||||
question: A Question object with the question to be asked to the user.
|
question: A Question object with the question to be asked to the user.
|
||||||
_loops: A list of local EventLoops to spin in when blocking.
|
_loops: A list of local EventLoops to spin in when blocking.
|
||||||
_queue: A deque of waiting questions.
|
_queue: A deque of waiting questions.
|
||||||
_prompt: The associated Prompt widget.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, prompt):
|
def __init__(self, prompt):
|
||||||
@ -68,7 +67,6 @@ class Prompter:
|
|||||||
self._loops = []
|
self._loops = []
|
||||||
self._queue = collections.deque()
|
self._queue = collections.deque()
|
||||||
self._busy = False
|
self._busy = False
|
||||||
self._prompt = prompt
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<{}>'.format(self.__class__.__name__)
|
return '<{}>'.format(self.__class__.__name__)
|
||||||
@ -87,11 +85,12 @@ class Prompter:
|
|||||||
"""Get a PromptContext based on the current state."""
|
"""Get a PromptContext based on the current state."""
|
||||||
if not self._busy:
|
if not self._busy:
|
||||||
return None
|
return None
|
||||||
|
prompt = objreg.get('prompt')
|
||||||
ctx = PromptContext(question=self.question,
|
ctx = PromptContext(question=self.question,
|
||||||
text=self._prompt.txt.text(),
|
text=prompt.txt.text(),
|
||||||
input_text=self._prompt.lineedit.text(),
|
input_text=prompt.lineedit.text(),
|
||||||
echo_mode=self._prompt.lineedit.echoMode(),
|
echo_mode=prompt.lineedit.echoMode(),
|
||||||
input_visible=self._prompt.lineedit.isVisible())
|
input_visible=prompt.lineedit.isVisible())
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
def _restore_ctx(self, ctx):
|
def _restore_ctx(self, ctx):
|
||||||
@ -103,15 +102,16 @@ class Prompter:
|
|||||||
Return: True if a context was restored, False otherwise.
|
Return: True if a context was restored, False otherwise.
|
||||||
"""
|
"""
|
||||||
log.statusbar.debug("Restoring context {}".format(ctx))
|
log.statusbar.debug("Restoring context {}".format(ctx))
|
||||||
|
prompt = objreg.get('prompt')
|
||||||
if ctx is None:
|
if ctx is None:
|
||||||
self._prompt.hide_prompt.emit()
|
prompt.hide_prompt.emit()
|
||||||
self._busy = False
|
self._busy = False
|
||||||
return False
|
return False
|
||||||
self.question = ctx.question
|
self.question = ctx.question
|
||||||
self._prompt.txt.setText(ctx.text)
|
prompt.txt.setText(ctx.text)
|
||||||
self._prompt.lineedit.setText(ctx.input_text)
|
prompt.lineedit.setText(ctx.input_text)
|
||||||
self._prompt.lineedit.setEchoMode(ctx.echo_mode)
|
prompt.lineedit.setEchoMode(ctx.echo_mode)
|
||||||
self._prompt.lineedit.setVisible(ctx.input_visible)
|
prompt.lineedit.setVisible(ctx.input_visible)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _display_question(self):
|
def _display_question(self):
|
||||||
@ -123,6 +123,7 @@ class Prompter:
|
|||||||
Raise:
|
Raise:
|
||||||
ValueError if the set PromptMode is invalid.
|
ValueError if the set PromptMode is invalid.
|
||||||
"""
|
"""
|
||||||
|
prompt = objreg.get('prompt')
|
||||||
if self.question.mode == usertypes.PromptMode.yesno:
|
if self.question.mode == usertypes.PromptMode.yesno:
|
||||||
if self.question.default is None:
|
if self.question.default is None:
|
||||||
suffix = ""
|
suffix = ""
|
||||||
@ -130,29 +131,29 @@ class Prompter:
|
|||||||
suffix = " (yes)"
|
suffix = " (yes)"
|
||||||
else:
|
else:
|
||||||
suffix = " (no)"
|
suffix = " (no)"
|
||||||
self._prompt.txt.setText(self.question.text + suffix)
|
prompt.txt.setText(self.question.text + suffix)
|
||||||
self._prompt.lineedit.hide()
|
prompt.lineedit.hide()
|
||||||
mode = usertypes.KeyMode.yesno
|
mode = usertypes.KeyMode.yesno
|
||||||
elif self.question.mode == usertypes.PromptMode.text:
|
elif self.question.mode == usertypes.PromptMode.text:
|
||||||
self._prompt.txt.setText(self.question.text)
|
prompt.txt.setText(self.question.text)
|
||||||
if self.question.default:
|
if self.question.default:
|
||||||
self._prompt.lineedit.setText(self.question.default)
|
prompt.lineedit.setText(self.question.default)
|
||||||
self._prompt.lineedit.show()
|
prompt.lineedit.show()
|
||||||
mode = usertypes.KeyMode.prompt
|
mode = usertypes.KeyMode.prompt
|
||||||
elif self.question.mode == usertypes.PromptMode.user_pwd:
|
elif self.question.mode == usertypes.PromptMode.user_pwd:
|
||||||
self._prompt.txt.setText(self.question.text)
|
prompt.txt.setText(self.question.text)
|
||||||
if self.question.default:
|
if self.question.default:
|
||||||
self._prompt.lineedit.setText(self.question.default)
|
prompt.lineedit.setText(self.question.default)
|
||||||
self._prompt.lineedit.show()
|
prompt.lineedit.show()
|
||||||
mode = usertypes.KeyMode.prompt
|
mode = usertypes.KeyMode.prompt
|
||||||
elif self.question.mode == usertypes.PromptMode.alert:
|
elif self.question.mode == usertypes.PromptMode.alert:
|
||||||
self._prompt.txt.setText(self.question.text + ' (ok)')
|
prompt.txt.setText(self.question.text + ' (ok)')
|
||||||
self._prompt.lineedit.hide()
|
prompt.lineedit.hide()
|
||||||
mode = usertypes.KeyMode.prompt
|
mode = usertypes.KeyMode.prompt
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid prompt mode!")
|
raise ValueError("Invalid prompt mode!")
|
||||||
self._prompt.lineedit.setFocus()
|
prompt.lineedit.setFocus()
|
||||||
self._prompt.show_prompt.emit()
|
prompt.show_prompt.emit()
|
||||||
self._busy = True
|
self._busy = True
|
||||||
return mode
|
return mode
|
||||||
|
|
||||||
@ -176,11 +177,12 @@ class Prompter:
|
|||||||
@pyqtSlot(usertypes.KeyMode)
|
@pyqtSlot(usertypes.KeyMode)
|
||||||
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."""
|
||||||
|
prompt = objreg.get('prompt')
|
||||||
if mode in (usertypes.KeyMode.prompt, usertypes.KeyMode.yesno):
|
if mode in (usertypes.KeyMode.prompt, usertypes.KeyMode.yesno):
|
||||||
self._prompt.txt.setText('')
|
prompt.txt.setText('')
|
||||||
self._prompt.lineedit.clear()
|
prompt.lineedit.clear()
|
||||||
self._prompt.lineedit.setEchoMode(QLineEdit.Normal)
|
prompt.lineedit.setEchoMode(QLineEdit.Normal)
|
||||||
self._prompt.hide_prompt.emit()
|
prompt.hide_prompt.emit()
|
||||||
self._busy = False
|
self._busy = False
|
||||||
if self.question.answer is None and not self.question.is_aborted:
|
if self.question.answer is None and not self.question.is_aborted:
|
||||||
self.question.cancel()
|
self.question.cancel()
|
||||||
@ -196,22 +198,23 @@ class Prompter:
|
|||||||
This executes the next action depending on the question mode, e.g. asks
|
This executes the next action depending on the question mode, e.g. asks
|
||||||
for the password or leaves the mode.
|
for the password or leaves the mode.
|
||||||
"""
|
"""
|
||||||
|
prompt = objreg.get('prompt')
|
||||||
if (self.question.mode == usertypes.PromptMode.user_pwd and
|
if (self.question.mode == usertypes.PromptMode.user_pwd and
|
||||||
self.question.user is None):
|
self.question.user is None):
|
||||||
# User just entered an username
|
# User just entered an username
|
||||||
self.question.user = self._prompt.lineedit.text()
|
self.question.user = prompt.lineedit.text()
|
||||||
self._prompt.txt.setText("Password:")
|
prompt.txt.setText("Password:")
|
||||||
self._prompt.lineedit.clear()
|
prompt.lineedit.clear()
|
||||||
self._prompt.lineedit.setEchoMode(QLineEdit.Password)
|
prompt.lineedit.setEchoMode(QLineEdit.Password)
|
||||||
elif self.question.mode == usertypes.PromptMode.user_pwd:
|
elif self.question.mode == usertypes.PromptMode.user_pwd:
|
||||||
# User just entered a password
|
# User just entered a password
|
||||||
password = self._prompt.lineedit.text()
|
password = prompt.lineedit.text()
|
||||||
self.question.answer = (self.question.user, password)
|
self.question.answer = (self.question.user, password)
|
||||||
modeman.leave(usertypes.KeyMode.prompt, 'prompt accept')
|
modeman.leave(usertypes.KeyMode.prompt, 'prompt accept')
|
||||||
self.question.done()
|
self.question.done()
|
||||||
elif self.question.mode == usertypes.PromptMode.text:
|
elif self.question.mode == usertypes.PromptMode.text:
|
||||||
# User just entered text.
|
# User just entered text.
|
||||||
self.question.answer = self._prompt.lineedit.text()
|
self.question.answer = prompt.lineedit.text()
|
||||||
modeman.leave(usertypes.KeyMode.prompt, 'prompt accept')
|
modeman.leave(usertypes.KeyMode.prompt, 'prompt accept')
|
||||||
self.question.done()
|
self.question.done()
|
||||||
elif self.question.mode == usertypes.PromptMode.yesno:
|
elif self.question.mode == usertypes.PromptMode.yesno:
|
||||||
|
Loading…
Reference in New Issue
Block a user