Files
purple-electrumwallet/electrum/gui/qml/__init__.py
T

119 lines
4.0 KiB
Python
Raw Normal View History

import os
import signal
import sys
import threading
2022-07-27 10:23:35 +02:00
from typing import TYPE_CHECKING
try:
2023-07-14 13:51:08 +02:00
import PyQt6
except Exception as e:
from electrum import GuiImportError
raise GuiImportError(
"Error: Could not import PyQt6. On Linux systems, "
"you may try 'sudo apt-get install python3-pyqt6'") from e
try:
2023-07-14 13:51:08 +02:00
import PyQt6.QtQml
except Exception as e:
from electrum import GuiImportError
raise GuiImportError(
"Error: Could not import PyQt6.QtQml. On Linux systems, "
"you may try 'sudo apt-get install python3-pyqt6.qtquick'") from e
2023-07-14 13:51:08 +02:00
from PyQt6.QtCore import (Qt, QCoreApplication, QLocale, QTranslator, QTimer, QT_VERSION_STR, PYQT_VERSION_STR)
from PyQt6.QtGui import QGuiApplication
sys._GUI_QT_VERSION = 6 # used by gui/common_qt
2023-09-22 16:34:28 +02:00
from electrum.i18n import _
from electrum.plugin import run_hook
2023-01-10 17:01:54 +01:00
from electrum.util import profiler
2022-07-27 10:23:35 +02:00
from electrum.logging import Logger
2023-03-28 15:24:40 +00:00
from electrum.gui import BaseElectrumGui
if TYPE_CHECKING:
from electrum.daemon import Daemon
from electrum.simple_config import SimpleConfig
from electrum.plugin import Plugins
from .qeapp import ElectrumQmlApplication, Exception_Hook
2021-12-01 01:02:52 +01:00
2023-04-11 15:19:29 +02:00
2023-01-30 12:52:50 +01:00
class ElectrumTranslator(QTranslator):
def __init__(self, parent=None):
super().__init__(parent)
def translate(self, context, source_text, disambiguation, n):
return _(source_text, context=context)
2023-01-30 12:52:50 +01:00
2023-07-11 12:51:37 +02:00
2023-03-28 15:24:40 +00:00
class ElectrumGui(BaseElectrumGui, Logger):
@profiler
def __init__(self, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'):
2023-03-28 15:24:40 +00:00
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
Logger.__init__(self)
2023-04-11 15:19:29 +02:00
# uncomment to debug plugin and import tracing
# os.environ['QML_IMPORT_TRACE'] = '1'
# os.environ['QT_DEBUG_PLUGINS'] = '1'
2023-04-24 11:27:36 +02:00
os.environ['QT_ANDROID_DISABLE_ACCESSIBILITY'] = '1'
# set default locale to en_GB. This is for l10n (e.g. number formatting, number input etc),
# but not for i18n, which is handled by the Translator
# this can be removed once the backend wallet is fully l10n aware
QLocale.setDefault(QLocale('en_GB'))
2023-01-10 17:01:54 +01:00
self.logger.info(f"Qml GUI starting up... Qt={QT_VERSION_STR}, PyQt={PYQT_VERSION_STR}")
self.logger.info("CWD=%s" % os.getcwd())
# Uncomment this call to verify objects are being properly
# GC-ed when windows are closed
#network.add_jobs([DebugMem([Abstract_Wallet, SPV, Synchronizer,
# ElectrumWindow], interval=5)])
2023-07-14 13:51:08 +02:00
2023-01-10 17:01:54 +01:00
if hasattr(Qt, "AA_ShareOpenGLContexts"):
QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
if hasattr(QGuiApplication, 'setDesktopFileName'):
QGuiApplication.setDesktopFileName('electrum.desktop')
2021-09-10 12:04:36 +02:00
2022-10-31 16:13:22 +00:00
if "QT_QUICK_CONTROLS_STYLE" not in os.environ:
2021-09-10 12:04:36 +02:00
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
self.gui_thread = threading.current_thread()
2023-03-28 15:24:40 +00:00
self.app = ElectrumQmlApplication(sys.argv, config=config, daemon=daemon, plugins=plugins)
2023-01-30 12:52:50 +01:00
self.translator = ElectrumTranslator()
self.app.installTranslator(self.translator)
2023-01-10 17:01:54 +01:00
# timer
self.timer = QTimer(self.app)
self.timer.setSingleShot(False)
self.timer.setInterval(500) # msec
2023-09-22 16:34:28 +02:00
self.timer.timeout.connect(lambda: None) # periodically enter python scope
2023-01-10 17:01:54 +01:00
# hook for crash reporter
Exception_Hook.maybe_setup(config=config, slot=self.app.appController.crash)
# Initialize any QML plugins
2023-07-11 12:51:37 +02:00
run_hook('init_qml', self.app)
self.app.engine.load('electrum/gui/qml/components/main.qml')
def close(self):
self.app.quit()
def main(self):
if not self.app._valid:
return
self.timer.start()
2023-07-14 13:51:08 +02:00
signal.signal(signal.SIGINT, lambda *args: self._handle_sigint())
self.logger.info('Entering main loop')
2023-07-14 13:51:08 +02:00
self.app.exec()
def _handle_sigint(self):
self.app.appController.wantClose = True
self.stop()
def stop(self):
self.logger.info('closing GUI')
self.app.quit()