KDEGames

kchatbasemodel.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2001 Andreas Beckermann <[email protected]>
4  SPDX-FileCopyrightText: 2007 Gael de Chalendar (aka Kleag) <[email protected]>
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 
21 class KChatBaseMessagePrivate : public QSharedData
22 {
23 public:
24  KChatBaseMessagePrivate() = default;
25 
26 public:
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 
46 KChatBaseMessage &KChatBaseMessage::operator=(const KChatBaseMessage &other) = default;
47 
48 class KChatBaseModelPrivate
49 {
50 public:
51  KChatBaseModelPrivate() = default;
52 
53 public:
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);
123  setSystemMessageFont(font);
124 }
125 
127 {
128  return d->mSystemNameFont;
129 }
130 
132 {
133  return d->mSystemMessageFont;
134 }
135 
137 {
138  if (!conf) {
139  conf = KSharedConfig::openConfig().data();
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) {
153  conf = KSharedConfig::openConfig().data();
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 
169 void KChatBaseModel::setMaxItems(int maxItems)
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 
187 QVariant 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 
199 int KChatBaseModel::rowCount(const QModelIndex &parent) const
200 {
201  if (parent.isValid())
202  return 0;
203  else
204  return d->m_messages.size();
205 }
206 
207 void 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));
213  endInsertRows();
214 
215  while (maxItems() > -1 && rowCount() > maxItems()) {
216  beginRemoveRows(QModelIndex(), row, row);
217  d->m_messages.pop_front();
218  endRemoveRows();
219  }
220 }
221 
222 void 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));
228  endInsertRows();
229 }
230 
231 #include "moc_kchatbasemodel.cpp"
bool removeRow(int row, const QModelIndex &parent)
QString readEntry(const char *key, const char *aDefault=nullptr) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
const QFont & nameFont() const
This font should be used for the name (the "from: " part) of a message.
DisplayRole
void beginRemoveRows(const QModelIndex &parent, int first, int last)
virtual bool removeRows(int row, int count, const QModelIndex &parent)
QVariant fromValue(const T &value)
KChatBaseMessage()
Default constructor.
void setSystemMessageFont(const QFont &font)
Same as setMessageFont but applies only to system messages.
void setMessageFont(const QFont &font)
Set the font that is used for the message part of a message.
QVariant data(const QModelIndex &index, int role) const override
Reimplementation of the inherited method.
virtual ~KChatBaseMessage()
Default destructor.
virtual void addMessage(const QString &fromName, const QString &text)
Add a text in the listbox.
void slotClear()
This clears all messages in the view.
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 setSystemBothFont(const QFont &font)
Same as setBothFont but applies only to system messages.
int maxItems() const
KChatBaseModel(QObject *parent=nullptr)
Default constructor.
virtual void addSystemMessage(const QString &fromName, const QString &text)
This works just like addMessage but adds a system message.
void setSystemNameFont(const QFont &font)
Same as setNameFont but applies only to system messages.
void setBothFont(const QFont &font)
This sets both - nameFont and messageFont to font.
void clear()
Clear all messages in the list.
~KChatBaseModel() override
Default destructor.
bool isValid() const const
int row() const const
const QFont & systemMessageFont() const
Same as systemMessageFont but applies only to system messages.
virtual void saveConfig(KConfig *conf=nullptr)
Save the configuration of the dialog to a KConfig object.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Reimplementation of the inherited method.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setMaxItems(int maxItems)
Set the maximum number of items in the list.
const QFont & messageFont() const
This font should be used for a message.
MessageType
The different types of messages.
QObject * parent() const const
QString message
virtual void readConfig(KConfig *conf=nullptr)
Read the configuration from a KConfig object.
The class of the elements stored in the chat list model.
void setNameFont(const QFont &font)
Set the font that is used for the name part of a message.
const QFont & systemNameFont() const
Same as systemNameFont but applies only to system messages.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 03:50:32 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.