libkdegames Library API Documentation

kchatbase.cpp

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kchatbase.h"
00021 
00022 #include <klineedit.h>
00023 #include <klocale.h>
00024 #include <kstandarddirs.h>
00025 #include <kconfig.h>
00026 #include <kapplication.h>
00027 #include <kdebug.h>
00028 
00029 #include <qlayout.h>
00030 #include <qcombobox.h>
00031 #include <qpainter.h>
00032 
00033 class KChatBaseTextPrivate
00034 {
00035 public:
00036     KChatBaseTextPrivate()
00037     {
00038         mNameFont = 0;
00039         mMessageFont = 0;
00040     }
00041 
00042     QString mName;
00043     QString mMessage;
00044 
00045     const QFont* mNameFont;
00046     const QFont* mMessageFont;
00047 };
00048 
00049 
00050 KChatBaseText::KChatBaseText(const QString& name, const QString& message) : QListBoxText()
00051 {
00052  init();
00053  setName(name);
00054  setMessage(message);
00055 }
00056 
00057 KChatBaseText::KChatBaseText(const QString& message) : QListBoxText()
00058 {
00059  init();
00060  setMessage(message);
00061 }
00062 
00063 KChatBaseText::~KChatBaseText()
00064 { 
00065  delete d;
00066 }
00067 
00068 void KChatBaseText::init()
00069 {
00070  d = new KChatBaseTextPrivate;
00071 }
00072 
00073 void KChatBaseText::setName(const QString& n)
00074 {
00075 // d->mName = n;
00076  d->mName = QString("%1: ").arg(n);
00077  setText(QString("%1: %2").arg(name()).arg(message())); // esp. for sorting
00078 }
00079 
00080 void KChatBaseText::setMessage(const QString& m)
00081 {
00082  d->mMessage = m;
00083  setText(QString("%1: %2").arg(name()).arg(message())); // esp. for sorting
00084 }
00085 
00086 const QString& KChatBaseText::name() const
00087 { return d->mName; }
00088 
00089 const QString& KChatBaseText::message() const
00090 { return d->mMessage; }
00091 
00092 QFont KChatBaseText::nameFont() const
00093 {
00094  if (d->mNameFont) {
00095     return *d->mNameFont; 
00096  } else if (listBox()) {
00097     return listBox()->font();
00098  } else {
00099     return QFont();
00100  }
00101 }
00102 
00103 QFont KChatBaseText::messageFont() const
00104 {
00105  if (d->mMessageFont) {
00106     return *d->mMessageFont; 
00107  } else if (listBox()) {
00108     return listBox()->font();
00109  } else {
00110     return QFont();
00111  }
00112 }
00113 
00114 void KChatBaseText::setNameFont(const QFont* f)
00115 { d->mNameFont = f; }
00116 
00117 void KChatBaseText::setMessageFont(const QFont* f)
00118 { d->mMessageFont = f; }
00119 
00120 void KChatBaseText::paint(QPainter* painter)
00121 {
00122  QFontMetrics fm = painter->fontMetrics();
00123  painter->setFont(nameFont());
00124  painter->drawText(3, fm.ascent() + fm.leading()/2, name());
00125  painter->setFont(messageFont());
00126  painter->drawText(3 + QFontMetrics(nameFont()).width(name()), fm.ascent() + fm.leading()/2, message());
00127 }
00128 
00129 int KChatBaseText::width(QListBox* lb) const
00130 {
00131  int w = 0;
00132  if (lb) {
00133     w += 6;
00134     w += QFontMetrics(nameFont()).width(name());
00135     w += QFontMetrics(messageFont()).width(message());
00136  }
00137 // int w = lb ? lb->fontMetrics().width( text() ) + 6 : 0; // QT orig
00138  return QMAX(w, QApplication::globalStrut().width());
00139 }
00140 
00141 int KChatBaseText::height(QListBox* lb) const
00142 {
00143  int h = 0;
00144  if (lb) {
00145     h += 2;
00146     // AB: is lineSpacing still correct?
00147     if (QFontMetrics(nameFont()).lineSpacing() > QFontMetrics(messageFont()).lineSpacing()) { 
00148         h += QFontMetrics(nameFont()).lineSpacing();
00149     } else {
00150         h += QFontMetrics(messageFont()).lineSpacing();
00151     }
00152  }
00153 // int h = lb ? lb->fontMetrics().lineSpacing() + 2 : 0; // QT orig
00154  return QMAX(h, QApplication::globalStrut().height());
00155 }
00156 
00157 
00158 
00159 class KChatBasePrivate
00160 {
00161 public:
00162     KChatBasePrivate() 
00163     { 
00164         mBox = 0;
00165         mEdit = 0;
00166         mCombo = 0;
00167 
00168         mAcceptMessage = true;
00169         mMaxItems = -1;
00170     }
00171     QListBox* mBox;
00172     KLineEdit* mEdit;
00173     QComboBox* mCombo;
00174     bool mAcceptMessage;
00175     int mMaxItems;
00176 
00177     QValueList<int> mIndex2Id;
00178 
00179     QFont mNameFont;
00180     QFont mMessageFont;
00181     QFont mSystemNameFont;
00182     QFont mSystemMessageFont;
00183 };
00184 
00185 KChatBase::KChatBase(QWidget* parent, bool noComboBox) : QFrame(parent)
00186 {
00187  init(noComboBox); 
00188 }
00189 
00190 KChatBase::~KChatBase()
00191 {
00192 // kdDebug(11000) << "KChatBase: DESTRUCT (" << this << ")" << endl;
00193  saveConfig();
00194  delete d;
00195 }
00196 
00197 void KChatBase::init(bool noComboBox)
00198 {
00199 // kdDebug(11000) << "KChatBase: INIT (" << this << ")" << endl;
00200 
00201  d = new KChatBasePrivate;
00202 
00203  setMinimumWidth(100);
00204  setMinimumHeight(150);
00205  
00206  QVBoxLayout* l = new QVBoxLayout(this);
00207 
00208  d->mBox = new QListBox(this);
00209  connect(d->mBox, SIGNAL(rightButtonClicked(QListBoxItem*, const QPoint&)), 
00210         this, SIGNAL(rightButtonClicked(QListBoxItem*, const QPoint&)));
00211  l->addWidget(d->mBox);
00212  d->mBox->setVScrollBarMode(QScrollView::AlwaysOn);
00213  d->mBox->setHScrollBarMode(QScrollView::AlwaysOff);
00214  d->mBox->setFocusPolicy(QWidget::NoFocus);
00215 // d->mBox->setSelectionMode(QListBox::NoSelection);
00216  d->mBox->setSelectionMode(QListBox::Single);
00217 
00218  l->addSpacing(5);
00219 
00220  QHBoxLayout* h = new QHBoxLayout(l);
00221  d->mEdit = new KLineEdit(this);
00222  d->mEdit->setHandleSignals(false);
00223  d->mEdit->setTrapReturnKey(true);
00224  d->mEdit->completionObject(); // add the completion object
00225  d->mEdit->setCompletionMode(KGlobalSettings::CompletionNone);
00226  connect(d->mEdit, SIGNAL(returnPressed(const QString&)), this, SLOT(slotReturnPressed(const QString&)));
00227  h->addWidget(d->mEdit);
00228 
00229  if (!noComboBox) {
00230     d->mCombo = new QComboBox(this);
00231     h->addWidget(d->mCombo);
00232     addSendingEntry(i18n("Send to All Players"), SendToAll);//FIXME: where to put the id?
00233  }
00234 
00235  d->mAcceptMessage = true; // by default
00236  setMaxItems(-1); // unlimited
00237 
00238  if (kapp) {
00239     // kapp might be NULL as well - in case we are in Qt designer.
00240     readConfig();
00241  }
00242 }
00243 
00244 bool KChatBase::acceptMessage() const
00245 { return d->mAcceptMessage; }
00246 
00247 void KChatBase::setAcceptMessage(bool a)
00248 { d->mAcceptMessage = a; }
00249 
00250 bool KChatBase::addSendingEntry(const QString& text, int id)
00251 {
00252 //FIXME: is ID used correctly? 
00253 // do we need ID at all? 
00254 // what the hell should be here?
00255 // d->mCombo->insertItem(i18n("Send to All Players"), SendToAll);
00256  return insertSendingEntry(text, id);
00257 }
00258 
00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool 00259 bool