KDEGames

kchatbase.h
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
4 SPDX-FileCopyrightText: 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#ifndef __KCHATBASE_H__
10#define __KCHATBASE_H__
11
12// own
13#include "kdegamesprivate_export.h"
14// KF
15#include <KCompletion>
16// Qt
17#include <QFrame>
18// Std
19#include <memory>
20
21class KConfig;
22
23class KChatBasePrivate;
24class KChatBaseModel;
26class QModelIndex;
27class QPoint;
28
29/**
30 * \class KChatBase kchatbase.h <KChatBase>
31 *
32 * @short The base class for chat widgets
33 *
34 * This is the base class for both KChat and KGameChat. KGameChat is the class
35 * you want to use if you write a KGame based game as it will do most things for
36 * you. KChat is more or less the same but not KGame dependent
37 *
38 * KChatBase provides a complete chat widget, featuring different sending means
39 * (e.g. "send to all", "send to player1", "send to group2" and so on - see
40 * addSendingEntry). It also provides full auto-completion capabilities (see
41 * KCompletion and KLineEdit) which defaults to disabled. The user can
42 * change this by right-clicking on the KLineEdit widget and selecting the
43 * desired behaviour. You can also change this manually by calling
44 * setCompletionMode.
45 *
46 * To make KhatBase useful you have to overwrite at least returnPressed.
47 * Here you should send the message to all of your clients (or just some of
48 * them, depending on sendingEntry).
49 *
50 * To add a message just call addMessage with the nickname of the player
51 * who sent the message and the message itself.
52 *
53 * You probably don't want to use the abstract class KChatBase directly but use
54 * one of the derived classes KChat or KGameChat. The latter is the
55 * widget of choice if you develop a KGame application as you don't have to
56 * do anything but providing a KGame object. If you want to change the kind of
57 * elements displayed (using pixmaps for example), then you will also have to
58 * derive the KChatBaseModel and KChatBaseItemDelegate classes.
59 *
60 * @author Andreas Beckermann <b_mann@gmx.de>
61 * @author Gael de Chalendar (aka Kleag) <kleag@free.fr> for the port to Model/View
62 */
63class KDEGAMESPRIVATE_EXPORT KChatBase : public QFrame
64{
65 Q_OBJECT
66
67public:
68 /**
69 * @param parent The parent widget for this widget.
70 * @param model
71 * @param delegate
72 * @param noComboBox If true then the combo box where the player can
73 * choose where to send messages to (either globally or just to some
74 * players) will not be added.
75 */
76 explicit KChatBase(QWidget *parent, KChatBaseModel *model = nullptr, KChatBaseItemDelegate *delegate = nullptr, bool noComboBox = false);
77
78 /**
79 * Destruct the KChatBase object
80 *
81 * Also calls saveConfig
82 */
83 ~KChatBase() override;
84
85 enum SendingIds {
86 SendToAll = 0
87 };
88
89 /**
90 * @return The name that will be shown for messages from this widget. Either the
91 * string that was set by setFromName or the name of the player
92 * that was set by setFromPlayer
93 */
94 virtual QString fromName() const = 0;
95
96 /**
97 * Adds a new entry in the combo box. The default is "send to all
98 * players" only. This function is provided for convenience. You can
99 * also call insertSendingEntry with index = -1.
100 * See also nextId!
101 * @param text The text of the new entry
102 * @param id An ID for this entry. This must be unique for this
103 * entry. It has nothing to do with the position of the entry in the
104 * combo box. See nextId
105 * @return True if successful, otherwise false (e.g. if the id is already used)
106 */
107 bool addSendingEntry(const QString &text, int id);
108
109 /**
110 * Inserts a new entry in the combo box.
111 * @param text The entry
112 * @param id An ID for this entry. This must be unique for this
113 * entry. It has nothing to do with the position of the entry in the
114 * combo box!
115 * @see nextId
116 * @param index The position of the entry. If -1 the entry will be added
117 * at the bottom
118 * @return True if successful, otherwise false (e.g. if the id is already used)
119 */
120 bool insertSendingEntry(const QString &text, int id, int index = -1);
121
122 /**
123 * This changes a combo box entry.
124 * @param text The new text of the entry
125 * @param id The ID of the item to be changed
126 */
127 void changeSendingEntry(const QString &text, int id);
128
129 /**
130 * This selects a combo box entry.
131 * @param id The ID of the item to be selected
132 */
133 void setSendingEntry(int id);
134
135 /**
136 * Removes the entry with the ID id from the combo box. Note that id is
137 * _not_ the index of the entry!
138 * @see addSendingEntry
139 * @param id The unique id of the entry
140 */
141 void removeSendingEntry(int id);
142
143 /**
144 * @return The _unique ID_ of the sending entry that has been selected.
145 * @see addSendingEntry
146 *
147 * Note that the entry "send to all" _always_ uses
148 * KChatBase::SendToAll, i.e. 0 as id!
149 */
150 int sendingEntry() const;
151
152 /**
153 * @return The index of the combo box entry with the given id
154 */
155 int findIndex(int id) const;
156
157 /**
158 * @return An ID that has not yet been used in the combo box.
159 * @see addSendingEntry
160 */
161 int nextId() const;
162
163 /**
164 * @return True if this widget is able to send messages (see
165 * returnPressed) and false if not. The default implementation returns
166 * the value which has been set by setAcceptMessage (true by
167 * default)
168 */
169 virtual bool acceptMessage() const;
170
171 /**
172 * See KLineEdit::setCompletionMode
173 */
174 void setCompletionMode(KCompletion::CompletionMode mode);
175
176 /**
177 * Set the font that is used for the name part of a message. See also
178 * nameFont and setBothFont
179 */
180 void setNameFont(const QFont &font);
181
182 /**
183 * Set the font that is used for the message part of a message.
184 * @see messageFont, setBothFont
185 */
186 void setMessageFont(const QFont &font);
187
188 /**
189 * This sets both - nameFont and messageFont to font. You
190 * probably want to use this if you don't wish to distinguish between
191 * these parts of a message.
192 * @param font A font used for both nameFont and messageFont
193 */
194 void setBothFont(const QFont &font);
195
196 /**
197 * Same as setNameFont but applies only to system messages.
198 * @see layoutSystemMessage
199 */
200 void setSystemNameFont(const QFont &font);
201
202 /**
203 * Same as setMessageFont but applies only to system messages.
204 * @see layoutSystemMessage
205 */
206 void setSystemMessageFont(const QFont &font);
207
208 /**
209 * Same as setBothFont but applies only to system messages.
210 * @see layoutSystemMessage
211 */
212 void setSystemBothFont(const QFont &font);
213
214 /**
215 * This font should be used for the name (the "from: " part) of a
216 * message. layoutMessage uses this to set the font using
217 * KChatBaseItemDelegate::setNameFont but if you want to overwrite
218 * layoutMessage you should do this yourself.
219 * @return The font that is used for the name part of the message.
220 */
221 QFont nameFont() const;
222
223 /**
224 * This font should be used for a message. layoutMessage sets the
225 * font of a message using KChatBaseItemDelegate::setMessageFont but if you
226 * replace layoutMessage with your own function you should use
227 * messageFont() yourself.
228 * @return The font that is used for a message
229 */
230 QFont messageFont() const;
231
232 /**
233 * Same as systemNameFont but applies only to system messages.
234 * @see layoutSystemMessage
235 */
236 QFont systemNameFont() const;
237
238 /**
239 * Same as systemMessageFont but applies only to system messages.
240 * @see layoutSystemMessage
241 */
242 QFont systemMessageFont() const;
243
244 /**
245 * Save the configuration of the dialog to a KConfig object. If
246 * the supplied KConfig pointer is NULL then KGlobal::config() is used
247 * instead (and the group is changed to "KChatBase") butr the current
248 * group is restored at the end.
249 * @param conf A pointer to the KConfig object to save the config
250 * to. If you use 0 then KGlobal::config() is used and the group is changed
251 * to "KChatBase" (the current group is restored at the end).
252 */
253 virtual void saveConfig(KConfig *conf = nullptr);
254
255 /**
256 * Read the configuration from a KConfig object. If the pointer is
257 * NULL KGlobal::config() is used and the group is changed to "KChatBase".
258 * The current KConfig::group is restored after this call.
259 */
260 virtual void readConfig(KConfig *conf = nullptr);
261
262 /**
263 * Set the maximum number of items in the list. If the number of item
264 * exceeds the maximum as many items are deleted (oldest first) as
265 * necessary. The number of items will never exceed this value.
266 * @param maxItems the maximum number of items. -1 (default) for
267 * unlimited.
268 */
269 void setMaxItems(int maxItems);
270
271 /**
272 * Clear all messages in the list.
273 */
274 void clear();
275
276 /**
277 * @return The maximum number of messages in the list. -1 is unlimited. See also
278 * setMaxItems
279 */
280 int maxItems() const;
281
282 KChatBaseModel *model();
283 void setModel(KChatBaseModel *m);
284
285public Q_SLOTS:
286 /**
287 * Add a text in the listbox. See also signalSendMessage()
288 *
289 * Maybe you want to replace this with a function that creates a nicer text
290 * than "fromName: text"
291 *
292 * Update: the function layoutMessage is called by this now. This
293 * means that you will get user defined outlook on the messages :-)
294 * @param fromName The player who sent this message
295 * @param text The text to be added
296 */
297 virtual void addMessage(const QString &fromName, const QString &text);
298
299 /**
300 * This works just like addMessage but adds a system message.
301 * layoutSystemMessage is used to generate the displayed item. System
302 * messages will have a different look than player messages.
303 *
304 * You may wish to use this to display status information from your game.
305 */
306 virtual void addSystemMessage(const QString &fromName, const QString &text);
307
308 /**
309 * This clears all messages in the view. Note that only the messages are
310 * cleared, not the sender names in the combo box!
311 */
312 void slotClear();
313
314 /**
315 * @param a If false this widget cannot send a message until
316 * setAcceptMessage(true) is called
317 */
318 void setAcceptMessage(bool a);
319
320protected:
321 /**
322 * This is called whenever the user pushed return ie wants to send a
323 * message.
324 *
325 * Note that you MUST add the message to the widget when this function
326 * is called as it has already been added to the KCompletion object
327 * of the KLineEdit widget!
328 *
329 * Must be implemented in derived classes
330 * @param text The message to be sent
331 */
332 virtual void returnPressed(const QString &text) = 0;
333
334 /**
335 * Replace to customize the combo box.
336 *
337 * Default: i18n("Send to %1).arg(name)
338 * @param name The name of the player
339 * @return The string as it will be shown in the combo box
340 */
341 virtual QString comboBoxItem(const QString &name) const;
342
343 /**
344 * Returns the model index of the message at the viewport coordinates point
345 * @param pos position to check index for
346 * @return model index of message with coordinates pos
347 */
348 QModelIndex indexAt(QPoint pos) const;
349
350private Q_SLOTS:
351 /**
352 * Check if a text was entered and if acceptMessage returns true.
353 * Then add the message to the KCompletion object of the KLineEdit
354 * widget and call returnPressed
355 */
356 void slotReturnPressed(const QString &);
357
358 /**
359 * Implements custom menu which is applicable for one chat message
360 * @param pos point where custom menu has been requested (widget coordinates)
361 */
362 virtual void customMenuHandler(QPoint pos);
363
364protected:
365 KChatBase(KChatBasePrivate &dd, QWidget *parent, bool noComboBox);
366
367private:
368 friend class KGameChat;
369 Q_DECLARE_PRIVATE_D(d, KChatBase)
370 std::unique_ptr<KChatBasePrivate> const d;
371 // KF6 TODO: change private d to protected d_ptr, use normal Q_DECLARE_PRIVATE, remove subclass friend
372};
373
374#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 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
Q_SLOTSQ_SLOTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:13:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.