Skip to content
Snippets Groups Projects
mainwindow.py 5.65 KiB
Newer Older
inso's avatar
inso committed
'''
Created on 1 févr. 2014

@author: inso
'''
from cutecoin.gen_resources.mainwindow_uic import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog
inso's avatar
inso committed
from PyQt5.QtCore import QSignalMapper, QModelIndex, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from cutecoin.gui.process_cfg_account import ProcessConfigureAccount
from cutecoin.gui.transfer import TransferMoneyDialog
from cutecoin.gui.currency_tab import CurrencyTabWidget
from cutecoin.gui.add_contact import AddContactDialog
from cutecoin.gui.import_account import ImportAccountDialog
from cutecoin.gui.certification import CertificationDialog

inso's avatar
inso committed
from ucoinpy.api import bma

inso's avatar
inso committed
import time


class BlockchainInspector(QThread):
    def __init__(self, community):
        QThread.__init__(self)
        self.community = community
        self.exiting = False
        self.last_block = self.community.request(bma.blockchain.Current)['number']

    def __del__(self):
        self.exiting = True
        self.wait()

    def run(self):
        while not self.exiting:
            time.sleep(10)
            current_block = self.community.request(bma.blockchain.Current)
            if self.last_block != current_block['number']:
                logging.debug("New block, {0} mined in {1}".format(self.last_block,
                                                                   self.community.currency))
                self.new_block_mined.emit(current_block)
                self.last_block = current_block['number']

    new_block_mined = pyqtSignal(int)
inso's avatar
inso committed

inso's avatar
inso committed
class MainWindow(QMainWindow, Ui_MainWindow):
inso's avatar
inso committed
    '''
    classdocs
    '''

    def __init__(self, app):
inso's avatar
inso committed
        '''
        Constructor
        '''
        # Set up the user interface from Designer.
inso's avatar
inso committed
        super().__init__()
inso's avatar
inso committed
        self.setupUi(self)
        self.refresh()
    def open_add_account_dialog(self):
        dialog = ProcessConfigureAccount(self.app, None)
        dialog.accepted.connect(self.refresh)
inso's avatar
inso committed
        dialog.exec_()

    def action_change_account(self, account_name):
        self.app.change_current_account(self.app.get_account(account_name))
        self.refresh()
    def open_transfer_money_dialog(self):
        dialog = TransferMoneyDialog(self.app.current_account)
inso's avatar
inso committed
        dialog.accepted.connect(self.refresh_wallets)
        dialog.exec_()
        currency_tab = self.currencies_tabwidget.currentWidget()
        currency_tab.list_transactions_sent.model().dataChanged.emit(
                                                             QModelIndex(),
                                                             QModelIndex(), ())
    def open_certification_dialog(self):
        dialog = CertificationDialog(self.app.current_account)
        dialog.exec_()

    def open_add_contact_dialog(self):
        AddContactDialog(self.app.current_account, self).exec_()
inso's avatar
inso committed

    def open_configure_account_dialog(self):
        dialog = ProcessConfigureAccount(self.app, self.app.current_account)
inso's avatar
inso committed
        dialog.accepted.connect(self.refresh_wallets)
inso's avatar
inso committed
        dialog.exec_()
inso's avatar
inso committed

inso's avatar
inso committed
    def refresh_wallets(self):
        currency_tab = self.currencies_tabwidget.currentWidget()
        currency_tab.refresh_wallets()

    '''
    Refresh main window
    When the selected account changes, all the widgets
    in the window have to be refreshed
    '''

    def refresh(self):
        self.menu_change_account.clear()
        signal_mapper = QSignalMapper(self)
inso's avatar
inso committed
        for account_name in self.app.accounts.keys():
            action = QAction(account_name, self)
            self.menu_change_account.addAction(action)
inso's avatar
inso committed
            signal_mapper.setMapping(action, account_name)
            action.triggered.connect(signal_mapper.map)
            signal_mapper.mapped[str].connect(self.action_change_account)
        if self.app.current_account is None:
inso's avatar
inso committed
            self.menu_contacts.setEnabled(False)
            self.menu_actions.setEnabled(False)
inso's avatar
inso committed
            self.menu_contacts.setEnabled(True)
            self.menu_actions.setEnabled(True)
            self.label_account_name.setText(
                "Current account : " +
                self.app.current_account.name)
inso's avatar
inso committed

            self.currencies_tabwidget.clear()
            for community in self.app.current_account.communities:
                tab_currency = CurrencyTabWidget(self.app, community)
inso's avatar
inso committed
                bc_inspector = BlockchainInspector(community)
                bc_inspector.new_block_mined.connect(tab_currency.refresh_block)
                bc_inspector.start()
                tab_currency.refresh()
                self.currencies_tabwidget.addTab(tab_currency,
                                                 QIcon(":/icons/currency_icon"),
                                                 community.name())

            self.menu_contacts_list.clear()
            for contact in self.app.current_account.contacts:
                self.menu_contacts_list.addAction(contact.name)

    def import_account(self):
        dialog = ImportAccountDialog(self.app, self)
        dialog.accepted.connect(self.refresh)
        dialog.exec_()

    def export_account(self):
        selected_file = QFileDialog.getSaveFileName(self,
                                          "Export an account",
                                          "",
                                          "All account files (*.acc)")
        path = ""
        if selected_file[0][-4:] == ".acc":
            path = selected_file[0]
        else:
            path = selected_file[0] + ".acc"
        self.app.export_account(path, self.app.current_account)

    def closeEvent(self, event):
        if self.app.current_account:
            self.app.save_cache(self.app.current_account)
        super().closeEvent(event)