Runnable with both Python3 and Python2, with both PyQt5 and PyQt4 by using Qt.py #2250

Open
kashikoibumi wants to merge 127 commits from kashikoibumi/py3qt into v0.6
2 changed files with 144 additions and 117 deletions
Showing only changes of commit b9fca03ed1 - Show all commits

View File

@ -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 Queue import Empty
from PyQt5 import QtGui
from account import getSortedAccounts from account import getSortedAccounts
from addresses import decodeAddress, addBMIfNotPresent from addresses import decodeAddress, addBMIfNotPresent
from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue from queues import addressGeneratorQueue, apiAddressGeneratorReturnQueue
from tr import _translate from tr import _translate
from utils import str_chan from utils import str_chan
@ -16,22 +18,18 @@ from utils import str_chan
class AddressPassPhraseValidatorMixin(object): class AddressPassPhraseValidatorMixin(object):
"""Bitmessage address or passphrase validator class for Qt UI""" """Bitmessage address or passphrase validator class for Qt UI"""
def setParams( def setParams(
self, self, passPhraseObject=None, addressObject=None,
passPhraseObject=None, feedBackObject=None, button=None, addressMandatory=True
addressObject=None,
feedBackObject=None,
buttonBox=None,
addressMandatory=True,
): ):
"""Initialisation""" """Initialization"""
self.addressObject = addressObject self.addressObject = addressObject
self.passPhraseObject = passPhraseObject self.passPhraseObject = passPhraseObject
self.feedBackObject = feedBackObject self.feedBackObject = feedBackObject
self.buttonBox = buttonBox
self.addressMandatory = addressMandatory self.addressMandatory = addressMandatory
self.isValid = False self.isValid = False
# save default text # save default text
self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text() self.okButton = button
self.okButtonLabel = button.text()
def setError(self, string): def setError(self, string):
"""Indicate that the validation is pending or failed""" """Indicate that the validation is pending or failed"""
@ -42,13 +40,13 @@ class AddressPassPhraseValidatorMixin(object):
self.feedBackObject.setStyleSheet("QLabel { color : red; }") self.feedBackObject.setStyleSheet("QLabel { color : red; }")
self.feedBackObject.setText(string) self.feedBackObject.setText(string)
self.isValid = False self.isValid = False
if self.buttonBox: if self.okButton:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False) self.okButton.setEnabled(False)
if string is not None and self.feedBackObject is not None: if string is not None and self.feedBackObject is not None:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText( self.okButton.setText(
_translate("AddressValidator", "Invalid")) _translate("AddressValidator", "Invalid"))
else: else:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText( self.okButton.setText(
_translate("AddressValidator", "Validating...")) _translate("AddressValidator", "Validating..."))
def setOK(self, string): def setOK(self, string):
@ -60,9 +58,9 @@ class AddressPassPhraseValidatorMixin(object):
self.feedBackObject.setStyleSheet("QLabel { }") self.feedBackObject.setStyleSheet("QLabel { }")
self.feedBackObject.setText(string) self.feedBackObject.setText(string)
self.isValid = True self.isValid = True
if self.buttonBox: if self.okButton:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True) self.okButton.setEnabled(True)
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel) self.okButton.setText(self.okButtonLabel)
def checkQueue(self): def checkQueue(self):
"""Validator queue loop""" """Validator queue loop"""
@ -75,7 +73,8 @@ class AddressPassPhraseValidatorMixin(object):
while True: while True:
try: try:
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False) addressGeneratorReturnValue = \
apiAddressGeneratorReturnQueue.get(False)
except Empty: except Empty:
if gotOne: if gotOne:
break break
@ -85,96 +84,120 @@ class AddressPassPhraseValidatorMixin(object):
gotOne = True gotOne = True
if not addressGeneratorReturnValue: if not addressGeneratorReturnValue:
self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, 0)
if addressGeneratorReturnValue[0] == 'chan name does not match address':
self.setError(
_translate(
"AddressValidator", "AddressValidator",
"Although the Bitmessage address you " "Address already present as one of your identities."
"entered was valid, it doesn't match the chan name.")) ))
return (QtGui.QValidator.Intermediate, 0) return
self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid.")) 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): def returnValid(self):
"""Return the value of whether the validation was successful""" """Return the value of whether the validation was successful"""
if self.isValid: return QtGui.QValidator.Acceptable if self.isValid \
return QtGui.QValidator.Acceptable else QtGui.QValidator.Intermediate
return QtGui.QValidator.Intermediate
def validate(self, s, pos): def validate(self, s, pos):
"""Top level validator method""" """Top level validator method"""
if self.addressObject is None: try:
address = self.addressObject.text().encode('utf-8')
except AttributeError:
address = None address = None
else: try:
address = str(self.addressObject.text().toUtf8()) passPhrase = self.passPhraseObject.text().encode('utf-8')
if address == "": except AttributeError:
address = None
if self.passPhraseObject is None:
passPhrase = "" passPhrase = ""
else:
passPhrase = str(self.passPhraseObject.text().toUtf8())
if passPhrase == "":
passPhrase = None
# no chan name # no chan name
if passPhrase is None: if not passPhrase:
self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "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: # check if address already exists:
if address in getSortedAccounts(): if address in getSortedAccounts():
self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos)
# version too high
if decodeAddress(address)[0] == 'versiontoohigh':
self.setError(
_translate(
"AddressValidator", "AddressValidator",
"Address too new. Although that Bitmessage" "Address already present as one of your identities."
" address might be valid, its version number" ))
" is too new for us to handle. Perhaps you need" return (QtGui.QValidator.Intermediate, s, pos)
" to upgrade Bitmessage."))
return (QtGui.QValidator.Intermediate, pos)
status = decodeAddress(address)[0]
# version too high
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 # invalid
if decodeAddress(address)[0] != 'success': if status != 'success':
self.setError(_translate("AddressValidator", "The Bitmessage address is not valid.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "AddressValidator",
"The Bitmessage address is not valid."
))
return (QtGui.QValidator.Intermediate, s, pos)
# this just disables the OK button without changing the feedback text # this just disables the OK button without changing the feedback text
# but only if triggered by textEdited, not by clicking the Ok button # 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) self.setError(None)
# check through generator # check through generator
if address is None: if not address:
addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False)) addressGeneratorQueue.put((
'createChan', 4, 1,
str_chan + ' ' + passPhrase, passPhrase, False
))
else: else:
addressGeneratorQueue.put( addressGeneratorQueue.put((
('joinChan', addBMIfNotPresent(address), 'joinChan', addBMIfNotPresent(address),
"{} {}".format(str_chan, passPhrase), passPhrase, False)) "{} {}".format(str_chan, passPhrase), passPhrase, False
))
if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus(): if self.okButton.hasFocus():
return (self.returnValid(), pos) return (self.returnValid(), s, pos)
return (QtGui.QValidator.Intermediate, pos) else:
return (QtGui.QValidator.Intermediate, s, pos)
def checkData(self): def checkData(self):
"""Validator Qt signal interface""" """Validator Qt signal interface"""
return self.validate("", 0) return self.validate(u"", 0)
class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
"""AddressValidator class for Qt UI""" """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) super(AddressValidator, self).__init__(parent)
self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory) self.setParams(
passPhraseObject, parent, feedBackObject, button,
addressMandatory)
class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
"""PassPhraseValidator class for Qt UI""" """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) super(PassPhraseValidator, self).__init__(parent)
self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory) self.setParams(
parent, addressObject, feedBackObject, button,
addressMandatory)

View File

@ -1,10 +1,8 @@
""" """
src/bitmessageqt/newchandialog.py NewChanDialog class definition
=================================
""" """
from PyQt4 import QtCore, QtGui from PyQt5 import QtCore, QtWidgets
import widgets import widgets
from addresses import addBMIfNotPresent from addresses import addBMIfNotPresent
@ -15,30 +13,21 @@ from tr import _translate
from utils import str_chan from utils import str_chan
class NewChanDialog(QtGui.QDialog): class NewChanDialog(QtWidgets.QDialog):
"""The `New Chan` dialog""" """The "New Chan" dialog"""
def __init__(self, parent=None): def __init__(self, parent=None):
super(NewChanDialog, self).__init__(parent) super(NewChanDialog, self).__init__(parent)
widgets.load('newchandialog.ui', self) widgets.load('newchandialog.ui', self)
self.parent = parent self.parent = parent
self.chanAddress.setValidator( self.chanAddress.setValidator(AddressValidator(
AddressValidator( self.chanAddress, self.chanPassPhrase, self.validatorFeedback,
self.chanAddress, self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
self.chanPassPhrase, self.chanPassPhrase.setValidator(PassPhraseValidator(
self.validatorFeedback, self.chanPassPhrase, self.chanAddress, self.validatorFeedback,
self.buttonBox, self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
False))
self.chanPassPhrase.setValidator(
PassPhraseValidator(
self.chanPassPhrase,
self.chanAddress,
self.validatorFeedback,
self.buttonBox,
False))
self.timer = QtCore.QTimer() self.timer = QtCore.QTimer()
QtCore.QObject.connect( # pylint: disable=no-member self.timer.timeout.connect(self.delayedUpdateStatus)
self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus)
self.timer.start(500) # milliseconds self.timer.start(500) # milliseconds
self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.show() self.show()
@ -52,32 +41,47 @@ class NewChanDialog(QtGui.QDialog):
self.timer.stop() self.timer.stop()
self.hide() self.hide()
apiAddressGeneratorReturnQueue.queue.clear() apiAddressGeneratorReturnQueue.queue.clear()
if self.chanAddress.text().toUtf8() == "": passPhrase = self.chanPassPhrase.text().encode('utf-8')
addressGeneratorQueue.put( if self.chanAddress.text() == "":
('createChan', 4, 1, str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()), addressGeneratorQueue.put((
self.chanPassPhrase.text().toUtf8(), 'createChan', 4, 1,
True)) str_chan + ' ' + passPhrase, passPhrase, True
))
else: else:
addressGeneratorQueue.put( addressGeneratorQueue.put((
('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()), 'joinChan', addBMIfNotPresent(self.chanAddress.text()),
str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()), str_chan + ' ' + passPhrase, passPhrase, True
self.chanPassPhrase.text().toUtf8(), ))
True))
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True) addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
if addressGeneratorReturnValue and addressGeneratorReturnValue[0] != 'chan name does not match address': if (
UISignalQueue.put(('updateStatusBar', _translate( len(addressGeneratorReturnValue) > 0
"newchandialog", "Successfully created / joined chan %1").arg(unicode(self.chanPassPhrase.text())))) 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.setCurrentIndex(
self.parent.ui.tabWidget.indexOf(self.parent.ui.chans) self.parent.ui.tabWidget.indexOf(self.parent.ui.chans)
) )
self.done(QtGui.QDialog.Accepted) self.done(QtWidgets.QDialog.Accepted)
else: else:
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed"))) UISignalQueue.put((
self.done(QtGui.QDialog.Rejected) 'updateStatusBar',
_translate("newchandialog", "Chan creation / joining failed")
))
self.done(QtWidgets.QDialog.Rejected)
def reject(self): def reject(self):
"""Cancel joining the chan""" """Cancel joining the chan"""
self.timer.stop() self.timer.stop()
self.hide() self.hide()
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled"))) UISignalQueue.put((
self.done(QtGui.QDialog.Rejected) 'updateStatusBar',
_translate("newchandialog", "Chan creation / joining cancelled")
))
self.done(QtWidgets.QDialog.Rejected)