KDE3Support
k3iconview.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Torben Weis <weis@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "k3iconview.h" 00020 00021 //#include <config.h> 00022 00023 #include <QtCore/QTimer> 00024 #include <QtGui/QPainter> 00025 #include <QtGui/QPixmapCache> 00026 #include <QtGui/QActionEvent> 00027 00028 #include "kwordwrap.h" 00029 #include <kconfig.h> 00030 #include <kdebug.h> 00031 #include <kglobal.h> 00032 #include <kiconeffect.h> 00033 #include <kglobalsettings.h> 00034 00035 #include <kcursor.h> 00036 #include <QApplication> 00037 00038 class K3IconView::K3IconViewPrivate 00039 { 00040 public: 00041 K3IconViewPrivate() { 00042 mode = K3IconView::Execute; 00043 fm = 0L; 00044 doAutoSelect = true; 00045 textHeight = 0; 00046 dragHoldItem = 0L; 00047 } 00048 K3IconView::Mode mode; 00049 bool doAutoSelect; 00050 QFontMetrics *fm; 00051 QPixmapCache maskCache; 00052 int textHeight; 00053 Q3IconViewItem *dragHoldItem; 00054 QTimer dragHoldTimer; 00055 QTimer doubleClickIgnoreTimer; 00056 }; 00057 00058 K3IconView::K3IconView( QWidget *parent, const char *name, Qt::WFlags f ) 00059 : Q3IconView( parent, name, f ) 00060 { 00061 d = new K3IconViewPrivate; 00062 00063 connect( this, SIGNAL( onViewport() ), 00064 this, SLOT( slotOnViewport() ) ); 00065 connect( this, SIGNAL( onItem( Q3IconViewItem * ) ), 00066 this, SLOT( slotOnItem( Q3IconViewItem * ) ) ); 00067 slotSettingsChanged( KGlobalSettings::SETTINGS_MOUSE ); 00068 connect( KGlobalSettings::self(), SIGNAL( settingsChanged(int) ), SLOT( slotSettingsChanged(int) ) ); 00069 00070 m_pCurrentItem = 0L; 00071 00072 m_pAutoSelect = new QTimer( this ); 00073 connect( m_pAutoSelect, SIGNAL( timeout() ), 00074 this, SLOT( slotAutoSelect() ) ); 00075 00076 connect( &d->dragHoldTimer, SIGNAL(timeout()), this, SLOT(slotDragHoldTimeout()) ); 00077 } 00078 00079 K3IconView::~K3IconView() 00080 { 00081 delete d->fm; 00082 delete d; 00083 } 00084 00085 00086 void K3IconView::setMode( K3IconView::Mode mode ) 00087 { 00088 d->mode = mode; 00089 } 00090 00091 K3IconView::Mode K3IconView::mode() const 00092 { 00093 return d->mode; 00094 } 00095 00096 void K3IconView::slotOnItem( Q3IconViewItem *item ) 00097 { 00098 if ( item ) { 00099 if ( m_bUseSingle ) { 00100 if ( m_bChangeCursorOverItem ) 00101 viewport()->setCursor(QCursor(Qt::PointingHandCursor)); 00102 00103 if ( (m_autoSelectDelay > -1) ) { 00104 m_pAutoSelect->setSingleShot( true ); 00105 m_pAutoSelect->start( m_autoSelectDelay ); 00106 } 00107 } 00108 m_pCurrentItem = item; 00109 } 00110 } 00111 00112 void K3IconView::slotOnViewport() 00113 { 00114 if ( m_bUseSingle && m_bChangeCursorOverItem ) 00115 viewport()->unsetCursor(); 00116 00117 m_pAutoSelect->stop(); 00118 m_pCurrentItem = 0L; 00119 } 00120 00121 void K3IconView::slotSettingsChanged(int category) 00122 { 00123 if ( category != KGlobalSettings::SETTINGS_MOUSE ) 00124 return; 00125 m_bUseSingle = KGlobalSettings::singleClick(); 00126 //kDebug() << "K3IconView::slotSettingsChanged for mouse, usesingle=" << m_bUseSingle; 00127 00128 disconnect( this, SIGNAL( mouseButtonClicked( int, Q3IconViewItem *, 00129 const QPoint & ) ), 00130 this, SLOT( slotMouseButtonClicked( int, Q3IconViewItem *, 00131 const QPoint & ) ) ); 00132 // disconnect( this, SIGNAL( doubleClicked( QIconViewItem *, 00133 // const QPoint & ) ), 00134 // this, SLOT( slotExecute( QIconViewItem *, 00135 // const QPoint & ) ) ); 00136 00137 if( m_bUseSingle ) { 00138 connect( this, SIGNAL( mouseButtonClicked( int, Q3IconViewItem *, 00139 const QPoint & ) ), 00140 this, SLOT( slotMouseButtonClicked( int, Q3IconViewItem *, 00141 const QPoint & ) ) ); 00142 } 00143 else { 00144 // connect( this, SIGNAL( doubleClicked( QIconViewItem *, 00145 // const QPoint & ) ), 00146 // this, SLOT( slotExecute( QIconViewItem *, 00147 // const QPoint & ) ) ); 00148 } 00149 00150 m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon(); 00151 m_autoSelectDelay = m_bUseSingle ? KGlobalSettings::autoSelectDelay() : -1; 00152 00153 if( !m_bUseSingle || !m_bChangeCursorOverItem ) 00154 viewport()->unsetCursor(); 00155 } 00156 00157 void K3IconView::slotAutoSelect() 00158 { 00159 // check that the item still exists 00160 if( index( m_pCurrentItem ) == -1 || !d->doAutoSelect ) 00161 return; 00162 00163 //Give this widget the keyboard focus. 00164 if( !hasFocus() ) 00165 setFocus(); 00166 00167 Qt::KeyboardModifiers keybstate = QApplication::keyboardModifiers(); 00168 Q3IconViewItem* previousItem = currentItem(); 00169 setCurrentItem( m_pCurrentItem ); 00170 00171 if( m_pCurrentItem ) { 00172 //Shift pressed? 00173 if( (keybstate & Qt::ShiftModifier) ) { 00174 //Temporary implementation of the selection until QIconView supports it 00175 bool block = signalsBlocked(); 00176 blockSignals( true ); 00177 00178 //No Ctrl? Then clear before! 00179 if( !(keybstate & Qt::ControlModifier) ) 00180 clearSelection(); 00181 00182 bool select = !m_pCurrentItem->isSelected(); 00183 bool update = viewport()->updatesEnabled(); 00184 viewport()->setUpdatesEnabled( false ); 00185 00186 //Calculate the smallest rectangle that contains the current Item 00187 //and the one that got the autoselect event 00188 QRect r; 00189 QRect redraw; 00190 if ( previousItem ) { 00191 r = QRect( qMin( previousItem->x(), m_pCurrentItem->x() ), 00192 qMin( previousItem->y(), m_pCurrentItem->y() ), 00193 0, 0 ); 00194 if ( previousItem->x() < m_pCurrentItem->x() ) 00195 r.setWidth( m_pCurrentItem->x() - previousItem->x() + m_pCurrentItem->width() ); 00196 else 00197 r.setWidth( previousItem->x() - m_pCurrentItem->x() + previousItem->width() ); 00198 if ( previousItem->y() < m_pCurrentItem->y() ) 00199 r.setHeight( m_pCurrentItem->y() - previousItem->y() + m_pCurrentItem->height() ); 00200 else 00201 r.setHeight( previousItem->y() - m_pCurrentItem->y() + previousItem->height() ); 00202 r = r.normalized(); 00203 } 00204 else 00205 r = QRect( 0, 0, 0, 0 ); 00206 00207 //Check for each item whether it is within the rectangle. 00208 //If yes, select it 00209 for( Q3IconViewItem* i = firstItem(); i; i = i->nextItem() ) { 00210 if( i->intersects( r ) ) { 00211 redraw = redraw.unite( i->rect() ); 00212 setSelected( i, select, true ); 00213 } 00214 } 00215 00216 blockSignals( block ); 00217 viewport()->setUpdatesEnabled( update ); 00218 repaintContents( redraw, false ); 00219 00220 emit selectionChanged(); 00221 00222 if( selectionMode() == Q3IconView::Single ) 00223 emit selectionChanged( m_pCurrentItem ); 00224 00225 //setSelected( m_pCurrentItem, true, (keybstate & ControlButton), (keybstate & ShiftButton) ); 00226 } 00227 else if( (keybstate & Qt::ControlModifier) ) 00228 setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected(), true ); 00229 else 00230 setSelected( m_pCurrentItem, true ); 00231 } 00232 else 00233 kDebug() << "K3IconView: That's not supposed to happen!!!!"; 00234 } 00235 00236 void K3IconView::emitExecute( Q3IconViewItem *item, const QPoint &pos ) 00237 { 00238 if ( d->mode != Execute ) 00239 { 00240 // kDebug() << "K3IconView::emitExecute : not in execute mode !"; 00241 return; 00242 } 00243 00244 Qt::KeyboardModifiers keybstate = QApplication::keyboardModifiers(); 00245 00246 m_pAutoSelect->stop(); 00247 00248 //Don't emit executed if in SC mode and Shift or Ctrl are pressed 00249 if( !( m_bUseSingle && ((keybstate & Qt::ShiftModifier) || (keybstate & Qt::ControlModifier)) ) ) { 00250 setSelected( item, false ); 00251 viewport()->unsetCursor(); 00252 emit executed( item ); 00253 emit executed( item, pos ); 00254 } 00255 } 00256 00257 void K3IconView::updateDragHoldItem( QDropEvent *e ) 00258 { 00259 Q3IconViewItem *item = findItem( e->pos() ); 00260 00261 if ( d->dragHoldItem != item) 00262 { 00263 d->dragHoldItem = item; 00264 if( item ) 00265 { 00266 d->dragHoldTimer.setSingleShot( true ); 00267 d->dragHoldTimer.start( 1000 ); 00268 } 00269 else 00270 { 00271 d->dragHoldTimer.stop(); 00272 } 00273 } 00274 } 00275 00276 void K3IconView::focusOutEvent( QFocusEvent *fe ) 00277 { 00278 m_pAutoSelect->stop(); 00279 00280 Q3IconView::focusOutEvent( fe ); 00281 } 00282 00283 void K3IconView::leaveEvent( QEvent *e ) 00284 { 00285 m_pAutoSelect->stop(); 00286 00287 Q3IconView::leaveEvent( e ); 00288 } 00289 00290 void K3IconView::contentsMousePressEvent( QMouseEvent *e ) 00291 { 00292 if( (selectionMode() == Extended) && (e->modifiers() & Qt::ShiftModifier) && !(e->modifiers() & Qt::ControlModifier) ) { 00293 bool block = signalsBlocked(); 00294 blockSignals( true ); 00295 00296 clearSelection(); 00297 00298 blockSignals( block ); 00299 } 00300 00301 Q3IconView::contentsMousePressEvent( e ); 00302 d->doAutoSelect = false; 00303 } 00304 00305 void K3IconView::contentsMouseDoubleClickEvent ( QMouseEvent * e ) 00306 { 00307 Q3IconView::contentsMouseDoubleClickEvent( e ); 00308 00309 Q3IconViewItem* item = findItem( e->pos() ); 00310 00311 if( item ) { 00312 if( (e->button() == Qt::LeftButton) && !m_bUseSingle ) 00313 emitExecute( item, e->globalPos() ); 00314 00315 emit doubleClicked( item, e->globalPos() ); 00316 } 00317 d->doubleClickIgnoreTimer.setSingleShot(true); 00318 d->doubleClickIgnoreTimer.start(0); 00319 } 00320 00321 void K3IconView::slotMouseButtonClicked( int btn, Q3IconViewItem *item, const QPoint &pos ) 00322 { 00323 //kDebug() << " K3IconView::slotMouseButtonClicked() item=" << item; 00324 if( d->doubleClickIgnoreTimer.isActive() ) 00325 return; // Ignore double click 00326 00327 if( (btn == Qt::LeftButton) && item ) 00328 emitExecute( item, pos ); 00329 } 00330 00331 void K3IconView::contentsMouseReleaseEvent( QMouseEvent *e ) 00332 { 00333 d->doAutoSelect = true; 00334 Q3IconView::contentsMouseReleaseEvent( e ); 00335 } 00336 00337 void K3IconView::contentsDragEnterEvent( QDragEnterEvent *e ) 00338 { 00339 updateDragHoldItem( e ); 00340 Q3IconView::contentsDragEnterEvent( e ); 00341 } 00342 00343 void K3IconView::contentsDragLeaveEvent( QDragLeaveEvent *e ) 00344 { 00345 d->dragHoldTimer.stop(); 00346 d->dragHoldItem = 0L; 00347 Q3IconView::contentsDragLeaveEvent( e ); 00348 } 00349 00350 00351 void K3IconView::contentsDragMoveEvent( QDragMoveEvent *e ) 00352 { 00353 updateDragHoldItem( e ); 00354 Q3IconView::contentsDragMoveEvent( e ); 00355 } 00356 00357 void K3IconView::contentsDropEvent( QDropEvent* e ) 00358 { 00359 d->dragHoldTimer.stop(); 00360 Q3IconView::contentsDropEvent( e ); 00361 } 00362 00363 void K3IconView::slotDragHoldTimeout() 00364 { 00365 Q3IconViewItem *tmp = d->dragHoldItem; 00366 d->dragHoldItem = 0L; 00367 00368 emit held( tmp ); 00369 } 00370 00371 void K3IconView::takeItem( Q3IconViewItem * item ) 00372 { 00373 if ( item == d->dragHoldItem ) 00374 { 00375 d->dragHoldTimer.stop(); 00376 d->dragHoldItem = 0L; 00377 } 00378 00379 Q3IconView::takeItem( item ); 00380 } 00381 00382 void K3IconView::cancelPendingHeldSignal() 00383 { 00384 d->dragHoldTimer.stop(); 00385 d->dragHoldItem = 0L; 00386 } 00387 00388 void K3IconView::wheelEvent( QWheelEvent *e ) 00389 { 00390 if (horizontalScrollBar() && (arrangement() == Q3IconView::TopToBottom)) { 00391 QWheelEvent ce(e->pos(), e->delta(), e->buttons(), e->modifiers(), Qt::Horizontal); 00392 QApplication::sendEvent( horizontalScrollBar(), &ce); 00393 if (ce.isAccepted()) { 00394 e->accept(); 00395 return; 00396 } 00397 } 00398 Q3IconView::wheelEvent(e); 00399 } 00400 00401 void K3IconView::setFont( const QFont &font ) 00402 { 00403 delete d->fm; 00404 d->fm = 0L; 00405 Q3IconView::setFont( font ); 00406 } 00407 00408 QFontMetrics *K3IconView::itemFontMetrics() const 00409 { 00410 if (!d->fm) { 00411 // QIconView creates one too, but we can't access it 00412 d->fm = new QFontMetrics( font() ); 00413 } 00414 return d->fm; 00415 } 00416 00417 QPixmap K3IconView::selectedIconPixmap( QPixmap *pix, const QColor &col ) const 00418 { 00419 QPixmap m; 00420 if ( d->maskCache.find( QString::number( pix->serialNumber() ), m ) ) 00421 return m; 00422 m = *pix; 00423 { 00424 QPainter p(&m); 00425 QColor h = col; 00426 h.setAlphaF(0.5); 00427 p.setCompositionMode(QPainter::CompositionMode_SourceAtop); 00428 p.fillRect(m.rect(), h); 00429 p.end(); 00430 } 00431 d->maskCache.insert( QString::number( pix->serialNumber() ), m ); 00432 return m; 00433 } 00434 00435 int K3IconView::iconTextHeight() const 00436 { 00437 return d->textHeight > 0 ? d->textHeight : ( wordWrapIconText() ? 99 : 1 ); 00438 } 00439 00440 void K3IconView::setIconTextHeight( int n ) 00441 { 00442 int oldHeight = iconTextHeight(); 00443 if ( n > 1 ) 00444 d->textHeight = n; 00445 else 00446 d->textHeight = 1; 00447 00448 // so that Qt still shows the tooltip when even a wrapped text is too long 00449 setWordWrapIconText( false ); 00450 00451 // update view if needed 00452 if ( iconTextHeight() != oldHeight ) 00453 setFont( font() ); // hack to recalc items 00454 } 00455 00457 00458 struct K3IconViewItem::K3IconViewItemPrivate 00459 { 00460 QSize m_pixmapSize; 00461 }; 00462 00463 void K3IconViewItem::init() 00464 { 00465 m_wordWrap = 0L; 00466 d = 0L; 00467 calcRect(); 00468 } 00469 00470 K3IconViewItem::~K3IconViewItem() 00471 { 00472 delete m_wordWrap; 00473 delete d; 00474 } 00475 00476 void K3IconViewItem::calcRect( const QString& text_ ) 00477 { 00478 Q_ASSERT( iconView() ); 00479 if ( !iconView() ) 00480 return; 00481 delete m_wordWrap; 00482 m_wordWrap = 0L; 00483 #ifndef NDEBUG // be faster for the end-user, such a bug will have been fixed before hand :) 00484 if ( !qobject_cast<K3IconView*>(iconView()) ) 00485 { 00486 kWarning() << "K3IconViewItem used in a " << iconView()->metaObject()->className() << " !!"; 00487 return; 00488 } 00489 #endif 00490 //kDebug() << "K3IconViewItem::calcRect - " << text(); 00491 K3IconView *view = static_cast<K3IconView *>(iconView()); 00492 QRect itemIconRect = pixmapRect(); 00493 QRect itemTextRect = textRect(); 00494 QRect itemRect = rect(); 00495 00496 int pw = 0; 00497 int ph = 0; 00498 00499 #ifndef QT_NO_PICTURE 00500 if ( picture() ) { 00501 QRect br = picture()->boundingRect(); 00502 pw = br.width() + 2; 00503 ph = br.height() + 2; 00504 } else 00505 #endif 00506 { 00507 // Qt uses unknown_icon if no pixmap. Let's see if we need that - I doubt it 00508 if (!pixmap()) 00509 return; 00510 pw = pixmap()->width() + 2; 00511 ph = pixmap()->height() + 2; 00512 } 00513 itemIconRect.setWidth( pw ); 00514 #if 1 // FIXME 00515 // There is a bug in Qt which prevents the item from being placed 00516 // properly when the pixmapRect is not at the top of the itemRect, so we 00517 // have to increase the height of the pixmapRect and leave it at the top 00518 // of the itemRect... 00519 if ( d && !d->m_pixmapSize.isNull() ) 00520 itemIconRect.setHeight( d->m_pixmapSize.height() + 2 ); 00521 else 00522 #endif 00523 itemIconRect.setHeight( ph ); 00524 00525 int tw = 0; 00526 if ( d && !d->m_pixmapSize.isNull() ) 00527 tw = view->maxItemWidth() - ( view->itemTextPos() == Q3IconView::Bottom ? 0 : 00528 d->m_pixmapSize.width() + 2 ); 00529 else 00530 tw = view->maxItemWidth() - ( view->itemTextPos() == Q3IconView::Bottom ? 0 : 00531 itemIconRect.width() ); 00532 00533 QFontMetrics *fm = view->itemFontMetrics(); 00534 QString t; 00535 QRect r; 00536 00537 // When is text_ set ? Doesn't look like it's ever set. 00538 t = text_.isEmpty() ? text() : text_; 00539 00540 // Max text height 00541 int nbLines = static_cast<K3IconView*>( iconView() )->iconTextHeight(); 00542 int height = nbLines > 0 ? fm->height() * nbLines : 0xFFFFFFFF; 00543 00544 // Should not be higher than pixmap if text is alongside icons 00545 if ( view->itemTextPos() != Q3IconView::Bottom ) { 00546 if ( d && !d->m_pixmapSize.isNull() ) 00547 height = qMin( d->m_pixmapSize.height() + 2, height ); 00548 else 00549 height = qMin( itemIconRect.height(), height ); 00550 height = qMax( height, fm->height() ); 00551 } 00552 00553 // Calculate the word-wrap 00554 QRect outerRect( 0, 0, tw - 6, height ); 00555 m_wordWrap = KWordWrap::formatText( *fm, outerRect, 0, t ); 00556 r = m_wordWrap->boundingRect(); 00557 00558 int realWidth = qMax( qMin( r.width() + 4, tw ), fm->width( "X" ) ); 00559 itemTextRect.setWidth( realWidth ); 00560 itemTextRect.setHeight( r.height() ); 00561 00562 int w = 0; int h = 0; int y = 0; 00563 if ( view->itemTextPos() == Q3IconView::Bottom ) { 00564 // If the pixmap size has been specified, use it 00565 if ( d && !d->m_pixmapSize.isNull() ) 00566 { 00567 w = qMax( itemTextRect.width(), d->m_pixmapSize.width() + 2 ); 00568 h = itemTextRect.height() + d->m_pixmapSize.height() + 2 + 1; 00569 #if 0 // FIXME 00570 // Waiting for the qt bug to be solved, the pixmapRect must 00571 // stay on the top... 00572 y = d->m_pixmapSize.height() + 2 - itemIconRect.height(); 00573 #endif 00574 } 00575 else { 00576 w = qMax( itemTextRect.width(), itemIconRect.width() ); 00577 h = itemTextRect.height() + itemIconRect.height() + 1; 00578 } 00579 00580 itemRect.setWidth( w ); 00581 itemRect.setHeight( h ); 00582 int width = qMax( w, QApplication::globalStrut().width() ); // see QIconViewItem::width() 00583 int height = qMax( h, QApplication::globalStrut().height() ); // see QIconViewItem::height() 00584 itemTextRect = QRect( ( width - itemTextRect.width() ) / 2, height - itemTextRect.height(), 00585 itemTextRect.width(), itemTextRect.height() ); 00586 itemIconRect = QRect( ( width - itemIconRect.width() ) / 2, y, 00587 itemIconRect.width(), itemIconRect.height() ); 00588 } else { 00589 // If the pixmap size has been specified, use it 00590 if ( d && !d->m_pixmapSize.isNull() ) 00591 { 00592 h = qMax( itemTextRect.height(), d->m_pixmapSize.height() + 2 ); 00593 #if 0 // FIXME 00594 // Waiting for the qt bug to be solved, the pixmapRect must 00595 // stay on the top... 00596 y = ( d->m_pixmapSize.height() + 2 - itemIconRect.height() ) / 2; 00597 #endif 00598 } 00599 else 00600 h = qMax( itemTextRect.height(), itemIconRect.height() ); 00601 w = itemTextRect.width() + itemIconRect.width() + 1; 00602 00603 itemRect.setWidth( w ); 00604 itemRect.setHeight( h ); 00605 int width = qMax( w, QApplication::globalStrut().width() ); // see QIconViewItem::width() 00606 int height = qMax( h, QApplication::globalStrut().height() ); // see QIconViewItem::height() 00607 00608 itemTextRect = QRect( width - itemTextRect.width(), ( height - itemTextRect.height() ) / 2, 00609 itemTextRect.width(), itemTextRect.height() ); 00610 if ( itemIconRect.height() > itemTextRect.height() ) // icon bigger than text -> center vertically 00611 itemIconRect = QRect( 0, ( height - itemIconRect.height() ) / 2, 00612 itemIconRect.width(), itemIconRect.height() ); 00613 else // icon smaller than text -> place in top or center with first line 00614 itemIconRect = QRect( 0, qMax(( fm->height() - itemIconRect.height() ) / 2 + y, 0), 00615 itemIconRect.width(), itemIconRect.height() ); 00616 if ( ( itemIconRect.height() <= 20 ) && ( itemTextRect.height() < itemIconRect.height() ) ) 00617 { 00618 itemTextRect.setHeight( itemIconRect.height() - 2 ); 00619 itemTextRect.setY( itemIconRect.y() ); 00620 } 00621 } 00622 00623 if ( itemIconRect != pixmapRect() ) 00624 setPixmapRect( itemIconRect ); 00625 if ( itemTextRect != textRect() ) 00626 setTextRect( itemTextRect ); 00627 if ( itemRect != rect() ) 00628 setItemRect( itemRect ); 00629 00630 // Done by setPixmapRect, setTextRect and setItemRect ! [and useless if no rect changed] 00631 //view->updateItemContainer( this ); 00632 00633 } 00634 00635 void K3IconViewItem::paintItem( QPainter *p, const QColorGroup &cg ) 00636 { 00637 Q3IconView* view = iconView(); 00638 Q_ASSERT( view ); 00639 if ( !view ) 00640 return; 00641 #ifndef NDEBUG // be faster for the end-user, such a bug will have been fixed before hand :) 00642 if ( !qobject_cast<K3IconView*>(view) ) 00643 { 00644 kWarning() << "K3IconViewItem used in a " << view->metaObject()->className() << " !!"; 00645 return; 00646 } 00647 #endif 00648 00649 p->save(); 00650 00651 paintPixmap(p, cg); 00652 paintText(p, cg); 00653 00654 p->restore(); 00655 } 00656 00657 KWordWrap * K3IconViewItem::wordWrap() 00658 { 00659 return m_wordWrap; 00660 } 00661 00662 void K3IconViewItem::paintPixmap( QPainter *p, const QColorGroup &cg ) 00663 { 00664 K3IconView *kview = static_cast<K3IconView *>(iconView()); 00665 00666 #ifndef QT_NO_PICTURE 00667 if ( picture() ) { 00668 QPicture *pic = picture(); 00669 if ( isSelected() ) { 00670 // TODO something as nice as selectedIconPixmap if possible ;) 00671 p->fillRect( pixmapRect( false ), QBrush( cg.color(QPalette::Highlight), Qt::Dense4Pattern) ); 00672 } 00673 p->drawPicture( x()-pic->boundingRect().x(), y()-pic->boundingRect().y(), *pic ); 00674 } else 00675 #endif 00676 { 00677 int iconX = pixmapRect( false ).x(); 00678 int iconY = pixmapRect( false ).y(); 00679 00680 QPixmap *pix = pixmap(); 00681 if ( !pix || pix->isNull() ) 00682 return; 00683 00684 #if 1 // FIXME 00685 // Move the pixmap manually because the pixmapRect is at the 00686 // top of the itemRect 00687 // (won't be needed anymore in future versions of qt) 00688 if ( d && !d->m_pixmapSize.isNull() ) 00689 { 00690 int offset = 0; 00691 if ( kview->itemTextPos() == Q3IconView::Bottom ) 00692 offset = d->m_pixmapSize.height() - pix->height(); 00693 else 00694 offset = ( d->m_pixmapSize.height() - pix->height() ) / 2; 00695 if ( offset > 0 ) 00696 iconY += offset; 00697 } 00698 #endif 00699 if ( isSelected() ) { 00700 QPixmap selectedPix = kview->selectedIconPixmap( pix, cg.color( QPalette::Highlight ) ); 00701 p->drawPixmap( iconX, iconY, selectedPix ); 00702 } else { 00703 p->drawPixmap( iconX, iconY, *pix ); 00704 } 00705 } 00706 } 00707 00708 void K3IconViewItem::paintText( QPainter *p, const QColorGroup &cg ) 00709 { 00710 int textX = textRect( false ).x() + 2; 00711 int textY = textRect( false ).y(); 00712 00713 if ( isSelected() ) { 00714 p->fillRect( textRect( false ), cg.color( QPalette::Highlight ) ); 00715 p->setPen( QPen( cg.color( QPalette::HighlightedText ) ) ); 00716 } else { 00717 if ( iconView()->itemTextBackground() != Qt::NoBrush ) 00718 p->fillRect( textRect( false ), iconView()->itemTextBackground() ); 00719 p->setPen( cg.color( QPalette::Text ) ); 00720 } 00721 00722 int align = iconView()->itemTextPos() == Q3IconView::Bottom ? Qt::AlignHCenter : Qt::AlignLeft; 00723 m_wordWrap->drawText( p, textX, textY, align | KWordWrap::Truncate ); 00724 } 00725 00726 QSize K3IconViewItem::pixmapSize() const 00727 { 00728 return d ? d->m_pixmapSize : QSize( 0, 0 ); 00729 } 00730 00731 void K3IconViewItem::setPixmapSize( const QSize& size ) 00732 { 00733 if ( !d ) 00734 d = new K3IconViewItemPrivate; 00735 00736 d->m_pixmapSize = size; 00737 } 00738 00739 #include "k3iconview.moc"
KDE 4.6 API Reference