KDEGames

kchatbasemodel.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann ([email protected])
4  Copyright (C) 2007 Gael de Chalendar (aka Kleag) <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kchatbasemodel.h"
22 
23 #include <KConfig>
24 #include <KConfigGroup>
25 #include <KSharedConfig>
26 
27 #include <QFont>
28 
29 class KChatBaseMessagePrivate
30 {
31 public:
32  KChatBaseMessagePrivate()
33  {
34  m_type = KChatBaseMessage::Normal;
35  }
36 
38 };
39 
41 {
42  d = new KChatBaseMessagePrivate();
43 }
44 
45 KChatBaseMessage::KChatBaseMessage(const QString& sender, const QString& message,
46  MessageType type) :
47  QPair<QString,QString>(sender,message)
48 {
49  d = new KChatBaseMessagePrivate();
50  d->m_type = type;
51 }
52 
54  QPair< QString, QString >(m)
55 {
56  d = new KChatBaseMessagePrivate();
57  d->m_type = m.d->m_type;
58 }
59 
61 {
62  delete d;
63 }
64 
65 
66 class KChatBaseModelPrivate
67 {
68 public:
69  KChatBaseModelPrivate()
70  {
71  mAcceptMessage = true;
72  mMaxItems = -1;
73  }
74  bool mAcceptMessage;
75  int mMaxItems;
76 
77  QList<int> mIndex2Id;
78 
79  QFont mNameFont;
80  QFont mMessageFont;
81  QFont mSystemNameFont;
82  QFont mSystemMessageFont;
83 
84  QList< KChatBaseMessage > m_messages;
85 
86 };
87 
88 
90  : QAbstractListModel(parent), d(new KChatBaseModelPrivate())
91 {
92 }
93 
95 {
96 
97  qCDebug(GAMES_LIB) << "KChatBaseModelPrivate: DESTRUCT (" << this << ")";
98  saveConfig();
99 }
100 
102 {
103  clear();
104 }
105 
107 {
108  d->mNameFont = font;
109 }
110 
112 {
113  d->mMessageFont = font;
114 }
115 
117 {
118  setNameFont(font);
119  setMessageFont(font);
120 }
121 
123 { return d->mNameFont; }
124 
126 { return d->mMessageFont; }
127 
129 {
130  d->mSystemNameFont = font;
131 }
132 
134 {
135  d->mSystemMessageFont = font;
136 }
137 
139 {
140  setSystemNameFont(font);
141  setSystemMessageFont(font);
142 }
143 
145 { return d->mSystemNameFont; }
146 
148 { return d->mSystemMessageFont; }
149 
151 {
152  if (!conf) {
153  conf = KSharedConfig::openConfig().data();
154  }
155  KConfigGroup cg(conf, "KChatBaseModelPrivate");
156 
157  cg.writeEntry("NameFont", nameFont());
158  cg.writeEntry("MessageFont", messageFont());
159  cg.writeEntry("SystemNameFont", systemNameFont());
160  cg.writeEntry("SystemMessageFont", systemMessageFont());
161  cg.writeEntry("MaxMessages", maxItems());
162 }
163 
165 {
166  if (!conf) {
167  conf = KSharedConfig::openConfig().data();
168  }
169  KConfigGroup cg(conf, "KChatBaseModelPrivate");
170 
171  setNameFont(cg.readEntry("NameFont", QFont()));
172  setMessageFont(cg.readEntry("MessageFont", QFont()));
173  setSystemNameFont(cg.readEntry("SystemNameFont", QFont()));
174  setSystemMessageFont(cg.readEntry("SystemMessageFont", QFont()));
175  setMaxItems(cg.readEntry("MaxMessages", -1));
176 }
177 
179 {
180  removeRows(0, rowCount());
181 }
182 
184 {
185  d->mMaxItems = maxItems;
186  //TODO cut too many messages
187  if (maxItems == 0) {
188  clear();
189  } else if (maxItems > 0) {
190  while (rowCount() > (int)maxItems)
191  {
192  removeRow(0);
193  }
194  }
195 }
196 
198 {
199  return d->mMaxItems;
200 }
201 
202 
204 {
205  if (!index.isValid())
206  return QVariant();
207 
208  if (role == Qt::DisplayRole)
209  {
210  KChatBaseMessage p = d->m_messages[index.row()];
211  return QVariant::fromValue(p);
212  }
213  return QVariant();
214 }
215 
217 {
218  if (parent.isValid())
219  return 0;
220  else
221  return d->m_messages.size();
222 }
223 
224 void KChatBaseModel::addMessage(const QString& fromName, const QString& text)
225 {
226  int row;
227  row = d->m_messages.size();
228  beginInsertRows(QModelIndex(), row, row);
229  d->m_messages.push_back(KChatBaseMessage(fromName,text));
230  endInsertRows();
231 
232  while (maxItems()>-1 && rowCount() > maxItems())
233  {
234  beginRemoveRows(QModelIndex(), row, row);
235  d->m_messages.pop_front();
236  endRemoveRows();
237  }
238 }
239 
240 void KChatBaseModel::addSystemMessage(const QString& fromName, const QString& text)
241 {
242  int row;
243  row = d->m_messages.size();
244  beginInsertRows(QModelIndex(), row, row);
245  d->m_messages.push_back(KChatBaseMessage(fromName,text,KChatBaseMessage::System));
246  endInsertRows();
247 }
248 
249 
250 
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-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.