libkdegames Library API Documentation

kgameproperty.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., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 /* 00021 $Id: kgameproperty.cpp,v 1.28 2002/09/21 22:02:41 andreas Exp $ 00022 */ 00023 00024 #include "kgameproperty.h" 00025 #include "kgamepropertyhandler.h" 00026 #include "kgamemessage.h" 00027 #include "kplayer.h" 00028 #include "kgame.h" 00029 00030 #define KPLAYERHANDLER_LOAD_COOKIE 6239 00031 00032 KGamePropertyBase::KGamePropertyBase(int id, KGame* parent) 00033 { 00034 init(); 00035 registerData(id, parent); 00036 } 00037 00038 KGamePropertyBase::KGamePropertyBase(int id, KPlayer* parent) 00039 { 00040 init(); 00041 registerData(id, parent); 00042 } 00043 00044 KGamePropertyBase::KGamePropertyBase(int id, KGamePropertyHandler* owner) 00045 { 00046 init(); 00047 registerData(id, owner); 00048 } 00049 00050 KGamePropertyBase::KGamePropertyBase() 00051 { 00052 init(); 00053 } 00054 00055 KGamePropertyBase::~KGamePropertyBase() 00056 { 00057 unregisterData(); 00058 } 00059 00060 void KGamePropertyBase::init() 00061 { 00062 mOwner = 0; 00063 setDirty(false); 00064 00065 // this is very useful and used by e.g. KGameDialog so 00066 // it is activated by default. Big games may profit by deactivating it to get 00067 // a better performance. 00068 setEmittingSignal(true); 00069 00070 setOptimized(false); 00071 00072 //setReadOnly(false); 00073 mFlags.bits.locked = false ; // setLocked(false); is NOT possible as it checks whether isLocked() allows to change the status 00074 00075 // local is default 00076 setPolicy(PolicyLocal); 00077 } 00078 00079 int KGamePropertyBase::registerData(int id, KGame* owner, QString name) 00080 { return registerData(id, owner->dataHandler(), name); } 00081 00082 int KGamePropertyBase::registerData(int id, KPlayer* owner, QString name) 00083 { return registerData(id, owner->dataHandler(), name); } 00084 00085 int KGamePropertyBase::registerData( KGamePropertyHandler* owner,PropertyPolicy p, QString name) 00086 { return registerData(-1, owner,p, name); } 00087 00088 int KGamePropertyBase::registerData(int id, KGamePropertyHandler* owner, QString name) 00089 { return registerData(id, owner,PolicyUndefined, name); } 00090 00091 int KGamePropertyBase::registerData(int id, KGamePropertyHandler* owner,PropertyPolicy p, QString name) 00092 { 00093 // we don't support changing the id 00094 if (!owner) { 00095 kdWarning(11001) << k_funcinfo << "Resetting owner=0. Sure you want to do this?" << endl; 00096 mOwner=0; 00097 return -1; 00098 } 00099 if (!mOwner) { 00100 if (id==-1) { 00101 id=owner->uniquePropertyId(); 00102 } 00103 mId = id; 00104 mOwner = owner; 00105 mOwner->addProperty(this, name); 00106 if (p!=PolicyUndefined) { 00107 setPolicy(p); 00108 } else { 00109 setPolicy(mOwner->policy()); 00110 } 00111 } 00112 return mId; 00113 } 00114 00115 void KGamePropertyBase::unregisterData() 00116 { 00117 if (!mOwner) { 00118 return; 00119 } 00120 mOwner->removeProperty(this); 00121 mOwner = 0; 00122 } 00123 00124 bool KGamePropertyBase::sendProperty() 00125 { 00126 QByteArray b; 00127 QDataStream s(b, IO_WriteOnly); 00128 KGameMessage::createPropertyHeader(s, id()); 00129 save(s); 00130 if (mOwner) { 00131 return mOwner->sendProperty(s); 00132 } else { 00133 kdError(11001) << k_funcinfo << "Cannot send because there is no receiver defined" << endl; 00134 return false; 00135 } 00136 } 00137 00138 bool KGamePropertyBase::sendProperty(const QByteArray& data) 00139 { 00140 QByteArray b; 00141 QDataStream s(b, IO_WriteOnly); 00142 KGameMessage::createPropertyHeader(s, id()); 00143 s.writeRawBytes(data.data(), data.size()); 00144 if (mOwner) { 00145 return mOwner->sendProperty(s); 00146 } else { 00147 kdError(11001) << k_funcinfo << ": Cannot send because there is no receiver defined" << endl; 00148 return false; 00149 } 00150 } 00151 00152 bool KGamePropertyBase::lock() 00153 { 00154 if (isLocked()) { 00155 return false; 00156 } 00157 setLock(true); 00158 return true; 00159 } 00160 00161 bool KGamePropertyBase::unlock(bool force) 00162 { 00163 if (isLocked() && !force) { 00164 return false; 00165 } 00166 setLock(false); 00167 return true; 00168 } 00169 00170 void KGamePropertyBase::setLock(bool l) 00171 { 00172 QByteArray b; 00173 QDataStream s(b, IO_WriteOnly); 00174 KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock); 00175 00176 s << (Q_INT8)l; 00177 if (mOwner) { 00178 mOwner->sendProperty(s); 00179 } else { 00180 kdError(11001) << k_funcinfo << ": Cannot send because there is no receiver defined" << endl; 00181 return ; 00182 } 00183 } 00184 00185 void KGamePropertyBase::emitSignal() 00186 { 00187 //kdDebug(11001) << k_funcinfo << ": mOwnerP="<< mOwner << " id=" << id() << endl; 00188 if (mOwner ) { 00189 mOwner->emitSignal(this); 00190 } else { 00191 kdError(11001) << k_funcinfo << ":id="<<id()<<" Cannot emitSignal because there is no handler set" << endl; 00192 } 00193 } 00194 00195 void KGamePropertyBase::command(QDataStream& s, int cmd, bool isSender) 00196 { 00197 switch (cmd) { 00198 case CmdLock: 00199 { 00200 if (!isSender) { 00201 Q_INT8 locked; 00202 s >> locked; 00203 mFlags.bits.locked = (bool)locked ; 00204 break; 00205 } 00206 } 00207 default: // probably in derived classes 00208 break; 00209 } 00210 } 00211
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 13 12:48:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003