libkdegames
kchatbase.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 d->mModel=m;
00068 }
00069
00070 KChatBaseModel* KChatBase::model()
00071 {
00072 return d->mModel;
00073 }
00074
00075 KChatBase::KChatBase(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox) : QFrame(parent)
00076 {
00077 KChatBaseModel* mod = model;
00078 if (mod==0)
00079 mod = new KChatBaseModel(parent);
00080 KChatBaseItemDelegate* del = delegate;
00081 if (del == 0)
00082 del = new KChatBaseItemDelegate(parent);
00083
00084 d = new KChatBasePrivate(mod, del);
00085
00086 setMinimumWidth(100);
00087 setMinimumHeight(150);
00088
00089 QVBoxLayout* l = new QVBoxLayout(this);
00090
00091 d->mBox = new QListView();
00092 d->mBox->setModel(d->mModel);
00093 d->mBox->setItemDelegate(d->mDelegate);
00094 l->addWidget(d->mBox);
00095
00096 connect(d->mModel, SIGNAL(rowsInserted (const QModelIndex&,int,int)),
00097 d->mBox, SLOT(scrollToBottom()));
00098
00099
00100 d->mBox->setFocusPolicy(Qt::NoFocus);
00101 d->mBox->setSelectionMode(QAbstractItemView::SingleSelection);
00102
00103 l->addSpacing(5);
00104
00105 QHBoxLayout* h = new QHBoxLayout;
00106 l->addLayout(h);
00107 d->mEdit = new KLineEdit(this);
00108 d->mEdit->setHandleSignals(false);
00109 d->mEdit->setTrapReturnKey(true);
00110 d->mEdit->completionObject();
00111 d->mEdit->setCompletionMode(KGlobalSettings::CompletionNone);
00112 connect(d->mEdit, SIGNAL(returnPressed(const QString&)), this, SLOT(slotReturnPressed(const QString&)));
00113 h->addWidget(d->mEdit);
00114
00115 if (!noComboBox) {
00116 d->mCombo = new QComboBox(this);
00117 h->addWidget(d->mCombo);
00118 addSendingEntry(i18n("Send to All Players"), SendToAll);
00119 }
00120
00121 d->mAcceptMessage = true;
00122 setMaxItems(-1);
00123
00124 readConfig();
00125 }
00126
00127 KChatBase::~KChatBase()
00128 {
00129
00130 saveConfig();
00131 delete d;
00132 }
00133
00134 bool KChatBase::acceptMessage() const
00135 { return d->mAcceptMessage; }
00136
00137 void KChatBase::setAcceptMessage(bool a)
00138 { d->mAcceptMessage = a; }
00139
00140 bool KChatBase::addSendingEntry(const QString& text, int id)
00141 {
00142
00143
00144
00145
00146 return insertSendingEntry(text, id);
00147 }
00148
00149 bool KChatBase::insertSendingEntry(const QString& text, int id, int index)
00150 {
00151 if (!d->mCombo) {
00152 kWarning(11000) << "KChatBase: Cannot add an entry to the combo box";
00153 return false;
00154 }
00155 if (d->mIndex2Id.indexOf(id) != -1) {
00156 kError(11000) << "KChatBase: Cannot add more than one entry with the same ID! ";
00157 kError(11000) << "KChatBase: Text="<<text;
00158 return false;
00159 }
00160 d->mCombo->insertItem(index, text);
00161 if (index < 0) {
00162 d->mIndex2Id.append(id);
00163 } else {
00164 d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
00165 }
00166 if (d->mIndex2Id.count() != d->mCombo->count()) {
00167 kError(11000) << "KChatBase: internal ERROR - local IDs do not match combo box entries!";
00168 }
00169 return true;
00170 }
00171
00172 int KChatBase::sendingEntry() const
00173 {
00174 if (!d->mCombo) {
00175 kWarning(11001) << "Cannot retrieve index from NULL combo box";
00176 return -1;
00177 }
00178 int index = d->mCombo->currentIndex();
00179 if ( index > 0 && index < d->mIndex2Id.size()) {
00180 kWarning(11000) << "could not find the selected sending entry!";
00181 return -1;
00182 }
00183 return d->mIndex2Id[index];
00184 }
00185
00186 void KChatBase::removeSendingEntry(int id)
00187 {
00188 if (!d->mCombo) {
00189 kWarning(11000) << "KChatBase: Cannot remove an entry from the combo box";
00190 return;
00191 }
00192 d->mCombo->removeItem(findIndex(id));
00193 d->mIndex2Id.removeAll(id);
00194 }
00195
00196 void KChatBase::changeSendingEntry(const QString& text, int id)
00197 {
00198 if (!d->mCombo) {
00199 kWarning(11000) << "KChatBase: Cannot change an entry in the combo box";
00200 return;
00201 }
00202 int index = findIndex(id);
00203 d->mCombo->setItemText(index, text);
00204 }
00205
00206 void KChatBase::setSendingEntry(int id)
00207 {
00208 if (!d->mCombo) {
00209 kWarning(11000) << "KChatBase: Cannot set an entry in the combo box";
00210 return;
00211 }
00212 d->mCombo->setCurrentIndex(findIndex(id));
00213 }
00214
00215 int KChatBase::findIndex(int id) const
00216 {
00217 return d->mIndex2Id.indexOf(id);
00218 }
00219
00220 int KChatBase::nextId() const
00221 {
00222 int i = SendToAll + 1;
00223 while (d->mIndex2Id.indexOf(i) != -1) {
00224 i++;
00225 }
00226 return i;
00227 }
00228
00229 void KChatBase::slotReturnPressed(const QString& text)
00230 {
00231 if (text.length() <= 0) {
00232
00233 return;
00234 } else if (!acceptMessage()) {
00235 return;
00236 }
00237 d->mEdit->completionObject()->addItem(text);
00238 d->mEdit->clear();
00239 returnPressed(text);
00240 }
00241
00242 QString KChatBase::comboBoxItem(const QString& name) const
00243 {
00244 return i18n("Send to %1", name);
00245 }
00246
00247 void KChatBase::slotClear()
00248 {
00249 clear();
00250 }
00251
00252 void KChatBase::setCompletionMode(KGlobalSettings::Completion mode)
00253 { d->mEdit->setCompletionMode(mode); }
00254
00255 void KChatBase::saveConfig(KConfig* conf)
00256 {
00257 if (conf == 0) {
00258 return;
00259 }
00260 d->mModel->saveConfig(conf);
00261 }
00262
00263 void KChatBase::readConfig(KConfig* conf)
00264 {
00265 if (conf == 0) {
00266 return;
00267 }
00268 d->mModel->readConfig(conf);
00269 }
00270
00271 void KChatBase::clear()
00272 {
00273 d->mModel->removeRows(0, d->mModel->rowCount());
00274 }
00275
00276 void KChatBase::setMaxItems(int maxItems)
00277 {
00278 d->mModel->setMaxItems(maxItems);
00279
00280 if (maxItems == 0) {
00281 clear();
00282 } else if (maxItems > 0) {
00283 while (d->mModel->rowCount() > (int)maxItems) {
00284 d->mModel->removeRow(0);
00285 }
00286 }
00287 }
00288
00289 int KChatBase::maxItems() const
00290 {
00291 return d->mModel->maxItems();
00292 }
00293
00294 QFont KChatBase::nameFont() const
00295 {
00296 return d->mModel->nameFont();
00297 }
00298
00299 QFont KChatBase::messageFont() const
00300 {
00301 return d->mModel->messageFont();
00302 }
00303
00304 QFont KChatBase::systemNameFont() const
00305 {
00306 return d->mModel->systemNameFont();
00307 }
00308
00309 QFont KChatBase::systemMessageFont() const
00310 {
00311 return d->mModel->systemMessageFont();
00312 }
00313
00314 void KChatBase::setNameFont(const QFont& font)
00315 {
00316 d->mModel->setNameFont(font);
00317 }
00318
00319 void KChatBase::setMessageFont(const QFont& font)
00320 {
00321 d->mModel->setMessageFont(font);
00322 }
00323
00324 void KChatBase::setBothFont(const QFont& font)
00325 {
00326 d->mModel->setBothFont(font);
00327 }
00328
00329 void KChatBase::setSystemNameFont(const QFont& font)
00330 {
00331 d->mModel->setSystemNameFont(font);
00332 }
00333
00334 void KChatBase::setSystemMessageFont(const QFont& font)
00335 {
00336 d->mModel->setSystemMessageFont(font);
00337 }
00338
00339 void KChatBase::setSystemBothFont(const QFont& font)
00340 {
00341 d->mModel->setSystemBothFont(font);
00342 }
00343
00344 void KChatBase::addMessage(const QString& fromName, const QString& text)
00345 {
00346 d->mModel->addMessage(fromName, text);
00347 }
00348
00349 void KChatBase::addSystemMessage(const QString& fromName, const QString& text)
00350 {
00351 d->mModel->addSystemMessage(fromName, text);
00352 }
00353
00354 #include "kchatbase.moc"