Changed the "add card" dialog to be a tab page instead.

master
Pacman Ghost 7 years ago
parent fe5ffd4324
commit a589147a12
  1. 45
      add_card_widget.py
  2. 57
      main_window.py
  3. 6
      ui/add_card_widget.ui

@ -1,49 +1,57 @@
import os
from PyQt5 import uic
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog , QListWidgetItem
from PyQt5.QtCore import Qt , pyqtSignal
from PyQt5.QtWidgets import QWidget , QListWidgetItem
import asl_cards.db as db
from asl_cards.db import AslCard
from constants import *
import globals
# ---------------------------------------------------------------------
class AddCardDialog( QDialog ) :
class AddCardWidget( QWidget ) :
"""Allow the user to select an ASL card, based on nationality & card type."""
# define our signals
accepted_signal = pyqtSignal( AslCard , name="accepted" )
cancelled_signal = pyqtSignal( name="cancelled" )
def __init__( self , parent ) :
# initialize
self.selected_card = None
super(AddCardDialog,self).__init__( parent=parent )
# initialize the dialog
uic.loadUi( os.path.join(globals.base_dir,"ui/add_card_dialog.ui") , self )
self.setMinimumSize( self.size() )
super().__init__( parent=parent )
# initialize the widget
uic.loadUi( os.path.join(globals.base_dir,"ui/add_card_widget.ui") , self )
self.lb_cards.setSortingEnabled( True )
w = self.buttons_widget
self.xmargin = w.x()
self.ymargin = self.size().height() - (w.y() + w.height())
# load the dialog
# load the widget
for nationality in globals.cards :
self.cbo_nationality.addItem( nationality )
# connect our handlers
self.cbo_nationality.currentIndexChanged[str].connect( self.on_nationality_changed )
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] :
rb.clicked.connect( self.on_card_type_changed )
# select the initial nationality (this will load the rest of the dialog)
# select the initial nationality (this will load the rest of the widget)
self.cbo_nationality.setCurrentIndex( 0 )
self.on_nationality_changed( self.cbo_nationality.itemText(0) )
def on_ok( self ) :
# accept the currently selected card
"""Accept the currently selected card."""
item = self.lb_cards.currentItem()
self.selected_card = item.data(Qt.UserRole) if item else None
self.accept()
card = item.data(Qt.UserRole) if item else None
self.accepted_signal.emit( card )
def on_cancel( self ) :
"""Cancel the widget."""
self.cancelled_signal.emit()
def on_nationality_changed( self , val ) :
"""Update the dialog when the active nationality is changed."""
"""Update the widget when the active nationality is changed."""
# reload the available cards for the selected nationality
cards = globals.cards[ val ]
self.lb_cards.clear()
@ -65,7 +73,7 @@ class AddCardDialog( QDialog ) :
self.on_card_type_changed()
def on_card_type_changed( self ) :
"""Update the dialog when the active card type is changed."""
"""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() :
@ -87,6 +95,13 @@ class AddCardDialog( QDialog ) :
self.lb_cards.setCurrentRow( 0 )
self.lb_cards.setFocus()
def keyPressEvent( self , evt ) :
# handle the event
if evt.key() == Qt.Key_Return :
if not self.lb_cards.currentItem() :
QApplication.beep()
self.on_ok()
def resizeEvent( self , evt ) :
# handle the event
w = self.buttons_widget

@ -3,13 +3,13 @@ import os
from PyQt5.QtCore import Qt , QPoint , QSize
from PyQt5.QtWidgets import QApplication , QMainWindow , QVBoxLayout , QHBoxLayout , QWidget , QTabWidget , QLabel
from PyQt5.QtWidgets import QDialog , QMessageBox , QAction
from PyQt5.QtWidgets import QMessageBox , QAction
from PyQt5.QtGui import QPainter , QPixmap , QIcon , QBrush
import asl_cards.db as db
from constants import *
import globals
from add_card_dialog import AddCardDialog
from add_card_widget import AddCardWidget
from startup_widget import StartupWidget
# ---------------------------------------------------------------------
@ -68,11 +68,10 @@ class MainWindow( QMainWindow ) :
self.close_tab_action.triggered.connect( self.on_close_tab )
file_menu.addAction( self.close_tab_action )
action = QAction( "E&xit" , self )
action.setShortcut( "Ctrl+Q" )
action.setStatusTip( "Close the program." )
action.triggered.connect( self.close )
file_menu.addAction( action )
self.tab_widget = None # FIXME! remove this when we make the "add card" dialog a tab
self.tab_widget = None
self._update_ui()
# load the window settings
self.resize( globals.app_settings.value( MAINWINDOW_SIZE , QSize(500,300) ) )
@ -112,6 +111,14 @@ class MainWindow( QMainWindow ) :
"""Update the window's UI."""
self.close_tab_action.setEnabled( self.tab_widget.count() > 0 if self.tab_widget else False )
def _find_add_card_tab( self ) :
"""Find the "add card" tab."""
if self.tab_widget :
for i in range(0,self.tab_widget.count()) :
if type( self.tab_widget.widget(i) ) is AddCardWidget :
return i
return None
def closeEvent( self , evt ) :
"""Handle window close."""
# confirm the close
@ -142,18 +149,38 @@ class MainWindow( QMainWindow ) :
self.close()
def on_add_card( self ) :
dlg = AddCardDialog( self )
rc = dlg.exec()
if rc == QDialog.Accepted :
# add a new tab for the selected card
card = dlg.selected_card
if not card :
assert False
return
w = AslCardWidget( card )
index = self.tab_widget.addTab( w , card.name )
"""Ask the user to select a new ASL card to show."""
# check if the "add card" tab is already open
index = self._find_add_card_tab()
if index is not None :
# yup - switch to it
self.tab_widget.setCurrentIndex( index )
self._update_ui()
return
# nope - open it
widget = AddCardWidget( self )
widget.accepted_signal.connect( self.on_add_card_accepted )
widget.cancelled_signal.connect( self.on_add_card_cancelled )
index = self.tab_widget.insertTab(
self.tab_widget.currentIndex() + 1 ,
widget , "(new card)"
)
self.tab_widget.setCurrentIndex( index )
self._update_ui()
widget.setFocus() # FIXME! s.b. to first child control
def on_add_card_accepted( self , card ) :
"""Handle the user's card selection."""
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 )
self.tab_widget.setCurrentIndex( index )
self._update_ui()
def on_add_card_cancelled( self ) :
"""Cancel the "add card" widget."""
index = self._find_add_card_tab()
assert index is not None
self.tab_widget.removeTab( index )
self._update_ui()
def on_close_tab( self ) :
"""Close the current tab."""

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyDialog</class>
<widget class="QDialog" name="MyDialog">
<class>AddCardWidget</class>
<widget class="QWidget" name="AddCardWidget">
<property name="geometry">
<rect>
<x>0</x>
@ -57,7 +57,7 @@
</rect>
</property>
<property name="text">
<string>&amp;Ordnance</string>
<string>Or&amp;dnance</string>
</property>
</widget>
<widget class="QComboBox" name="cbo_nationality">
Loading…
Cancel
Save