KDEGames

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

KDE's Doxygen guidelines are available online.