kgamedialog.cpp

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
00004     Copyright (C) 2001 Martin Heni (martin@heni-online.de)
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qlayout.h>
00022 #include <qvbox.h>
00023 
00024 #include <klocale.h>
00025 
00026 #include "kgame.h"
00027 #include "kplayer.h"
00028 #include "kgamedialogconfig.h"
00029 
00030 #include "kgamedialog.h"
00031 
00032 #include "kgamedialog.moc"
00033 
00034 class KGameDialogPrivate
00035 {
00036 public:
00037     KGameDialogPrivate() 
00038     {
00039         mGamePage = 0;
00040         mNetworkPage = 0;
00041         mMsgServerPage = 0;
00042         mTopLayout = 0;
00043 
00044         mNetworkConfig = 0;
00045         mGameConfig = 0;
00046 
00047         mOwner = 0;
00048         mGame = 0;
00049     }
00050 
00051     QVBox* mGamePage;
00052     QVBox* mNetworkPage;
00053     QVBox* mMsgServerPage;// unused here?
00054     QVBoxLayout* mTopLayout;
00055     KGameDialogNetworkConfig* mNetworkConfig;
00056     KGameDialogGeneralConfig* mGameConfig;
00057 
00058 // a list of all config widgets added to this dialog
00059     QPtrList<KGameDialogConfig> mConfigWidgets;
00060 
00061 // just pointers:
00062     KPlayer* mOwner;
00063     KGame* mGame;
00064 };
00065 
00066 KGameDialog::KGameDialog(KGame* g, KPlayer* owner, const QString& title,
00067         QWidget* parent, bool modal)
00068     : KDialogBase(Tabbed, title, Ok|Default|Apply,
00069     Ok, parent, 0, modal, true)
00070 {
00071  init(g, owner);
00072 }
00073 
00074 KGameDialog::KGameDialog(KGame* g, KPlayer* owner, const QString& title, 
00075         QWidget* parent, long initConfigs, int chatMsgId, bool modal)
00076     : KDialogBase(Tabbed, title, Ok|Default|Apply,
00077     Ok, parent, 0, modal, true)
00078 {
00079  init(g, owner);
00080  if ((ConfigOptions)initConfigs!=NoConfig) {
00081     initDefaultDialog((ConfigOptions)initConfigs, chatMsgId);
00082  }
00083 }
00084 
00085 void KGameDialog::init(KGame* g, KPlayer* owner)
00086 {
00087 //AB: do we need a "Cancel" Button? currently removed
00088 
00089 // kdDebug(11001) << k_funcinfo << ": this=" << this << endl;
00090  d = new KGameDialogPrivate;
00091 
00092  setOwner(owner);
00093  setKGame(g);
00094  if (g) {
00095     setAdmin(g->isAdmin());
00096  } else {
00097     setAdmin(false);
00098  }
00099 }
00100 
00101 void KGameDialog::initDefaultDialog(ConfigOptions initConfigs, int chatMsgId)
00102 {
00103  if (initConfigs & GameConfig) {
00104     kdDebug() << "add gameconf" << endl;
00105     addGameConfig(new KGameDialogGeneralConfig(0));
00106  }
00107  if (initConfigs & NetworkConfig) {
00108     addNetworkConfig(new KGameDialogNetworkConfig(0));
00109  }
00110  if (initConfigs & (MsgServerConfig) ) {
00111     addMsgServerConfig(new KGameDialogMsgServerConfig(0));
00112  }
00113  if (initConfigs & ChatConfig) {
00114     KGameDialogChatConfig * c = new KGameDialogChatConfig(chatMsgId, 0);
00115     if (d->mGamePage) {
00116         addChatWidget(c, d->mGamePage);
00117     } else {
00118         addConfigPage(c, i18n("&Chat"));
00119     }
00120  }
00121  if (initConfigs & BanPlayerConfig) {
00122     // add the connection management system - ie the widget where the ADMIN can
00123     // kick players out
00124     if (d->mNetworkPage) {
00125         // put it on the network page
00126         addConnectionList(new KGameDialogConnectionConfig(0), d->mNetworkPage);
00127     } else {
00128         // if no network page available put it on an own page
00129         addConfigPage(new KGameDialogConnectionConfig(0), i18n("C&onnections"));
00130     }
00131  }
00132 }
00133 
00134 KGameDialog::~KGameDialog()
00135 {
00136 // kdDebug(11001) << "DESTRUCT KGameDialog" << this << endl;
00137  d->mConfigWidgets.setAutoDelete(true);
00138  d->mConfigWidgets.clear();
00139  delete d;
00140 }
00141 
00142 void KGameDialog::addGameConfig(KGameDialogGeneralConfig* conf)
00143 {
00144  if (!conf) {
00145     return;
00146  }
00147  d->mGameConfig = conf;
00148  d->mGamePage = addConfigPage(d->mGameConfig, i18n("&Game"));
00149 }
00150 
00151 void KGameDialog::addNetworkConfig(KGameDialogNetworkConfig* netConf)
00152 {
00153  if (!netConf) {
00154     return;
00155  }
00156  d->mNetworkConfig = netConf;
00157  d->mNetworkPage = addConfigPage(netConf, i18n("&Network"));
00158 }
00159 
00160 void KGameDialog::addMsgServerConfig(KGameDialogMsgServerConfig* msgConf)
00161 {
00162  if (!msgConf) {
00163     return;
00164  }
00165  d->mMsgServerPage = addConfigPage(msgConf, i18n("&Message Server"));
00166 }
00167 
00168 void KGameDialog::addChatWidget(KGameDialogChatConfig* chat, QVBox* parent)
00169 {
00170  if (!chat) {
00171     return;
00172  }
00173  if (!parent) {
00174     parent = d->mGamePage;
00175  }
00176  if (!parent) {
00177     kdError(11001) << "cannot add chat widget without page" << endl;
00178     return;
00179  }
00180  addConfigWidget(chat, parent);
00181 }
00182 
00183 void KGameDialog::addConnectionList(KGameDialogConnectionConfig* c, QVBox* parent)
00184 {
00185  if (!c) {
00186     return;
00187  }
00188  if (!parent) {
00189     parent = d->mNetworkPage;
00190  }
00191  if (!parent) {
00192     kdError(11001) << "Cannot add connection list without page" << endl;
00193     return;
00194  }
00195  addConfigWidget(c, parent);
00196 }
00197 
00198 QVBox *KGameDialog::configPage(ConfigOptions which)
00199 {
00200  QVBox *box = 0;
00201  switch(which)
00202  {
00203     case NetworkConfig:
00204         box = d->mNetworkPage;
00205         break;
00206     case GameConfig:
00207         box = d->mGamePage;
00208         break;
00209     case MsgServerConfig:
00210         box = d->mMsgServerPage;
00211         break;
00212     default:
00213         kdError(11001) << k_funcinfo << ": Parameter " << which << " not supported" << endl;
00214  }
00215  return box;
00216 }
00217 
00218 QVBox* KGameDialog::addConfigPage(KGameDialogConfig* widget, const QString& title)
00219 {
00220  if (!widget) {
00221     kdError(11001) << "Cannot add NULL config widget" << endl;
00222     return 0;
00223  }
00224  QVBox* page = addVBoxPage(title);
00225  addConfigWidget(widget, page);
00226  return page;
00227 }
00228 
00229 void KGameDialog::addConfigWidget(KGameDialogConfig* widget, QWidget* parent)
00230 {
00231  if (!widget) {
00232     kdError(11001) << "Cannot add NULL config widget" << endl;
00233     return;
00234  }
00235  if (!parent) {
00236     kdError(11001) << "Cannot reparent to NULL widget" << endl;
00237     return;
00238  }
00239 // kdDebug(11001) << "reparenting widget" << endl;
00240  widget->reparent(parent, QPoint(0,0));
00241  d->mConfigWidgets.append(widget);
00242  connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(slotRemoveConfigWidget(QObject*)));
00243  if (!d->mGame) {
00244     kdWarning(11001) << "No game has been set!" << endl;
00245  } else {
00246     widget->setKGame(d->mGame);
00247     widget->setAdmin(d->mGame->isAdmin());
00248  }
00249  if (!d->mOwner) {
00250     kdWarning(11001) << "No player has been set!" << endl;
00251  } else {
00252     widget->setOwner(d->mOwner);
00253  }
00254  widget->show();
00255 }
00256 
00257 KGameDialogGeneralConfig* KGameDialog::gameConfig() const
00258 { return d->mGameConfig; }
00259 KGameDialogNetworkConfig* KGameDialog::networkConfig() const
00260 { return d->mNetworkConfig; }
00261 
00262 void KGameDialog::slotApply()
00263 {
00264  submitToKGame();
00265 }
00266 
00267 void KGameDialog::slotDefault()
00268 {
00269  if (!d->mGame) {
00270     return;
00271  }
00272  
00273 //TODO *only*  call setKGame/setOwner for the *current* page!!
00274  setKGame(d->mGame);
00275  setOwner(d->mOwner);
00276 }
00277 
00278 void KGameDialog::slotOk()
00279 {
00280  slotApply();
00281  QDialog::accept();
00282 }
00283 
00284 void KGameDialog::setOwner(KPlayer* owner)
00285 {
00286 //AB: note: NULL player is ok!
00287  d->mOwner = owner;
00288  for (int unsigned i = 0; i < d->mConfigWidgets.count(); i++) {
00289     if (d->mConfigWidgets.at(i)) {
00290         d->mConfigWidgets.at(i)->setOwner(d->mOwner);
00291         //TODO: hide playerName in KGameDialogGeneralConfig
00292     } else {
00293         kdError(11001) << "NULL widget??" << endl;
00294     }
00295  }
00296 }
00297 
00298 void KGameDialog::setKGame(KGame* g)
00299 {
00300  if (d->mGame) {
00301     disconnect(d->mGame, 0, this, 0);
00302  }
00303  d->mGame = g;
00304  for (int unsigned i = 0; i < d->mConfigWidgets.count(); i++) {
00305     d->mConfigWidgets.at(i)->setKGame(d->mGame);
00306  }
00307  if (d->mGame) {
00308     setAdmin(d->mGame->isAdmin());
00309     connect(d->mGame, SIGNAL(destroyed()), this, SLOT(slotUnsetKGame()));
00310     connect(d->mGame, SIGNAL(signalAdminStatusChanged(bool)), 
00311             this, SLOT(setAdmin(bool)));
00312  }
00313 }
00314 
00315 void KGameDialog::setAdmin(bool admin)
00316 {
00317  for (int unsigned i = 0; i < d->mConfigWidgets.count(); i++) {
00318     d->mConfigWidgets.at(i)->setAdmin(admin);
00319  }
00320 }
00321 
00322 void KGameDialog::slotUnsetKGame() // called when KGame is destroyed
00323 { setKGame(0); }
00324 
00325 void KGameDialog::submitToKGame()
00326 {
00327  if (!d->mGame) {
00328     kdError(11001) << k_funcinfo << ": no game has been set" << endl;
00329     return;
00330  }
00331  if (!d->mOwner) {
00332     kdError(11001) << k_funcinfo << ": no player has been set" << endl;
00333     return;
00334  }
00335 
00336  for (int unsigned i = 0; i < d->mConfigWidgets.count(); i++) {
00337 // kdDebug(11001) << "submit to kgame " << i << endl;
00338     d->mConfigWidgets.at(i)->submitToKGame(d->mGame, d->mOwner);
00339 // kdDebug(11001) << "done: submit to kgame " << i << endl;
00340  }
00341 }
00342 
00343 void KGameDialog::slotRemoveConfigWidget(QObject* configWidget)
00344 {
00345  d->mConfigWidgets.removeRef((KGameDialogConfig*)configWidget);
00346 }
00347 
KDE Home | KDE Accessibility Home | Description of Access Keys