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

KDE's Doxygen guidelines are available online.