Prevent garbage collection of TxDialogs

The transaction dialogs have a habit of randomly disappearing,
because of garbage collection.  This was particularly common
if you viewed the details of a tx in your history immediately
after electrum startup, or after pressing Broadcast.

Other tweaks:

- Distinguish saved and broadcast.
- When signed, consider unsaved and prompt to save if not
  subsequently saved or broadcast.
- Hide broadcast button after broadcast.
- Hook into the closeEvent so closing the window with the mouse
  has the same effect as pressing the close button.
This commit is contained in:
Neil Booth 2015-07-04 12:26:28 +09:00
parent 9a823f5fe6
commit 3446e1fd56

View file

@ -31,8 +31,11 @@ from electrum.plugins import run_hook
from util import * from util import *
dialogs = [] # Otherwise python randomly garbage collects the dialogs...
def show_transaction(tx, parent, desc=None, prompt_if_unsaved=False): def show_transaction(tx, parent, desc=None, prompt_if_unsaved=False):
d = TxDialog(tx, parent, desc, prompt_if_unsaved) d = TxDialog(tx, parent, desc, prompt_if_unsaved)
dialogs.append(d)
d.show() d.show()
class TxDialog(QWidget): class TxDialog(QWidget):
@ -45,7 +48,9 @@ class TxDialog(QWidget):
tx_dict = tx.as_dict() tx_dict = tx.as_dict()
self.parent = parent self.parent = parent
self.wallet = parent.wallet self.wallet = parent.wallet
self.saved = not prompt_if_unsaved self.prompt_if_unsaved = prompt_if_unsaved
self.saved = False
self.broadcast = False
self.desc = desc self.desc = desc
QWidget.__init__(self) QWidget.__init__(self)
@ -82,7 +87,6 @@ class TxDialog(QWidget):
self.broadcast_button = b = QPushButton(_("Broadcast")) self.broadcast_button = b = QPushButton(_("Broadcast"))
b.clicked.connect(self.do_broadcast) b.clicked.connect(self.do_broadcast)
b.hide()
self.save_button = b = QPushButton(_("Save")) self.save_button = b = QPushButton(_("Save"))
b.clicked.connect(self.save) b.clicked.connect(self.save)
@ -113,15 +117,19 @@ class TxDialog(QWidget):
def do_broadcast(self): def do_broadcast(self):
self.parent.broadcast_transaction(self.tx, self.desc) self.parent.broadcast_transaction(self.tx, self.desc)
self.saved = True self.broadcast = True
self.update()
def close(self): def closeEvent(self, event):
if not self.saved: if (self.prompt_if_unsaved and not self.saved and not self.broadcast
if QMessageBox.question( and QMessageBox.question(
self, _('Message'), _('This transaction is not saved. Close anyway?'), self, _('Warning'),
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) == QMessageBox.No: _('This transaction is not saved. Close anyway?'),
return QMessageBox.Yes | QMessageBox.No) == QMessageBox.No):
QWidget.close(self) event.ignore()
else:
event.accept()
dialogs.remove(self)
def show_qr(self): def show_qr(self):
text = self.tx.raw.decode('hex') text = self.tx.raw.decode('hex')
@ -134,6 +142,8 @@ class TxDialog(QWidget):
def sign(self): def sign(self):
def sign_done(success): def sign_done(success):
self.prompt_if_unsaved = True
self.saved = False
self.update() self.update()
self.parent.send_tx(self.tx, sign_done) self.parent.send_tx(self.tx, sign_done)
@ -152,6 +162,7 @@ class TxDialog(QWidget):
tx_hash = self.tx.hash() tx_hash = self.tx.hash()
desc = self.desc desc = self.desc
time_str = None time_str = None
self.broadcast_button.hide()
if self.tx.is_complete(): if self.tx.is_complete():
status = _("Signed") status = _("Signed")
@ -164,8 +175,7 @@ class TxDialog(QWidget):
else: else:
time_str = _('Pending') time_str = _('Pending')
status = _("%d confirmations")%conf status = _("%d confirmations")%conf
self.broadcast_button.hide() elif not self.broadcast:
else:
self.broadcast_button.show() self.broadcast_button.show()
# cannot broadcast when offline # cannot broadcast when offline
if self.parent.network is None: if self.parent.network is None:
@ -173,7 +183,6 @@ class TxDialog(QWidget):
else: else:
s, r = self.tx.signature_count() s, r = self.tx.signature_count()
status = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r) status = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r)
self.broadcast_button.hide()
tx_hash = _('Unknown'); tx_hash = _('Unknown');
if self.wallet.can_sign(self.tx): if self.wallet.can_sign(self.tx):