File indexing completed on 2024-05-12 08:00:35

0001 /*
0002  *  Copyright 2010 Parker Coates <coates@kde.org>
0003  *
0004  *  This program is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU General Public License as
0006  *  published by the Free Software Foundation; either version 2 of
0007  *  the License, or (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  *
0017  */
0018 
0019 #include "numbereddealdialog.h"
0020 
0021 // own
0022 #include "dealerinfo.h"
0023 // KF
0024 #include <KComboBox>
0025 #include <KLocalizedString>
0026 // Qt
0027 #include <QDialogButtonBox>
0028 #include <QFormLayout>
0029 #include <QPushButton>
0030 #include <QSpinBox>
0031 #include <QVBoxLayout>
0032 
0033 NumberedDealDialog::NumberedDealDialog(QWidget *parent)
0034     : QDialog(parent)
0035 {
0036     setWindowTitle(i18n("New Numbered Deal"));
0037     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0038     QVBoxLayout *mainLayout = new QVBoxLayout;
0039     setLayout(mainLayout);
0040     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0041     okButton->setDefault(true);
0042     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &NumberedDealDialog::accept);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &NumberedDealDialog::reject);
0045 
0046     QWidget *widget = new QWidget(this);
0047     QFormLayout *layout = new QFormLayout(widget);
0048     layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
0049 
0050     m_gameType = new KComboBox(widget);
0051     layout->addRow(i18n("Game:"), m_gameType);
0052 
0053     m_dealNumber = new QSpinBox(widget);
0054     m_dealNumber->setRange(1, INT_MAX);
0055     layout->addRow(i18n("Deal number:"), m_dealNumber);
0056 
0057     QMap<QString, int> nameToIdMap;
0058     const auto games = DealerInfoList::self()->games();
0059     for (DealerInfo *game : games) {
0060         const auto ids = game->distinctIds();
0061         for (int id : ids)
0062             nameToIdMap.insert(game->nameForId(id), id);
0063     }
0064 
0065     QMap<QString, int>::const_iterator it = nameToIdMap.constBegin();
0066     QMap<QString, int>::const_iterator end = nameToIdMap.constEnd();
0067     for (; it != end; ++it) {
0068         // Map combobox indices to game IDs
0069         m_indexToIdMap[m_gameType->count()] = it.value();
0070         m_gameType->addItem(it.key());
0071     }
0072 
0073     m_gameType->setFocus();
0074     m_gameType->setMaxVisibleItems(m_indexToIdMap.size());
0075 
0076     setGameType(m_indexToIdMap[0]);
0077 
0078     connect(okButton, &QPushButton::clicked, this, &NumberedDealDialog::handleOkClicked);
0079     mainLayout->addWidget(widget);
0080     mainLayout->addWidget(buttonBox);
0081 }
0082 
0083 void NumberedDealDialog::setGameType(int gameId)
0084 {
0085     int comboIndex = m_indexToIdMap.key(gameId);
0086     m_gameType->setCurrentIndex(comboIndex);
0087 }
0088 
0089 void NumberedDealDialog::setDealNumber(int dealNumber)
0090 {
0091     m_dealNumber->setValue(dealNumber);
0092 }
0093 
0094 void NumberedDealDialog::setVisible(bool visible)
0095 {
0096     if (visible) {
0097         m_dealNumber->setFocus();
0098         m_dealNumber->selectAll();
0099     }
0100 
0101     QDialog::setVisible(visible);
0102 }
0103 
0104 void NumberedDealDialog::handleOkClicked()
0105 {
0106     Q_EMIT dealChosen(m_indexToIdMap.value(m_gameType->currentIndex()), m_dealNumber->value());
0107 }
0108 
0109 #include "moc_numbereddealdialog.cpp"