KDEGames

kchatbasemodel.cpp
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#include "kchatbasemodel.h"
10
11// own
12#include <kdegamesprivate_logging.h>
13// KF
14#include <KConfig>
15#include <KConfigGroup>
16#include <KSharedConfig>
17// Qt
18#include <QFont>
19#include <QSharedData>
20
21class KChatBaseMessagePrivate : public QSharedData
22{
23public:
24 KChatBaseMessagePrivate() = default;
25
26public:
27 KChatBaseMessage::MessageType m_type = KChatBaseMessage::Normal;
28};
29
31 : d(new KChatBaseMessagePrivate())
32{
33}
34
36 : QPair<QString, QString>(sender, message)
37 , d(new KChatBaseMessagePrivate())
38{
39 d->m_type = type;
40}
41
43
45
46KChatBaseMessage &KChatBaseMessage::operator=(const KChatBaseMessage &other) = default;
47
48class KChatBaseModelPrivate
49{
50public:
51 KChatBaseModelPrivate() = default;
52
53public:
54 bool mAcceptMessage = true;
55 int mMaxItems = 1;
56
57 QList<int> mIndex2Id;
58
59 QFont mNameFont;
60 QFont mMessageFont;
61 QFont mSystemNameFont;
62 QFont mSystemMessageFont;
63
64 QList<KChatBaseMessage> m_messages;
65};
66
68 : QAbstractListModel(parent)
69 , d(new KChatBaseModelPrivate())
70{
71}
72
74{
75 qCDebug(KDEGAMESPRIVATE_LOG) << "KChatBaseModelPrivate: DESTRUCT (" << this << ")";
76 saveConfig();
77}
78
80{
81 clear();
82}
83
85{
86 d->mNameFont = font;
87}
88
90{
91 d->mMessageFont = font;
92}
93
95{
96 setNameFont(font);
97 setMessageFont(font);
98}
99
101{
102 return d->mNameFont;
103}
104
106{
107 return d->mMessageFont;
108}
109
111{
112 d->mSystemNameFont = font;
113}
114
116{
117 d->mSystemMessageFont = font;
118}
119
121{
122 setSystemNameFont(font);
124}
125
127{
128 return d->mSystemNameFont;
129}
130
132{
133 return d->mSystemMessageFont;
134}
135
137{
138 if (!conf) {
140 }
141 KConfigGroup cg(conf, QStringLiteral("KChatBaseModelPrivate"));
142
143 cg.writeEntry("NameFont", nameFont());
144 cg.writeEntry("MessageFont", messageFont());
145 cg.writeEntry("SystemNameFont", systemNameFont());
146 cg.writeEntry("SystemMessageFont", systemMessageFont());
147 cg.writeEntry("MaxMessages", maxItems());
148}
149
151{
152 if (!conf) {
154 }
155 KConfigGroup cg(conf, QStringLiteral("KChatBaseModelPrivate"));
156
157 setNameFont(cg.readEntry("NameFont", QFont()));
158 setMessageFont(cg.readEntry("MessageFont", QFont()));
159 setSystemNameFont(cg.readEntry("SystemNameFont", QFont()));
160 setSystemMessageFont(cg.readEntry("SystemMessageFont", QFont()));
161 setMaxItems(cg.readEntry("MaxMessages", -1));
162}
163
165{
166 removeRows(0, rowCount());
167}
168
170{
171 d->mMaxItems = maxItems;
172 // TODO cut too many messages
173 if (maxItems == 0) {
174 clear();
175 } else if (maxItems > 0) {
176 while (rowCount() > (int)maxItems) {
177 removeRow(0);
178 }
179 }
180}
181
183{
184 return d->mMaxItems;
185}
186
187QVariant KChatBaseModel::data(const QModelIndex &index, int role) const
188{
189 if (!index.isValid())
190 return QVariant();
191
192 if (role == Qt::DisplayRole) {
193 KChatBaseMessage p = d->m_messages[index.row()];
194 return QVariant::fromValue(p);
195 }
196 return QVariant();
197}
198
199int KChatBaseModel::rowCount(const QModelIndex &parent) const
200{
201 if (parent.isValid())
202 return 0;
203 else
204 return d->m_messages.size();
205}
206
207void KChatBaseModel::addMessage(const QString &fromName, const QString &text)
208{
209 int row;
210 row = d->m_messages.size();
211 beginInsertRows(QModelIndex(), row, row);
212 d->m_messages.push_back(KChatBaseMessage(fromName, text));
214
215 while (maxItems() > -1 && rowCount() > maxItems()) {
216 beginRemoveRows(QModelIndex(), row, row);
217 d->m_messages.pop_front();
219 }
220}
221
222void KChatBaseModel::addSystemMessage(const QString &fromName, const QString &text)
223{
224 int row;
225 row = d->m_messages.size();
226 beginInsertRows(QModelIndex(), row, row);
227 d->m_messages.push_back(KChatBaseMessage(fromName, text, KChatBaseMessage::System));
229}
230
231#include "moc_kchatbasemodel.cpp"
The class of the elements stored in the chat list model.
KChatBaseMessage()
Default constructor.
virtual ~KChatBaseMessage()
Default destructor.
MessageType
The different types of messages.
virtual void addSystemMessage(const QString &fromName, const QString &text)
This works just like addMessage but adds a system message.
void clear()
Clear all messages in the list.
void setSystemNameFont(const QFont &font)
Same as setNameFont but applies only to system messages.
void setSystemMessageFont(const QFont &font)
Same as setMessageFont but applies only to system messages.
const QFont & messageFont() const
This font should be used for a message.
void slotClear()
This clears all messages in the view.
virtual void readConfig(KConfig *conf=nullptr)
Read the configuration from a KConfig object.
void setMessageFont(const QFont &font)
Set the font that is used for the message part of a message.
const QFont & systemMessageFont() const
Same as systemMessageFont but applies only to system messages.
~KChatBaseModel() override
Default destructor.
void setNameFont(const QFont &font)
Set the font that is used for the name part of a message.
virtual void addMessage(const QString &fromName, const QString &text)
Add a text in the listbox.
int maxItems() const
void setSystemBothFont(const QFont &font)
Same as setBothFont but applies only to system messages.
void setBothFont(const QFont &font)
This sets both - nameFont and messageFont to font.
const QFont & nameFont() const
This font should be used for the name (the "from: " part) of a message.
const QFont & systemNameFont() const
Same as systemNameFont but applies only to system messages.
void setMaxItems(int maxItems)
Set the maximum number of items in the list.
virtual void saveConfig(KConfig *conf=nullptr)
Save the configuration of the dialog to a KConfig object.
QVariant data(const QModelIndex &index, int role) const override
Reimplementation of the inherited method.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Reimplementation of the inherited method.
KChatBaseModel(QObject *parent=nullptr)
Default constructor.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
bool removeRow(int row, const QModelIndex &parent)
virtual bool removeRows(int row, int count, const QModelIndex &parent)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
QObject * parent() const const
T qobject_cast(QObject *object)
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:48:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.