kcarddialog.cpp 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
    This file is part of the KDE games library
    Copyright 2008 Andreas Pakulat <apaku@gmx.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kcarddialog.h"
#include "ui_kgamecardselector.h"

23 24
#include <QDialogButtonBox>
#include <QListWidgetItem>
25 26
#include <QPainter>
#include <QPixmap>
Luigi Toscano's avatar
Luigi Toscano committed
27 28
#include <QPushButton>
#include <QVBoxLayout>
29 30 31 32

#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
33
#include "lskat_debug.h"
34 35 36 37 38 39 40 41 42 43

#include "carddeckinfo.h"
#include "carddeckinfo_p.h"

/**
 * Local information of the dialog.
 */
class KCardWidgetPrivate
{
public:
44 45 46 47 48 49
    /**
     * Constructor
     */
    KCardWidgetPrivate()
    {
    }
50

51 52 53 54
    /**
     * Currently chosen front side name.
     */
    QString currentDeck;
55

56 57 58 59
    /**
     * The UI elements.
     */
    Ui::KGameCardSelectorBase ui;
60 61 62
};

// Create the dialog from a config group
63
KCardWidget::KCardWidget(QWidget *parent)
64 65
           : QWidget(parent), d(new KCardWidgetPrivate)
{
66 67 68 69
    // GUI
    setupGUI();
    insertCardIcons();
    setDeckName(CardDeckInfo::defaultDeckName());
70 71
}

72
void KCardWidget::readSettings(const KConfigGroup &group)
73
{
74
    setDeckName(CardDeckInfo::deckName(group));
75 76 77
}

// Store the config group settings
78
void KCardWidget::saveSettings(KConfigGroup &group) const
79
{
80
    CardDeckInfo::writeDeckName(group, d->currentDeck);
81 82 83 84 85
}

// Setup the user interface
void KCardWidget::setupGUI()
{
86 87 88
    // Inner widget
    Ui::KGameCardSelectorBase *ui = &(d->ui);
    d->ui.setupUi(this);
89

90 91
    // Set lists and preview
    insertCardIcons();
92 93


94
    // Connect signals
95
    connect(ui->list, &QListWidget::itemSelectionChanged, this, &KCardWidget::updateSelection);
96 97 98 99 100
}

// Destroy the dialog
KCardWidget::~KCardWidget()
{
101
    delete d;
102 103 104
}

// Retrieve selected card name
105
QString KCardWidget::deckName() const
106
{
107
    return d->currentDeck;
108 109 110 111 112
}

// Build list widget
void KCardWidget::insertCardIcons()
{
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    // Clear GUI
    d->ui.list->clear();

    // Rebuild list
    QSize itemSize;
    foreach (const QString &name, CardDeckInfo::deckNames())
    {
        KCardThemeInfo v = CardDeckInfo::deckInfo(name);
        // Show only SVG files?
        if (v.svgfile.isEmpty()) continue;

        const int iconSize = 48;
        QPixmap resizedCard = v.preview.scaled(QSize(iconSize, iconSize), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        QPixmap previewPixmap(iconSize, iconSize);
        previewPixmap.fill(Qt::transparent);
        QPainter p(&previewPixmap);
        p.drawPixmap((iconSize - resizedCard.width()) / 2, (iconSize-resizedCard.height()) / 2, resizedCard);
        p.end();

        QListWidgetItem *item = new QListWidgetItem(v.name, d->ui.list);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        item->setToolTip(v.name);
        item->setData(Qt::DecorationRole, previewPixmap);
        item->setData(Qt::UserRole, v.noi18Name);
        itemSize = itemSize.expandedTo(previewPixmap.size());
    }
139

140
    setDeckName(CardDeckInfo::defaultDeckName());
141

142
    d->ui.list->setIconSize(itemSize);
143 144 145
}

// Update front preview
146
void KCardWidget::updateSelection()
147
{
148 149
    QList<QListWidgetItem *> l = d->ui.list->selectedItems();
    if (!l.isEmpty())
150
        setDeckName(l.first()->data(Qt::UserRole).toString());
151 152 153
}

// Update front preview
154
void KCardWidget::setDeckName(const QString &name)
155
{
156 157
    // Clear item?
    if (name.isEmpty())
158
    {
159 160 161 162 163 164
        QList<QListWidgetItem *> items = d->ui.list->selectedItems();
        if (!items.isEmpty())
            items.first()->setSelected(false);
        d->ui.previewImage->setPixmap(QPixmap());
        d->ui.cardName->setText(QString());
        d->ui.cardDescription->setText(QString());
165
    }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    else
    {
        for (int i = 0; i < d->ui.list->count(); ++i)
        {
            QListWidgetItem *item = d->ui.list->item(i);
            if (item->data(Qt::UserRole).toString() == name)
            {
                item->setSelected(true);
                d->ui.list->scrollToItem(item);
                break;
            }
        }

        KCardThemeInfo info = CardDeckInfo::deckInfo(name);
        QFont font;
        font.setBold(true);
        d->ui.cardName->setText(info.name);
        d->ui.cardName->setFont(font);

        d->ui.cardDescription->setText(info.comment);
        QPixmap pixmap= info.preview;
        if (pixmap.height() > d->ui.previewImage->height())
            pixmap = pixmap.scaledToHeight(d->ui.previewImage->height(), Qt::SmoothTransformation);
        if (pixmap.width() > d->ui.previewImage->width())
            pixmap = pixmap.scaledToWidth(d->ui.previewImage->width(), Qt::SmoothTransformation);
        d->ui.previewImage->setPixmap(pixmap);
    }
    d->currentDeck = name;
194 195
}

196
KCardDialog::KCardDialog(KCardWidget *widget)
197
{
Luigi Toscano's avatar
Luigi Toscano committed
198 199 200 201 202 203 204 205
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    mainLayout->addWidget(widget);
    setWindowTitle(i18n("Card Deck Selection"));
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
206 207
    connect(buttonBox, &QDialogButtonBox::accepted, this, &KCardDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &KCardDialog::reject);
Luigi Toscano's avatar
Luigi Toscano committed
208
    mainLayout->addWidget(buttonBox);
209
}