KDEGames

kgamechat.h
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2001-2002 Andreas Beckermann <b_mann@gmx.de>
4 SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#ifndef __KGAMECHAT_H__
10#define __KGAMECHAT_H__
11
12// own
13#include "../kchatbase.h"
14#include "kdegamesprivate_export.h"
15// Qt
16#include <QString>
17
18class KPlayer;
19class KGame;
21
22class KGameChatPrivate;
23
24/**
25 * \class KGameChat kgamechat.h <KGame/KGameChat>
26 *
27 * @short A Chat widget for KGame-based games
28 *
29 * Call @ref setFromPlayer() first - this will be used as the "from" part of
30 * every message you will send. Otherwise it won't work! You can also use the
31 * fromPlayer parameter in the constructor though...
32 *
33 * @author Andreas Beckermann <b_mann@gmx.de>
34 */
35class KDEGAMESPRIVATE_EXPORT KGameChat : public KChatBase
36{
37 Q_OBJECT
38
39public:
40 /**
41 * Construct a @ref KGame chat widget on @p game that used @p msgid for
42 * the chat message. The @p fromPlayer is the local player (see @ref
43 * setFromPlayer).
44 */
45 KGameChat(KGame *game, int msgid, KPlayer *fromPlayer, QWidget *parent, KChatBaseModel *model = nullptr, KChatBaseItemDelegate *delegate = nullptr);
46
47 /**
48 * @overload
49 * To make use of this widget you need to call @ref setFromPlayer
50 * manually.
51 */
52 KGameChat(KGame *game, int msgId, QWidget *parent, KChatBaseModel *model = nullptr, KChatBaseItemDelegate *delegate = nullptr);
53
54 /**
55 * @overload
56 * This constructs a widget that is not usable. You must call at least
57 * setGame, setFromPlayer and setMessageId manually.
58 */
59 explicit KGameChat(QWidget *parent);
60
61 ~KGameChat() override;
62
63 enum SendingIds {
64 SendToGroup = 1
65 };
66
67 /**
68 * This sets the fromPlayer to @p player. The fromPlayer is the
69 * player that will appear as "from" when you send messages through this
70 * widget.
71 * @param player The player of this widget
72 */
73 void setFromPlayer(KPlayer *player);
74
75 KPlayer *fromPlayer() const;
76
77 /**
78 * Set the @ref KGame object for this chat widget. All messages will be
79 * sent through this object. You don't have to implement any send
80 * functions, just call this function, call @ref setFromPlayer and be
81 * done :-)
82 * @param g The @ref KGame object the messages will be sent through
83 */
84 void setKGame(KGame *g);
85
86 KGame *game() const;
87
88 /**
89 * @return The id of the messages produced by KGameChat. The id will be
90 * used in @ref KGame as parameter msgid in the method @ref KGame::sendMessage
91 */
92 int messageId() const;
93
94 /**
95 * Change the message id of the chat widget. It is recommended that you
96 * don't use this but prefer the constructor instead, but in certain
97 * situations (such as using this widget in Qt designer) it may be
98 * useful to change the message id.
99 *
100 * See also @ref messageId
101 */
102 void setMessageId(int msgid);
103
104 /**
105 * reimplemented from @ref KChatBase
106 * @return @ref KPlayer::name() for the player set by @ref setFromPlayer
107 */
108 QString fromName() const override;
109
110public Q_SLOTS:
111 void addMessage(const QString &fromName, const QString &text) override
112 {
113 KChatBase::addMessage(fromName, text);
114 }
115 virtual void addMessage(int fromId, const QString &text);
116
117 void slotReceiveMessage(int, const QByteArray &, quint32 receiver, quint32 sender);
118
119protected:
120 /**
121 * @param id The ID of the sending entry, as returned by @ref KChatBase sendingEntry
122 * @return True if the entry "send to all" was selected, otherwise false
123 */
124 bool isSendToAllMessage(int id) const;
125
126 /**
127 * Used to indicate whether a message shall be sent to a group of
128 * players. Note that this was not yet implemented when this doc was
129 * written so this description might be wrong. (FIXME)
130 * @param id The ID of the sending entry, as returned by @ref KChatBase sendingEntry
131 * @return True if the message is meant to be sent to a group (see @ref KPlayer::group
132 * ), e.g. if "send to my group" was selected.
133 */
134 bool isToGroupMessage(int id) const;
135
136 /**
137 * Used to indicate whether the message shall be sent to a single player
138 * only. Note that you can also call @ref isSendToAllMessage and @ref
139 * isToGroupMessage - if both return false it must be a player message.
140 * This behaviour might be changed later - so don't depend on it.
141 *
142 * See also toPlayerId
143 * @param id The ID of the sending entry, as returned by
144 * @ref KChatBase sendingEntry
145 * @return True if the message shall be sent to a special player,
146 * otherwise false.
147 */
148 bool isToPlayerMessage(int id) const;
149
150 /**
151 * @param id The ID of the sending entry, as returned by
152 * @ref KChatBase sendingEntry
153 * @return The ID of the player (see KPlayer::id) the sending entry
154 * belongs to. Note that the parameter id is an id as returned by
155 * @ref KChatBase sendingEntry and the id this method returns is a
156 * KPlayer ID. If @ref isToPlayerMessage returns false this method
157 * returns -1
158 */
159 int playerId(int id) const;
160
161 /**
162 * @param playerId The ID of the KPlayer object
163 * @return The ID of the sending entry (see KChatBase) or -1 if
164 * the player id was not found.
165 */
166 int sendingId(int playerId) const;
167
168 /**
169 * @return True if the player with this ID was added before (see
170 * slotAddPlayer)
171 */
172 bool hasPlayer(int id) const;
173
174 /**
175 * @param name The name of the added player
176 * @return A string that will be added as sending entry in @ref
177 * KChatBase. By default this is "send to name" where name is the name
178 * that you specify. See also KChatBase::addSendingEntry
179 */
180 virtual QString sendToPlayerEntry(const QString &name) const;
181
182protected Q_SLOTS:
183 /**
184 * Unsets a KGame object that has been set using setKGame
185 * before. You don't have to call this - this is usually done
186 * automatically.
187 */
188 void slotUnsetKGame();
189
190 void slotPropertyChanged(KGamePropertyBase *, KPlayer *);
191 void slotAddPlayer(KPlayer *);
192 void slotRemovePlayer(KPlayer *);
193
194 /**
195 * Called when KPlayer::signalNetworkData is emitted. The message
196 * gets forwarded to slotReceiveMessage if @p me equals
197 * fromPlayer.
198 */
199 void slotReceivePrivateMessage(int msgid, const QByteArray &buffer, quint32 sender, KPlayer *me);
200
201protected:
202 void returnPressed(const QString &text) override;
203
204private:
205 void init(KGame *g, int msgid);
206
207private:
208 Q_DECLARE_PRIVATE_D(KChatBase::d, KGameChat)
209};
210
211#endif
A delegate (see the Qt Model/View module for details) to paint the lines of the KChatBase list model ...
The model used to store messages displayed in the chat dialog messages list.
The base class for chat widgets.
Definition kchatbase.h:64
virtual QString fromName() const =0
virtual void addMessage(const QString &fromName, const QString &text)
Add a text in the listbox.
virtual void returnPressed(const QString &text)=0
This is called whenever the user pushed return ie wants to send a message.
A Chat widget for KGame-based games.
Definition kgamechat.h:36
Base class of KGameProperty.
The main KDE game object.
Definition kgame.h:47
Base class for a game player.
Definition kplayer.h:60
Q_SLOTSQ_SLOTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.