Use TaskThread to simplify ThreadedButton
And improve labels dialog
This commit is contained in:
@@ -51,41 +51,21 @@ class EnterButton(QPushButton):
|
|||||||
|
|
||||||
|
|
||||||
class ThreadedButton(QPushButton):
|
class ThreadedButton(QPushButton):
|
||||||
def __init__(self, text, func, on_success=None, before=None):
|
def __init__(self, text, task, on_success=None, on_error=None):
|
||||||
QPushButton.__init__(self, text)
|
QPushButton.__init__(self, text)
|
||||||
self.before = before
|
self.task = task
|
||||||
self.run_task = func
|
|
||||||
self.on_success = on_success
|
self.on_success = on_success
|
||||||
self.clicked.connect(self.do_exec)
|
self.on_error = on_error
|
||||||
self.connect(self, SIGNAL('done'), self.done)
|
self.clicked.connect(self.run_task)
|
||||||
self.connect(self, SIGNAL('error'), self.on_error)
|
|
||||||
|
def run_task(self):
|
||||||
|
self.setEnabled(False)
|
||||||
|
self.thread = TaskThread(self)
|
||||||
|
self.thread.add(self.task, self.on_success, self.done, self.on_error)
|
||||||
|
|
||||||
def done(self):
|
def done(self):
|
||||||
if self.on_success:
|
|
||||||
self.on_success()
|
|
||||||
self.setEnabled(True)
|
self.setEnabled(True)
|
||||||
|
self.thread.stop()
|
||||||
def on_error(self):
|
|
||||||
QMessageBox.information(None, _("Error"), self.error)
|
|
||||||
self.setEnabled(True)
|
|
||||||
|
|
||||||
def do_func(self):
|
|
||||||
self.setEnabled(False)
|
|
||||||
try:
|
|
||||||
self.result = self.run_task()
|
|
||||||
except BaseException as e:
|
|
||||||
traceback.print_exc(file=sys.stdout)
|
|
||||||
self.error = str(e.message)
|
|
||||||
self.emit(SIGNAL('error'))
|
|
||||||
return
|
|
||||||
self.emit(SIGNAL('done'))
|
|
||||||
|
|
||||||
def do_exec(self):
|
|
||||||
if self.before:
|
|
||||||
self.before()
|
|
||||||
t = threading.Thread(target=self.do_func)
|
|
||||||
t.setDaemon(True)
|
|
||||||
t.start()
|
|
||||||
|
|
||||||
|
|
||||||
class WWLabel(QLabel):
|
class WWLabel(QLabel):
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from PyQt4.QtCore import *
|
|||||||
from electrum.plugins import hook
|
from electrum.plugins import hook
|
||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
from electrum_gui.qt import EnterButton
|
from electrum_gui.qt import EnterButton
|
||||||
from electrum_gui.qt.util import ThreadedButton, Buttons, CancelButton
|
from electrum_gui.qt.util import ThreadedButton, Buttons
|
||||||
from electrum_gui.qt.util import WindowModalDialog, OkButton
|
from electrum_gui.qt.util import WindowModalDialog, OkButton
|
||||||
|
|
||||||
from labels import LabelsPlugin
|
from labels import LabelsPlugin
|
||||||
@@ -28,28 +28,29 @@ class Plugin(LabelsPlugin):
|
|||||||
def settings_dialog(self, window):
|
def settings_dialog(self, window):
|
||||||
wallet = window.parent().wallet
|
wallet = window.parent().wallet
|
||||||
d = WindowModalDialog(window, _("Label Settings"))
|
d = WindowModalDialog(window, _("Label Settings"))
|
||||||
|
hbox = QHBoxLayout()
|
||||||
|
hbox.addWidget(QLabel("Label sync options:"))
|
||||||
|
upload = ThreadedButton("Force upload",
|
||||||
|
partial(self.push_thread, wallet),
|
||||||
|
partial(self.done_processing, d))
|
||||||
|
download = ThreadedButton("Force download",
|
||||||
|
partial(self.pull_thread, wallet, True),
|
||||||
|
partial(self.done_processing, d))
|
||||||
|
vbox = QVBoxLayout()
|
||||||
|
vbox.addWidget(upload)
|
||||||
|
vbox.addWidget(download)
|
||||||
|
hbox.addLayout(vbox)
|
||||||
vbox = QVBoxLayout(d)
|
vbox = QVBoxLayout(d)
|
||||||
layout = QGridLayout()
|
vbox.addLayout(hbox)
|
||||||
vbox.addLayout(layout)
|
vbox.addSpacing(20)
|
||||||
layout.addWidget(QLabel("Label sync options: "), 2, 0)
|
vbox.addLayout(Buttons(OkButton(d)))
|
||||||
self.upload = ThreadedButton("Force upload",
|
|
||||||
partial(self.push_thread, wallet),
|
|
||||||
self.done_processing)
|
|
||||||
layout.addWidget(self.upload, 2, 1)
|
|
||||||
self.download = ThreadedButton("Force download",
|
|
||||||
partial(self.pull_thread, wallet, True),
|
|
||||||
self.done_processing)
|
|
||||||
layout.addWidget(self.download, 2, 2)
|
|
||||||
self.accept = OkButton(d, _("Done"))
|
|
||||||
vbox.addLayout(Buttons(CancelButton(d), self.accept))
|
|
||||||
return bool(d.exec_())
|
return bool(d.exec_())
|
||||||
|
|
||||||
def on_pulled(self, wallet):
|
def on_pulled(self, wallet):
|
||||||
self.obj.emit(SIGNAL('labels_changed'), wallet)
|
self.obj.emit(SIGNAL('labels_changed'), wallet)
|
||||||
|
|
||||||
def done_processing(self):
|
def done_processing(self, dialog, result):
|
||||||
QMessageBox.information(None, _("Labels synchronised"),
|
dialog.show_message(_("Your labels have been synchronised."))
|
||||||
_("Your labels have been synchronised."))
|
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
def on_new_window(self, window):
|
def on_new_window(self, window):
|
||||||
|
|||||||
Reference in New Issue
Block a user