KDEGames

kchatbase.cpp
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
4 SPDX-FileCopyrightText: 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
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#include <kdegamesprivate_kgame_logging.h>
16#include <kdegamesprivate_logging.h>
17// KF
18#include <KConfig>
19#include <KLineEdit>
20#include <KLocalizedString>
21// Qt
22#include <QComboBox>
23#include <QHBoxLayout>
24#include <QList>
25#include <QListView>
26#include <QVBoxLayout>
27
28KChatBasePrivate::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
41void KChatBase::setModel(KChatBaseModel *m)
42{
44
45 // delete d->mModel;
46 d->mModel = m;
47}
48
49KChatBaseModel *KChatBase::model()
50{
52
53 return d->mModel;
54}
55
56KChatBase::KChatBase(QWidget *parent, KChatBaseModel *model, KChatBaseItemDelegate *delegate, bool noComboBox)
57 : KChatBase(*new KChatBasePrivate(model, delegate, parent), parent, noComboBox)
58{
59}
60
61KChatBase::KChatBase(KChatBasePrivate &dd, QWidget *parent, bool noComboBox)
62 : QFrame(parent)
63 , d(&dd)
64{
65 setMinimumWidth(100);
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
108{
109 Q_D(const KChatBase);
110
111 return d->mBox->indexAt(pos);
112}
113
114void KChatBase::customMenuHandler(QPoint pos)
115{
116 qCDebug(KDEGAMESPRIVATE_LOG) << "custom menu has been requested at position=" << pos << ". Implement handler at subclass if you need it.";
117}
118
120{
121 // qCDebug(KDEGAMESPRIVATE_LOG) << "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
139bool 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
148bool KChatBase::insertSendingEntry(const QString &text, int id, int index)
149{
150 Q_D(KChatBase);
151
152 if (!d->mCombo) {
153 qCWarning(KDEGAMESPRIVATE_LOG) << "KChatBase: Cannot add an entry to the combo box";
154 return false;
155 }
156 if (d->mIndex2Id.indexOf(id) != -1) {
157 qCCritical(KDEGAMESPRIVATE_LOG) << "KChatBase: Cannot add more than one entry with the same ID! ";
158 qCCritical(KDEGAMESPRIVATE_LOG) << "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(KDEGAMESPRIVATE_LOG) << "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(KDEGAMESPRIVATE_KGAME_LOG) << "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(KDEGAMESPRIVATE_LOG) << "could not find the selected sending entry!";
186 return -1;
187}
188
190{
191 Q_D(KChatBase);
192
193 if (!d->mCombo) {
194 qCWarning(KDEGAMESPRIVATE_LOG) << "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
204void KChatBase::changeSendingEntry(const QString &text, int id)
205{
206 Q_D(KChatBase);
207
208 if (!d->mCombo) {
209 qCWarning(KDEGAMESPRIVATE_LOG) << "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(KDEGAMESPRIVATE_LOG) << "KChatBase: Cannot set an entry in the combo box";
222 return;
223 }
224 d->mCombo->setCurrentIndex(findIndex(id));
225}
226
227int KChatBase::findIndex(int id) const
228{
229 Q_D(const KChatBase);
230
231 return d->mIndex2Id.indexOf(id);
232}
233
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
245void 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
304void 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
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
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
396void KChatBase::addMessage(const QString &fromName, const QString &text)
397{
398 Q_D(KChatBase);
399
400 d->mModel->addMessage(fromName, text);
401}
402
403void 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"
A delegate (see the Qt Model/View module for details) to paint the lines of the KChatBase list model ...
The model used to store messages displayed in the chat dialog messages list.
The base class for chat widgets.
Definition kchatbase.h:64
void setBothFont(const QFont &font)
This sets both - nameFont and messageFont to font.
void slotClear()
This clears all messages in the view.
KChatBase(QWidget *parent, KChatBaseModel *model=nullptr, KChatBaseItemDelegate *delegate=nullptr, bool noComboBox=false)
Definition kchatbase.cpp:56
int maxItems() const
virtual QString fromName() const =0
void setSendingEntry(int id)
This selects a combo box entry.
bool insertSendingEntry(const QString &text, int id, int index=-1)
Inserts a new entry in the combo box.
void setNameFont(const QFont &font)
Set the font that is used for the name part of a message.
virtual void addSystemMessage(const QString &fromName, const QString &text)
This works just like addMessage but adds a system message.
QFont systemMessageFont() const
Same as systemMessageFont but applies only to system messages.
void changeSendingEntry(const QString &text, int id)
This changes a combo box entry.
void setAcceptMessage(bool a)
void setSystemNameFont(const QFont &font)
Same as setNameFont but applies only to system messages.
void setSystemBothFont(const QFont &font)
Same as setBothFont but applies only to system messages.
int nextId() const
void setSystemMessageFont(const QFont &font)
Same as setMessageFont but applies only to system messages.
void setMessageFont(const QFont &font)
Set the font that is used for the message part of a message.
int sendingEntry() const
virtual void addMessage(const QString &fromName, const QString &text)
Add a text in the listbox.
virtual QString comboBoxItem(const QString &name) const
Replace to customize the combo box.
void setCompletionMode(KCompletion::CompletionMode mode)
See KLineEdit::setCompletionMode.
virtual void returnPressed(const QString &text)=0
This is called whenever the user pushed return ie wants to send a message.
virtual void saveConfig(KConfig *conf=nullptr)
Save the configuration of the dialog to a KConfig object.
virtual bool acceptMessage() const
virtual void readConfig(KConfig *conf=nullptr)
Read the configuration from a KConfig object.
QFont systemNameFont() const
Same as systemNameFont but applies only to system messages.
QFont messageFont() const
This font should be used for a message.
void clear()
Clear all messages in the list.
int findIndex(int id) const
QFont nameFont() const
This font should be used for the name (the "from: " part) of a message.
~KChatBase() override
Destruct the KChatBase object.
bool addSendingEntry(const QString &text, int id)
Adds a new entry in the combo box.
void removeSendingEntry(int id)
Removes the entry with the ID id from the combo box.
QModelIndex indexAt(QPoint pos) const
Returns the model index of the message at the viewport coordinates point.
void setMaxItems(int maxItems)
Set the maximum number of items in the list.
void returnKeyPressed(const QString &text)
QString i18n(const char *text, const TYPE &arg...)
void rowsInserted(const QModelIndex &parent, int first, int last)
void addLayout(QLayout *layout, int stretch)
void addSpacing(int size)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
qsizetype length() const const
CustomContextMenu
void customContextMenuRequested(const QPoint &pos)
void setMinimumHeight(int minh)
void setMinimumWidth(int minw)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.