20 #include "kchatbase.h"
22 #include <klineedit.h>
24 #include <kstandarddirs.h>
26 #include <kapplication.h>
30 #include <tqcombobox.h>
31 #include <tqpainter.h>
33 class KChatBaseTextPrivate
36 KChatBaseTextPrivate()
45 const TQFont* mNameFont;
46 const TQFont* mMessageFont;
68 void KChatBaseText::init()
70 d =
new KChatBaseTextPrivate;
76 d->mName = TQString(
"%1: ").arg(n);
77 setText(TQString(
"%1: %2").arg(
name()).arg(
message()));
83 setText(TQString(
"%1: %2").arg(
name()).arg(
message()));
90 {
return d->mMessage; }
96 }
else if (listBox()) {
97 return listBox()->font();
105 if (d->mMessageFont) {
106 return *d->mMessageFont;
107 }
else if (listBox()) {
108 return listBox()->font();
115 { d->mNameFont = f; }
118 { d->mMessageFont = f; }
120 void KChatBaseText::paint(TQPainter* painter)
122 TQFontMetrics fm = painter->fontMetrics();
124 painter->drawText(3, fm.ascent() + fm.leading()/2,
name());
126 painter->drawText(3 + TQFontMetrics(
nameFont()).width(
name()), fm.ascent() + fm.leading()/2,
message());
129 int KChatBaseText::width(TQListBox* lb)
const
138 return TQMAX(w, TQApplication::globalStrut().width());
141 int KChatBaseText::height(TQListBox* lb)
const
147 if (TQFontMetrics(
nameFont()).lineSpacing() > TQFontMetrics(
messageFont()).lineSpacing()) {
148 h += TQFontMetrics(
nameFont()).lineSpacing();
154 return TQMAX(h, TQApplication::globalStrut().height());
159 class KChatBasePrivate
168 mAcceptMessage =
true;
177 TQValueList<int> mIndex2Id;
181 TQFont mSystemNameFont;
182 TQFont mSystemMessageFont;
197 void KChatBase::init(
bool noComboBox)
201 d =
new KChatBasePrivate;
203 setMinimumWidth(100);
204 setMinimumHeight(150);
206 TQVBoxLayout* l =
new TQVBoxLayout(
this);
208 d->mBox =
new TQListBox(
this);
211 l->addWidget(d->mBox);
212 d->mBox->setVScrollBarMode(TQScrollView::AlwaysOn);
213 d->mBox->setHScrollBarMode(TQScrollView::AlwaysOff);
214 d->mBox->setFocusPolicy(TQ_NoFocus);
216 d->mBox->setSelectionMode(TQListBox::Single);
220 TQHBoxLayout* h =
new TQHBoxLayout(l);
221 d->mEdit =
new KLineEdit(
this);
222 d->mEdit->setHandleSignals(
false);
223 d->mEdit->setTrapReturnKey(
true);
224 d->mEdit->completionObject();
225 d->mEdit->setCompletionMode(KGlobalSettings::CompletionNone);
226 connect(d->mEdit, TQT_SIGNAL(
returnPressed(
const TQString&)),
this, TQT_SLOT(slotReturnPressed(
const TQString&)));
227 h->addWidget(d->mEdit);
230 d->mCombo =
new TQComboBox(
this);
231 h->addWidget(d->mCombo);
235 d->mAcceptMessage =
true;
245 {
return d->mAcceptMessage; }
248 { d->mAcceptMessage = a; }
262 kdWarning(11000) <<
"KChatBase: Cannot add an entry to the combo box" << endl;
265 if (d->mIndex2Id.findIndex(
id) != -1) {
266 kdError(11000) <<
"KChatBase: Cannot add more than one entry with the same ID! " << endl;
267 kdError(11000) <<
"KChatBase: Text="<<text<<endl;
270 d->mCombo->insertItem(text, index);
272 d->mIndex2Id.append(
id);
274 d->mIndex2Id.insert(d->mIndex2Id.at(index), id);
276 if (d->mIndex2Id.count() != (uint)d->mCombo->count()) {
277 kdError(11000) <<
"KChatBase: internal ERROR - local IDs do not match combo box entries!" << endl;
285 kdWarning(11001) <<
"Cannot retrieve index from NULL combo box" << endl;
288 int index = d->mCombo->currentItem();
289 if (d->mIndex2Id.at(index) == d->mIndex2Id.end()) {
290 kdWarning(11000) <<
"could not find the selected sending entry!" << endl;
293 return d->mIndex2Id[index];
299 kdWarning(11000) <<
"KChatBase: Cannot remove an entry from the combo box" << endl;
303 d->mIndex2Id.remove(
id);
309 kdWarning(11000) <<
"KChatBase: Cannot change an entry in the combo box" << endl;
313 d->mCombo->changeItem(text, index);
319 kdWarning(11000) <<
"KChatBase: Cannot set an entry in the combo box" << endl;
322 d->mCombo->setCurrentItem(
findIndex(
id));
327 return d->mIndex2Id.findIndex(
id);
332 int i = SendToAll + 1;
333 while (d->mIndex2Id.findIndex(i) != -1) {
341 d->mBox->insertItem(text);
342 int index = d->mBox->count() -1;
343 d->mBox->setBottomItem(index);
345 d->mBox->removeItem(0);
363 TQListBoxItem* message;
364 if (text.startsWith(
"/me ")) {
367 pix.load(locate(
"data", TQString::fromLatin1(
"kdegames/pics/star.png")));
371 message = (TQListBoxItem*)
new TQListBoxPixmap(pix, i18n(
"%1 %2").arg(fromName).arg(text.mid(3)));
377 message = (TQListBoxItem*)m;
390 return (TQListBoxItem*)m;
393 void KChatBase::slotReturnPressed(
const TQString& text)
395 if (text.length() <= 0) {
401 d->mEdit->completionObject()->addItem(text);
409 return i18n(
"Send to %1").arg(name);
418 { d->mEdit->setCompletionMode(mode); }
423 d->mBox->triggerUpdate(
false);
428 d->mMessageFont = font;
429 d->mBox->triggerUpdate(
false);
439 {
return d->mNameFont; }
442 {
return d->mMessageFont; }
446 d->mSystemNameFont = font;
447 d->mBox->triggerUpdate(
false);
452 d->mSystemMessageFont = font;
453 d->mBox->triggerUpdate(
false);
463 {
return d->mSystemNameFont; }
466 {
return d->mSystemMessageFont; }
472 conf = kapp->config();
473 oldGroup = conf->group();
474 conf->setGroup(
"KChatBase");
477 conf->writeEntry(
"NameFont",
nameFont());
481 conf->writeEntry(
"MaxMessages",
maxItems());
483 if (!oldGroup.isNull()) {
484 conf->setGroup(oldGroup);
492 conf = kapp->config();
493 oldGroup = conf->group();
494 conf->setGroup(
"KChatBase");
501 setMaxItems(conf->readNumEntry(
"MaxMessages", -1));
503 if (!oldGroup.isNull()) {
504 conf->setGroup(oldGroup);
519 }
else if (maxItems > 0) {
520 while (d->mBox->count() > (
unsigned int)maxItems) {
521 d->mBox->removeItem(0);
527 {
return d->mMaxItems; }
530 #include "kchatbase.moc"