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