From b9fca03ed10ffbfc9bbecbf09dce893901175275 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 3 Mar 2021 18:15:38 +0200 Subject: [PATCH] Prepare newchandialog module for PyQt5/qtpy --- src/bitmessageqt/addressvalidator.py | 177 +++++++++++++++------------ src/bitmessageqt/newchandialog.py | 84 +++++++------ 2 files changed, 144 insertions(+), 117 deletions(-) diff --git a/src/bitmessageqt/addressvalidator.py b/src/bitmessageqt/addressvalidator.py index 89c17891..0b969173 100644 --- a/src/bitmessageqt/addressvalidator.py +++ b/src/bitmessageqt/addressvalidator.py @@ -1,14 +1,16 @@ """ -Address validator module. +The validator for address and passphrase QLineEdits +used in `.dialogs.NewChanDialog`. """ -# pylint: disable=too-many-branches,too-many-arguments +# pylint: disable=too-many-arguments -from PyQt4 import QtGui from Queue import Empty +from PyQt5 import QtGui + from account import getSortedAccounts from addresses import decodeAddress, addBMIfNotPresent -from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue +from queues import addressGeneratorQueue, apiAddressGeneratorReturnQueue from tr import _translate from utils import str_chan @@ -16,22 +18,18 @@ from utils import str_chan class AddressPassPhraseValidatorMixin(object): """Bitmessage address or passphrase validator class for Qt UI""" def setParams( - self, - passPhraseObject=None, - addressObject=None, - feedBackObject=None, - buttonBox=None, - addressMandatory=True, + self, passPhraseObject=None, addressObject=None, + feedBackObject=None, button=None, addressMandatory=True ): - """Initialisation""" + """Initialization""" self.addressObject = addressObject self.passPhraseObject = passPhraseObject self.feedBackObject = feedBackObject - self.buttonBox = buttonBox self.addressMandatory = addressMandatory self.isValid = False # save default text - self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text() + self.okButton = button + self.okButtonLabel = button.text() def setError(self, string): """Indicate that the validation is pending or failed""" @@ -42,13 +40,13 @@ class AddressPassPhraseValidatorMixin(object): self.feedBackObject.setStyleSheet("QLabel { color : red; }") self.feedBackObject.setText(string) self.isValid = False - if self.buttonBox: - self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False) + if self.okButton: + self.okButton.setEnabled(False) if string is not None and self.feedBackObject is not None: - self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText( + self.okButton.setText( _translate("AddressValidator", "Invalid")) else: - self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText( + self.okButton.setText( _translate("AddressValidator", "Validating...")) def setOK(self, string): @@ -60,9 +58,9 @@ class AddressPassPhraseValidatorMixin(object): self.feedBackObject.setStyleSheet("QLabel { }") self.feedBackObject.setText(string) self.isValid = True - if self.buttonBox: - self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True) - self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel) + if self.okButton: + self.okButton.setEnabled(True) + self.okButton.setText(self.okButtonLabel) def checkQueue(self): """Validator queue loop""" @@ -75,7 +73,8 @@ class AddressPassPhraseValidatorMixin(object): while True: try: - addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False) + addressGeneratorReturnValue = \ + apiAddressGeneratorReturnQueue.get(False) except Empty: if gotOne: break @@ -85,96 +84,120 @@ class AddressPassPhraseValidatorMixin(object): gotOne = True if not addressGeneratorReturnValue: - self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) - return (QtGui.QValidator.Intermediate, 0) - if addressGeneratorReturnValue[0] == 'chan name does not match address': - self.setError( - _translate( - "AddressValidator", - "Although the Bitmessage address you " - "entered was valid, it doesn't match the chan name.")) - return (QtGui.QValidator.Intermediate, 0) - self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid.")) + self.setError(_translate( + "AddressValidator", + "Address already present as one of your identities." + )) + return + if addressGeneratorReturnValue[0] == \ + 'chan name does not match address': + self.setError(_translate( + "AddressValidator", + "Although the Bitmessage address you entered was valid," + " it doesn\'t match the chan name." + )) + return + self.setOK(_translate( + "MainWindow", "Passphrase and address appear to be valid.")) def returnValid(self): """Return the value of whether the validation was successful""" - if self.isValid: - return QtGui.QValidator.Acceptable - return QtGui.QValidator.Intermediate + return QtGui.QValidator.Acceptable if self.isValid \ + else QtGui.QValidator.Intermediate def validate(self, s, pos): """Top level validator method""" - if self.addressObject is None: + try: + address = self.addressObject.text().encode('utf-8') + except AttributeError: address = None - else: - address = str(self.addressObject.text().toUtf8()) - if address == "": - address = None - if self.passPhraseObject is None: + try: + passPhrase = self.passPhraseObject.text().encode('utf-8') + except AttributeError: passPhrase = "" - else: - passPhrase = str(self.passPhraseObject.text().toUtf8()) - if passPhrase == "": - passPhrase = None # no chan name - if passPhrase is None: - self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name.")) - return (QtGui.QValidator.Intermediate, pos) + if not passPhrase: + self.setError(_translate( + "AddressValidator", + "Chan name/passphrase needed. You didn't enter a chan name." + )) + return (QtGui.QValidator.Intermediate, s, pos) - if self.addressMandatory or address is not None: + if self.addressMandatory or address: # check if address already exists: if address in getSortedAccounts(): - self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) - return (QtGui.QValidator.Intermediate, pos) + self.setError(_translate( + "AddressValidator", + "Address already present as one of your identities." + )) + return (QtGui.QValidator.Intermediate, s, pos) + status = decodeAddress(address)[0] # version too high - if decodeAddress(address)[0] == 'versiontoohigh': - self.setError( - _translate( - "AddressValidator", - "Address too new. Although that Bitmessage" - " address might be valid, its version number" - " is too new for us to handle. Perhaps you need" - " to upgrade Bitmessage.")) - return (QtGui.QValidator.Intermediate, pos) - + if status == 'versiontoohigh': + self.setError(_translate( + "AddressValidator", + "Address too new. Although that Bitmessage address" + " might be valid, its version number is too new" + " for us to handle. Perhaps you need to upgrade" + " Bitmessage." + )) + return (QtGui.QValidator.Intermediate, s, pos) # invalid - if decodeAddress(address)[0] != 'success': - self.setError(_translate("AddressValidator", "The Bitmessage address is not valid.")) - return (QtGui.QValidator.Intermediate, pos) + if status != 'success': + self.setError(_translate( + "AddressValidator", + "The Bitmessage address is not valid." + )) + return (QtGui.QValidator.Intermediate, s, pos) # this just disables the OK button without changing the feedback text # but only if triggered by textEdited, not by clicking the Ok button - if not self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus(): + if not self.okButton.hasFocus(): self.setError(None) # check through generator - if address is None: - addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False)) + if not address: + addressGeneratorQueue.put(( + 'createChan', 4, 1, + str_chan + ' ' + passPhrase, passPhrase, False + )) else: - addressGeneratorQueue.put( - ('joinChan', addBMIfNotPresent(address), - "{} {}".format(str_chan, passPhrase), passPhrase, False)) + addressGeneratorQueue.put(( + 'joinChan', addBMIfNotPresent(address), + "{} {}".format(str_chan, passPhrase), passPhrase, False + )) - if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus(): - return (self.returnValid(), pos) - return (QtGui.QValidator.Intermediate, pos) + if self.okButton.hasFocus(): + return (self.returnValid(), s, pos) + else: + return (QtGui.QValidator.Intermediate, s, pos) def checkData(self): """Validator Qt signal interface""" - return self.validate("", 0) + return self.validate(u"", 0) class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): """AddressValidator class for Qt UI""" - def __init__(self, parent=None, passPhraseObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True): + def __init__( + self, parent=None, passPhraseObject=None, feedBackObject=None, + button=None, addressMandatory=True + ): super(AddressValidator, self).__init__(parent) - self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory) + self.setParams( + passPhraseObject, parent, feedBackObject, button, + addressMandatory) class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): """PassPhraseValidator class for Qt UI""" - def __init__(self, parent=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=False): + def __init__( + self, parent=None, addressObject=None, feedBackObject=None, + button=None, addressMandatory=False + ): super(PassPhraseValidator, self).__init__(parent) - self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory) + self.setParams( + parent, addressObject, feedBackObject, button, + addressMandatory) diff --git a/src/bitmessageqt/newchandialog.py b/src/bitmessageqt/newchandialog.py index c0629cd7..f4a89eea 100644 --- a/src/bitmessageqt/newchandialog.py +++ b/src/bitmessageqt/newchandialog.py @@ -1,10 +1,8 @@ """ -src/bitmessageqt/newchandialog.py -================================= - +NewChanDialog class definition """ -from PyQt4 import QtCore, QtGui +from PyQt5 import QtCore, QtWidgets import widgets from addresses import addBMIfNotPresent @@ -15,30 +13,21 @@ from tr import _translate from utils import str_chan -class NewChanDialog(QtGui.QDialog): - """The `New Chan` dialog""" +class NewChanDialog(QtWidgets.QDialog): + """The "New Chan" dialog""" def __init__(self, parent=None): super(NewChanDialog, self).__init__(parent) widgets.load('newchandialog.ui', self) self.parent = parent - self.chanAddress.setValidator( - AddressValidator( - self.chanAddress, - self.chanPassPhrase, - self.validatorFeedback, - self.buttonBox, - False)) - self.chanPassPhrase.setValidator( - PassPhraseValidator( - self.chanPassPhrase, - self.chanAddress, - self.validatorFeedback, - self.buttonBox, - False)) + self.chanAddress.setValidator(AddressValidator( + self.chanAddress, self.chanPassPhrase, self.validatorFeedback, + self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False)) + self.chanPassPhrase.setValidator(PassPhraseValidator( + self.chanPassPhrase, self.chanAddress, self.validatorFeedback, + self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False)) self.timer = QtCore.QTimer() - QtCore.QObject.connect( # pylint: disable=no-member - self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus) + self.timer.timeout.connect(self.delayedUpdateStatus) self.timer.start(500) # milliseconds self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.show() @@ -52,32 +41,47 @@ class NewChanDialog(QtGui.QDialog): self.timer.stop() self.hide() apiAddressGeneratorReturnQueue.queue.clear() - if self.chanAddress.text().toUtf8() == "": - addressGeneratorQueue.put( - ('createChan', 4, 1, str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()), - self.chanPassPhrase.text().toUtf8(), - True)) + passPhrase = self.chanPassPhrase.text().encode('utf-8') + if self.chanAddress.text() == "": + addressGeneratorQueue.put(( + 'createChan', 4, 1, + str_chan + ' ' + passPhrase, passPhrase, True + )) else: - addressGeneratorQueue.put( - ('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()), - str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()), - self.chanPassPhrase.text().toUtf8(), - True)) + addressGeneratorQueue.put(( + 'joinChan', addBMIfNotPresent(self.chanAddress.text()), + str_chan + ' ' + passPhrase, passPhrase, True + )) addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True) - if addressGeneratorReturnValue and addressGeneratorReturnValue[0] != 'chan name does not match address': - UISignalQueue.put(('updateStatusBar', _translate( - "newchandialog", "Successfully created / joined chan %1").arg(unicode(self.chanPassPhrase.text())))) + if ( + len(addressGeneratorReturnValue) > 0 + and addressGeneratorReturnValue[0] + != 'chan name does not match address' + ): + UISignalQueue.put(( + 'updateStatusBar', + _translate( + "newchandialog", + "Successfully created / joined chan {0}" + ).format(passPhrase) + )) self.parent.ui.tabWidget.setCurrentIndex( self.parent.ui.tabWidget.indexOf(self.parent.ui.chans) ) - self.done(QtGui.QDialog.Accepted) + self.done(QtWidgets.QDialog.Accepted) else: - UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed"))) - self.done(QtGui.QDialog.Rejected) + UISignalQueue.put(( + 'updateStatusBar', + _translate("newchandialog", "Chan creation / joining failed") + )) + self.done(QtWidgets.QDialog.Rejected) def reject(self): """Cancel joining the chan""" self.timer.stop() self.hide() - UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled"))) - self.done(QtGui.QDialog.Rejected) + UISignalQueue.put(( + 'updateStatusBar', + _translate("newchandialog", "Chan creation / joining cancelled") + )) + self.done(QtWidgets.QDialog.Rejected)