kchatbasemodel.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
00004     Copyright (C) 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kchatbasemodel.h"
00022 
00023 #include <kconfig.h>
00024 #include <kconfiggroup.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <ksharedconfig.h>
00028 #include <QFont>
00029 
00030 class KChatBaseMessagePrivate
00031 {
00032 public:
00033   KChatBaseMessagePrivate()
00034   {
00035     m_type = KChatBaseMessage::Normal;
00036   }
00037 
00038   KChatBaseMessage::MessageType m_type;
00039 };
00040 
00041 KChatBaseMessage::KChatBaseMessage()
00042 {
00043   d = new KChatBaseMessagePrivate();
00044 }
00045     
00046 KChatBaseMessage::KChatBaseMessage(const QString& sender, const QString& message, 
00047       MessageType type) :
00048   QPair<QString,QString>(sender,message)
00049 {
00050   d = new KChatBaseMessagePrivate();
00051   d->m_type = type;
00052 }
00053 
00054 KChatBaseMessage::KChatBaseMessage(const KChatBaseMessage& m) : 
00055     QPair< QString, QString >(m)
00056 {
00057   d = new KChatBaseMessagePrivate();
00058   d->m_type = m.d->m_type;
00059 }
00060 
00061 KChatBaseMessage::~KChatBaseMessage() 
00062 {
00063   delete d;
00064 }
00065     
00066 
00067 class KChatBaseModelPrivate
00068 {
00069 public:
00070   KChatBaseModelPrivate()
00071   {
00072     mAcceptMessage = true;
00073     mMaxItems = -1;
00074   }
00075   bool mAcceptMessage;
00076   int mMaxItems;
00077 
00078   QList<int> mIndex2Id;
00079 
00080   QFont mNameFont;
00081   QFont mMessageFont;
00082   QFont mSystemNameFont;
00083   QFont mSystemMessageFont;
00084 
00085   QList< KChatBaseMessage > m_messages;
00086 
00087 };
00088 
00089 
00090 KChatBaseModel::KChatBaseModel(QObject *parent)
00091      : QAbstractListModel(parent), d(new KChatBaseModelPrivate())
00092 {
00093 }
00094 
00095 KChatBaseModel::~KChatBaseModel()
00096 {
00097 // kDebug(11000) << "KChatBaseModelPrivate: DESTRUCT (" << this << ")";
00098  saveConfig();
00099 }
00100 
00101 void KChatBaseModel::slotClear()
00102 {
00103  clear();
00104 }
00105 
00106 void KChatBaseModel::setNameFont(const QFont& font)
00107 {
00108  d->mNameFont = font;
00109 }
00110 
00111 void KChatBaseModel::setMessageFont(const QFont& font)
00112 {
00113  d->mMessageFont = font;
00114 }
00115 
00116 void KChatBaseModel::setBothFont(const QFont& font)
00117 {
00118  setNameFont(font);
00119  setMessageFont(font);
00120 }
00121 
00122 const QFont& KChatBaseModel::nameFont() const
00123 { return d->mNameFont; }
00124 
00125 const QFont& KChatBaseModel::messageFont() const
00126 { return d->mMessageFont; }
00127 
00128 void KChatBaseModel::setSystemNameFont(const QFont& font)
00129 {
00130  d->mSystemNameFont = font;
00131 }
00132 
00133 void KChatBaseModel::setSystemMessageFont(const QFont& font)
00134 {
00135  d->mSystemMessageFont = font;
00136 }
00137 
00138 void KChatBaseModel::setSystemBothFont(const QFont& font)
00139 {
00140  setSystemNameFont(font);
00141  setSystemMessageFont(font);
00142 }
00143 
00144 const QFont& KChatBaseModel::systemNameFont() const
00145 { return d->mSystemNameFont; }
00146 
00147 const QFont& KChatBaseModel::systemMessageFont() const
00148 { return d->mSystemMessageFont; }
00149 
00150 void KChatBaseModel::saveConfig(KConfig* conf)
00151 {
00152  if (!conf) {
00153         conf = KGlobal::config().data();
00154  }
00155  KConfigGroup cg(conf, "KChatBaseModelPrivate");
00156 
00157  cg.writeEntry("NameFont", nameFont());
00158  cg.writeEntry("MessageFont", messageFont());
00159  cg.writeEntry("SystemNameFont", systemNameFont());
00160  cg.writeEntry("SystemMessageFont", systemMessageFont());
00161  cg.writeEntry("MaxMessages", maxItems());
00162 }
00163 
00164 void KChatBaseModel::readConfig(KConfig* conf)
00165 {
00166  if (!conf) {
00167         conf = KGlobal::config().data();
00168  }
00169  KConfigGroup cg(conf, "KChatBaseModelPrivate");
00170 
00171  setNameFont(cg.readEntry("NameFont", QFont()));
00172  setMessageFont(cg.readEntry("MessageFont", QFont()));
00173  setSystemNameFont(cg.readEntry("SystemNameFont", QFont()));
00174  setSystemMessageFont(cg.readEntry("SystemMessageFont", QFont()));
00175  setMaxItems(cg.readEntry("MaxMessages", -1));
00176 }
00177 
00178 void KChatBaseModel::clear()
00179 {
00180  removeRows(0, rowCount());
00181 }
00182 
00183 void KChatBaseModel::setMaxItems(int maxItems)
00184 {
00185  d->mMaxItems = maxItems;
00186  //TODO cut too many messages
00187  if (maxItems == 0) {
00188         clear();
00189  } else if (maxItems > 0) {
00190         while (rowCount() > (int)maxItems) 
00191   {
00192                 removeRow(0);
00193         }
00194  }
00195 }
00196 
00197 int KChatBaseModel::maxItems() const
00198 { 
00199   return d->mMaxItems; 
00200 }
00201 
00202 
00203 QVariant KChatBaseModel::data(const QModelIndex &index, int role) const
00204 {
00205   if (!index.isValid())
00206       return QVariant();
00207 
00208   if (role == Qt::DisplayRole)
00209   {
00210     KChatBaseMessage p = d->m_messages[index.row()];
00211     return QVariant::fromValue(p);
00212   }
00213   return QVariant();
00214 }
00215 
00216 int KChatBaseModel::rowCount(const QModelIndex &parent) const
00217 {
00218     if (parent.isValid())
00219         return 0;
00220     else
00221         return d->m_messages.size();
00222 }
00223 
00224 void KChatBaseModel::addMessage(const QString& fromName, const QString& text)
00225 {
00226   int row;
00227   row = d->m_messages.size();
00228   beginInsertRows(QModelIndex(), row, row);
00229   d->m_messages.push_back(KChatBaseMessage(fromName,text));
00230   endInsertRows();
00231   
00232   while (maxItems()>-1 && rowCount() > maxItems()) 
00233   {
00234     beginRemoveRows(QModelIndex(), row, row);
00235     d->m_messages.pop_front();
00236     endRemoveRows();
00237   }
00238 }
00239 
00240 void KChatBaseModel::addSystemMessage(const QString& fromName, const QString& text)
00241 {
00242   int row;
00243   row = d->m_messages.size();
00244   beginInsertRows(QModelIndex(), row, row);
00245   d->m_messages.push_back(KChatBaseMessage(fromName,text,KChatBaseMessage::System));
00246   endInsertRows();
00247 }
00248 
00249 
00250 #include "kchatbasemodel.moc"

Generated on Sun Mar 16 08:02:52 2008 for Libkdegames by  doxygen 1.5.3