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
|
/*******************************************************************
aListBox
$$Id$$
List box that outputs a right click mouse so I can popup a qpopupmenu.
Does special sorting, and maintains a two part list, one for ops,
other for users.
nothing special.
*******************************************************************/
#include "alistbox.h"
#include "nickColourMaker.h"
#include <tqdragobject.h>
#include <tqpainter.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <kurldrag.h>
aListBox::aListBox(TQWidget *parent, const char *name )
: TQListBox(parent,name)
{
clear();
p_scroll = TQPalette(palette());
setAcceptDrops( true );
connect(this, TQ_SIGNAL(selected (const TQString&)),
this, TQ_SIGNAL(selectedNick(const TQString&)));
m_nickListDirty = true;
updateNickPrefixWidth();
connect( this, TQ_SIGNAL( contextMenuRequested( TQListBoxItem *, const TQPoint & ) ),
this, TQ_SLOT( reEmitContextMenuRequest( TQListBoxItem * ) ) );
}
aListBox::~aListBox()
{
}
void aListBox::reEmitContextMenuRequest( TQListBoxItem *item )
{
emit contextMenuRequested( index( item ) );
}
void aListBox::clear()
{
TQListBox::clear();
}
void aListBox::inSort ( nickListItem *lbi)
{
int insert;
bool found;
insert = searchFor(lbi->text(), found, lbi->op());
if(found == TRUE){
//kdDebug(5008) << lbi->text() << " is already in nick list!" << endl;
return;
}
insertItem(lbi, insert);
// for(uint index = 0; index < count(); index++){
// debug("%d is %s", index, text(index));
// }
m_nickListDirty = true;
}
void aListBox::inSort ( TQString text, bool top)
{
nickListItem *nli = new nickListItem();
nli->setText(text);
if(top == TRUE)
nli->setOp(TRUE);
inSort(nli);
}
int aListBox::findSep()
{
uint i = 0;
for(; i < count(); i++)
if(item(i)->op() == FALSE)
break; // stop now
return i;
}
int aListBox::searchFor(const TQString &nick, bool &found, bool top)
{
int min = 0, max = 0;
int current = 0, compare = 0;
int real_max = 0;
int insert;
found = FALSE;
// If there's nothing in the list, don't try and search it, etc
if(count() == 0){
insert = 0;
}
else{
int sep = findSep();
if(sep >= 0){
if(top == TRUE){
min = 0;
max = (sep >= 1) ? sep - 1 : 0;
}
else{
min = sep;
max = count() - 1;
}
}
else
current = -1;
real_max = max;
current = (min + max)/2; // + (max-min)%2;
insert = current;
int last_current = -1;
uint loop = 0; // Most loops should be log_2 count(), but...
do {
if(current == last_current){
// debug("Insert looping on %s", nick.data());
// current++;
break; // we're looping, so stop
}
if(current >= max)
break; // Don't go too far
last_current = current;
compare = text(current).lower().compare(nick.lower());
if(compare < 0){
min = current;
insert = current + 1;
// debug("1 < 0: %s is greater then: %s, min: %d max: %d current: %d", nick.data(), text(current), min, max, current);
}
else if(compare > 0){
max = current;
insert = current;
// debug("1 > 0: %s is less then: %s, min: %d max: %d current: %d", nick.data(), text(current), min, max, current);
}
else {// We got a match?
insert = current;
min = current;
found = TRUE;
break;
}
current = (min + max)/2;
loop++; // Infinite loop detector increment
} while(max != min && loop < count());
if(current >= real_max - 1){
compare = text(real_max).lower().compare(nick.lower());
if(compare < 0){
min = current;
insert = real_max + 1;
// debug("End check got one!");
}
else if (compare == 0){// We got a match
insert = real_max + 1;
min = real_max;
found = TRUE;
}
}
// Sanity check
if((top == TRUE && insert > sep) ||
(top == FALSE && insert < sep)){
insert = sep;
}
if(loop == count())
{
// debug("Loop inifitly on: %s", nick.data());
}
if(found == TRUE){
// debug("Found %s", nick.data());
return min; // We found one, so return the number found
}
}
// debug("%s is at %d", nick.data(), insert);
return insert;
}
bool aListBox::isTop(int index)
{
if(index >= findSep())
return FALSE;
else
return TRUE;
}
int aListBox::findNick(const TQString &str)
{
bool found;
int index;
index = searchFor(str, found, TRUE);
if(found == TRUE)
return index;
index = searchFor(str, found, FALSE);
if(found == TRUE)
return index;
// debug("Did not find: %s", str.data());
return -1;
}
nickListItem *aListBox::item(int index){
return (nickListItem *) TQListBox::item(index);
}
void aListBox::dragMoveEvent( TQDragMoveEvent *e )
{
bool ok = (count() > 0 && KURLDrag::canDecode( e ));
if(!ok)
ok = TQTextDrag::canDecode(e);
e->accept( ok );
if ( ok )
setCurrentItem( itemAt( e->pos() ));
}
void aListBox::dropEvent( TQDropEvent *e )
{
TQListBoxItem *item = itemAt( e->pos() );
if ( !item )
return;
setCurrentItem( item );
TQStringList urls;
KURLDrag::decodeLocalFiles( e, urls );
TQString text;
if ( !urls.isEmpty() )
emit urlsDropped( urls, item->text() );
else if(TQTextDrag::decode(e, text)){
emit textDropped( item, text);
}
}
bool aListBox::needNickPrefix() const
{
if ( m_nickListDirty ) {
updateNeedNickPrefixFlag();
const_cast<aListBox *>( this )->updateNickPrefixWidth();
}
return m_needNickPrefix;
}
void aListBox::updateNeedNickPrefixFlag() const
{
m_needNickPrefix = false;
if(ksopts->useColourNickList == false){
TQListBoxItem *item = firstItem();
for (; item; item = item->next() )
{
nickListItem *nickItem = static_cast<nickListItem *>( item );
if ( nickItem->op() ||
nickItem->voice() ||
nickItem->away() ||
nickItem->ircOp() )
{
m_needNickPrefix = true;
break;
}
}
}
m_nickListDirty = false;
}
void aListBox::fontChange( const TQFont &f )
{
TQListBox::fontChange( f );
updateNickPrefixWidth();
}
void aListBox::updateNickPrefixWidth()
{
TQFontMetrics metrics( font() );
m_nickPrefixWidth = 0;
if(ksopts->useColourNickList == false){
nickListItem *item = static_cast<nickListItem *>( firstItem() );
for ( ; item; item = static_cast<nickListItem *>( item->next() ) )
m_nickPrefixWidth = kMax( m_nickPrefixWidth, metrics.width( item->nickPrefix() ) );
// padding
}
m_nickPrefixWidth += metrics.width( " " );
}
void aListBox::clearAdvOps()
{
nickListItem *item = static_cast<nickListItem *>( firstItem() );
for ( ; item; item = static_cast<nickListItem *>( item->next() ) ){
if(item->away() || item->ircOp()){
item->setIrcOp(false);
item->setAway(false);
updateItem(item);
}
}
triggerUpdate(false);
m_nickListDirty = true;
}
nickListItem::nickListItem()
: TQListBoxItem()
{
is_op = FALSE;
is_voice = FALSE;
is_away = FALSE;
is_ircop = FALSE;
forcedCol = 0x0;
}
nickListItem::nickListItem(const nickListItem &old)
: TQListBoxItem()
{
is_op = old.is_op;
is_voice = old.is_voice;
is_away = old.is_away;
is_ircop = old.is_ircop;
string = old.string;
forcedCol = old.forcedCol;
}
nickListItem::~nickListItem()
{
string.truncate(0);
}
void nickListItem::setOp(bool _op)
{
is_op = _op;
if ( listBox() )
static_cast<aListBox *>( listBox() )->setNickListDirty();
}
void nickListItem::setVoice(bool _voice)
{
is_voice = _voice;
if ( listBox() )
static_cast<aListBox *>( listBox() )->setNickListDirty();
}
void nickListItem::setAway(bool _away)
{
is_away = _away;
if ( listBox() )
static_cast<aListBox *>( listBox() )->setNickListDirty();
}
void nickListItem::setIrcOp(bool _ircop)
{
is_ircop = _ircop;
if ( listBox() )
static_cast<aListBox *>( listBox() )->setNickListDirty();
}
void nickListItem::forceColour(const TQColor *col)
{
forcedCol = col;
}
void nickListItem::paint(TQPainter *p)
{
TQFontMetrics fm = p->fontMetrics();
int yPos; // vertical text position
int nickPosX = 3;
yPos = fm.ascent() + fm.leading()/2;
TQPen pen = p->pen();
TQFont font = p->font();
if(ksopts->useColourNickList == true) {
if(ksopts->nickColourization){
if(!isSelected()) {
if(forcedCol && forcedCol->isValid())
p->setPen(*forcedCol);
else
p->setPen(nickColourMaker::colourMaker()->findFg(text()));
}
else {
p->setPen(ksopts->selForegroundColor);
}
if(is_voice == TRUE){
TQPen open = p->pen();
p->setPen(ksopts->channelColor);
p->drawText( nickPosX, yPos, TQString("+") );
nickPosX += fm.width( "+" );
p->setPen(open);
// p->fillRect(0,0, listBox()->maxItemWidth(), height(listBox()), TQBrush(ksopts->channelColor, TQt::Dense7Pattern));
}
if(is_op == TRUE) {
TQPen open = p->pen();
p->setPen(ksopts->errorColor);
p->drawText( nickPosX, yPos, TQString("@") );
nickPosX += fm.width( "@" );
p->setPen(open);
// p->fillRect(0,0, listBox()->maxItemWidth(), height(listBox()), TQBrush(ksopts->errorColor, TQt::Dense7Pattern));
}
if(is_away == TRUE)
p->setPen(p->pen().color().dark(150));
if(is_ircop == TRUE){
TQPen open = p->pen();
p->setPen(ksopts->errorColor);
p->drawText( nickPosX, yPos, TQString("*") );
nickPosX += fm.width( "*" );
p->setPen(open);
}
}
else {
if(is_voice == TRUE)
p->setPen(ksopts->channelColor);
if(is_op == TRUE)
p->setPen(ksopts->errorColor);
if(is_away == TRUE)
p->setPen(p->pen().color().dark(150));
if(is_ircop == TRUE){
TQFont bfont = font;
bfont.setBold(TRUE);
p->setFont(bfont);
}
}
}
else {
}
if(ksopts->useColourNickList == false) {
aListBox *lb = static_cast<aListBox *>( listBox() );
if ( lb->needNickPrefix() )
{
p->drawText( 3, yPos, nickPrefix() );
nickPosX += lb->nickPrefixWidth();
}
}
p->drawText( nickPosX, yPos, text() );
p->setPen(pen);
p->setFont(font);
}
TQString nickListItem::nickPrefix() const
{
TQString prefix;
if ( voice() )
prefix += aListBox::nickPrefixVoice();
if ( op() )
prefix += aListBox::nickPrefixOp();
if ( away() )
prefix += aListBox::nickPrefixAway();
if ( ircOp() )
prefix += aListBox::nickPrefixIrcOp();
if ( !prefix.isEmpty() )
prefix.prepend( "+" );
return prefix;
}
int nickListItem::height(const TQListBox *lb ) const
{
return lb->fontMetrics().lineSpacing() + 1;
}
int nickListItem::width(const TQListBox *lb ) const
{
return
static_cast<const aListBox *>( lb )->nickPrefixWidth() +
lb->fontMetrics().width( text() ) + 6;
}
const TQPixmap* nickListItem::pixmap() const
{
return 0l;
}
nickListItem &nickListItem::operator= (const nickListItem &nli)
{
string = nli.string;
is_op = nli.is_op;
is_voice = nli.is_voice;
is_away = nli.is_away;
is_ircop = nli.is_ircop;
return (*this);
}
#include "alistbox.moc"
|