summaryrefslogtreecommitdiffstats
path: root/libtdegames/kchatbase.cpp
blob: 5422c552871a055f491a0a25aeb017a5416e714c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
    This file is part of the TDE games library
    Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kchatbase.h"

#include <klineedit.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kdebug.h>

#include <tqlayout.h>
#include <tqcombobox.h>
#include <tqpainter.h>

class KChatBaseTextPrivate
{
public:
	KChatBaseTextPrivate()
	{
		mNameFont = 0;
		mMessageFont = 0;
	}

	TQString mName;
	TQString mMessage;

	const TQFont* mNameFont;
	const TQFont* mMessageFont;
};


KChatBaseText::KChatBaseText(const TQString& name, const TQString& message) : TQListBoxText()
{
 init();
 setName(name);
 setMessage(message);
}

KChatBaseText::KChatBaseText(const TQString& message) : TQListBoxText()
{
 init();
 setMessage(message);
}

KChatBaseText::~KChatBaseText()
{ 
 delete d;
}

void KChatBaseText::init()
{
 d = new KChatBaseTextPrivate;
}

void KChatBaseText::setName(const TQString& n)
{
// d->mName = n;
 d->mName = TQString("%1: ").arg(n);
 setText(TQString("%1: %2").arg(name()).arg(message())); // esp. for sorting
}

void KChatBaseText::setMessage(const TQString& m)
{
 d->mMessage = m;
 setText(TQString("%1: %2").arg(name()).arg(message())); // esp. for sorting
}

const TQString& KChatBaseText::name() const
{ return d->mName; }

const TQString& KChatBaseText::message() const
{ return d->mMessage; }

TQFont KChatBaseText::nameFont() const
{
 if (d->mNameFont) {
	return *d->mNameFont; 
 } else if (listBox()) {
	return listBox()->font();
 } else {
	return TQFont();
 }
}

TQFont KChatBaseText::messageFont() const
{
 if (d->mMessageFont) {
	return *d->mMessageFont; 
 } else if (listBox()) {
	return listBox()->font();
 } else {
	return TQFont();
 }
}

void KChatBaseText::setNameFont(const TQFont* f)
{ d->mNameFont = f; }

void KChatBaseText::setMessageFont(const TQFont* f)
{ d->mMessageFont = f; }

void KChatBaseText::paint(TQPainter* painter)
{
 TQFontMetrics fm = painter->fontMetrics();
 painter->setFont(nameFont());
 painter->drawText(3, fm.ascent() + fm.leading()/2, name());
 painter->setFont(messageFont());
 painter->drawText(3 + TQFontMetrics(nameFont()).width(name()), fm.ascent() + fm.leading()/2, message());
}

int KChatBaseText::width(TQListBox* lb) const
{
 int w = 0;
 if (lb) {
	w += 6;
	w += TQFontMetrics(nameFont()).width(name());
	w += TQFontMetrics(messageFont()).width(message());
 }
// int w = lb ? lb->fontMetrics().width( text() ) + 6 : 0; // QT orig
 return TQMAX(w, TQApplication::globalStrut().width());
}

int KChatBaseText::height(TQListBox* lb) const
{
 int h = 0;
 if (lb) {
	h += 2;
	// AB: is lineSpacing still correct?
	if (TQFontMetrics(nameFont()).lineSpacing() > TQFontMetrics(messageFont()).lineSpacing()) { 
		h += TQFontMetrics(nameFont()).lineSpacing();
	} else {
		h += TQFontMetrics(messageFont()).lineSpacing();
	}
 }
// int h = lb ? lb->fontMetrics().lineSpacing() + 2 : 0; // QT orig
 return TQMAX(h, TQApplication::globalStrut().height());
}



class KChatBasePrivate
{
public:
	KChatBasePrivate() 
	{ 
		mBox = 0;
		mEdit = 0;
		mCombo = 0;

		mAcceptMessage = true;
		mMaxItems = -1;
	}
	TQListBox* mBox;
	KLineEdit* mEdit;
	TQComboBox* mCombo;
	bool mAcceptMessage;
	int mMaxItems;

	TQValueList<int> mIndex2Id;

	TQFont mNameFont;
	TQFont mMessageFont;
	TQFont mSystemNameFont;
	TQFont mSystemMessageFont;
};

KChatBase::KChatBase(TQWidget* parent, bool noComboBox) : TQFrame(parent)
{
 init(noComboBox); 
}

KChatBase::~KChatBase()
{
// kdDebug(11000) << "KChatBase: DESTRUCT (" << this << ")" << endl;
 saveConfig();
 delete d;
}

void KChatBase::init(bool noComboBox)
{
// kdDebug(11000) << "KChatBase: INIT (" << this << ")" << endl;

 d = new KChatBasePrivate;

 setMinimumWidth(100);
 setMinimumHeight(150);
 
 TQVBoxLayout* l = new TQVBoxLayout(this);

 d->mBox = new TQListBox(this);
 connect(d->mBox, TQT_SIGNAL(rightButtonClicked(TQListBoxItem*, const TQPoint&)), 
		this, TQT_SIGNAL(rightButtonClicked(TQListBoxItem*, const TQPoint&)));
 l->addWidget(d->mBox);
 d->mBox->setVScrollBarMode(TQScrollView::AlwaysOn);
 d->mBox->setHScrollBarMode(TQScrollView::AlwaysOff);
 d->mBox->setFocusPolicy(TQ_NoFocus);
// d->mBox->setSelectionMode(TQListBox::NoSelection);
 d->mBox->setSelectionMode(TQListBox::Single);

 l->addSpacing(5);

 TQHBoxLayout* h = new TQHBoxLayout(l);
 d->mEdit = new KLineEdit(this);
 d->mEdit->setHandleSignals(false);
 d->mEdit->setTrapReturnKey(true);
 d->mEdit->completionObject(); // add the completion object
 d->mEdit->setCompletionMode(TDEGlobalSettings::CompletionNone);
 connect(d->mEdit, TQT_SIGNAL(returnPressed(const TQString&)), this, TQT_SLOT(slotReturnPressed(const TQString&)));
 h->addWidget(d->mEdit);

 if (!noComboBox) {
	d->mCombo = new TQComboBox(this);
	h->addWidget(d->mCombo);
	addSendingEntry(i18n("Send to All Players"), SendToAll);//FIXME: where to put the id?
 }

 d->mAcceptMessage = true; // by default
 setMaxItems(-1); // unlimited

 if (kapp) {
	// kapp might be NULL as well - in case we are in TQt designer.
	readConfig();
 }
}

bool KChatBase::acceptMessage() const
{ return d->mAcceptMessage; }

void KChatBase::setAcceptMessage(bool a)
{ d->mAcceptMessage = a; }

bool KChatBase::addSendingEntry(const TQString& text, int id)
{
//FIXME: is ID used correctly? 
// do we need ID at all? 
// what the hell should be here?
// d->mCombo->insertItem(i18n("Send to All Players"), SendToAll);
 return insertSendingEntry(text, id);
}

bool KChatBase::insertSendingEntry(const TQString& text, int id, int index)
{
 if (!d->mCombo) {
	kdWarning(11000) << "KChatBase: Cannot add an entry to the combo box" << endl;
	return false;
 }
 if (d->mIndex2Id.findIndex(id) != -1) {
	kdError(11000) << "KChatBase: Cannot add more than one entry with the same ID! " << endl;
	kdError(11000) << "KChatBase: Text="<<text<<endl;
	return false;
 }
 d->mCombo->insertItem(text, index);
 if (index < 0) {
	d->mIndex2Id.append(id);
 } else {
	d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
 }
 if (d->mIndex2Id.count() != (uint)d->mCombo->count()) {
	kdError(11000) << "KChatBase: internal ERROR - local IDs do not match combo box entries!" << endl;
 }
 return true;
}

int KChatBase::sendingEntry() const
{
 if (!d->mCombo) {
	kdWarning(11001) << "Cannot retrieve index from NULL combo box" << endl;
	return -1;
 }
 int index = d->mCombo->currentItem();
 if (d->mIndex2Id.at(index) == d->mIndex2Id.end()) {
	kdWarning(11000) << "could not find the selected sending entry!" << endl;
	return -1;
 }
 return d->mIndex2Id[index];
}

void KChatBase::removeSendingEntry(int id)
{
 if (!d->mCombo) {
	kdWarning(11000) << "KChatBase: Cannot remove an entry from the combo box" << endl;
	return;
 }
 d->mCombo->removeItem(findIndex(id));
 d->mIndex2Id.remove(id);
}

void KChatBase::changeSendingEntry(const TQString& text, int id)
{
 if (!d->mCombo) {
	kdWarning(11000) << "KChatBase: Cannot change an entry in the combo box" << endl;
	return;
 }
 int index = findIndex(id);
 d->mCombo->changeItem(text, index);
}

void KChatBase::setSendingEntry(int id)
{
 if (!d->mCombo) {
	kdWarning(11000) << "KChatBase: Cannot set an entry in the combo box" << endl;
	return;
 }
 d->mCombo->setCurrentItem(findIndex(id));
}
 
int KChatBase::findIndex(int id) const
{
 return d->mIndex2Id.findIndex(id);
}

int KChatBase::nextId() const
{
 int i = SendToAll + 1;
 while (d->mIndex2Id.findIndex(i) != -1) {
	i++;
 }
 return i;
}

void KChatBase::addItem(const TQListBoxItem* text)
{
 d->mBox->insertItem(text); 
 int index = d->mBox->count() -1;
 d->mBox->setBottomItem(index);//FIXME: don't scroll to bottom if user scrolled down manually
 if (maxItems() >= 0 && d->mBox->count() > (unsigned int)maxItems()) {
	d->mBox->removeItem(0);
 }
}

void KChatBase::addMessage(const TQString& fromName, const TQString& text)
{
//maybe "%1 says: %2" or so
 addItem(layoutMessage(fromName, text));
}

void KChatBase::addSystemMessage(const TQString& fromName, const TQString& text)
{
 addItem(layoutSystemMessage(fromName, text));
}

TQListBoxItem* KChatBase::layoutMessage(const TQString& fromName, const TQString& text)
{
 //TODO: KChatBaseConfigure? - e.g. color
 TQListBoxItem* message;
 if (text.startsWith("/me ")) {
	// replace "/me" by a nice star. leave one space after the star
	TQPixmap pix;
	pix.load(locate("data", TQString::fromLatin1("tdegames/pics/star.png")));
	
	//TODO KChatBasePixmap? Should change the font here!
	
	message = (TQListBoxItem*)new TQListBoxPixmap(pix, i18n("%1 %2").arg(fromName).arg(text.mid(3)));
 } else {
	// the text is not edited in any way. just return an item
	KChatBaseText* m = new KChatBaseText(fromName, text);
	m->setNameFont(&d->mNameFont);
	m->setMessageFont(&d->mMessageFont);
	message = (TQListBoxItem*)m;
 }
 return message;
}

TQListBoxItem* KChatBase::layoutSystemMessage(const TQString& fromName, const TQString& text)
{
 //TODO: KChatBaseConfigure? - e.g. color

 // no need to check for /me etc.
 KChatBaseText* m = new KChatBaseText(i18n("--- %1").arg(fromName), text);
 m->setNameFont(&d->mSystemNameFont);
 m->setMessageFont(&d->mSystemMessageFont);
 return (TQListBoxItem*)m;
}

void KChatBase::slotReturnPressed(const TQString& text)
{
 if (text.length() <= 0) {
	// no text entered - probably hit return by accident
	return;
 } else if (!acceptMessage()) {
	return;
 }
 d->mEdit->completionObject()->addItem(text);
// connect(d->mEdit, TQT_SIGNAL(returnPressed(const TQString&)), comp, TQT_SLOT(addItem(const TQString&)));
 d->mEdit->clear();
 returnPressed(text);
}

TQString KChatBase::comboBoxItem(const TQString& name) const
{ // TODO: such a function for "send to all" and "send to my group"
 return i18n("Send to %1").arg(name);
}

void KChatBase::slotClear()
{
 d->mBox->clear();
}

void KChatBase::setCompletionMode(TDEGlobalSettings::Completion mode)
{ d->mEdit->setCompletionMode(mode); }

void KChatBase::setNameFont(const TQFont& font)
{
 d->mNameFont = font; 
 d->mBox->triggerUpdate(false);
}

void KChatBase::setMessageFont(const TQFont& font)
{
 d->mMessageFont = font; 
 d->mBox->triggerUpdate(false);
}

void KChatBase::setBothFont(const TQFont& font)
{
 setNameFont(font);
 setMessageFont(font);
}

const TQFont& KChatBase::nameFont() const
{ return d->mNameFont; }

const TQFont& KChatBase::messageFont() const
{ return d->mMessageFont; }

void KChatBase::setSystemNameFont(const TQFont& font)
{
 d->mSystemNameFont = font; 
 d->mBox->triggerUpdate(false);
}

void KChatBase::setSystemMessageFont(const TQFont& font)
{
 d->mSystemMessageFont = font; 
 d->mBox->triggerUpdate(false);
}

void KChatBase::setSystemBothFont(const TQFont& font)
{
 setSystemNameFont(font);
 setSystemMessageFont(font);
}

const TQFont& KChatBase::systemNameFont() const
{ return d->mSystemNameFont; }

const TQFont& KChatBase::systemMessageFont() const
{ return d->mSystemMessageFont; }

void KChatBase::saveConfig(TDEConfig* conf)
{
 TQString oldGroup;
 if (!conf) {
	conf = kapp->config();
	oldGroup = conf->group();
	conf->setGroup("KChatBase");
 }

 conf->writeEntry("NameFont", nameFont());
 conf->writeEntry("MessageFont", messageFont());
 conf->writeEntry("SystemNameFont", systemNameFont());
 conf->writeEntry("SystemMessageFont", systemMessageFont());
 conf->writeEntry("MaxMessages", maxItems());

 if (!oldGroup.isNull()) {
	conf->setGroup(oldGroup);
 }
}

void KChatBase::readConfig(TDEConfig* conf)
{
 TQString oldGroup;
 if (!conf) {
	conf = kapp->config();
	oldGroup = conf->group();
	conf->setGroup("KChatBase");
 }

 setNameFont(conf->readFontEntry("NameFont"));
 setMessageFont(conf->readFontEntry("MessageFont"));
 setSystemNameFont(conf->readFontEntry("SystemNameFont"));
 setSystemMessageFont(conf->readFontEntry("SystemMessageFont"));
 setMaxItems(conf->readNumEntry("MaxMessages", -1));

 if (!oldGroup.isNull()) {
	conf->setGroup(oldGroup);
 }
}

void KChatBase::clear()
{
 d->mBox->clear();
}

void KChatBase::setMaxItems(int maxItems)
{
 d->mMaxItems = maxItems;
 //TODO cut too many messages
 if (maxItems == 0) {
	clear();
 } else if (maxItems > 0) {
	while (d->mBox->count() > (unsigned int)maxItems) {
		d->mBox->removeItem(0);
	}
 }
}

int KChatBase::maxItems() const
{ return d->mMaxItems; }


#include "kchatbase.moc"