Create generate_password_textbox

This commit is contained in:
Mizaki 2023-05-16 00:25:12 +01:00
parent 5996bd3588
commit 057725c5da

View File

@ -41,19 +41,6 @@ def generate_api_widgets(
else:
QtWidgets.QMessageBox.warning(None, "API Test Failed", check_text)
visibleIcon = QtGui.QIcon(str(graphics_path / "eye.svg"))
hiddenIcon = QtGui.QIcon(str(graphics_path / "hidden.svg"))
def show_key(le_key: QtWidgets.QLineEdit, le_action: QtWidgets.QAction) -> None:
current_state = le_key.echoMode()
if current_state == 0:
le_key.setEchoMode(QtWidgets.QLineEdit.EchoMode.PasswordEchoOnEdit)
le_action.setIcon(visibleIcon)
else:
le_key.setEchoMode(QtWidgets.QLineEdit.EchoMode.Normal)
le_action.setIcon(hiddenIcon)
# get the actual config objects in case they have overwritten the default
talker_key = config[1][f"talker_{talker_id}"][1][f"{talker_id}_key"]
talker_url = config[1][f"talker_{talker_id}"][1][f"{talker_id}_url"]
@ -65,14 +52,7 @@ def generate_api_widgets(
if talker_key.file:
# record the current row so we know where to add the button
btn_test_row = layout.rowCount()
le_key = generate_textbox(talker_key, layout)
# Generate show/hide icon for key/password within the text box
le_key.setEchoMode(QtWidgets.QLineEdit.EchoMode.PasswordEchoOnEdit)
le_key_toggle = le_key.addAction(visibleIcon, QtWidgets.QLineEdit.TrailingPosition)
le_key_toggle.setToolTip("Show/Hide")
le_key_toggle.triggered.connect(partial(show_key, le_key=le_key, le_action=le_key_toggle))
le_key = generate_password_textbox(talker_key, layout)
# To enable setting and getting
sources["tabs"][talker_id].widgets[f"talker_{talker_id}_{talker_id}_key"] = le_key
@ -140,6 +120,51 @@ def generate_textbox(option: settngs.Setting, layout: QtWidgets.QGridLayout) ->
return widget
def generate_password_textbox(option: settngs.Setting, layout: QtWidgets.QGridLayout) -> QtWidgets.QLineEdit:
class PasswordEdit(QtWidgets.QLineEdit):
"""
Password LineEdit with icons to show/hide password entries.
Taken from https://github.com/pythonguis/python-qtwidgets/tree/master/qtwidgets
Based on this example https://kushaldas.in/posts/creating-password-input-widget-in-pyqt.html by Kushal Das.
"""
def __init__(self, show_visibility=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.visibleIcon = QtGui.QIcon(str(graphics_path / "eye.svg"))
self.hiddenIcon = QtGui.QIcon(str(graphics_path / "hidden.svg"))
self.setEchoMode(QtWidgets.QLineEdit.Password)
if show_visibility:
# Add the password hide/shown toggle at the end of the edit box.
self.togglepasswordAction = self.addAction(self.visibleIcon, QtWidgets.QLineEdit.TrailingPosition)
self.togglepasswordAction.setToolTip("Show/Hide")
self.togglepasswordAction.triggered.connect(self.on_toggle_password_Action)
self.password_shown = False
def on_toggle_password_Action(self):
if not self.password_shown:
self.setEchoMode(QtWidgets.QLineEdit.Normal)
self.password_shown = True
self.togglepasswordAction.setIcon(self.hiddenIcon)
else:
self.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_shown = False
self.togglepasswordAction.setIcon(self.visibleIcon)
row = layout.rowCount()
lbl = QtWidgets.QLabel(option.display_name)
lbl.setToolTip(option.help)
layout.addWidget(lbl, row, 0)
widget = PasswordEdit()
widget.setToolTip(option.help)
layout.addWidget(widget, row, 1)
return widget
def settings_to_talker_form(sources: dict[str, QtWidgets.QWidget], config: settngs.Config[settngs.Namespace]) -> None:
# Set the active talker via id in sources combo box
sources["cbx_select_talker"].setCurrentIndex(sources["cbx_select_talker"].findData(config[0].talker_source))