KDEGames

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

KDE's Doxygen guidelines are available online.