File indexing completed on 2024-04-21 04:05:22

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kcarddialog.h"
0009 #include "ui_kgamecardselector.h"
0010 
0011 #include <QDialogButtonBox>
0012 #include <QListWidgetItem>
0013 #include <QPainter>
0014 #include <QPixmap>
0015 #include <QPushButton>
0016 #include <QVBoxLayout>
0017 
0018 #include <KConfigGroup>
0019 #include <KLocalizedString>
0020 #include <KSharedConfig>
0021 
0022 #include "carddeckinfo.h"
0023 #include "carddeckinfo_p.h"
0024 #include "lskat_debug.h"
0025 
0026 /**
0027  * Local information of the dialog.
0028  */
0029 class KCardWidgetPrivate
0030 {
0031 public:
0032     /**
0033      * Constructor
0034      */
0035     KCardWidgetPrivate()
0036     {
0037     }
0038 
0039     /**
0040      * Currently chosen front side name.
0041      */
0042     QString currentDeck;
0043 
0044     /**
0045      * The UI elements.
0046      */
0047     Ui::KGameCardSelectorBase ui;
0048 };
0049 
0050 // Create the dialog from a config group
0051 KCardWidget::KCardWidget(QWidget *parent)
0052            : QWidget(parent), d(new KCardWidgetPrivate)
0053 {
0054     // GUI
0055     setupGUI();
0056     insertCardIcons();
0057     setDeckName(CardDeckInfo::defaultDeckName());
0058 }
0059 
0060 void KCardWidget::readSettings(const KConfigGroup &group)
0061 {
0062     setDeckName(CardDeckInfo::deckName(group));
0063 }
0064 
0065 // Store the config group settings
0066 void KCardWidget::saveSettings(KConfigGroup &group) const
0067 {
0068     CardDeckInfo::writeDeckName(group, d->currentDeck);
0069 }
0070 
0071 // Setup the user interface
0072 void KCardWidget::setupGUI()
0073 {
0074     // Inner widget
0075     Ui::KGameCardSelectorBase *ui = &(d->ui);
0076     d->ui.setupUi(this);
0077 
0078     // Set lists and preview
0079     insertCardIcons();
0080 
0081 
0082     // Connect signals
0083     connect(ui->list, &QListWidget::itemSelectionChanged, this, &KCardWidget::updateSelection);
0084 }
0085 
0086 // Destroy the dialog
0087 KCardWidget::~KCardWidget()
0088 {
0089     delete d;
0090 }
0091 
0092 // Retrieve selected card name
0093 QString KCardWidget::deckName() const
0094 {
0095     return d->currentDeck;
0096 }
0097 
0098 // Build list widget
0099 void KCardWidget::insertCardIcons()
0100 {
0101     // Clear GUI
0102     d->ui.list->clear();
0103 
0104     // Rebuild list
0105     QSize itemSize;
0106     const QStringList decknames = CardDeckInfo::deckNames();
0107     for (const QString &name : decknames)
0108     {
0109         KCardThemeInfo v = CardDeckInfo::deckInfo(name);
0110         // Show only SVG files?
0111         if (v.svgfile.isEmpty()) continue;
0112 
0113         const int iconSize = 48;
0114         QPixmap resizedCard = v.preview.scaled(QSize(iconSize, iconSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
0115         QPixmap previewPixmap(iconSize, iconSize);
0116         previewPixmap.fill(Qt::transparent);
0117         QPainter p(&previewPixmap);
0118         p.drawPixmap((iconSize - resizedCard.width()) / 2, (iconSize-resizedCard.height()) / 2, resizedCard);
0119         p.end();
0120 
0121         QListWidgetItem *item = new QListWidgetItem(v.name, d->ui.list);
0122         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
0123         item->setToolTip(v.name);
0124         item->setData(Qt::DecorationRole, previewPixmap);
0125         item->setData(Qt::UserRole, v.noi18Name);
0126         itemSize = itemSize.expandedTo(previewPixmap.size());
0127     }
0128 
0129     setDeckName(CardDeckInfo::defaultDeckName());
0130 
0131     d->ui.list->setIconSize(itemSize);
0132 }
0133 
0134 // Update front preview
0135 void KCardWidget::updateSelection()
0136 {
0137     QList<QListWidgetItem *> l = d->ui.list->selectedItems();
0138     if (!l.isEmpty())
0139         setDeckName(l.first()->data(Qt::UserRole).toString());
0140 }
0141 
0142 // Update front preview
0143 void KCardWidget::setDeckName(const QString &name)
0144 {
0145     // Clear item?
0146     if (name.isEmpty())
0147     {
0148         QList<QListWidgetItem *> items = d->ui.list->selectedItems();
0149         if (!items.isEmpty())
0150             items.first()->setSelected(false);
0151         d->ui.previewImage->setPixmap(QPixmap());
0152         d->ui.cardName->setText(QString());
0153         d->ui.cardDescription->setText(QString());
0154     }
0155     else
0156     {
0157         for (int i = 0; i < d->ui.list->count(); ++i)
0158         {
0159             QListWidgetItem *item = d->ui.list->item(i);
0160             if (item->data(Qt::UserRole).toString() == name)
0161             {
0162                 item->setSelected(true);
0163                 d->ui.list->scrollToItem(item);
0164                 break;
0165             }
0166         }
0167 
0168         KCardThemeInfo info = CardDeckInfo::deckInfo(name);
0169         QFont font;
0170         font.setBold(true);
0171         d->ui.cardName->setText(info.name);
0172         d->ui.cardName->setFont(font);
0173 
0174         d->ui.cardDescription->setText(info.comment);
0175         QPixmap pixmap= info.preview;
0176         if (pixmap.height() > d->ui.previewImage->height())
0177             pixmap = pixmap.scaledToHeight(d->ui.previewImage->height(), Qt::SmoothTransformation);
0178         if (pixmap.width() > d->ui.previewImage->width())
0179             pixmap = pixmap.scaledToWidth(d->ui.previewImage->width(), Qt::SmoothTransformation);
0180         d->ui.previewImage->setPixmap(pixmap);
0181     }
0182     d->currentDeck = name;
0183 }
0184 
0185 KCardDialog::KCardDialog(KCardWidget *widget)
0186 {
0187     QVBoxLayout *mainLayout = new QVBoxLayout;
0188     setLayout(mainLayout);
0189     mainLayout->addWidget(widget);
0190     setWindowTitle(i18nc("@title:window", "Card Deck Selection"));
0191     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0192     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0193     okButton->setDefault(true);
0194     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0195     connect(buttonBox, &QDialogButtonBox::accepted, this, &KCardDialog::accept);
0196     connect(buttonBox, &QDialogButtonBox::rejected, this, &KCardDialog::reject);
0197     mainLayout->addWidget(buttonBox);
0198 }
0199 
0200 #include "moc_kcarddialog.cpp"