summaryrefslogtreecommitdiffstats
path: root/tdeui/kiconview.cpp
blob: 04b78f3890a5e914b2d5b91fb131770537c6e45e (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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/* This file is part of the KDE libraries
   Copyright (C) 1999 Torben Weis <weis@kde.org>

   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 "config.h"

#include <tqtimer.h>
#include <tqpainter.h>
#include <tqpixmapcache.h>
#include <tqcleanuphandler.h>

#include "kiconview.h"
#include "kwordwrap.h"
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <tdeglobalsettings.h>
#include <tdeapplication.h>
#include <kipc.h> 

#include <kcursor.h>
#include <kpixmap.h>
#include <kpixmapeffect.h>

class TDEIconView::TDEIconViewPrivate
{
public:
    TDEIconViewPrivate() {
        mode = TDEIconView::Execute;
        fm = 0L;
        doAutoSelect = true;
        textHeight = 0;
        dragHoldItem = 0L;
    }
    TDEIconView::Mode mode;
    bool doAutoSelect;
    TQFontMetrics *fm;
    TQPixmapCache maskCache;
    int textHeight;
    TQIconViewItem *dragHoldItem;
    TQTimer dragHoldTimer;
    TQTimer doubleClickIgnoreTimer;
};

TDEIconView::TDEIconView( TQWidget *parent, const char *name, WFlags f )
    : TQIconView( parent, name, f )
{
    d = new TDEIconViewPrivate;

    connect( this, TQT_SIGNAL( onViewport() ),
             this, TQT_SLOT( slotOnViewport() ) );
    connect( this, TQT_SIGNAL( onItem( TQIconViewItem * ) ),
             this, TQT_SLOT( slotOnItem( TQIconViewItem * ) ) );
    slotSettingsChanged( TDEApplication::SETTINGS_MOUSE );
    if ( kapp ) { // maybe null when used inside designer
        connect( kapp, TQT_SIGNAL( settingsChanged(int) ), TQT_SLOT( slotSettingsChanged(int) ) );
        kapp->addKipcEventMask( KIPC::SettingsChanged );
    }

    m_pCurrentItem = 0L;

    m_pAutoSelect = new TQTimer( this );
    connect( m_pAutoSelect, TQT_SIGNAL( timeout() ),
             this, TQT_SLOT( slotAutoSelect() ) );

    connect( &d->dragHoldTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotDragHoldTimeout()) );
}

TDEIconView::~TDEIconView()
{
    delete d->fm;
    delete d;
}


void TDEIconView::setMode( TDEIconView::Mode mode )
{
    d->mode = mode;
}

TDEIconView::Mode TDEIconView::mode() const
{
    return d->mode;
}

void TDEIconView::slotOnItem( TQIconViewItem *item )
{
    if ( item ) {
        if ( m_bUseSingle ) {
            if ( m_bChangeCursorOverItem )
                viewport()->setCursor( KCursor().handCursor() );

            if ( (m_autoSelectDelay > -1) ) {
                m_pAutoSelect->start( m_autoSelectDelay, true );
            }
        }
        m_pCurrentItem = item;
    }
}

void TDEIconView::slotOnViewport()
{
    if ( m_bUseSingle && m_bChangeCursorOverItem )
        viewport()->unsetCursor();

    m_pAutoSelect->stop();
    m_pCurrentItem = 0L;
}

void TDEIconView::slotSettingsChanged(int category)
{
    if ( category != TDEApplication::SETTINGS_MOUSE )
      return;
    m_bUseSingle = TDEGlobalSettings::singleClick();
    //kdDebug() << "TDEIconView::slotSettingsChanged for mouse, usesingle=" << m_bUseSingle << endl;

    disconnect( this, TQT_SIGNAL( mouseButtonClicked( int, TQIconViewItem *,
						  const TQPoint & ) ),
		this, TQT_SLOT( slotMouseButtonClicked( int, TQIconViewItem *,
						    const TQPoint & ) ) );
//         disconnect( this, TQT_SIGNAL( doubleClicked( TQIconViewItem *,
// 						 const TQPoint & ) ),
// 		    this, TQT_SLOT( slotExecute( TQIconViewItem *,
// 					     const TQPoint & ) ) );

    if( m_bUseSingle ) {
      connect( this, TQT_SIGNAL( mouseButtonClicked( int, TQIconViewItem *,
						 const TQPoint & ) ),
	       this, TQT_SLOT( slotMouseButtonClicked( int, TQIconViewItem *,
						   const TQPoint & ) ) );
    }
    else {
//         connect( this, TQT_SIGNAL( doubleClicked( TQIconViewItem *,
// 					      const TQPoint & ) ),
//                  this, TQT_SLOT( slotExecute( TQIconViewItem *,
// 					  const TQPoint & ) ) );
    }

    m_bChangeCursorOverItem = TDEGlobalSettings::changeCursorOverIcon();
    m_autoSelectDelay = m_bUseSingle ? TDEGlobalSettings::autoSelectDelay() : -1;

    if( !m_bUseSingle || !m_bChangeCursorOverItem )
        viewport()->unsetCursor();
}

void TDEIconView::slotAutoSelect()
{
  // check that the item still exists
  if( index( m_pCurrentItem ) == -1 || !d->doAutoSelect )
    return;

  //Give this widget the keyboard focus.
  if( !hasFocus() )
    setFocus();

  ButtonState keybstate = TDEApplication::keyboardMouseState();
  TQIconViewItem* previousItem = currentItem();
  setCurrentItem( m_pCurrentItem );

  if( m_pCurrentItem ) {
    //Shift pressed?
    if( (keybstate & ShiftButton) ) {
      //Temporary implementation of the selection until TQIconView supports it
      bool block = signalsBlocked();
      blockSignals( true );

      //No Ctrl? Then clear before!
      if( !(keybstate & ControlButton) )
	clearSelection();

      bool select = !m_pCurrentItem->isSelected();
      bool update = viewport()->isUpdatesEnabled();
      viewport()->setUpdatesEnabled( false );

      //Calculate the smallest rectangle that contains the current Item
      //and the one that got the autoselect event
      TQRect r;
      TQRect redraw;
      if ( previousItem )
	r = TQRect( TQMIN( previousItem->x(), m_pCurrentItem->x() ),
		   TQMIN( previousItem->y(), m_pCurrentItem->y() ),
		   0, 0 );
      else
	r = TQRect( 0, 0, 0, 0 );
      if ( previousItem->x() < m_pCurrentItem->x() )
	r.setWidth( m_pCurrentItem->x() - previousItem->x() + m_pCurrentItem->width() );
      else
	r.setWidth( previousItem->x() - m_pCurrentItem->x() + previousItem->width() );
      if ( previousItem->y() < m_pCurrentItem->y() )
	r.setHeight( m_pCurrentItem->y() - previousItem->y() + m_pCurrentItem->height() );
      else
	r.setHeight( previousItem->y() - m_pCurrentItem->y() + previousItem->height() );
      r = r.normalize();

      //Check for each item whether it is within the rectangle.
      //If yes, select it
      for( TQIconViewItem* i = firstItem(); i; i = i->nextItem() ) {
	if( i->intersects( r ) ) {
	  redraw = redraw.unite( i->rect() );
	  setSelected( i, select, true );
	}
      }

      blockSignals( block );
      viewport()->setUpdatesEnabled( update );
      repaintContents( redraw, false );

      emit selectionChanged();

      if( selectionMode() == TQIconView::Single )
	emit selectionChanged( m_pCurrentItem );

      //setSelected( m_pCurrentItem, true, (keybstate & ControlButton), (keybstate & ShiftButton) );
    }
    else if( (keybstate & ControlButton) )
      setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected(), true );
    else
      setSelected( m_pCurrentItem, true );
  }
  else
    kdDebug() << "TDEIconView: That's not supposed to happen!!!!" << endl;
}

void TDEIconView::emitExecute( TQIconViewItem *item, const TQPoint &pos )
{
  if ( d->mode != Execute )
  {
    // kdDebug() << "TDEIconView::emitExecute : not in execute mode !" << endl;
    return;
  }

  ButtonState keybstate = TDEApplication::keyboardMouseState();

  m_pAutoSelect->stop();

  //Don't emit executed if in SC mode and Shift or Ctrl are pressed
  if( !( m_bUseSingle && ((keybstate & ShiftButton) || (keybstate & ControlButton)) ) ) {
    setSelected( item, false );
    viewport()->unsetCursor();
    emit executed( item );
    emit executed( item, pos );
  }
}

void TDEIconView::updateDragHoldItem( TQDropEvent *e )
{
  TQIconViewItem *item = findItem( e->pos() );

  if ( d->dragHoldItem != item)
  {
    d->dragHoldItem = item;
    if( item  )
    {
      d->dragHoldTimer.start( 1000, true );
    }
    else
    {
      d->dragHoldTimer.stop();
    }
  }
}

void TDEIconView::focusOutEvent( TQFocusEvent *fe )
{
  m_pAutoSelect->stop();

  TQIconView::focusOutEvent( fe );
}

void TDEIconView::leaveEvent( TQEvent *e )
{
  m_pAutoSelect->stop();

  TQIconView::leaveEvent( e );
}

void TDEIconView::contentsMousePressEvent( TQMouseEvent *e )
{
  if( (selectionMode() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) ) {
    bool block = signalsBlocked();
    blockSignals( true );

    clearSelection();

    blockSignals( block );
  }

  TQIconView::contentsMousePressEvent( e );
  d->doAutoSelect = false;
}

void TDEIconView::contentsMouseDoubleClickEvent ( TQMouseEvent * e )
{
  TQIconView::contentsMouseDoubleClickEvent( e );

  TQIconViewItem* item = findItem( e->pos() );

  if( item ) {
    if( (e->button() == Qt::LeftButton) && !m_bUseSingle )
      emitExecute( item, e->globalPos() );

    emit doubleClicked( item, e->globalPos() );
  }
  d->doubleClickIgnoreTimer.start(0, true);
}

void TDEIconView::slotMouseButtonClicked( int btn, TQIconViewItem *item, const TQPoint &pos )
{
  //kdDebug() << " TDEIconView::slotMouseButtonClicked() item=" << item << endl;
  if( d->doubleClickIgnoreTimer.isActive() )
    return; // Ignore double click
    
  if( (btn == Qt::LeftButton) && item )
    emitExecute( item, pos );
}

void TDEIconView::contentsMouseReleaseEvent( TQMouseEvent *e )
{
    d->doAutoSelect = true;
    TQIconView::contentsMouseReleaseEvent( e );
}

void TDEIconView::contentsDragEnterEvent( TQDragEnterEvent *e )
{
    updateDragHoldItem( e );
    TQIconView::contentsDragEnterEvent( e );
}

void TDEIconView::contentsDragLeaveEvent( TQDragLeaveEvent *e )
{
    d->dragHoldTimer.stop();
    d->dragHoldItem = 0L;
    TQIconView::contentsDragLeaveEvent( e );
}


void TDEIconView::contentsDragMoveEvent( TQDragMoveEvent *e )
{
    updateDragHoldItem( e );
    TQIconView::contentsDragMoveEvent( e );
}

void TDEIconView::contentsDropEvent( TQDropEvent* e )
{
    d->dragHoldTimer.stop();
    TQIconView::contentsDropEvent( e );
}

void TDEIconView::slotDragHoldTimeout()
{
    TQIconViewItem *tmp = d->dragHoldItem;
    d->dragHoldItem = 0L;

    emit held( tmp );
}

void TDEIconView::takeItem( TQIconViewItem * item )
{
    if ( item == d->dragHoldItem )
    {
        d->dragHoldTimer.stop();
        d->dragHoldItem = 0L;
    }

    TQIconView::takeItem( item );
}

void TDEIconView::cancelPendingHeldSignal()
{
    d->dragHoldTimer.stop();
    d->dragHoldItem = 0L;
}

void TDEIconView::wheelEvent( TQWheelEvent *e )
{
    if (horizontalScrollBar() && (arrangement() == TQIconView::TopToBottom)) {
        TQWheelEvent ce(e->pos(), e->delta(), e->state(), Qt::Horizontal);
        TQApplication::sendEvent( horizontalScrollBar(), &ce);
	if (ce.isAccepted()) {
            e->accept();
	    return;
	}
    }
    TQIconView::wheelEvent(e);
}

void TDEIconView::setFont( const TQFont &font )
{
    delete d->fm;
    d->fm = 0L;
    TQIconView::setFont( font );
}

TQFontMetrics *TDEIconView::itemFontMetrics() const
{
    if (!d->fm) {
        // TQIconView creates one too, but we can't access it
        d->fm = new TQFontMetrics( font() );
    }
    return d->fm;
}

TQPixmap TDEIconView::selectedIconPixmap( TQPixmap *pix, const TQColor &col ) const
{
    TQPixmap m;
    if ( d->maskCache.find( TQString::number( pix->serialNumber() ), m ) )
	return m;
    m = KPixmapEffect::selectedPixmap( KPixmap(*pix), col );
    d->maskCache.insert( TQString::number( pix->serialNumber() ), m );
    return m;
}

int TDEIconView::iconTextHeight() const
{
    return d->textHeight > 0 ? d->textHeight : ( wordWrapIconText() ? 99 : 1 );
}

void TDEIconView::setIconTextHeight( int n )
{
    int oldHeight = iconTextHeight();
    if ( n > 1 )
        d->textHeight = n;
    else
        d->textHeight = 1;

    // so that Qt still shows the tooltip when even a wrapped text is too long
    setWordWrapIconText( false );

    // update view if needed
    if ( iconTextHeight() != oldHeight )
        setFont( font() );  // hack to recalc items
}

/////////////

class TDEIconViewItem::TDEIconViewItemPrivate
{
    public:
        TDEIconViewItemPrivate() {
            m_pixmapSize = TQSize(0,0);
        }

    public:
        TQSize m_pixmapSize;
        int realTextHeight;
};

void TDEIconViewItem::init()
{
    m_wordWrap = 0L;
    d = 0L;
    calcRect();
}

TDEIconViewItem::~TDEIconViewItem()
{
    delete m_wordWrap;
    if (d) {
        delete d;
    }
}

void TDEIconViewItem::calcRect( const TQString& text_ )
{
    if ( !d ) {
        d = new TDEIconViewItemPrivate;
    }
    d->realTextHeight = -1;

    bool drawRoundedRect = TDEGlobalSettings::iconUseRoundedRect();

    Q_ASSERT( iconView() );
    if ( !iconView() )
        return;
    delete m_wordWrap;
    m_wordWrap = 0L;
#ifndef NDEBUG // be faster for the end-user, such a bug will have been fixed before hand :)
    if ( !iconView()->inherits("TDEIconView") )
    {
        kdWarning() << "TDEIconViewItem used in a " << iconView()->className() << " !!" << endl;
        return;
    }
#endif
    //kdDebug() << "TDEIconViewItem::calcRect - " << text() << endl;
    TDEIconView *view = static_cast<TDEIconView *>(iconView());
    TQRect itemIconRect = pixmapRect();
    TQRect itemTextRect = textRect();
    TQRect itemRect = rect();

    int pw = 0;
    int ph = 0;

#ifndef TQT_NO_PICTURE
    if ( picture() ) {
        TQRect br = picture()->boundingRect();
        pw = br.width() + 2;
        ph = br.height() + 2;
    } else
#endif
    {
        // Qt uses unknown_icon if no pixmap. Let's see if we need that - I doubt it
        if (!pixmap())
            return;
        pw = pixmap()->width() + 2;
        ph = pixmap()->height() + 2;
    }
    itemIconRect.setWidth( pw );
#if 1 // FIXME 
    // There is a bug in Qt which prevents the item from being placed
    // properly when the pixmapRect is not at the top of the itemRect, so we
    // have to increase the height of the pixmapRect and leave it at the top
    // of the itemRect...
    if ( d && !d->m_pixmapSize.isNull() ) {
        itemIconRect.setHeight( d->m_pixmapSize.height() + 2 );
    }
    else
#endif
    itemIconRect.setHeight( ph );

    int tw = 0;
    if ( d && !d->m_pixmapSize.isNull() ) {
        tw = view->maxItemWidth() - ( view->itemTextPos() == TQIconView::Bottom ? 0 :
                                      d->m_pixmapSize.width() + 2 );
    }
    else {
        tw = view->maxItemWidth() - ( view->itemTextPos() == TQIconView::Bottom ? 0 :
                                      itemIconRect.width() );
    }

    TQFontMetrics *fm = view->itemFontMetrics();
    TQString t;
    TQRect r;

    // When is text_ set ? Doesn't look like it's ever set.
    t = text_.isEmpty() ? text() : text_;

    // Max text height
    int nbLines = static_cast<TDEIconView*>( iconView() )->iconTextHeight();
    int height = nbLines > 0 ? fm->height() * nbLines : 0xFFFFFFFF;

    // Should not be higher than pixmap if text is alongside icons
    if ( view->itemTextPos() != TQIconView::Bottom ) {
        if ( d && !d->m_pixmapSize.isNull() )
            height = TQMIN( d->m_pixmapSize.height() + 2, height );
        else
            height = TQMIN( itemIconRect.height(), height );
        height = TQMAX( height, fm->height() );
    }

    // Calculate the word-wrap
    TQRect outerRect( 0, 0, tw - 6, height );
    m_wordWrap = KWordWrap::formatText( *fm, outerRect, 0, t );
    r = m_wordWrap->boundingRect();

    int realWidth = TQMAX( TQMIN( r.width() + 4, tw ), fm->width( "X" ) );
    if (drawRoundedRect == true) {
      itemTextRect.setWidth( realWidth + 2);
    }
    else {
      itemTextRect.setWidth( realWidth );
    }
    itemTextRect.setHeight( r.height() );

    int w = 0;    int h = 0;    int y = 0;
    if ( view->itemTextPos() == TQIconView::Bottom ) {
        // If the pixmap size has been specified, use it
        if ( d && !d->m_pixmapSize.isNull() )
        {
            w = TQMAX( itemTextRect.width(), d->m_pixmapSize.width() + 2 );
            h = itemTextRect.height() + d->m_pixmapSize.height() + 2 + 1;
#if 0 // FIXME 
            // Waiting for the qt bug to be solved, the pixmapRect must
            // stay on the top...
            y = d->m_pixmapSize.height() + 2 - itemIconRect.height();
#endif
        }
        else {
            w = TQMAX( itemTextRect.width(), itemIconRect.width() );
            h = itemTextRect.height() + itemIconRect.height() + 1;
        }

        itemRect.setWidth( w );
        itemRect.setHeight( h );
        int width = TQMAX( w, TQApplication::globalStrut().width() ); // see TQIconViewItem::width()
        int height = TQMAX( h, TQApplication::globalStrut().height() ); // see TQIconViewItem::height()
        itemTextRect = TQRect( ( width - itemTextRect.width() ) / 2, height - itemTextRect.height(),
                              itemTextRect.width(), itemTextRect.height() );
        itemIconRect = TQRect( ( width - itemIconRect.width() ) / 2, y,
                              itemIconRect.width(), itemIconRect.height() );
    }
    else {
        // If the pixmap size has been specified, use it
        if ( d && !d->m_pixmapSize.isNull() ) {
            h = TQMAX( itemTextRect.height(), d->m_pixmapSize.height() + 2 );
#if 0 // FIXME 
            // Waiting for the qt bug to be solved, the pixmapRect must
            // stay on the top...
            y = ( d->m_pixmapSize.height() + 2 - itemIconRect.height() ) / 2;
#endif
        }
        else {
            h = TQMAX( itemTextRect.height(), itemIconRect.height() );
        }
        w = itemTextRect.width() + itemIconRect.width() + 1;

        itemRect.setWidth( w );
        itemRect.setHeight( h );
        int width = TQMAX( w, TQApplication::globalStrut().width() ); // see TQIconViewItem::width()
        int height = TQMAX( h, TQApplication::globalStrut().height() ); // see TQIconViewItem::height()

        itemTextRect = TQRect( width - itemTextRect.width(), ( height - itemTextRect.height() ) / 2,
                              itemTextRect.width(), itemTextRect.height() );
        if ( itemIconRect.height() > itemTextRect.height() ) { // icon bigger than text -> center vertically
            itemIconRect = TQRect( 0, ( height - itemIconRect.height() ) / 2,
                                  itemIconRect.width(), itemIconRect.height() );
        }
        else { // icon smaller than text -> place in top or center with first line
	    itemIconRect = TQRect( 0, TQMAX(( fm->height() - itemIconRect.height() ) / 2 + y, 0),
                                  itemIconRect.width(), itemIconRect.height() );
        }
        if ( ( itemIconRect.height() <= 20 ) && ( itemTextRect.height() < itemIconRect.height() ) ) {
            d->realTextHeight = itemTextRect.height();
            itemTextRect.setY( itemIconRect.y() );
            itemTextRect.setHeight( itemIconRect.height() - 2 );
        }
    }

    if ( itemIconRect != pixmapRect() ) {
        setPixmapRect( itemIconRect );
    }
    if ( itemTextRect != textRect() ) {
        setTextRect( itemTextRect );
    }
    if ( itemRect != rect() ) {
        setItemRect( itemRect );
    }

    // Done by setPixmapRect, setTextRect and setItemRect !  [and useless if no rect changed]
    //view->updateItemContainer( this );

}

void TDEIconViewItem::paintItem( TQPainter *p, const TQColorGroup &cg )
{
    TQIconView* view = iconView();
    Q_ASSERT( view );
    if ( !view )
        return;
#ifndef NDEBUG // be faster for the end-user, such a bug will have been fixed before hand :)
    if ( !view->inherits("TDEIconView") )
    {
        kdWarning() << "TDEIconViewItem used in a " << view->className() << " !!" << endl;
        return;
    }
#endif

    p->save();

    paintPixmap(p, cg);
    paintText(p, cg);

    p->restore();
}

KWordWrap * TDEIconViewItem::wordWrap()
{
    return m_wordWrap;
}

void TDEIconViewItem::paintPixmap( TQPainter *p, const TQColorGroup &cg )
{
    TDEIconView *kview = static_cast<TDEIconView *>(iconView());

#ifndef TQT_NO_PICTURE
    if ( picture() ) {
	TQPicture *pic = picture();
	if ( isSelected() ) {
            // TODO something as nice as selectedIconPixmap if possible ;)
	    p->fillRect( pixmapRect( false ), TQBrush( cg.highlight(), TQBrush::Dense4Pattern) );
	}
	p->drawPicture( x()-pic->boundingRect().x(), y()-pic->boundingRect().y(), *pic );
    } else
#endif
    {
        int iconX = pixmapRect( false ).x();
        int iconY = pixmapRect( false ).y();

        TQPixmap *pix = pixmap();
        if ( !pix || pix->isNull() )
            return;

#if 1 // FIXME 
        // Move the pixmap manually because the pixmapRect is at the
        // top of the itemRect
        // (won't be needed anymore in future versions of qt)
        if ( d && !d->m_pixmapSize.isNull() )
        {
            int offset = 0;
            if ( kview->itemTextPos() == TQIconView::Bottom )
                offset = d->m_pixmapSize.height() - pix->height();
            else
                offset = ( d->m_pixmapSize.height() - pix->height() ) / 2;
            if ( offset > 0 )
                iconY += offset;
        }
#endif
        if ( isSelected() ) {
            TQPixmap selectedPix = kview->selectedIconPixmap( pix, cg.highlight() );
            p->drawPixmap( iconX, iconY, selectedPix );
        } else {
            p->drawPixmap( iconX, iconY, *pix );
        }
    }
}

void TDEIconViewItem::paintText( TQPainter *p, const TQColorGroup &cg )
{
    bool drawRoundedRect = TDEGlobalSettings::iconUseRoundedRect();
    int textX;
    if (drawRoundedRect == true) {
      textX = textRect( false ).x() + 4;
    }
    else {
      textX = textRect( false ).x() + 2;
    }
    int textY;
    if ( d && (d->realTextHeight != -1) ) {
      textY = textRect( false ).y() + ((rect().height() - d->realTextHeight) / 2);
    }
    else {
      textY = textRect( false ).y();
    }

    if ( isSelected() ) {
      if (drawRoundedRect == true) {
	p->setBrush(TQBrush(cg.highlight()));
	p->setPen(TQPen(cg.highlight()));
	p->drawRoundRect( textRect( false ) ,1000/textRect(false).width(),1000/textRect(false).height() );
      }
      else {
        p->fillRect( textRect( false ), cg.highlight() );
      }
        p->setPen( TQPen( cg.highlightedText() ) );
    }
    else {
        if ( iconView()->itemTextBackground() != Qt::NoBrush ) {
            p->fillRect( textRect( false ), iconView()->itemTextBackground() );
        }
        p->setPen( cg.text() );
    }

    int align = iconView()->itemTextPos() == TQIconView::Bottom ? AlignHCenter : AlignAuto;
    m_wordWrap->drawText( p, textX, textY, align | KWordWrap::Truncate );
}

TQSize TDEIconViewItem::pixmapSize() const
{
    return d ? d->m_pixmapSize : TQSize( 0, 0 );
}

void TDEIconViewItem::setPixmapSize( const TQSize& size )
{
    if ( !d ) {
        d = new TDEIconViewItemPrivate;
    }

    d->m_pixmapSize = size;
}

void TDEIconView::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kiconview.moc"