20 #include "kchatbase.h" 22 #include <klineedit.h> 23 #include <tdelocale.h> 24 #include <kstandarddirs.h> 25 #include <tdeconfig.h> 26 #include <tdeapplication.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(TDEGlobalSettings::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(
"tdegames/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" A TQListBoxText implementation for KChatBase.
const TQFont & systemMessageFont() const
Same as systemMessageFont but applies only to system messages.
virtual void returnPressed(const TQString &text)=0
This is called whenever the user pushed return ie wants to send a message.
KChatBaseText(const TQString &player, const TQString &text)
Constructs a KChatBaseText object with the player and text part.
void removeSendingEntry(int id)
Removes the entry with the ID id from the combo box.
virtual TQString comboBoxItem(const TQString &name) const
Replace to customise the combo box.
void setBothFont(const TQFont &font)
This sets both - nameFont and messageFont to font.
const TQString & message() const
const TQFont & systemNameFont() const
Same as systemNameFont but applies only to system messages.
int findIndex(int id) const
virtual const TQString & fromName() const =0
virtual void addItem(const TQListBoxItem *item)
This member function is mainly internally used to add a message.
virtual void addMessage(const TQString &fromName, const TQString &text)
Add a text in the listbox.
const TQFont & messageFont() const
This font should be used for a message.
virtual TQListBoxItem * layoutSystemMessage(const TQString &fromName, const TQString &text)
Create a TQListBoxItem for this message.
virtual bool acceptMessage() const
virtual TQListBoxItem * layoutMessage(const TQString &fromName, const TQString &text)
Create a TQListBoxItem for this message.
void setSystemMessageFont(const TQFont &font)
Same as setMessageFont but applies only to system messages.
virtual void addSystemMessage(const TQString &fromName, const TQString &text)
This works just like addMessage but adds a system message.
virtual void saveConfig(TDEConfig *conf=0)
Save the configuration of the dialog to a TDEConfig object.
void setName(const TQString &name)
Set the name part of a message.
virtual ~KChatBaseText()
Destruct a KChatBaseText object.
void slotClear()
This clears all messages in the view.
void setSystemNameFont(const TQFont &font)
Same as setNameFont but applies only to system messages.
void setMessageFont(const TQFont *font)
Set the font for the message text.
void setCompletionMode(TDEGlobalSettings::Completion mode)
See KLineEdit::setCompletionMode.
void clear()
Clear all messages in the list.
void changeSendingEntry(const TQString &text, int id)
This changes a combo box entry.
bool addSendingEntry(const TQString &text, int id)
Adds a new entry in the combo box.
void rightButtonClicked(TQListBoxItem *, const TQPoint &)
Emitted when the user right-clicks on a list item.
KChatBase(TQWidget *parent, bool noComboBox=false)
void setAcceptMessage(bool a)
TQFont nameFont() const
You can set the font of the sender name independently of the message itself.
const TQFont & nameFont() const
This font should be used for the name (the "from: " part) of a message.
void setMessage(const TQString &message)
Set the text part of a message.
void setMaxItems(int maxItems)
Set the maximum number of items in the list.
void setSendingEntry(int id)
This selects a combo box entry.
void setMessageFont(const TQFont &font)
Set the font that used used for the message part of a message.
virtual void readConfig(TDEConfig *conf=0)
Read the configuration from a TDEConfig object.
virtual ~KChatBase()
Destruct the KChatBase object.
void setNameFont(const TQFont *font)
Set the font for the name.
void setSystemBothFont(const TQFont &font)
Same as setBothFont but applies only to system messages.
bool insertSendingEntry(const TQString &text, int id, int index=-1)
Inserts a new entry in the combo box.
const TQString & name() const
TQFont messageFont() const
You can set the font of the message independently of the sender name.
void setNameFont(const TQFont &font)
Set the font that used used for the name part of a message.