Show a flag in the tab for each card.

master
Pacman Ghost 7 years ago
parent f8dbc27801
commit 4d173ab30b
  1. 3
      _freeze.py
  2. 50
      asl_cards/natinfo.py
  3. BIN
      asl_cards/natinfo/flags/allied-minor.png
  4. BIN
      asl_cards/natinfo/flags/american.png
  5. BIN
      asl_cards/natinfo/flags/axis-minor.png
  6. BIN
      asl_cards/natinfo/flags/british.png
  7. BIN
      asl_cards/natinfo/flags/chinese-gmd.png
  8. BIN
      asl_cards/natinfo/flags/chinese.png
  9. BIN
      asl_cards/natinfo/flags/finnish.png
  10. BIN
      asl_cards/natinfo/flags/french.png
  11. BIN
      asl_cards/natinfo/flags/german.png
  12. BIN
      asl_cards/natinfo/flags/italian.png
  13. BIN
      asl_cards/natinfo/flags/japanese.png
  14. BIN
      asl_cards/natinfo/flags/russian.png
  15. 81
      asl_cards/natinfo/natinfo.json
  16. 0
      asl_cards/tests/natinfo-data/flags/american.png
  17. 0
      asl_cards/tests/natinfo-data/flags/japanese-flag.gif
  18. 20
      asl_cards/tests/natinfo-data/natinfo.json
  19. 32
      asl_cards/tests/test_natinfo.py
  20. 8
      main.py
  21. 7
      main_window.py

@ -25,7 +25,8 @@ extra_files = []
extra_files.extend( get_extra_files( "index/*.txt" ) )
extra_files.extend( get_extra_files( "resources/*.ico" ) )
extra_files.extend( get_extra_files( "resources/*.png" ) )
extra_files.extend( get_extra_files( "ui/*ui" ) )
extra_files.extend( get_extra_files( "ui/*.ui" ) )
extra_files.extend( get_extra_files( "natinfo/*" ) )
build_options = {
"packages": [ "os" , "sqlalchemy" ] ,
"excludes": [ "tkinter" ] ,

@ -0,0 +1,50 @@
""" Manage information about each nationality.
"""
import sys
import os
import json
_nat_info = None
_base_dir = None
# ---------------------------------------------------------------------
def _get_key( nat ) :
"""Get the JSON key for a nationality.
We require the JSON keys to be lower-case, with no spaces (not strictly necessary, but not a bad idea :-/).
"""
return nat.lower().replace( " " , "-" )
# ---------------------------------------------------------------------
def load( base_dir ) :
"""Load the nationality information."""
global _base_dir , _nat_info
_base_dir = os.path.abspath( base_dir )
fname = os.path.join( base_dir , "natinfo.json" )
if os.path.isfile( fname ) :
with open( fname , "r" ) as fp :
_nat_info = json.load( fp )
else :
_nat_info = {}
def dump() :
"""Dump the nationality information."""
for nat in sorted(_nat_info.keys()) :
print( "{}:".format( nat ) )
print( "- flag = {}".format( get_flag(nat) ) )
def get_flag( nat ) :
"""Locate the flag image file for a nationality.
These are set in the JSON data file at $/{nat}/flag. If there is no entry, the default is $/flags/${nat}.png
"""
key = _get_key( nat )
try :
fname = _nat_info[ key ][ "flag" ]
except ( KeyError , TypeError ) :
fname = key + ".png"
fname = os.path.join( _base_dir , os.path.join("flags", fname) )
return fname if os.path.isfile(fname) else None

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

@ -0,0 +1,81 @@
{
"allied-minor": {
} ,
"american": {
} ,
"axis-minor": {
} ,
"belgian": {
"flag": "allied-minor.png"
} ,
"british": {
} ,
"bulgarian": {
"flag": "axis-minor.png"
} ,
"chinese": {
} ,
"croatian": {
"flag": "axis-minor.png"
} ,
"danish": {
"flag": "axis-minor.png"
} ,
"dutch": {
"flag": "allied-minor.png"
} ,
"finnish": {
"flag": "axis-minor.png"
} ,
"french": {
} ,
"german": {
} ,
"greek": {
"flag": "allied-minor.png"
} ,
"hungarian": {
"flag": "axis-minor.png"
} ,
"italian": {
} ,
"japanese": {
} ,
"polish": {
"flag": "allied-minor.png"
} ,
"romanian": {
"flag": "axis-minor.png"
} ,
"russian": {
} ,
"slovakian": {
"flag": "axis-minor.png"
} ,
"yugoslav": {
"flag": "allied-minor.png"
}
}

@ -0,0 +1,20 @@
{
"german": {
"_comment_": "this nationality has no flag"
} ,
"russian": {
"flag": "_missing-file_"
} ,
"american" :{
"_comment_": "this nationality has a default flag"
} ,
"japanese" :{
"flag": "japanese-flag.gif"
} ,
"allied-minor": {
}
}

@ -0,0 +1,32 @@
#!/usr/bin/env python3
import sys
import os
import unittest
from _test_case_base import TestCaseBase
from asl_cards import natinfo
# ---------------------------------------------------------------------
class TestNatInfo( TestCaseBase ) :
"""Test nationality info."""
@classmethod
def setUpClass( cls ) :
# load the nationality info
base_dir = os.path.split( __file__ )[ 0 ]
fname = os.path.join( base_dir , "natinfo-data" )
natinfo.load( fname )
def test_flags( self ) :
"""Test locating the flag image files for each nationality."""
self.assertIsNone( natinfo.get_flag( "xxx" ) )
self.assertIsNone( natinfo.get_flag( "german" ) )
self.assertTrue( natinfo.get_flag("american").endswith( "/flags/american.png" ) )
self.assertTrue( natinfo.get_flag("japanese").endswith( "/flags/japanese-flag.gif" ) )
# ---------------------------------------------------------------------
if __name__ == "__main__" :
unittest.main()

@ -7,6 +7,7 @@ import getopt
from PyQt5.QtCore import QSettings , QDir
from PyQt5.QtWidgets import QApplication
from asl_cards import natinfo
from constants import *
import globals
@ -53,6 +54,13 @@ def do_main( args ) :
fname = os.path.join( os.path.split(settings_fname)[0] , "debug.ini" )
globals.debug_settings = QSettings( fname , QSettings.IniFormat )
# initialize
dname = os.path.join( globals.base_dir , "natinfo" )
if not os.path.isdir( dname ) :
# nb: for a dev environment (or you can create a symlink natinfo -> asl_cards/natinfo)
dname = os.path.join( globals.base_dir , "asl_cards/natinfo" )
natinfo.load( dname )
# do main processing
app = QApplication( sys.argv )
from main_window import MainWindow

@ -7,6 +7,7 @@ from PyQt5.QtWidgets import QMessageBox , QAction
from PyQt5.QtGui import QPainter , QPixmap , QIcon , QBrush
import asl_cards.db as db
from asl_cards import natinfo
from constants import *
import globals
from add_card_widget import AddCardWidget
@ -179,7 +180,11 @@ class MainWindow( QMainWindow ) :
index = self._find_add_card_tab()
assert index is not None
self.tab_widget.removeTab( index )
widget = self.tab_widget.insertTab( index , AslCardWidget(card) , card.name )
fname = natinfo.get_flag( card.nationality )
if fname :
widget = self.tab_widget.insertTab( index , AslCardWidget(card) , QIcon(fname) , card.name )
else :
widget = self.tab_widget.insertTab( index , AslCardWidget(card) , card.name )
self.tab_widget.setCurrentIndex( index )
self._update_ui()
def on_add_card_cancelled( self ) :

Loading…
Cancel
Save