• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

kchatbase.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 "kchatbase.h"
00022 #include "kchatbasemodel.h"
00023 #include "kchatbaseitemdelegate.h"
00024 
00025 #include <klineedit.h>
00026 #include <klocale.h>
00027 #include <kstandarddirs.h>
00028 #include <kconfig.h>
00029 #include <kdebug.h>
00030 #include <kglobal.h>
00031 #include <QLayout>
00032 #include <QComboBox>
00033 #include <QPixmap>
00034 #include <QList>
00035 #include <QApplication>
00036 #include <QListView>
00037 
00038 
00039 
00040 class KChatBasePrivate
00041 {
00042 public:
00043   KChatBasePrivate(KChatBaseModel* model, KChatBaseItemDelegate* delegate)
00044   {
00045     mBox = 0;
00046     mEdit = 0;
00047     mCombo = 0;
00048   
00049     mAcceptMessage = true;
00050 
00051     mModel = model;
00052     mDelegate = delegate;
00053   }
00054   QListView* mBox;
00055   KLineEdit* mEdit;
00056   QComboBox* mCombo;
00057   bool mAcceptMessage;
00058 
00059   QList<int> mIndex2Id;
00060 
00061   KChatBaseModel* mModel;
00062   KChatBaseItemDelegate* mDelegate;
00063 };
00064 
00065 void KChatBase::setModel(KChatBaseModel* m)
00066 {
00067     //delete d->mModel;
00068     d->mModel=m;
00069 }
00070 
00071 KChatBaseModel* KChatBase::model()
00072 {
00073     return d->mModel;
00074 }
00075 
00076 KChatBase::KChatBase(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox) : QFrame(parent)
00077 {
00078   KChatBaseModel* mod = model;
00079   if (mod==0)
00080     mod = new KChatBaseModel(parent);
00081   KChatBaseItemDelegate* del = delegate;
00082   if (del == 0)
00083     del = new KChatBaseItemDelegate(parent);
00084   
00085   d = new KChatBasePrivate(mod, del);
00086 
00087  setMinimumWidth(100);
00088  setMinimumHeight(150);
00089 
00090  QVBoxLayout* l = new QVBoxLayout(this);
00091 
00092  d->mBox = new QListView();
00093  d->mBox->setModel(d->mModel);
00094  d->mBox->setItemDelegate(d->mDelegate);
00095  l->addWidget(d->mBox);
00096 
00097  connect(d->mModel, SIGNAL(rowsInserted (const QModelIndex&,int,int)),
00098           d->mBox, SLOT(scrollToBottom()));
00099 
00100  connect(d->mBox, SIGNAL( customContextMenuRequested(const QPoint &)),
00101          this, SLOT(customMenuHandler(const QPoint &)));
00102 
00103  d->mBox->setContextMenuPolicy ( Qt::CustomContextMenu );
00104  d->mBox->setFocusPolicy(Qt::NoFocus);
00105  d->mBox->setSelectionMode(QAbstractItemView::SingleSelection);
00106 
00107  l->addSpacing(5);
00108 
00109  QHBoxLayout* h = new QHBoxLayout;
00110  l->addLayout(h);
00111  d->mEdit = new KLineEdit(this);
00112  d->mEdit->setHandleSignals(false);
00113  d->mEdit->setTrapReturnKey(true);
00114  d->mEdit->completionObject(); // add the completion object
00115  d->mEdit->setCompletionMode(KGlobalSettings::CompletionNone);
00116  connect(d->mEdit, SIGNAL(returnPressed(const QString&)), this, SLOT(slotReturnPressed(const QString&)));
00117  h->addWidget(d->mEdit);
00118 
00119  if (!noComboBox) {
00120     d->mCombo = new QComboBox(this);
00121     h->addWidget(d->mCombo);
00122     addSendingEntry(i18n("Send to All Players"), SendToAll);//FIXME: where to put the id?
00123  }
00124 
00125  d->mAcceptMessage = true; // by default
00126  setMaxItems(-1); // unlimited
00127 
00128  readConfig();
00129 }
00130 
00131 const QModelIndex KChatBase::indexAt(const QPoint& pos) const
00132 {
00133     return d->mBox->indexAt(pos);
00134 }
00135 
00136 void KChatBase::customMenuHandler(const QPoint &pos)
00137 {
00138     kDebug(10500) << "custom menu has been requested at position="<<pos<<". Implement handler at subclass if you need it.";
00139 }
00140 
00141 KChatBase::~KChatBase()
00142 {
00143 // kDebug(11000) << "KChatBase: DESTRUCT (" << this << ")";
00144  saveConfig();
00145  delete d;
00146 }
00147 
00148 bool KChatBase::acceptMessage() const
00149 { return d->mAcceptMessage; }
00150 
00151 void KChatBase::setAcceptMessage(bool a)
00152 { d->mAcceptMessage = a; }
00153 
00154 bool KChatBase::addSendingEntry(const QString& text, int id)
00155 {
00156 //FIXME: is ID used correctly?
00157 // do we need ID at all?
00158 // what the hell should be here?
00159 // d->mCombo->insertItem(i18n("Send to All Players"), SendToAll);
00160  return insertSendingEntry(text, id);
00161 }
00162 
00163 bool KChatBase::insertSendingEntry(const QString& text, int id, int index)
00164 {
00165  if (!d->mCombo) {
00166     kWarning(11000) << "KChatBase: Cannot add an entry to the combo box";
00167     return false;
00168  }
00169  if (d->mIndex2Id.indexOf(id) != -1) {
00170     kError(11000) << "KChatBase: Cannot add more than one entry with the same ID! ";
00171     kError(11000) << "KChatBase: Text="<<text;
00172     return false;
00173  }
00174  d->mCombo->insertItem(index, text);
00175  if (index < 0) {
00176     d->mIndex2Id.prepend(id);
00177  } else {
00178     d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
00179  }
00180  if (d->mIndex2Id.count() != d->mCombo->count()) {
00181     kError(11000) << "KChatBase: internal ERROR - local IDs do not match combo box entries!";
00182  }
00183  return true;
00184 }
00185 
00186 int KChatBase::sendingEntry() const
00187 {
00188  if (!d->mCombo) {
00189     kWarning(11001) << "Cannot retrieve index from NULL combo box";
00190     return -1;
00191  }
00192  const int index = d->mCombo->currentIndex();
00193  if ( index >= 0 && index <  d->mIndex2Id.size())
00194     return d->mIndex2Id[index];
00195 
00196  kWarning(11000) << "could not find the selected sending entry!";
00197  return -1;
00198 }
00199 
00200 void KChatBase::removeSendingEntry(int id)
00201 {
00202  if (!d->mCombo) {
00203     kWarning(11000) << "KChatBase: Cannot remove an entry from the combo box";
00204     return;
00205  }
00206  d->mCombo->removeItem(findIndex(id));
00207  d->mIndex2Id.removeAll(id);
00208 }
00209 
00210 void KChatBase::changeSendingEntry(const QString& text, int id)
00211 {
00212  if (!d->mCombo) {
00213     kWarning(11000) << "KChatBase: Cannot change an entry in the combo box";
00214     return;
00215  }
00216  int index = findIndex(id);
00217  d->mCombo->setItemText(index, text);
00218 }
00219 
00220 void KChatBase::setSendingEntry(int id)
00221 {
00222  if (!d->mCombo) {
00223     kWarning(11000) << "KChatBase: Cannot set an entry in the combo box";
00224     return;
00225  }
00226  d->mCombo->setCurrentIndex(findIndex(id));
00227 }
00228 
00229 int KChatBase::findIndex(int id) const
00230 {
00231  return d->mIndex2Id.indexOf(id);
00232 }
00233 
00234 int KChatBase::nextId() const
00235 {
00236  int i = SendToAll + 1;
00237  while (d->mIndex2Id.indexOf(i) != -1) {
00238     i++;
00239  }
00240  return i;
00241 }
00242 
00243 void KChatBase::slotReturnPressed(const QString& text)
00244 {
00245  if (text.length() <= 0) {
00246     // no text entered - probably hit return by accident
00247     return;
00248  } else if (!acceptMessage()) {
00249     return;
00250  }
00251  d->mEdit->completionObject()->addItem(text);
00252  d->mEdit->clear();
00253  returnPressed(text);
00254 }
00255 
00256 QString KChatBase::comboBoxItem(const QString& name) const
00257 { // TODO: such a function for "send to all" and "send to my group"
00258  return i18n("Send to %1", name);
00259 }
00260 
00261 void KChatBase::slotClear()
00262 {
00263  clear();
00264 }
00265 
00266 
00267 void KChatBase::setCompletionMode(KGlobalSettings::Completion mode)
00268 { d->mEdit->setCompletionMode(mode); }
00269 
00270 void KChatBase::saveConfig(KConfig* conf)
00271 {
00272   if (conf == 0) {
00273     return;
00274   }
00275   d->mModel->saveConfig(conf);
00276 }
00277 
00278 void KChatBase::readConfig(KConfig* conf)
00279 {
00280   if (conf == 0) {
00281     return;
00282   }
00283   d->mModel->readConfig(conf);
00284 }
00285 
00286 void KChatBase::clear()
00287 {
00288  d->mModel->removeRows(0, d->mModel->rowCount());
00289 }
00290 
00291 void KChatBase::setMaxItems(int maxItems)
00292 {
00293  d->mModel->setMaxItems(maxItems);
00294  //TODO cut too many messages
00295  if (maxItems == 0) {
00296     clear();
00297  } else if (maxItems > 0) {
00298   while (d->mModel->rowCount() > (int)maxItems) {
00299    d->mModel->removeRow(0);
00300   }
00301  }
00302 }
00303 
00304 int KChatBase::maxItems() const
00305 { 
00306   return d->mModel->maxItems(); 
00307 }
00308 
00309 QFont KChatBase::nameFont() const
00310 {
00311   return d->mModel->nameFont();
00312 }
00313 
00314 QFont KChatBase::messageFont() const
00315 {
00316   return d->mModel->messageFont();
00317 }
00318 
00319 QFont KChatBase::systemNameFont() const
00320 {
00321   return d->mModel->systemNameFont();
00322 }
00323 
00324 QFont KChatBase::systemMessageFont() const
00325 {
00326   return d->mModel->systemMessageFont();
00327 }
00328 
00329 void KChatBase::setNameFont(const QFont& font)
00330 {
00331   d->mModel->setNameFont(font);
00332 }
00333 
00334 void KChatBase::setMessageFont(const QFont& font)
00335 {
00336   d->mModel->setMessageFont(font);
00337 }
00338 
00339 void KChatBase::setBothFont(const QFont& font)
00340 {
00341   d->mModel->setBothFont(font);
00342 }
00343 
00344 void KChatBase::setSystemNameFont(const QFont& font)
00345 {
00346   d->mModel->setSystemNameFont(font);
00347 }
00348 
00349 void KChatBase::setSystemMessageFont(const QFont& font)
00350 {
00351   d->mModel->setSystemMessageFont(font);
00352 }
00353 
00354 void KChatBase::setSystemBothFont(const QFont& font)
00355 {
00356   d->mModel->setSystemBothFont(font);
00357 }
00358 
00359 void KChatBase::addMessage(const QString& fromName, const QString& text)
00360 {
00361    d->mModel->addMessage(fromName, text);
00362 }
00363 
00364 void KChatBase::addSystemMessage(const QString& fromName, const QString& text)
00365 {
00366    d->mModel->addSystemMessage(fromName, text);
00367 }
00368 
00369 #include "kchatbase.moc"

libkdegames

Skip menu "libkdegames"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal