Added the ability to filter cards.

master
Pacman Ghost 7 years ago
parent 1eca937d9c
commit 941ef99d27
  1. 64
      add_card_widget.py
  2. 2
      main_window.py
  3. 129
      ui/add_card_widget.ui

@ -36,6 +36,7 @@ class AddCardWidget( QWidget ) :
# connect our handlers
self.cbo_nationality.currentIndexChanged[str].connect( self.on_nationality_changed )
self.lb_cards.itemDoubleClicked.connect( self.on_card_doubleclicked )
self.le_filter.textChanged.connect( self.on_filter_textChanged )
self.ok_button.clicked.connect( self.on_ok )
self.cancel_button.clicked.connect( self.on_cancel )
for rb in [self.rb_vehicles,self.rb_ordnance] :
@ -44,6 +45,40 @@ class AddCardWidget( QWidget ) :
self.cbo_nationality.setCurrentIndex( 0 )
self.on_nationality_changed( self.cbo_nationality.itemText(0) )
def _reload_cards( self , focus ) :
"""Reload the available cards."""
# initialize
self.lb_cards.clear()
# figure out what type of cards to show
if self.rb_vehicles.isChecked() :
card_type = db.TAGTYPE_VEHICLE
elif self.rb_ordnance.isChecked() :
card_type = db.TAGTYPE_ORDNANCE
else :
return
cards = globals.cards[ self.cbo_nationality.currentText() ]
cards = cards.get( card_type )
if cards is None :
assert False
return
# prepare for filtering
def filter_val( str ) :
return str.replace(" ","").lower()
filter_text = filter_val( self.le_filter.text() )
# reload the available cards
for card in cards :
if filter_text and filter_val(card.name).find( filter_text ) < 0 :
continue
item = QListWidgetItem( card.name )
item.setData( Qt.UserRole , card )
self.lb_cards.addItem( item )
self.lbl_cards.setText(
"<html><b>Cards:</b>{}</html>".format( " <small><i>(filtered)</i></small>" if filter_text else "" )
)
self.lb_cards.setCurrentRow( 0 )
if focus :
self.lb_cards.setFocus()
def on_ok( self ) :
"""Accept the currently selected card."""
item = self.lb_cards.currentItem()
@ -74,30 +109,17 @@ class AddCardWidget( QWidget ) :
rb.setChecked( True )
break
# reload the cards
self.on_card_type_changed()
self.le_filter.setText( "" )
self._reload_cards( True )
def on_card_type_changed( self ) :
"""Update the widget when the active card type is changed."""
self.lb_cards.clear()
# figure out what type of cards to show
if self.rb_vehicles.isChecked() :
card_type = db.TAGTYPE_VEHICLE
elif self.rb_ordnance.isChecked() :
card_type = db.TAGTYPE_ORDNANCE
else :
return
# reload the available cards
cards = globals.cards[ self.cbo_nationality.currentText() ]
cards = cards.get( card_type )
if cards is None :
assert False
return
for card in cards :
item = QListWidgetItem( card.name )
item.setData( Qt.UserRole , card )
self.lb_cards.addItem( item )
self.lb_cards.setCurrentRow( 0 )
self.lb_cards.setFocus()
self.le_filter.setText( "" )
self._reload_cards( True )
def on_filter_textChanged( self , val ) :
"""Update the widget when the filter text is changed."""
self._reload_cards( False )
def on_card_doubleclicked( self , item ) :
# handle the event

@ -210,7 +210,7 @@ class MainWindow( QMainWindow ) :
)
self.tab_widget.setCurrentIndex( index )
self._update_ui()
widget.setFocus() # FIXME! s.b. to first child control
widget.lb_cards.setFocus()
def on_add_card_accepted( self , card ) :
"""Handle the user's card selection."""
index = self._find_add_card_tab()

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>450</width>
<height>300</height>
<width>440</width>
<height>356</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@ -43,7 +43,7 @@
<widget class="QRadioButton" name="rb_ordnance">
<property name="geometry">
<rect>
<x>290</x>
<x>300</x>
<y>0</y>
<width>89</width>
<height>26</height>
@ -56,7 +56,7 @@
<widget class="QComboBox" name="cbo_nationality">
<property name="geometry">
<rect>
<x>80</x>
<x>90</x>
<y>0</y>
<width>120</width>
<height>28</height>
@ -86,10 +86,16 @@
<rect>
<x>0</x>
<y>-1</y>
<width>75</width>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>&amp;Nationality:</string>
</property>
@ -100,21 +106,16 @@
<widget class="QRadioButton" name="rb_vehicles">
<property name="geometry">
<rect>
<x>210</x>
<x>220</x>
<y>0</y>
<width>79</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>&amp;Vehicles</string>
<string>Ve&amp;hicles</string>
</property>
</widget>
<zorder>label</zorder>
<zorder>rb_ordnance</zorder>
<zorder>cbo_nationality</zorder>
<zorder>label</zorder>
<zorder>rb_vehicles</zorder>
</widget>
</item>
<item>
@ -131,28 +132,90 @@
<height>20</height>
</size>
</property>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>51</width>
<height>19</height>
</rect>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
</size>
<property name="leftMargin">
<number>0</number>
</property>
<property name="text">
<string>&amp;Cards:</string>
<property name="topMargin">
<number>0</number>
</property>
<property name="buddy">
<cstring>lb_cards</cstring>
<property name="rightMargin">
<number>0</number>
</property>
</widget>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="lbl_cards">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
</size>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>&amp;Cards:</string>
</property>
<property name="buddy">
<cstring>lb_cards</cstring>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Fi&amp;lter:</string>
</property>
<property name="buddy">
<cstring>le_filter</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="le_filter">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
@ -237,7 +300,13 @@
</layout>
</widget>
<tabstops>
<tabstop>cbo_nationality</tabstop>
<tabstop>rb_vehicles</tabstop>
<tabstop>rb_ordnance</tabstop>
<tabstop>le_filter</tabstop>
<tabstop>lb_cards</tabstop>
<tabstop>cancel_button</tabstop>
<tabstop>ok_button</tabstop>
</tabstops>
<resources/>
<connections/>

Loading…
Cancel
Save