kchat.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <klocale.h>
00021 #include <kdebug.h>
00022
00023 #include "kchat.h"
00024
00025 class KChatPrivate
00026 {
00027 public:
00028 KChatPrivate()
00029 {
00030 }
00031
00032 bool mAutoAddMessages;
00033
00034 QMap<int, QString> mPlayerMap;
00035 int mPlayerId;
00036 int mFromId;
00037 };
00038
00039 KChat::KChat(QWidget* parent, bool twoPlayerGame) : KChatBase(parent, twoPlayerGame)
00040 {
00041 init();
00042 }
00043
00044 KChat::~KChat()
00045 {
00046 kdDebug(11000) << "DESTRUCT KChat " << this << endl;
00047 delete d;
00048 }
00049
00050 void KChat::init()
00051 {
00052 kdDebug(11001) << "INIT KChat " << this << endl;
00053 d = new KChatPrivate;
00054 d->mAutoAddMessages = true;
00055 d->mPlayerId = 1;
00056 d->mFromId = 1;
00057 }
00058
00059 void KChat::setFromNickname(const QString& n)
00060 { d->mFromId = addPlayer(n); }
00061 const QString& KChat::fromName() const
00062 { return player(fromId()); }
00063 void KChat::setAutoAddMessages(bool add)
00064 { d->mAutoAddMessages = add; }
00065 bool KChat::autoAddMessages() const
00066 { return d->mAutoAddMessages; }
00067 int KChat::uniqueId()
00068 { return d->mPlayerId++; }
00069 int KChat::fromId() const
00070 { return d->mFromId; }
00071 const QString& KChat::player(int id) const
00072 { return d->mPlayerMap[id]; }
00073
00074 void KChat::returnPressed(const QString& text)
00075 {
00076 int id = fromId();
00077 if (id < 0) {
00078
00079 kdWarning(11000) << "KChat: no fromNickname has been set!" << endl;
00080 }
00081 emit signalSendMessage(id, text);
00082 if (autoAddMessages()) {
00083 QString p = player(id);
00084 if (p.isNull()) {
00085 p = i18n("Unknown");
00086 }
00087 kdDebug(11000) << "auto adding message from player " << p << " ;id=" << id << endl;
00088 addMessage(p, text);
00089 }
00090 }
00091
00092 int KChat::addPlayer(const QString& nickname)
00093 {
00094 int id = uniqueId();
00095 d->mPlayerMap.insert(id, nickname);
00096 return id;
00097 }
00098
00099 void KChat::removePlayer(int id)
00100 {
00101 d->mPlayerMap.remove(id);
00102 }
00103
00104 void KChat::removePlayer(const QString& nickname)
00105 {
00106 QMap<int, QString>::Iterator it;
00107 for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) {
00108 if (it.data() == nickname) {
00109 d->mPlayerMap.remove(it);
00110 }
00111 }
00112 }
00113
00114
00115 #include "kchat.moc"
|