Skip to content

Commit 7ff7ada

Browse files
committed
Prepare newchandialog module for using qtpy
1 parent 6d5e6c7 commit 7ff7ada

File tree

2 files changed

+145
-118
lines changed

2 files changed

+145
-118
lines changed

src/bitmessageqt/addressvalidator.py

Lines changed: 101 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
"""
2-
Address validator module.
2+
The validator for address and passphrase QLineEdits
3+
used in `.dialogs.NewChanDialog`.
34
"""
4-
# pylint: disable=too-many-branches,too-many-arguments
5+
# pylint: disable=too-many-arguments
56

6-
from PyQt4 import QtGui
77
from Queue import Empty
88

9+
from qtpy import QtGui
10+
911
from account import getSortedAccounts
1012
from addresses import decodeAddress, addBMIfNotPresent
11-
from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue
13+
from queues import addressGeneratorQueue, apiAddressGeneratorReturnQueue
1214
from tr import _translate
1315
from utils import str_chan
1416

1517

1618
class AddressPassPhraseValidatorMixin(object):
1719
"""Bitmessage address or passphrase validator class for Qt UI"""
1820
def setParams(
19-
self,
20-
passPhraseObject=None,
21-
addressObject=None,
22-
feedBackObject=None,
23-
buttonBox=None,
24-
addressMandatory=True,
21+
self, passPhraseObject=None, addressObject=None,
22+
feedBackObject=None, button=None, addressMandatory=True
2523
):
26-
"""Initialisation"""
24+
"""Initialization"""
2725
self.addressObject = addressObject
2826
self.passPhraseObject = passPhraseObject
2927
self.feedBackObject = feedBackObject
30-
self.buttonBox = buttonBox
3128
self.addressMandatory = addressMandatory
3229
self.isValid = False
3330
# save default text
34-
self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text()
31+
self.okButton = button
32+
self.okButtonLabel = button.text()
3533

3634
def setError(self, string):
3735
"""Indicate that the validation is pending or failed"""
@@ -42,13 +40,13 @@ def setError(self, string):
4240
self.feedBackObject.setStyleSheet("QLabel { color : red; }")
4341
self.feedBackObject.setText(string)
4442
self.isValid = False
45-
if self.buttonBox:
46-
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
43+
if self.okButton:
44+
self.okButton.setEnabled(False)
4745
if string is not None and self.feedBackObject is not None:
48-
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
46+
self.okButton.setText(
4947
_translate("AddressValidator", "Invalid"))
5048
else:
51-
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
49+
self.okButton.setText(
5250
_translate("AddressValidator", "Validating..."))
5351

5452
def setOK(self, string):
@@ -60,9 +58,9 @@ def setOK(self, string):
6058
self.feedBackObject.setStyleSheet("QLabel { }")
6159
self.feedBackObject.setText(string)
6260
self.isValid = True
63-
if self.buttonBox:
64-
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
65-
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel)
61+
if self.okButton:
62+
self.okButton.setEnabled(True)
63+
self.okButton.setText(self.okButtonLabel)
6664

6765
def checkQueue(self):
6866
"""Validator queue loop"""
@@ -75,7 +73,8 @@ def checkQueue(self):
7573

7674
while True:
7775
try:
78-
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False)
76+
addressGeneratorReturnValue = \
77+
apiAddressGeneratorReturnQueue.get(False)
7978
except Empty:
8079
if gotOne:
8180
break
@@ -85,96 +84,120 @@ def checkQueue(self):
8584
gotOne = True
8685

8786
if not addressGeneratorReturnValue:
88-
self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
89-
return (QtGui.QValidator.Intermediate, 0)
90-
if addressGeneratorReturnValue[0] == 'chan name does not match address':
91-
self.setError(
92-
_translate(
93-
"AddressValidator",
94-
"Although the Bitmessage address you "
95-
"entered was valid, it doesn't match the chan name."))
96-
return (QtGui.QValidator.Intermediate, 0)
97-
self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid."))
87+
self.setError(_translate(
88+
"AddressValidator",
89+
"Address already present as one of your identities."
90+
))
91+
return
92+
if addressGeneratorReturnValue[0] == \
93+
'chan name does not match address':
94+
self.setError(_translate(
95+
"AddressValidator",
96+
"Although the Bitmessage address you entered was valid,"
97+
" it doesn\'t match the chan name."
98+
))
99+
return
100+
self.setOK(_translate(
101+
"MainWindow", "Passphrase and address appear to be valid."))
98102

99103
def returnValid(self):
100104
"""Return the value of whether the validation was successful"""
101-
if self.isValid:
102-
return QtGui.QValidator.Acceptable
103-
return QtGui.QValidator.Intermediate
105+
return QtGui.QValidator.Acceptable if self.isValid \
106+
else QtGui.QValidator.Intermediate
104107

105108
def validate(self, s, pos):
106109
"""Top level validator method"""
107-
if self.addressObject is None:
110+
try:
111+
address = self.addressObject.text().encode('utf-8')
112+
except AttributeError:
108113
address = None
109-
else:
110-
address = str(self.addressObject.text().toUtf8())
111-
if address == "":
112-
address = None
113-
if self.passPhraseObject is None:
114+
try:
115+
passPhrase = self.passPhraseObject.text().encode('utf-8')
116+
except AttributeError:
114117
passPhrase = ""
115-
else:
116-
passPhrase = str(self.passPhraseObject.text().toUtf8())
117-
if passPhrase == "":
118-
passPhrase = None
119118

120119
# no chan name
121-
if passPhrase is None:
122-
self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name."))
123-
return (QtGui.QValidator.Intermediate, pos)
124-
125-
if self.addressMandatory or address is not None:
120+
if not passPhrase:
121+
self.setError(_translate(
122+
"AddressValidator",
123+
"Chan name/passphrase needed. You didn't enter a chan name."
124+
))
125+
return (QtGui.QValidator.Intermediate, s, pos)
126+
127+
if self.addressMandatory or address:
126128
# check if address already exists:
127129
if address in getSortedAccounts():
128-
self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
129-
return (QtGui.QValidator.Intermediate, pos)
130+
self.setError(_translate(
131+
"AddressValidator",
132+
"Address already present as one of your identities."
133+
))
134+
return (QtGui.QValidator.Intermediate, s, pos)
130135

136+
status = decodeAddress(address)[0]
131137
# version too high
132-
if decodeAddress(address)[0] == 'versiontoohigh':
133-
self.setError(
134-
_translate(
135-
"AddressValidator",
136-
"Address too new. Although that Bitmessage"
137-
" address might be valid, its version number"
138-
" is too new for us to handle. Perhaps you need"
139-
" to upgrade Bitmessage."))
140-
return (QtGui.QValidator.Intermediate, pos)
141-
138+
if status == 'versiontoohigh':
139+
self.setError(_translate(
140+
"AddressValidator",
141+
"Address too new. Although that Bitmessage address"
142+
" might be valid, its version number is too new"
143+
" for us to handle. Perhaps you need to upgrade"
144+
" Bitmessage."
145+
))
146+
return (QtGui.QValidator.Intermediate, s, pos)
142147
# invalid
143-
if decodeAddress(address)[0] != 'success':
144-
self.setError(_translate("AddressValidator", "The Bitmessage address is not valid."))
145-
return (QtGui.QValidator.Intermediate, pos)
148+
if status != 'success':
149+
self.setError(_translate(
150+
"AddressValidator",
151+
"The Bitmessage address is not valid."
152+
))
153+
return (QtGui.QValidator.Intermediate, s, pos)
146154

147155
# this just disables the OK button without changing the feedback text
148156
# but only if triggered by textEdited, not by clicking the Ok button
149-
if not self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
157+
if not self.okButton.hasFocus():
150158
self.setError(None)
151159

152160
# check through generator
153-
if address is None:
154-
addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False))
161+
if not address:
162+
addressGeneratorQueue.put((
163+
'createChan', 4, 1,
164+
str_chan + ' ' + passPhrase, passPhrase, False
165+
))
155166
else:
156-
addressGeneratorQueue.put(
157-
('joinChan', addBMIfNotPresent(address),
158-
"{} {}".format(str_chan, passPhrase), passPhrase, False))
167+
addressGeneratorQueue.put((
168+
'joinChan', addBMIfNotPresent(address),
169+
"{} {}".format(str_chan, passPhrase), passPhrase, False
170+
))
159171

160-
if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
161-
return (self.returnValid(), pos)
162-
return (QtGui.QValidator.Intermediate, pos)
172+
if self.okButton.hasFocus():
173+
return (self.returnValid(), s, pos)
174+
else:
175+
return (QtGui.QValidator.Intermediate, s, pos)
163176

164177
def checkData(self):
165178
"""Validator Qt signal interface"""
166-
return self.validate("", 0)
179+
return self.validate(u"", 0)
167180

168181

169182
class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
170183
"""AddressValidator class for Qt UI"""
171-
def __init__(self, parent=None, passPhraseObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True):
184+
def __init__(
185+
self, parent=None, passPhraseObject=None, feedBackObject=None,
186+
button=None, addressMandatory=True
187+
):
172188
super(AddressValidator, self).__init__(parent)
173-
self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory)
189+
self.setParams(
190+
passPhraseObject, parent, feedBackObject, button,
191+
addressMandatory)
174192

175193

176194
class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
177195
"""PassPhraseValidator class for Qt UI"""
178-
def __init__(self, parent=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=False):
196+
def __init__(
197+
self, parent=None, addressObject=None, feedBackObject=None,
198+
button=None, addressMandatory=False
199+
):
179200
super(PassPhraseValidator, self).__init__(parent)
180-
self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory)
201+
self.setParams(
202+
parent, addressObject, feedBackObject, button,
203+
addressMandatory)

src/bitmessageqt/newchandialog.py

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""
2-
src/bitmessageqt/newchandialog.py
3-
=================================
4-
2+
NewChanDialog class definition
53
"""
64

7-
from PyQt4 import QtCore, QtGui
5+
from qtpy import QtCore, QtWidgets
86

97
import widgets
108
from addresses import addBMIfNotPresent
@@ -15,30 +13,21 @@
1513
from utils import str_chan
1614

1715

18-
class NewChanDialog(QtGui.QDialog):
19-
"""The `New Chan` dialog"""
16+
class NewChanDialog(QtWidgets.QDialog):
17+
"""The "New Chan" dialog"""
2018
def __init__(self, parent=None):
2119
super(NewChanDialog, self).__init__(parent)
2220
widgets.load('newchandialog.ui', self)
2321
self.parent = parent
24-
self.chanAddress.setValidator(
25-
AddressValidator(
26-
self.chanAddress,
27-
self.chanPassPhrase,
28-
self.validatorFeedback,
29-
self.buttonBox,
30-
False))
31-
self.chanPassPhrase.setValidator(
32-
PassPhraseValidator(
33-
self.chanPassPhrase,
34-
self.chanAddress,
35-
self.validatorFeedback,
36-
self.buttonBox,
37-
False))
22+
self.chanAddress.setValidator(AddressValidator(
23+
self.chanAddress, self.chanPassPhrase, self.validatorFeedback,
24+
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
25+
self.chanPassPhrase.setValidator(PassPhraseValidator(
26+
self.chanPassPhrase, self.chanAddress, self.validatorFeedback,
27+
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
3828

3929
self.timer = QtCore.QTimer()
40-
QtCore.QObject.connect( # pylint: disable=no-member
41-
self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus)
30+
self.timer.timeout.connect(self.delayedUpdateStatus)
4231
self.timer.start(500) # milliseconds
4332
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
4433
self.show()
@@ -52,32 +41,47 @@ def accept(self):
5241
self.timer.stop()
5342
self.hide()
5443
apiAddressGeneratorReturnQueue.queue.clear()
55-
if self.chanAddress.text().toUtf8() == "":
56-
addressGeneratorQueue.put(
57-
('createChan', 4, 1, str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
58-
self.chanPassPhrase.text().toUtf8(),
59-
True))
44+
passPhrase = self.chanPassPhrase.text().encode('utf-8')
45+
if self.chanAddress.text() == "":
46+
addressGeneratorQueue.put((
47+
'createChan', 4, 1,
48+
str_chan + ' ' + passPhrase, passPhrase, True
49+
))
6050
else:
61-
addressGeneratorQueue.put(
62-
('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()),
63-
str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
64-
self.chanPassPhrase.text().toUtf8(),
65-
True))
51+
addressGeneratorQueue.put((
52+
'joinChan', addBMIfNotPresent(self.chanAddress.text()),
53+
str_chan + ' ' + passPhrase, passPhrase, True
54+
))
6655
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
67-
if addressGeneratorReturnValue and addressGeneratorReturnValue[0] != 'chan name does not match address':
68-
UISignalQueue.put(('updateStatusBar', _translate(
69-
"newchandialog", "Successfully created / joined chan %1").arg(unicode(self.chanPassPhrase.text()))))
56+
if (
57+
len(addressGeneratorReturnValue) > 0
58+
and addressGeneratorReturnValue[0]
59+
!= 'chan name does not match address'
60+
):
61+
UISignalQueue.put((
62+
'updateStatusBar',
63+
_translate(
64+
"newchandialog",
65+
"Successfully created / joined chan {0}"
66+
).format(passPhrase)
67+
))
7068
self.parent.ui.tabWidget.setCurrentIndex(
7169
self.parent.ui.tabWidget.indexOf(self.parent.ui.chans)
7270
)
73-
self.done(QtGui.QDialog.Accepted)
71+
self.done(QtWidgets.QDialog.Accepted)
7472
else:
75-
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed")))
76-
self.done(QtGui.QDialog.Rejected)
73+
UISignalQueue.put((
74+
'updateStatusBar',
75+
_translate("newchandialog", "Chan creation / joining failed")
76+
))
77+
self.done(QtWidgets.QDialog.Rejected)
7778

7879
def reject(self):
7980
"""Cancel joining the chan"""
8081
self.timer.stop()
8182
self.hide()
82-
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled")))
83-
self.done(QtGui.QDialog.Rejected)
83+
UISignalQueue.put((
84+
'updateStatusBar',
85+
_translate("newchandialog", "Chan creation / joining cancelled")
86+
))
87+
self.done(QtWidgets.QDialog.Rejected)

0 commit comments

Comments
 (0)