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

KDE's Doxygen guidelines are available online.