KDEGames

kchatbasemodel.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 __KCHATBASEMODEL_H__
10#define __KCHATBASEMODEL_H__
11
12// own
13#include "kdegamesprivate_export.h"
14// Qt
15#include <QAbstractListModel>
16#include <QPair>
17#include <QSharedDataPointer>
18// Std
19#include <memory>
20
21class KChatBaseModelPrivate;
22class KChatBaseMessagePrivate;
23class KConfig;
24
25/**
26 * \class KChatBaseMessage kchatbasemodel.h <KChatBaseModel>
27 *
28 * @short The class of the elements stored in the chat list model
29 *
30 * It's a pair of strings where the first element is the sender name and the
31 * second one is the actual message. It furthermore indicates the type of the
32 * message: normal or system
33 */
34class KDEGAMESPRIVATE_EXPORT KChatBaseMessage : public QPair<QString, QString>
35{
36public:
37 /** The different types of messages */
38 enum MessageType { Normal, System };
39
40 /** Default constructor. Necessary for Qt metatypes */
42
43 /** Initializing constructor */
44 KChatBaseMessage(const QString &sender, const QString &message, MessageType type = Normal);
45
46 /** Copy constructor. Necessary for Qt metatypes */
48
49 KChatBaseMessage &operator=(const KChatBaseMessage &m);
50
51 /** Default destructor */
53
54private:
56};
57Q_DECLARE_METATYPE(KChatBaseMessage)
58
59/**
60 * \class KChatBaseModel kchatbasemodel.h <KChatBaseModel>
61 *
62 * The model used to store messages displayed in the chat dialog messages
63 * list. This is a list model and thus derived from @ref QAbstractListModel
64 * and implementing its abstract API.
65 */
66class KDEGAMESPRIVATE_EXPORT KChatBaseModel : public QAbstractListModel
67{
68 Q_OBJECT
69
70public:
71 /** Default constructor */
72 explicit KChatBaseModel(QObject *parent = nullptr);
73
74 /** Default destructor */
75 ~KChatBaseModel() override;
76
77 /**
78 * Reimplementation of the inherited method.
79 * @return The current number of messages in the list
80 */
81 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
82
83 /**
84 * Reimplementation of the inherited method.
85 * @return The KChatBaseMessage at the given index as a QVariant
86 */
87 QVariant data(const QModelIndex &index, int role) const override;
88
89 /**
90 * Set the font that is used for the name part of a message. See also
91 * nameFont and setBothFont
92 */
93 void setNameFont(const QFont &font);
94
95 /**
96 * Set the font that is used for the message part of a message.
97 * @see messageFont, setBothFont
98 */
99 void setMessageFont(const QFont &font);
100
101 /**
102 * This sets both - nameFont and messageFont to font. You
103 * probably want to use this if you don't wish to distinguish between
104 * these parts of a message.
105 * @param font A font used for both nameFont and messageFont
106 */
107 void setBothFont(const QFont &font);
108
109 /**
110 * Same as setNameFont but applies only to system messages.
111 */
112 void setSystemNameFont(const QFont &font);
113
114 /**
115 * Same as setMessageFont but applies only to system messages.
116 */
117 void setSystemMessageFont(const QFont &font);
118
119 /**
120 * Same as setBothFont but applies only to system messages.
121 */
122 void setSystemBothFont(const QFont &font);
123
124 /**
125 * This font should be used for the name (the "from: " part) of a
126 * message. layoutMessage uses this to set the font using
127 * KChatBaseItemDelegate::setNameFont but if you want to overwrite
128 * layoutMessage you should do this yourself.
129 * @return The font that is used for the name part of the message.
130 */
131 const QFont &nameFont() const;
132
133 /**
134 * This font should be used for a message. layoutMessage sets the
135 * font of a message using KChatBaseItemDelegate::setMessageFont but if you
136 * replace layoutMessage with your own function you should use
137 * messageFont() yourself.
138 * @return The font that is used for a message
139 */
140 const QFont &messageFont() const;
141
142 /**
143 * Same as systemNameFont but applies only to system messages.
144 */
145 const QFont &systemNameFont() const;
146
147 /**
148 * Same as systemMessageFont but applies only to system messages.
149 */
150 const QFont &systemMessageFont() const;
151
152 /**
153 * Save the configuration of the dialog to a KConfig object. If
154 * the supplied KConfig pointer is NULL then KGlobal::config() is used
155 * instead (and the group is changed to "KChatBase") butr the current
156 * group is restored at the end.
157 * @param conf A pointer to the KConfig object to save the config
158 * to. If you use 0 then KGlobal::config() is used and the group is changed
159 * to "KChatBase" (the current group is restored at the end).
160 */
161 virtual void saveConfig(KConfig *conf = nullptr);
162
163 /**
164 * Read the configuration from a KConfig object. If the pointer is
165 * NULL KGlobal::config() is used and the group is changed to "KChatBase".
166 * The current KConfig::group is restored after this call.
167 */
168 virtual void readConfig(KConfig *conf = nullptr);
169
170 /**
171 * Set the maximum number of items in the list. If the number of item
172 * exceeds the maximum as many items are deleted (oldest first) as
173 * necessary. The number of items will never exceed this value.
174 * @param maxItems the maximum number of items. -1 (default) for
175 * unlimited.
176 */
177 void setMaxItems(int maxItems);
178
179 /**
180 * Clear all messages in the list.
181 */
182 void clear();
183
184 /**
185 * @return The maximum number of messages in the list. -1 is unlimited. See also
186 * setMaxItems
187 */
188 int maxItems() const;
189
190public Q_SLOTS:
191 /**
192 * Add a text in the listbox. See also signalSendMessage()
193 *
194 * Maybe you want to replace this with a function that creates a nicer text
195 * than "fromName: text"
196 *
197 * Update: the function layoutMessage is called by this now. This
198 * means that you will get user defined outlook on the messages :-)
199 * @param fromName The player who sent this message
200 * @param text The text to be added
201 */
202 virtual void addMessage(const QString &fromName, const QString &text);
203
204 /**
205 * This works just like addMessage but adds a system message. System
206 * messages will have a different look than player messages.
207 *
208 * You may wish to use this to display status information from your game.
209 */
210 virtual void addSystemMessage(const QString &fromName, const QString &text);
211
212 /**
213 * This clears all messages in the view. Note that only the messages are
214 * cleared, not the sender names in the combo box!
215 */
216 void slotClear();
217
218private:
219 std::unique_ptr<KChatBaseModelPrivate> const d;
220};
221
222#endif
The class of the elements stored in the chat list model.
KChatBaseMessage(const KChatBaseMessage &m)
Copy constructor.
virtual ~KChatBaseMessage()
Default destructor.
MessageType
The different types of messages.
The model used to store messages displayed in the chat dialog messages list.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:50 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.