File indexing completed on 2024-05-19 07:51:58

0001 /*
0002     This file is part of Killbots.
0003 
0004     SPDX-FileCopyrightText: 2009 Parker Coates <coates@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "rulesetdetailsdialog.h"
0010 
0011 #include "ruleset.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QFormLayout>
0016 #include <QLabel>
0017 #include <QDialogButtonBox>
0018 #include <QPushButton>
0019 #include <QVBoxLayout>
0020 
0021 Killbots::RulesetDetailsDialog::RulesetDetailsDialog(QWidget *parent)
0022     : QDialog(parent)
0023 {
0024     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0025     mMainWidget = new QWidget(this);
0026     QVBoxLayout *mainLayout = new QVBoxLayout;
0027     setLayout(mainLayout);
0028     mainLayout->addWidget(mMainWidget);
0029     connect(buttonBox, &QDialogButtonBox::accepted, this, &RulesetDetailsDialog::accept);
0030     connect(buttonBox, &QDialogButtonBox::rejected, this, &RulesetDetailsDialog::reject);
0031     mainLayout->addWidget(buttonBox);
0032 }
0033 
0034 void Killbots::RulesetDetailsDialog::loadRuleset(const Ruleset *ruleset)
0035 {
0036     static QStringList maskedItems = QStringList() << QStringLiteral("Name") << QStringLiteral("Author") << QStringLiteral("AuthorContact") << QStringLiteral("Description");
0037     const QStringList junkheapEnumText {
0038     i18nc("@item Quantity of junkheaps that can be pushed", "None"),
0039         i18nc("@item Quantity of junkheaps that can be pushed", "One"),
0040         i18nc("@item Quantity of junkheaps that can be pushed", "Many"),
0041     };
0042 
0043     // If the dialog hasn't been setup already, do so.
0044     if (m_labels.size() == 0) {
0045         QFormLayout *layout = new QFormLayout(mMainWidget);
0046 
0047         const auto items = ruleset->items();
0048         for (const KConfigSkeletonItem *item : items) {
0049             if (!maskedItems.contains(item->name())) {
0050                 QString labelText = item->label().isEmpty() ? item->name() : item->label();
0051                 labelText = i18nc("%1 is a pretranslated string that we're turning into a label", "%1:", labelText);
0052 
0053                 QLabel *valueLabel = new QLabel();
0054                 m_labels[ item->name() ] = valueLabel;
0055                 layout->addRow(new QLabel(labelText), valueLabel);
0056             }
0057         }
0058 
0059     }
0060 
0061     QMap<QString, QLabel *>::const_iterator it = m_labels.constBegin();
0062     QMap<QString, QLabel *>::const_iterator end = m_labels.constEnd();
0063     for (; it != end; it++) {
0064         const KConfigSkeletonItem *item = ruleset->findItem(it.key());
0065 
0066         QString valueText;
0067         if (dynamic_cast<const KCoreConfigSkeleton::ItemBool *>(item)) {
0068             valueText = item->property().toBool() ? i18nc("@item", "Yes") : i18nc("@item", "No");
0069         } else if (it.key() == QLatin1String("PushableJunkheaps")) {
0070             valueText = junkheapEnumText.at(item->property().toInt());
0071         } else {
0072             valueText = item->property().toString();
0073         }
0074 
0075         it.value()->setText(valueText);
0076     }
0077 
0078     setWindowTitle(i18nc("@title:window", "Details of %1 Game Type", ruleset->name()));
0079 }
0080