1 | /* |
2 | * Copyright 2010 Parker Coates <coates@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License as |
6 | * published by the Free Software Foundation; either version 2 of |
7 | * the License, or (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | * |
17 | */ |
18 | |
19 | #include "numbereddealdialog.h" |
20 | |
21 | #include "dealerinfo.h" |
22 | |
23 | #include <KComboBox> |
24 | #include <KLocale> |
25 | |
26 | #include <QtCore/QList> |
27 | #include <QtGui/QFormLayout> |
28 | #include <QtGui/QSpinBox> |
29 | |
30 | |
31 | NumberedDealDialog::NumberedDealDialog( QWidget * parent ) |
32 | : KDialog( parent ) |
33 | { |
34 | setWindowTitle( i18n( "New Numbered Deal" ) ); |
35 | setButtons( KDialog::Ok | KDialog::Cancel ); |
36 | |
37 | QWidget * widget = new QWidget( this ); |
38 | QFormLayout * layout = new QFormLayout( widget ); |
39 | layout->setFieldGrowthPolicy( QFormLayout::AllNonFixedFieldsGrow ); |
40 | |
41 | m_gameType = new KComboBox( widget ); |
42 | layout->addRow( i18n( "Game:" ), m_gameType ); |
43 | |
44 | m_dealNumber = new QSpinBox( widget ); |
45 | m_dealNumber->setRange( 1, INT_MAX ); |
46 | layout->addRow( i18n( "Deal number:" ), m_dealNumber ); |
47 | |
48 | setMainWidget( widget ); |
49 | |
50 | QMap<QString,int> nameToIdMap; |
51 | foreach ( DealerInfo * game, DealerInfoList::self()->games() ) |
52 | foreach ( int id, game->distinctIds() ) |
53 | nameToIdMap.insert( game->nameForId( id ), id ); |
54 | |
55 | QMap<QString,int>::const_iterator it = nameToIdMap.constBegin(); |
56 | QMap<QString,int>::const_iterator end = nameToIdMap.constEnd(); |
57 | for ( ; it != end; ++it ) |
58 | { |
59 | // Map combobox indices to game IDs |
60 | m_indexToIdMap[m_gameType->count()] = it.value(); |
61 | m_gameType->addItem( it.key() ); |
62 | } |
63 | |
64 | m_gameType->setFocus(); |
65 | m_gameType->setMaxVisibleItems( m_indexToIdMap.size() ); |
66 | |
67 | setGameType( m_indexToIdMap[0] ); |
68 | |
69 | connect( this, SIGNAL(okClicked()), this, SLOT(handleOkClicked()) ); |
70 | } |
71 | |
72 | |
73 | void NumberedDealDialog::setGameType( int gameId ) |
74 | { |
75 | int comboIndex = m_indexToIdMap.key( gameId ); |
76 | m_gameType->setCurrentIndex( comboIndex ); |
77 | } |
78 | |
79 | |
80 | void NumberedDealDialog::setDealNumber( int dealNumber ) |
81 | { |
82 | m_dealNumber->setValue( dealNumber ); |
83 | } |
84 | |
85 | |
86 | void NumberedDealDialog::setVisible( bool visible ) |
87 | { |
88 | if ( visible ) |
89 | { |
90 | m_dealNumber->setFocus(); |
91 | m_dealNumber->selectAll(); |
92 | } |
93 | |
94 | KDialog::setVisible( visible ); |
95 | } |
96 | |
97 | |
98 | void NumberedDealDialog::handleOkClicked() |
99 | { |
100 | emit dealChosen( m_indexToIdMap.value( m_gameType->currentIndex() ), m_dealNumber->value() ); |
101 | } |
102 | |
103 | |
104 | #include "numbereddealdialog.moc" |
105 | |
106 | |