Skip to content

Commit

Permalink
JalIcons class modified to be a descendant of UserDict for better code.
Browse files Browse the repository at this point in the history
  • Loading branch information
titov-vv committed Feb 3, 2024
1 parent 456a382 commit 70d4898
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
62 changes: 31 additions & 31 deletions jal/db/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jal.db.asset import JalAsset
from jal.db.closed_trade import JalClosedTrade
from jal.widgets.helpers import ts2dt
from jal.widgets.icons import JalIcons
from jal.widgets.icons import JalIcon


# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, operation_data=None):
self._subtype = 0
self._data = None
self._view_rows = 1 # How many rows it will require operation in QTableView
self._icon = JalIcons().icon(JalIcons.NONE)
self._icon = JalIcon[JalIcon.NONE]
self._timestamp = 0
self._account = None
self._account_name = ''
Expand Down Expand Up @@ -300,10 +300,10 @@ def __init__(self, operation_id=None):
self._details.append(self._read_record(details_query, named=True))
self._amount = sum(Decimal(line['amount']) for line in self._details)
if self._amount < 0:
self._icon = JalIcons().icon(JalIcons.MINUS)
self._icon = JalIcon[JalIcon.MINUS]
self._oname = self.tr("Spending")
else:
self._icon = JalIcons().icon(JalIcons.PLUS)
self._icon = JalIcon[JalIcon.PLUS]
self._oname = self.tr("Income")
if self._currency:
self._view_rows = 2
Expand Down Expand Up @@ -399,11 +399,11 @@ class Dividend(LedgerTransaction):

def __init__(self, operation_id=None):
icons = {
Dividend.Dividend: JalIcons.DIVIDEND,
Dividend.BondInterest: JalIcons.BOND_INTEREST,
Dividend.StockDividend: JalIcons.STOCK_DIVIDEND,
Dividend.StockVesting: JalIcons.STOCK_VESTING,
Dividend.BondAmortization: JalIcons.BOND_AMORTIZATION
Dividend.Dividend: JalIcon.DIVIDEND,
Dividend.BondInterest: JalIcon.BOND_INTEREST,
Dividend.StockDividend: JalIcon.STOCK_DIVIDEND,
Dividend.StockVesting: JalIcon.STOCK_VESTING,
Dividend.BondAmortization: JalIcon.BOND_AMORTIZATION
}
self.names = {
Dividend.NA: self.tr("UNDEFINED"),
Expand All @@ -426,7 +426,7 @@ def __init__(self, operation_id=None):
self._subtype = self._data['type']
self._oname = self.names[self._subtype]
try:
self._icon = JalIcons().icon(icons[self._subtype])
self._icon = JalIcon[icons[self._subtype]]
except KeyError:
assert False, "Unknown dividend type"
self._timestamp = self._data['timestamp']
Expand Down Expand Up @@ -658,10 +658,10 @@ def __init__(self, operation_data=None):
self._note = self._data['note']
self._broker = self._account.organization()
if self._qty < Decimal('0'):
self._icon = JalIcons().icon(JalIcons.SELL)
self._icon = JalIcon[JalIcon.SELL]
self._oname = self.tr("Sell")
else:
self._icon = JalIcons().icon(JalIcons.BUY)
self._icon = JalIcon[JalIcon.BUY]
self._oname = self.tr("Buy")

def settlement(self) -> int:
Expand Down Expand Up @@ -779,9 +779,9 @@ class Transfer(LedgerTransaction):

def __init__(self, operation_id=None, display_type=None):
icons = {
Transfer.Outgoing: JalIcons.TRANSFER_OUT,
Transfer.Incoming: JalIcons.TRANSFER_IN,
Transfer.Fee: JalIcons.FEE
Transfer.Outgoing: JalIcon.TRANSFER_OUT,
Transfer.Incoming: JalIcon.TRANSFER_IN,
Transfer.Fee: JalIcon.FEE
}
self.names = {
Transfer.NA: self.tr("UNDEFINED"),
Expand Down Expand Up @@ -811,7 +811,7 @@ def __init__(self, operation_id=None, display_type=None):
self._fee_account_name = self._fee_account.name()
self._fee = Decimal(self._data['fee']) if self._data['fee'] else Decimal('0')
try:
self._icon = JalIcons().icon(icons[display_type])
self._icon = JalIcon[icons[display_type]]
self._oname = self.names[display_type]
except KeyError:
assert False, "Unknown transfer type"
Expand Down Expand Up @@ -1020,12 +1020,12 @@ class CorporateAction(LedgerTransaction):

def __init__(self, operation_id=None):
icons = {
CorporateAction.NA: JalIcons.NONE,
CorporateAction.Merger: JalIcons.MERGER,
CorporateAction.SpinOff: JalIcons.SPINOFF,
CorporateAction.Split: JalIcons.SPLIT,
CorporateAction.SymbolChange: JalIcons.SYMBOL_CHANGE,
CorporateAction.Delisting: JalIcons.DELISTING
CorporateAction.NA: JalIcon.NONE,
CorporateAction.Merger: JalIcon.MERGER,
CorporateAction.SpinOff: JalIcon.SPINOFF,
CorporateAction.Split: JalIcon.SPLIT,
CorporateAction.SymbolChange: JalIcon.SYMBOL_CHANGE,
CorporateAction.Delisting: JalIcon.DELISTING
}
self.names = {
CorporateAction.NA: self.tr("UNDEFINED"),
Expand All @@ -1049,7 +1049,7 @@ def __init__(self, operation_id=None):
self._oname = self.names[self._subtype]
if self._subtype == CorporateAction.SpinOff or self._view_rows < 2:
self._view_rows = 2
self._icon = JalIcons().icon(icons[self._subtype])
self._icon = JalIcon[icons[self._subtype]]
self._timestamp = self._data['timestamp']
self._account = jal.db.account.JalAccount(self._data['account_id'])
self._account_name = self._account.name()
Expand Down Expand Up @@ -1218,13 +1218,13 @@ class TermDeposit(LedgerTransaction):

def __init__(self, operation_id=None, display_type=None):
icons = {
DepositActions.Opening: JalIcons.DEPOSIT_OPEN,
DepositActions.TopUp: JalIcons.DEPOSIT_OPEN,
DepositActions.Renewal: JalIcons.DEPOSIT_OPEN,
DepositActions.PartialWithdrawal: JalIcons.DEPOSIT_CLOSE,
DepositActions.Closing: JalIcons.DEPOSIT_CLOSE,
DepositActions.InterestAccrued: JalIcons.INTEREST,
DepositActions.TaxWithheld: JalIcons.TAX
DepositActions.Opening: JalIcon.DEPOSIT_OPEN,
DepositActions.TopUp: JalIcon.DEPOSIT_OPEN,
DepositActions.Renewal: JalIcon.DEPOSIT_OPEN,
DepositActions.PartialWithdrawal: JalIcon.DEPOSIT_CLOSE,
DepositActions.Closing: JalIcon.DEPOSIT_CLOSE,
DepositActions.InterestAccrued: JalIcon.INTEREST,
DepositActions.TaxWithheld: JalIcon.TAX
}
super().__init__(operation_id)
self._otype = LedgerTransaction.TermDeposit
Expand All @@ -1245,7 +1245,7 @@ def __init__(self, operation_id=None, display_type=None):
self._amount = self._get_deposit_amount()
else:
self._amount = Decimal(self._data['amount'])
self._icon = JalIcons().icon(icons[self._action])
self._icon = JalIcon[icons[self._action]]
self._oname = f'{DepositActions().get_name(self._action)}'

def _get_deposit_amount(self) -> Decimal:
Expand Down
11 changes: 7 additions & 4 deletions jal/widgets/icons.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from collections import UserDict
from PySide6.QtGui import QIcon
from jal.constants import Setup
from jal.db.helpers import get_app_path


ICON_PREFIX = "ui_"
class JalIcons:
class JalIcon(UserDict):
NONE = 0
BOND_AMORTIZATION = 1
BOND_INTEREST = 2
Expand Down Expand Up @@ -57,13 +58,15 @@ class JalIcons:
# initiates class loading all icons listed in self._icon_files from given directory img_path (should and
# with a system directory separator)
def __init__(self):
super().__init__()
if self._icons: # Already loaded - nothing to do
return
img_path = get_app_path() + Setup.ICONS_PATH + os.sep + ICON_PREFIX
for icon_id, filename in self._icon_files.items():
self._icons[icon_id] = QIcon(img_path + filename)

def icon(self, icon_id: int) -> QIcon:
if icon_id not in self._icons:
@classmethod
def __class_getitem__(cls, key) -> QIcon:
if key not in cls._icons:
return QIcon()
return self._icons[icon_id]
return cls._icons[key]
3 changes: 3 additions & 0 deletions jal/widgets/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from jal.widgets.operations_widget import OperationsWidget
from jal.widgets.tax_widget import TaxWidget, MoneyFlowWidget
from jal.widgets.helpers import dependency_present
from jal.widgets.icons import JalIcon
from jal.widgets.reference_dialogs import AccountListDialog, AssetListDialog, TagsListDialog,\
CategoryListDialog, QuotesListDialog, PeerListDialog, BaseCurrencyDialog
from jal.constants import Setup
Expand All @@ -41,6 +42,8 @@ def __init__(self, language):
self.restoreState(base64.decodebytes(JalSettings().getValue('WindowState', '').encode('utf-8')))

self.ledger = Ledger()
self.icons = JalIcon() # This variable is used to initialize JalIcons class and keep its cache in memory.
# It is not used directly but icons are accessed via @classmethod of JalIcons class

# Customize Status bar and logs
self.ProgressBar = QProgressBar(self)
Expand Down

0 comments on commit 70d4898

Please sign in to comment.