• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/libkdegamesprivate

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • libkdegamesprivate
kchatbase.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
4  Copyright (C) 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
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 "kchatbase.h"
22 #include "kchatbasemodel.h"
23 #include "kchatbaseitemdelegate.h"
24 
25 #include <klineedit.h>
26 #include <klocale.h>
27 #include <kstandarddirs.h>
28 #include <kconfig.h>
29 #include <kdebug.h>
30 #include <kglobal.h>
31 #include <QLayout>
32 #include <QComboBox>
33 #include <QPixmap>
34 #include <QList>
35 #include <QApplication>
36 #include <QListView>
37 
38 
39 
40 class KChatBasePrivate
41 {
42 public:
43  KChatBasePrivate(KChatBaseModel* model, KChatBaseItemDelegate* delegate)
44  {
45  mBox = 0;
46  mEdit = 0;
47  mCombo = 0;
48 
49  mAcceptMessage = true;
50 
51  mModel = model;
52  mDelegate = delegate;
53  }
54  QListView* mBox;
55  KLineEdit* mEdit;
56  QComboBox* mCombo;
57  bool mAcceptMessage;
58 
59  QList<int> mIndex2Id;
60 
61  KChatBaseModel* mModel;
62  KChatBaseItemDelegate* mDelegate;
63 };
64 
65 void KChatBase::setModel(KChatBaseModel* m)
66 {
67  //delete d->mModel;
68  d->mModel=m;
69 }
70 
71 KChatBaseModel* KChatBase::model()
72 {
73  return d->mModel;
74 }
75 
76 KChatBase::KChatBase(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox) : QFrame(parent)
77 {
78  KChatBaseModel* mod = model;
79  if (mod==0)
80  mod = new KChatBaseModel(parent);
81  KChatBaseItemDelegate* del = delegate;
82  if (del == 0)
83  del = new KChatBaseItemDelegate(parent);
84 
85  d = new KChatBasePrivate(mod, del);
86 
87  setMinimumWidth(100);
88  setMinimumHeight(150);
89 
90  QVBoxLayout* l = new QVBoxLayout(this);
91 
92  d->mBox = new QListView();
93  d->mBox->setModel(d->mModel);
94  d->mBox->setItemDelegate(d->mDelegate);
95  l->addWidget(d->mBox);
96 
97  connect(d->mModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
98  d->mBox, SLOT(scrollToBottom()));
99 
100  connect(d->mBox, SIGNAL(customContextMenuRequested(QPoint)),
101  this, SLOT(customMenuHandler(QPoint)));
102 
103  d->mBox->setContextMenuPolicy ( Qt::CustomContextMenu );
104  d->mBox->setFocusPolicy(Qt::NoFocus);
105  d->mBox->setSelectionMode(QAbstractItemView::SingleSelection);
106 
107  l->addSpacing(5);
108 
109  QHBoxLayout* h = new QHBoxLayout;
110  l->addLayout(h);
111  d->mEdit = new KLineEdit(this);
112  d->mEdit->setHandleSignals(false);
113  d->mEdit->setTrapReturnKey(true);
114  d->mEdit->completionObject(); // add the completion object
115  d->mEdit->setCompletionMode(KGlobalSettings::CompletionNone);
116  connect(d->mEdit, SIGNAL(returnPressed(QString)), this, SLOT(slotReturnPressed(QString)));
117  h->addWidget(d->mEdit);
118 
119  if (!noComboBox) {
120  d->mCombo = new QComboBox(this);
121  h->addWidget(d->mCombo);
122  addSendingEntry(i18n("Send to All Players"), SendToAll);//FIXME: where to put the id?
123  }
124 
125  d->mAcceptMessage = true; // by default
126  setMaxItems(-1); // unlimited
127 
128  readConfig();
129 }
130 
131 const QModelIndex KChatBase::indexAt(const QPoint& pos) const
132 {
133  return d->mBox->indexAt(pos);
134 }
135 
136 void KChatBase::customMenuHandler(const QPoint &pos)
137 {
138  kDebug(10500) << "custom menu has been requested at position="<<pos<<". Implement handler at subclass if you need it.";
139 }
140 
141 KChatBase::~KChatBase()
142 {
143 // kDebug(11000) << "KChatBase: DESTRUCT (" << this << ")";
144  saveConfig();
145  delete d;
146 }
147 
148 bool KChatBase::acceptMessage() const
149 { return d->mAcceptMessage; }
150 
151 void KChatBase::setAcceptMessage(bool a)
152 { d->mAcceptMessage = a; }
153 
154 bool KChatBase::addSendingEntry(const QString& text, int id)
155 {
156 //FIXME: is ID used correctly?
157 // do we need ID at all?
158 // what the hell should be here?
159 // d->mCombo->insertItem(i18n("Send to All Players"), SendToAll);
160  return insertSendingEntry(text, id);
161 }
162 
163 bool KChatBase::insertSendingEntry(const QString& text, int id, int index)
164 {
165  if (!d->mCombo) {
166  kWarning(11000) << "KChatBase: Cannot add an entry to the combo box";
167  return false;
168  }
169  if (d->mIndex2Id.indexOf(id) != -1) {
170  kError(11000) << "KChatBase: Cannot add more than one entry with the same ID! ";
171  kError(11000) << "KChatBase: Text="<<text;
172  return false;
173  }
174  d->mCombo->insertItem(index, text);
175  if (index < 0) {
176  d->mIndex2Id.prepend(id);
177  } else {
178  d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
179  }
180  if (d->mIndex2Id.count() != d->mCombo->count()) {
181  kError(11000) << "KChatBase: internal ERROR - local IDs do not match combo box entries!";
182  }
183  return true;
184 }
185 
186 int KChatBase::sendingEntry() const
187 {
188  if (!d->mCombo) {
189  kWarning(11001) << "Cannot retrieve index from NULL combo box";
190  return -1;
191  }
192  const int index = d->mCombo->currentIndex();
193  if ( index >= 0 && index < d->mIndex2Id.size())
194  return d->mIndex2Id[index];
195 
196  kWarning(11000) << "could not find the selected sending entry!";
197  return -1;
198 }
199 
200 void KChatBase::removeSendingEntry(int id)
201 {
202  if (!d->mCombo) {
203  kWarning(11000) << "KChatBase: Cannot remove an entry from the combo box";
204  return;
205  }
206  int idx = findIndex(id);
207  //Guard, passing -1 will crash qcombobox
208  if (idx>=0) d->mCombo->removeItem(idx);
209  d->mIndex2Id.removeAll(id);
210 }
211 
212 void KChatBase::changeSendingEntry(const QString& text, int id)
213 {
214  if (!d->mCombo) {
215  kWarning(11000) << "KChatBase: Cannot change an entry in the combo box";
216  return;
217  }
218  int index = findIndex(id);
219  d->mCombo->setItemText(index, text);
220 }
221 
222 void KChatBase::setSendingEntry(int id)
223 {
224  if (!d->mCombo) {
225  kWarning(11000) << "KChatBase: Cannot set an entry in the combo box";
226  return;
227  }
228  d->mCombo->setCurrentIndex(findIndex(id));
229 }
230 
231 int KChatBase::findIndex(int id) const
232 {
233  return d->mIndex2Id.indexOf(id);
234 }
235 
236 int KChatBase::nextId() const
237 {
238  int i = SendToAll + 1;
239  while (d->mIndex2Id.indexOf(i) != -1) {
240  i++;
241  }
242  return i;
243 }
244 
245 void KChatBase::slotReturnPressed(const QString& text)
246 {
247  if (text.length() <= 0) {
248  // no text entered - probably hit return by accident
249  return;
250  } else if (!acceptMessage()) {
251  return;
252  }
253  d->mEdit->completionObject()->addItem(text);
254  d->mEdit->clear();
255  returnPressed(text);
256 }
257 
258 QString KChatBase::comboBoxItem(const QString& name) const
259 { // TODO: such a function for "send to all" and "send to my group"
260  return i18n("Send to %1", name);
261 }
262 
263 void KChatBase::slotClear()
264 {
265  clear();
266 }
267 
268 
269 void KChatBase::setCompletionMode(KGlobalSettings::Completion mode)
270 { d->mEdit->setCompletionMode(mode); }
271 
272 void KChatBase::saveConfig(KConfig* conf)
273 {
274  if (conf == 0) {
275  return;
276  }
277  d->mModel->saveConfig(conf);
278 }
279 
280 void KChatBase::readConfig(KConfig* conf)
281 {
282  if (conf == 0) {
283  return;
284  }
285  d->mModel->readConfig(conf);
286 }
287 
288 void KChatBase::clear()
289 {
290  d->mModel->removeRows(0, d->mModel->rowCount());
291 }
292 
293 void KChatBase::setMaxItems(int maxItems)
294 {
295  d->mModel->setMaxItems(maxItems);
296  //TODO cut too many messages
297  if (maxItems == 0) {
298  clear();
299  } else if (maxItems > 0) {
300  while (d->mModel->rowCount() > (int)maxItems) {
301  d->mModel->removeRow(0);
302  }
303  }
304 }
305 
306 int KChatBase::maxItems() const
307 {
308  return d->mModel->maxItems();
309 }
310 
311 QFont KChatBase::nameFont() const
312 {
313  return d->mModel->nameFont();
314 }
315 
316 QFont KChatBase::messageFont() const
317 {
318  return d->mModel->messageFont();
319 }
320 
321 QFont KChatBase::systemNameFont() const
322 {
323  return d->mModel->systemNameFont();
324 }
325 
326 QFont KChatBase::systemMessageFont() const
327 {
328  return d->mModel->systemMessageFont();
329 }
330 
331 void KChatBase::setNameFont(const QFont& font)
332 {
333  d->mModel->setNameFont(font);
334 }
335 
336 void KChatBase::setMessageFont(const QFont& font)
337 {
338  d->mModel->setMessageFont(font);
339 }
340 
341 void KChatBase::setBothFont(const QFont& font)
342 {
343  d->mModel->setBothFont(font);
344 }
345 
346 void KChatBase::setSystemNameFont(const QFont& font)
347 {
348  d->mModel->setSystemNameFont(font);
349 }
350 
351 void KChatBase::setSystemMessageFont(const QFont& font)
352 {
353  d->mModel->setSystemMessageFont(font);
354 }
355 
356 void KChatBase::setSystemBothFont(const QFont& font)
357 {
358  d->mModel->setSystemBothFont(font);
359 }
360 
361 void KChatBase::addMessage(const QString& fromName, const QString& text)
362 {
363  d->mModel->addMessage(fromName, text);
364 }
365 
366 void KChatBase::addSystemMessage(const QString& fromName, const QString& text)
367 {
368  d->mModel->addSystemMessage(fromName, text);
369 }
370 
371 #include "kchatbase.moc"
QWidget::customContextMenuRequested
void customContextMenuRequested(const QPoint &pos)
KChatBase::SendToAll
Definition: kchatbase.h:95
KChatBase::systemNameFont
QFont systemNameFont() const
Same as systemNameFont but applies only to system messages.
Definition: kchatbase.cpp:321
QModelIndex
KChatBase::indexAt
const QModelIndex indexAt(const QPoint &pos) const
Returns the model index of the message at the viewport coordinates point.
Definition: kchatbase.cpp:131
QWidget
KChatBase::setMessageFont
void setMessageFont(const QFont &font)
Set the font that used used for the message part of a message.
Definition: kchatbase.cpp:336
KChatBase::changeSendingEntry
void changeSendingEntry(const QString &text, int id)
This changes a combo box entry.
Definition: kchatbase.cpp:212
KChatBase::setSystemNameFont
void setSystemNameFont(const QFont &font)
Same as setNameFont but applies only to system messages.
Definition: kchatbase.cpp:346
KChatBase::addSendingEntry
bool addSendingEntry(const QString &text, int id)
Adds a new entry in the combo box.
Definition: kchatbase.cpp:154
KChatBase::removeSendingEntry
void removeSendingEntry(int id)
Removes the entry with the ID id from the combo box.
Definition: kchatbase.cpp:200
KChatBase::returnPressed
virtual void returnPressed(const QString &text)=0
This is called whenever the user pushed return ie wants to send a message.
QFont
QWidget::setMinimumWidth
void setMinimumWidth(int minw)
KChatBase::setCompletionMode
void setCompletionMode(KGlobalSettings::Completion mode)
See KLineEdit::setCompletionMode.
Definition: kchatbase.cpp:269
QHBoxLayout
KChatBase::findIndex
int findIndex(int id) const
Definition: kchatbase.cpp:231
KChatBaseItemDelegate
A delegate (see the Qt Model/View module for details) to paint the lines of the KChatBase list model ...
Definition: kchatbaseitemdelegate.h:37
QPoint
KChatBase::nameFont
QFont nameFont() const
This font should be used for the name (the "from: " part) of a message.
Definition: kchatbase.cpp:311
KChatBase::setSystemMessageFont
void setSystemMessageFont(const QFont &font)
Same as setMessageFont but applies only to system messages.
Definition: kchatbase.cpp:351
QBoxLayout::addSpacing
void addSpacing(int size)
QListView
KChatBase::KChatBase
KChatBase(QWidget *parent, KChatBaseModel *model=0, KChatBaseItemDelegate *delegate=0, bool noComboBox=false)
Definition: kchatbase.cpp:76
KChatBase::acceptMessage
virtual bool acceptMessage() const
Definition: kchatbase.cpp:148
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
KChatBase::messageFont
QFont messageFont() const
This font should be used for a message.
Definition: kchatbase.cpp:316
KChatBase::setModel
void setModel(KChatBaseModel *m)
Definition: kchatbase.cpp:65
KChatBase::addMessage
virtual void addMessage(const QString &fromName, const QString &text)
Add a text in the listbox.
Definition: kchatbase.cpp:361
KChatBase::addSystemMessage
virtual void addSystemMessage(const QString &fromName, const QString &text)
This works just like addMessage but adds a system message.
Definition: kchatbase.cpp:366
KChatBase::insertSendingEntry
bool insertSendingEntry(const QString &text, int id, int index=-1)
Inserts a new entry in the combo box.
Definition: kchatbase.cpp:163
KChatBase::readConfig
virtual void readConfig(KConfig *conf=0)
Read the configuration from a KConfig object.
Definition: kchatbase.cpp:280
QVBoxLayout
KChatBase::setNameFont
void setNameFont(const QFont &font)
Set the font that used used for the name part of a message.
Definition: kchatbase.cpp:331
KChatBase::slotClear
void slotClear()
This clears all messages in the view.
Definition: kchatbase.cpp:263
QString
QList< int >
KChatBase::nextId
int nextId() const
Definition: kchatbase.cpp:236
KChatBase::maxItems
int maxItems() const
Definition: kchatbase.cpp:306
kchatbasemodel.h
KChatBase::clear
void clear()
Clear all messages in the list.
Definition: kchatbase.cpp:288
QFrame
KChatBase::setAcceptMessage
void setAcceptMessage(bool a)
Definition: kchatbase.cpp:151
kchatbase.h
KChatBase::setMaxItems
void setMaxItems(int maxItems)
Set the maximum number of items in the list.
Definition: kchatbase.cpp:293
KChatBase::setSendingEntry
void setSendingEntry(int id)
This selects a combo box entry.
Definition: kchatbase.cpp:222
KChatBase::sendingEntry
int sendingEntry() const
Definition: kchatbase.cpp:186
KChatBase::~KChatBase
virtual ~KChatBase()
Destruct the KChatBase object.
Definition: kchatbase.cpp:141
QString::length
int length() const
QWidget::setMinimumHeight
void setMinimumHeight(int minh)
KChatBase::setSystemBothFont
void setSystemBothFont(const QFont &font)
Same as setBothFont but applies only to system messages.
Definition: kchatbase.cpp:356
kchatbaseitemdelegate.h
KChatBase::setBothFont
void setBothFont(const QFont &font)
This sets both - nameFont and messageFont to font.
Definition: kchatbase.cpp:341
KChatBase::systemMessageFont
QFont systemMessageFont() const
Same as systemMessageFont but applies only to system messages.
Definition: kchatbase.cpp:326
KChatBase::comboBoxItem
virtual QString comboBoxItem(const QString &name) const
Replace to customize the combo box.
Definition: kchatbase.cpp:258
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KChatBaseModel
The model used to store messages displayed in the chat dialog messages list.
Definition: kchatbasemodel.h:76
KChatBase::saveConfig
virtual void saveConfig(KConfig *conf=0)
Save the configuration of the dialog to a KConfig object.
Definition: kchatbase.cpp:272
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
KChatBase::model
KChatBaseModel * model()
Definition: kchatbase.cpp:71
QComboBox
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/libkdegamesprivate

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal