File indexing completed on 2024-07-21 03:46:59

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2001 Andreas Beckermann (b_mann@gmx.de)
0004     SPDX-FileCopyrightText: 2001 Martin Heni (kde at heni-online.de)
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include "kgamedialog.h"
0010 
0011 // own
0012 #include "kfourinline_debug.h"
0013 #include "kgamedialogconfig.h"
0014 // KDEGames
0015 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
0016 #include <libkdegamesprivate/kgame/kgame.h>
0017 // KF
0018 #include <KLocalizedString>
0019 // Qt
0020 #include <QList>
0021 #include <QPushButton>
0022 #include <QVBoxLayout>
0023 
0024 class KGameDialogPrivate
0025 {
0026 public:
0027     KGameDialogPrivate()
0028     {
0029         mNetworkPage = nullptr;
0030 
0031         mNetworkConfig = nullptr;
0032 
0033         mOwner = nullptr;
0034         mGame = nullptr;
0035     }
0036 
0037     QWidget *mNetworkPage;
0038     KGameDialogNetworkConfig *mNetworkConfig;
0039 
0040     // a list of all config widgets added to this dialog
0041     QList<KGameDialogConfig *> mConfigWidgets;
0042 
0043     // just pointers:
0044     KPlayer *mOwner;
0045     KGame *mGame;
0046 };
0047 
0048 KGameDialog::KGameDialog(KGame *g, KPlayer *owner, const QString &title, QWidget *parent)
0049     : KPageDialog(parent)
0050     , d(new KGameDialogPrivate)
0051 {
0052     setWindowTitle(title);
0053     // QT5 setButtons(Ok|Default|Apply|Cancel);
0054     // QT5 setDefaultButton(Ok);
0055     setFaceType(KPageDialog::Tabbed);
0056     setModal(true);
0057     init(g, owner);
0058     addNetworkConfig(new KGameDialogNetworkConfig());
0059     connect(button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &KGameDialog::slotOk);
0060     connect(button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &KGameDialog::slotDefault);
0061     connect(button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KGameDialog::slotApply);
0062 }
0063 
0064 void KGameDialog::init(KGame *g, KPlayer *owner)
0065 {
0066     // AB: do we need a "Cancel" Button? currently removed
0067 
0068     // qCDebug(KFOURINLINE_LOG) << ": this=" << this;
0069 
0070     setOwner(owner);
0071     setKGame(g);
0072     if (g) {
0073         setAdmin(g->isAdmin());
0074     } else {
0075         setAdmin(false);
0076     }
0077 }
0078 
0079 KGameDialog::~KGameDialog()
0080 {
0081     // qCDebug(KFOURINLINE_LOG) << "DESTRUCT KGameDialog" << this;
0082     qDeleteAll(d->mConfigWidgets);
0083     delete d;
0084 }
0085 
0086 void KGameDialog::addNetworkConfig(KGameDialogNetworkConfig *netConf)
0087 {
0088     if (!netConf) {
0089         return;
0090     }
0091     d->mNetworkConfig = netConf;
0092     d->mNetworkPage = addConfigPage(netConf, i18n("&Network"));
0093 }
0094 
0095 QWidget *KGameDialog::configPage()
0096 {
0097     return d->mNetworkPage;
0098 }
0099 
0100 QWidget *KGameDialog::addConfigPage(KGameDialogConfig *widget, const QString &title)
0101 {
0102     if (!widget) {
0103         qCCritical(KFOURINLINE_LOG) << "Cannot add NULL config widget";
0104         return nullptr;
0105     }
0106     QWidget *page = new QWidget();
0107     QVBoxLayout *pageVBoxLayout = new QVBoxLayout(page);
0108     pageVBoxLayout->setContentsMargins(0, 0, 0, 0);
0109     pageVBoxLayout->addWidget(widget);
0110     addPage(page, title);
0111     addConfigWidget(widget, page);
0112     return page;
0113 }
0114 
0115 void KGameDialog::addConfigWidget(KGameDialogConfig *widget, QWidget *parent)
0116 {
0117     if (!widget) {
0118         qCCritical(KFOURINLINE_LOG) << "Cannot add NULL config widget";
0119         return;
0120     }
0121     if (!parent) {
0122         qCCritical(KFOURINLINE_LOG) << "Cannot reparent to NULL widget";
0123         return;
0124     }
0125     // qCDebug(KFOURINLINE_LOG) << "reparenting widget";
0126     d->mConfigWidgets.append(widget);
0127     connect(widget, &QObject::destroyed, this, &KGameDialog::slotRemoveConfigWidget);
0128     if (!d->mGame) {
0129         qCWarning(KFOURINLINE_LOG) << "No game has been set!";
0130     } else {
0131         widget->setKGame(d->mGame);
0132         widget->setAdmin(d->mGame->isAdmin());
0133     }
0134     if (!d->mOwner) {
0135         qCWarning(KFOURINLINE_LOG) << "No player has been set!";
0136     } else {
0137         widget->setOwner(d->mOwner);
0138     }
0139     widget->show();
0140 }
0141 
0142 KGameDialogNetworkConfig *KGameDialog::networkConfig() const
0143 {
0144     return d->mNetworkConfig;
0145 }
0146 
0147 void KGameDialog::slotApply()
0148 {
0149     submitToKGame();
0150 }
0151 
0152 void KGameDialog::slotDefault()
0153 {
0154     if (!d->mGame) {
0155         return;
0156     }
0157 
0158     // TODO *only*  call setKGame/setOwner for the *current* page!!
0159     setKGame(d->mGame);
0160     setOwner(d->mOwner);
0161 }
0162 
0163 void KGameDialog::slotOk()
0164 {
0165     slotApply();
0166     QDialog::accept();
0167 }
0168 
0169 void KGameDialog::setOwner(KPlayer *owner)
0170 {
0171     // AB: note: NULL player is ok!
0172     d->mOwner = owner;
0173     for (int i = 0; i < d->mConfigWidgets.count(); i++) {
0174         if (d->mConfigWidgets.at(i)) {
0175             d->mConfigWidgets.at(i)->setOwner(d->mOwner);
0176         } else {
0177             qCCritical(KFOURINLINE_LOG) << "NULL widget??";
0178         }
0179     }
0180 }
0181 
0182 void KGameDialog::setKGame(KGame *g)
0183 {
0184     if (d->mGame) {
0185         disconnect(d->mGame, nullptr, this, nullptr);
0186     }
0187     d->mGame = g;
0188     for (int i = 0; i < d->mConfigWidgets.count(); i++) {
0189         d->mConfigWidgets.at(i)->setKGame(d->mGame);
0190     }
0191     if (d->mGame) {
0192         setAdmin(d->mGame->isAdmin());
0193         connect(d->mGame, &KGame::destroyed, this, &KGameDialog::slotUnsetKGame);
0194         connect(d->mGame, &KGame::signalAdminStatusChanged, this, &KGameDialog::setAdmin);
0195     }
0196 }
0197 
0198 void KGameDialog::setAdmin(bool admin)
0199 {
0200     for (int i = 0; i < d->mConfigWidgets.count(); i++) {
0201         d->mConfigWidgets.at(i)->setAdmin(admin);
0202     }
0203 }
0204 
0205 void KGameDialog::slotUnsetKGame() // called when KGame is destroyed
0206 {
0207     setKGame(nullptr);
0208 }
0209 
0210 void KGameDialog::submitToKGame()
0211 {
0212     if (!d->mGame) {
0213         qCCritical(KFOURINLINE_LOG) << ": no game has been set";
0214         return;
0215     }
0216     if (!d->mOwner) {
0217         qCCritical(KFOURINLINE_LOG) << ": no player has been set";
0218         return;
0219     }
0220 
0221     for (int i = 0; i < d->mConfigWidgets.count(); i++) {
0222         // qCDebug(KFOURINLINE_LOG) << "submit to kgame" << i;
0223         d->mConfigWidgets.at(i)->submitToKGame(d->mGame, d->mOwner);
0224         // qCDebug(KFOURINLINE_LOG) << "done: submit to kgame" << i;
0225     }
0226 }
0227 
0228 void KGameDialog::slotRemoveConfigWidget(QObject *configWidget)
0229 {
0230     d->mConfigWidgets.removeAll((KGameDialogConfig *)configWidget);
0231 }
0232 
0233 #include "moc_kgamedialog.cpp"