Skip to content
Snippets Groups Projects
Commit 8e3403ef authored by inso's avatar inso
Browse files

Added Toast system to display notifications

parent b6920858
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Toast</class>
<widget class="QMainWindow" name="Toast">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>358</width>
<height>87</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="display">
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
...@@ -184,7 +184,7 @@ class Account(QObject): ...@@ -184,7 +184,7 @@ class Account(QObject):
for w in self.wallets: for w in self.wallets:
w.refresh_progressed.connect(progressing, type=Qt.DirectConnection) w.refresh_progressed.connect(progressing, type=Qt.DirectConnection)
for c in self.communities: for c in self.communities:
w.refresh_cache(c) w.refresh_cache(c, [])
loaded_wallets = loaded_wallets + 1 loaded_wallets = loaded_wallets + 1
def set_display_referential(self, index): def set_display_referential(self, index):
......
...@@ -60,7 +60,7 @@ class Cache(): ...@@ -60,7 +60,7 @@ class Cache():
def transfers(self): def transfers(self):
return [t for t in self._transfers if t.state != Transfer.DROPPED] return [t for t in self._transfers if t.state != Transfer.DROPPED]
def _parse_transaction(self, community, tx, block_number, mediantime): def _parse_transaction(self, community, tx, block_number, mediantime, received_list):
logging.debug(tx) logging.debug(tx)
receivers = [o.pubkey for o in tx.outputs receivers = [o.pubkey for o in tx.outputs
if o.pubkey != tx.issuers[0]] if o.pubkey != tx.issuers[0]]
...@@ -113,11 +113,12 @@ class Cache(): ...@@ -113,11 +113,12 @@ class Cache():
for o in outputs: for o in outputs:
amount += o.amount amount += o.amount
metadata['amount'] = amount metadata['amount'] = amount
received = Received(tx, metadata.copy())
received_list.append(received)
self._transfers.append(received)
self._transfers.append(Received(tx,
metadata.copy()))
def _parse_block(self, community, block_number): def _parse_block(self, community, block_number, received_list):
block = community.request(bma.blockchain.Block, block = community.request(bma.blockchain.Block,
req_args={'number': block_number}) req_args={'number': block_number})
signed_raw = "{0}{1}\n".format(block['raw'], signed_raw = "{0}{1}\n".format(block['raw'],
...@@ -128,7 +129,8 @@ class Cache(): ...@@ -128,7 +129,8 @@ class Cache():
logging.debug("Error in {0}".format(block_number)) logging.debug("Error in {0}".format(block_number))
raise raise
for tx in block_doc.transactions: for tx in block_doc.transactions:
self._parse_transaction(community, tx, block_number, block_doc.mediantime) self._parse_transaction(community, tx, block_number,
block_doc.mediantime, received_list)
awaiting = [t for t in self._transfers awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING] if t.state == Transfer.AWAITING]
...@@ -139,8 +141,9 @@ class Cache(): ...@@ -139,8 +141,9 @@ class Cache():
transfer.check_registered(tx, block_number, transfer.check_registered(tx, block_number,
block_doc.mediantime) block_doc.mediantime)
def refresh(self, community): def refresh(self, community, received_list):
current_block = 0 current_block = 0
received_list = []
try: try:
block_data = community.current_blockid() block_data = community.current_blockid()
current_block = block_data['number'] current_block = block_data['number']
...@@ -160,7 +163,7 @@ class Cache(): ...@@ -160,7 +163,7 @@ class Cache():
self.wallet.refresh_progressed.emit(self.latest_block, current_block) self.wallet.refresh_progressed.emit(self.latest_block, current_block)
for block_number in parsed_blocks: for block_number in parsed_blocks:
self._parse_block(community, block_number) self._parse_block(community, block_number, received_list)
self.wallet.refresh_progressed.emit(current_block - block_number, self.wallet.refresh_progressed.emit(current_block - block_number,
current_block - self.latest_block) current_block - self.latest_block)
...@@ -249,7 +252,7 @@ class Wallet(QObject): ...@@ -249,7 +252,7 @@ class Wallet(QObject):
data[currency] = self.caches[currency].jsonify() data[currency] = self.caches[currency].jsonify()
return data return data
def refresh_cache(self, community): def refresh_cache(self, community, received_list):
''' '''
Refresh the cache of this wallet for the specified community. Refresh the cache of this wallet for the specified community.
...@@ -257,7 +260,7 @@ class Wallet(QObject): ...@@ -257,7 +260,7 @@ class Wallet(QObject):
''' '''
if community.currency not in self.caches: if community.currency not in self.caches:
self.caches[community.currency] = Cache(self) self.caches[community.currency] = Cache(self)
self.caches[community.currency].refresh(community) self.caches[community.currency].refresh(community, received_list)
def check_password(self, salt, password): def check_password(self, salt, password):
''' '''
......
...@@ -9,9 +9,12 @@ import time ...@@ -9,9 +9,12 @@ import time
from requests.exceptions import RequestException from requests.exceptions import RequestException
from ...tools.exceptions import NoPeerAvailable from ...tools.exceptions import NoPeerAvailable
from .watcher import Watcher from .watcher import Watcher
from PyQt5.QtCore import pyqtSignal
class BlockchainWatcher(Watcher): class BlockchainWatcher(Watcher):
new_transfers = pyqtSignal(list)
def __init__(self, account, community): def __init__(self, account, community):
super().__init__() super().__init__()
self.account = account self.account = account
...@@ -22,17 +25,20 @@ class BlockchainWatcher(Watcher): ...@@ -22,17 +25,20 @@ class BlockchainWatcher(Watcher):
def watch(self): def watch(self):
try: try:
received_list = []
block_number = self.community.network.latest_block block_number = self.community.network.latest_block
if self.last_block != block_number: if self.last_block != block_number:
if not self.exiting: if not self.exiting:
self.community.refresh_cache() self.community.refresh_cache()
for w in self.account.wallets: for w in self.account.wallets:
if not self.exiting: if not self.exiting:
w.refresh_cache(self.community) w.refresh_cache(self.community, received_list)
logging.debug("New block, {0} mined in {1}".format(block_number, logging.debug("New block, {0} mined in {1}".format(block_number,
self.community.currency)) self.community.currency))
self.last_block = block_number self.last_block = block_number
if len(received_list) > 0:
self.new_transfers.emit(received_list)
except NoPeerAvailable: except NoPeerAvailable:
pass pass
except RequestException as e: except RequestException as e:
......
...@@ -16,6 +16,7 @@ from .wallets_tab import WalletsTabWidget ...@@ -16,6 +16,7 @@ from .wallets_tab import WalletsTabWidget
from .transactions_tab import TransactionsTabWidget from .transactions_tab import TransactionsTabWidget
from .network_tab import NetworkTabWidget from .network_tab import NetworkTabWidget
from .informations_tab import InformationsTabWidget from .informations_tab import InformationsTabWidget
from .toast import Toast
from ..tools.exceptions import MembershipNotFoundError from ..tools.exceptions import MembershipNotFoundError
from ..core.person import Person from ..core.person import Person
...@@ -56,6 +57,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -56,6 +57,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
bc_watcher = self.app.monitor.blockchain_watcher(self.community) bc_watcher = self.app.monitor.blockchain_watcher(self.community)
bc_watcher.error.connect(self.display_error) bc_watcher.error.connect(self.display_error)
bc_watcher.watching_stopped.connect(self.refresh_data) bc_watcher.watching_stopped.connect(self.refresh_data)
bc_watcher.new_transfers.connect(self.notify_transfers)
person = Person.lookup(self.app.current_account.pubkey, self.community) person = Person.lookup(self.app.current_account.pubkey, self.community)
try: try:
...@@ -176,6 +178,18 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -176,6 +178,18 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
icon = '<img src=":/icons/disconnected" width="12" height="12"/>' icon = '<img src=":/icons/disconnected" width="12" height="12"/>'
self.status_label.setText("{0}{1}".format(icon, text)) self.status_label.setText("{0}{1}".format(icon, text))
@pyqtSlot(list)
def notify_transfers(self, transfers_list):
text = self.tr("Received {0} {1}")
amount = 0
currency = self.community.name
for t in transfers_list:
amount += t.metadata['amount']
text += """{0}
""".format(t.metadata['uid'])
text.format(amount, currency)
Toast(text)
def refresh_wallets(self): def refresh_wallets(self):
if self.app.current_account: if self.app.current_account:
self.tab_wallets.refresh() self.tab_wallets.refresh()
......
'''
Created on 1 mai 2015
@author: inso
'''
import sys, time
from PyQt5.QtCore import Qt, QThread
from PyQt5.QtWidgets import QMainWindow
from ..gen_resources.toast_uic import Ui_Toast
window = None # global
class Toast(QMainWindow, Ui_Toast):
def __init__(self, msg):
global window # some space outside the local stack
window = self # save pointer till killed to avoid GC
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setupUi(self)
self.display.setText(msg)
self.toastThread = ToastThread() # start thread to remove display
self.toastThread.finished.connect(self.toastDone)
self.toastThread.start()
self.show()
def toastDone(self):
global window
window = None # kill pointer to window object to close it and GC
class ToastThread(QThread):
def __init__(self):
QThread.__init__(self)
def run(self):
time.sleep(2.0)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment