kio Library API Documentation

kfileiconview.cpp

00001 // -*- c++ -*- 00002 /* This file is part of the KDE libraries 00003 Copyright (C) 1997 Stephan Kulow <coolo@kde.org> 00004 2000,2001,2002 Carsten Pfeiffer <pfeiffer@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <qfontmetrics.h> 00023 #include <qkeycode.h> 00024 #include <qlabel.h> 00025 #include <qpainter.h> 00026 #include <qpixmap.h> 00027 #include <qregexp.h> 00028 #include <qtimer.h> 00029 #include <qtooltip.h> 00030 00031 #include <kaction.h> 00032 #include <kapplication.h> 00033 #include <klocale.h> 00034 #include <kfileitem.h> 00035 #include <kiconeffect.h> 00036 #include <kglobalsettings.h> 00037 #include <kurldrag.h> 00038 #include <kio/previewjob.h> 00039 00040 #include "kfileiconview.h" 00041 #include "config-kfile.h" 00042 00043 #define DEFAULT_PREVIEW_SIZE 60 00044 #define DEFAULT_SHOW_PREVIEWS false 00045 #define DEFAULT_VIEW_MODE "SmallColumns" 00046 00047 KFileIconViewItem::~KFileIconViewItem() 00048 { 00049 fileInfo()->removeExtraData( iconView() ); 00050 } 00051 00052 class KFileIconView::KFileIconViewPrivate 00053 { 00054 public: 00055 KFileIconViewPrivate( KFileIconView *parent ) { 00056 previewIconSize = 60; 00057 job = 0; 00058 dropItem = 0; 00059 00060 noArrangement = false; 00061 ignoreMaximumSize = false; 00062 smallColumns = new KRadioAction( i18n("Small Icons"), 0, parent, 00063 SLOT( slotSmallColumns() ), 00064 parent->actionCollection(), 00065 "small columns" ); 00066 00067 largeRows = new KRadioAction( i18n("Large Icons"), 0, parent, 00068 SLOT( slotLargeRows() ), 00069 parent->actionCollection(), 00070 "large rows" ); 00071 00072 smallColumns->setExclusiveGroup(QString::fromLatin1("IconView mode")); 00073 largeRows->setExclusiveGroup(QString::fromLatin1("IconView mode")); 00074 00075 previews = new KToggleAction( i18n("Thumbnail Previews"), 0, 00076 parent->actionCollection(), 00077 "show previews" ); 00078 zoomIn = KStdAction::zoomIn( parent, SLOT( zoomIn() ), 00079 parent->actionCollection(), "zoomIn" ); 00080 zoomOut = KStdAction::zoomOut( parent, SLOT( zoomOut() ), 00081 parent->actionCollection(), "zoomOut" ); 00082 00083 previews->setGroup("previews"); 00084 zoomIn->setGroup("previews"); 00085 zoomOut->setGroup("previews"); 00086 00087 connect( previews, SIGNAL( toggled( bool )), 00088 parent, SLOT( slotPreviewsToggled( bool ))); 00089 00090 connect( &previewTimer, SIGNAL( timeout() ), 00091 parent, SLOT( showPreviews() )); 00092 connect( &autoOpenTimer, SIGNAL( timeout() ), 00093 parent, SLOT( slotAutoOpen() )); 00094 } 00095 00096 ~KFileIconViewPrivate() { 00097 if ( job ) 00098 job->kill(); 00099 } 00100 00101 KRadioAction *smallColumns, *largeRows; 00102 KAction *zoomIn, *zoomOut; 00103 KToggleAction *previews; 00104 KIO::PreviewJob *job; 00105 KFileIconViewItem *dropItem; 00106 QTimer previewTimer; 00107 QTimer autoOpenTimer; 00108 QStringList previewMimeTypes; 00109 int previewIconSize; 00110 bool noArrangement :1; 00111 bool ignoreMaximumSize :1; 00112 }; 00113 00114 KFileIconView::KFileIconView(QWidget *parent, const char *name) 00115 : KIconView(parent, name), KFileView() 00116 { 00117 d = new KFileIconViewPrivate( this ); 00118 00119 setViewName( i18n("Icon View") ); 00120 00121 toolTip = 0; 00122 setResizeMode( Adjust ); 00123 setMaxItemWidth( 300 ); 00124 setWordWrapIconText( false ); 00125 setArrangement( TopToBottom ); 00126 setAutoArrange( true ); 00127 setItemsMovable( false ); 00128 setMode( KIconView::Select ); 00129 KIconView::setSorting( true ); 00130 // as long as QIconView only shows tooltips when the cursor is over the 00131 // icon (and not the text), we have to create our own tooltips 00132 setShowToolTips( false ); 00133 //slotSmallColumns(); 00134 //d->smallColumns->setChecked( true ); 00135 slotPreviewsToggled( true ); 00136 00137 connect( this, SIGNAL( returnPressed(QIconViewItem *) ), 00138 SLOT( slotActivate( QIconViewItem *) ) ); 00139 00140 // we want single click _and_ double click (as convenience) 00141 connect( this, SIGNAL( clicked(QIconViewItem *, const QPoint&) ), 00142 SLOT( selected( QIconViewItem *) ) ); 00143 connect( this, SIGNAL( doubleClicked(QIconViewItem *, const QPoint&) ), 00144 SLOT( slotActivate( QIconViewItem *) ) ); 00145 00146 connect( this, SIGNAL( onItem( QIconViewItem * ) ), 00147 SLOT( showToolTip( QIconViewItem * ) ) ); 00148 connect( this, SIGNAL( onViewport() ), 00149 SLOT( removeToolTip() ) ); 00150 connect( this, SIGNAL( contextMenuRequested(QIconViewItem*,const QPoint&)), 00151 SLOT( slotActivateMenu( QIconViewItem*, const QPoint& ) ) ); 00152 00153 KFile::SelectionMode sm = KFileView::selectionMode(); 00154 switch ( sm ) { 00155 case KFile::Multi: 00156 QIconView::setSelectionMode( QIconView::Multi ); 00157 break; 00158 case KFile::Extended: 00159 QIconView::setSelectionMode( QIconView::Extended ); 00160 break; 00161 case KFile::NoSelection: 00162 QIconView::setSelectionMode( QIconView::NoSelection ); 00163 break; 00164 default: // fall through 00165 case KFile::Single: 00166 QIconView::setSelectionMode( QIconView::Single ); 00167 break; 00168 } 00169 00170 if ( sm == KFile::Multi || sm == KFile::Extended ) 00171 connect( this, SIGNAL( selectionChanged() ), 00172 SLOT( slotSelectionChanged() )); 00173 else 00174 connect( this, SIGNAL( selectionChanged( QIconViewItem * )), 00175 SLOT( highlighted( QIconViewItem * ))); 00176 00177 viewport()->installEventFilter( this ); 00178 00179 // for mimetype resolving 00180 m_resolver = new KMimeTypeResolver<KFileIconViewItem,KFileIconView>(this); 00181 } 00182 00183 KFileIconView::~KFileIconView() 00184 { 00185 delete m_resolver; 00186 removeToolTip(); 00187 delete d; 00188 } 00189 00190 void KFileIconView::readConfig( KConfig *kc, const QString& group ) 00191 { 00192 QString gr = group.isEmpty() ? QString("KFileIconView") : group; 00193 KConfigGroupSaver cs( kc, gr ); 00194 QString small = QString::fromLatin1("SmallColumns"); 00195 d->previewIconSize = kc->readNumEntry( "Preview Size", DEFAULT_PREVIEW_SIZE ); 00196 d->previews->setChecked( kc->readBoolEntry( "ShowPreviews", DEFAULT_SHOW_PREVIEWS ) ); 00197 00198 if ( kc->readEntry("ViewMode", DEFAULT_VIEW_MODE ) == small ) { 00199 d->smallColumns->setChecked( true ); 00200 slotSmallColumns(); 00201 } 00202 else { 00203 d->largeRows->setChecked( true ); 00204 slotLargeRows(); 00205 } 00206 00207 if ( d->previews->isChecked() ) 00208 showPreviews(); 00209 } 00210 00211 void KFileIconView::writeConfig( KConfig *kc, const QString& group ) 00212 { 00213 QString gr = group.isEmpty() ? QString("KFileIconView") : group; 00214 KConfigGroupSaver cs( kc, gr ); 00215 00216 QString viewMode = d->smallColumns->isChecked() ? 00217 QString::fromLatin1("SmallColumns") : 00218 QString::fromLatin1("LargeRows"); 00219 if(!kc->hasDefault( "ViewMode" ) && viewMode == DEFAULT_VIEW_MODE ) 00220 kc->revertToDefault( "ViewMode" ); 00221 else 00222 kc->writeEntry( "ViewMode", viewMode ); 00223 00224 int previewsIconSize = d->previewIconSize; 00225 if(!kc->hasDefault( "Preview Size" ) && previewsIconSize == DEFAULT_PREVIEW_SIZE ) 00226 kc->revertToDefault( "Preview Size" ); 00227 else 00228 kc->writeEntry( "Preview Size", previewsIconSize ); 00229 00230 bool showPreviews = d->previews->isChecked(); 00231 if(!kc->hasDefault( "ShowPreviews" ) && showPreviews == DEFAULT_SHOW_PREVIEWS ) 00232 kc->revertToDefault( "ShowPreviews" ); 00233 else 00234 kc->writeEntry( "ShowPreviews", showPreviews ); 00235 } 00236 00237 void KFileIconView::removeToolTip() 00238 { 00239 delete toolTip; 00240 toolTip = 0; 00241 } 00242 00243 void KFileIconView::showToolTip( QIconViewItem *item ) 00244 { 00245 delete toolTip; 00246 toolTip = 0; 00247 00248 if ( !item ) 00249 return; 00250 00251 int w = maxItemWidth() - ( itemTextPos() == QIconView::Bottom ? 0 : 00252 item->pixmapRect().width() ) - 4; 00253 if ( fontMetrics().width( item->text() ) >= w ) { 00254 toolTip = new QLabel( QString::fromLatin1(" %1 ").arg(item->text()), 0, 00255 "myToolTip", 00256 WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM ); 00257 toolTip->setFrameStyle( QFrame::Plain | QFrame::Box ); 00258 toolTip->setLineWidth( 1 ); 00259 toolTip->setAlignment( AlignLeft | AlignTop ); 00260 toolTip->move( QCursor::pos() + QPoint( 14, 14 ) ); 00261 toolTip->adjustSize(); 00262 QRect screen = QApplication::desktop()->screenGeometry( 00263 QApplication::desktop()->screenNumber(QCursor::pos())); 00264 if (toolTip->x()+toolTip->width() > screen.right()) { 00265 toolTip->move(toolTip->x()+screen.right()-toolTip->x()-toolTip->width(), toolTip->y()); 00266 } 00267 if (toolTip->y()+toolTip->height() > screen.bottom()) { 00268 toolTip->move(toolTip->x(), screen.bottom()-toolTip->y()-toolTip->height()+toolTip->y()); 00269 } 00270 toolTip->setFont( QToolTip::font() ); 00271 toolTip->setPalette( QToolTip::palette(), true ); 00272 toolTip->show(); 00273 } 00274 } 00275 00276 void KFileIconView::slotActivateMenu( QIconViewItem* item, const QPoint& pos ) 00277 { 00278 if ( !item ) { 00279 sig->activateMenu( 0, pos ); 00280 return; 00281 } 00282 KFileIconViewItem *i = (KFileIconViewItem*) item; 00283 sig->activateMenu( i->fileInfo(), pos ); 00284 } 00285 00286 void KFileIconView::hideEvent( QHideEvent *e ) 00287 { 00288 removeToolTip(); 00289 KIconView::hideEvent( e ); 00290 } 00291 00292 void KFileIconView::keyPressEvent( QKeyEvent *e ) 00293 { 00294 KIconView::keyPressEvent( e ); 00295 00296 // ignore Ctrl-Return so that the dialog can catch it. 00297 if ( (e->state() & ControlButton) && 00298 (e->key() == Key_Return || e->key() == Key_Enter) ) 00299 e->ignore(); 00300 } 00301 00302 void KFileIconView::setSelected( const KFileItem *info, bool enable ) 00303 { 00304 KFileIconViewItem *item = viewItem( info ); 00305 if ( item ) 00306 KIconView::setSelected( item, enable, true ); 00307 } 00308 00309 void KFileIconView::selectAll() 00310 { 00311 if (KFileView::selectionMode() == KFile::NoSelection || 00312 KFileView::selectionMode() == KFile::Single) 00313 return; 00314 00315 KIconView::selectAll( true ); 00316 } 00317 00318 void KFileIconView::clearSelection() 00319 { 00320 KIconView::clearSelection(); 00321 } 00322 00323 void KFileIconView::invertSelection() 00324 { 00325 KIconView::invertSelection(); 00326 } 00327 00328 void KFileIconView::clearView() 00329 { 00330 m_resolver->m_lstPendingMimeIconItems.clear(); 00331 00332 KIconView::clear(); 00333 stopPreview(); 00334 } 00335 00336 void KFileIconView::insertItem( KFileItem *i ) 00337 { 00338 KFileView::insertItem( i ); 00339 00340 QIconView* qview = static_cast<QIconView*>( this ); 00341 // Since creating and initializing an item leads to a repaint, 00342 // we disable updates on the IconView for a while. 00343 qview->setUpdatesEnabled( false ); 00344 KFileIconViewItem *item = new KFileIconViewItem( qview, i ); 00345 initItem( item, i, true ); 00346 qview->setUpdatesEnabled( true ); 00347 00348 if ( !i->isMimeTypeKnown() ) 00349 m_resolver->m_lstPendingMimeIconItems.append( item ); 00350 00351 i->setExtraData( this, item ); 00352 } 00353 00354 void KFileIconView::slotActivate( QIconViewItem *item ) 00355 { 00356 if ( !item ) 00357 return; 00358 const KFileItem *fi = ( (KFileIconViewItem*)item )->fileInfo(); 00359 if ( fi ) 00360 sig->activate( fi ); 00361 } 00362 00363 void KFileIconView::selected( QIconViewItem *item ) 00364 { 00365 if ( !item || (KApplication::keyboardMouseState() & (ShiftButton | ControlButton)) != 0 ) 00366 return; 00367 00368 if ( KGlobalSettings::singleClick() ) { 00369 const KFileItem *fi = ( (KFileIconViewItem*)item )->fileInfo(); 00370 if ( fi && (fi->isDir() || !onlyDoubleClickSelectsFiles()) ) 00371 sig->activate( fi ); 00372 } 00373 } 00374 00375 void KFileIconView::setCurrentItem( const KFileItem *item ) 00376 { 00377 KFileIconViewItem *it = viewItem( item ); 00378 if ( it ) 00379 KIconView::setCurrentItem( it ); 00380 } 00381 00382 KFileItem * KFileIconView::currentFileItem() const 00383 { 00384 KFileIconViewItem *current = static_cast<KFileIconViewItem*>( currentItem() ); 00385 if ( current ) 00386 return current->fileInfo(); 00387 00388 return 0L; 00389 } 00390 00391 void KFileIconView::highlighted( QIconViewItem *item ) 00392 { 00393 if ( !item ) 00394 return; 00395 const KFileItem *fi = ( (KFileIconViewItem*)item )->fileInfo(); 00396 if ( fi ) 00397 sig->highlightFile( fi ); 00398 } 00399 00400 void KFileIconView::setSelectionMode( KFile::SelectionMode sm ) 00401 { 00402 disconnect( SIGNAL( selectionChanged() ), this ); 00403 disconnect( SIGNAL( selectionChanged( QIconViewItem * )), this ); 00404 00405 KFileView::setSelectionMode( sm ); 00406 switch ( KFileView::selectionMode() ) { 00407 case KFile::Multi: 00408 QIconView::setSelectionMode( QIconView::Multi ); 00409 break; 00410 case KFile::Extended: 00411 QIconView::setSelectionMode( QIconView::Extended ); 00412 break; 00413 case KFile::NoSelection: 00414 QIconView::setSelectionMode( QIconView::NoSelection ); 00415 break; 00416 default: // fall through 00417 case KFile::Single: 00418 QIconView::setSelectionMode( QIconView::Single ); 00419 break; 00420 } 00421 00422 if ( sm == KFile::Multi || sm == KFile::Extended ) 00423 connect( this, SIGNAL( selectionChanged() ), 00424 SLOT( slotSelectionChanged() )); 00425 else 00426 connect( this, SIGNAL( selectionChanged( QIconViewItem * )), 00427 SLOT( highlighted( QIconViewItem * ))); 00428 } 00429 00430 bool KFileIconView::isSelected( const KFileItem *i ) const 00431 { 00432 KFileIconViewItem *item = viewItem( i ); 00433 return (item && item->isSelected()); 00434 } 00435 00436 void KFileIconView::updateView( bool b ) 00437 { 00438 if ( !b ) 00439 return; // eh? 00440 00441 KFileIconViewItem *item = static_cast<KFileIconViewItem*>(QIconView::firstItem()); 00442 if ( item ) { 00443 do { 00444 if ( d->previews->isChecked() ) { 00445 if ( canPreview( item->fileInfo() ) ) 00446 item->setPixmapSize( QSize( d->previewIconSize, d->previewIconSize ) ); 00447 } 00448 else { 00449 // unset pixmap size (used for previews) 00450 if ( !item->pixmapSize().isNull() ) 00451 item->setPixmapSize( QSize( 0, 0 ) ); 00452 } 00453 // recalculate item parameters but avoid an in-place repaint 00454 item->setPixmap( (item->fileInfo())->pixmap( myIconSize ), true, false ); 00455 item = static_cast<KFileIconViewItem *>(item->nextItem()); 00456 } while ( item != 0L ); 00457 } 00458 } 00459 00460 void KFileIconView::updateView( const KFileItem *i ) 00461 { 00462 KFileIconViewItem *item = viewItem( i ); 00463 if ( item ) 00464 initItem( item, i, true ); 00465 } 00466 00467 void KFileIconView::removeItem( const KFileItem *i ) 00468 { 00469 if ( !i ) 00470 return; 00471 00472 if ( d->job ) 00473 d->job->removeItem( i ); 00474 00475 KFileIconViewItem *item = viewItem( i ); 00476 m_resolver->m_lstPendingMimeIconItems.remove( item ); 00477 delete item; 00478 00479 KFileView::removeItem( i ); 00480 } 00481 00482 void KFileIconView::setIconSize( int size ) 00483 { 00484 myIconSize = size; 00485 updateIcons(); 00486 } 00487 00488 void KFileIconView::setPreviewSize( int size ) 00489 { 00490 if ( size < 30 ) 00491 size = 30; // minimum 00492 00493 d->previewIconSize = size; 00494 if ( d->previews->isChecked() ) 00495 showPreviews(); 00496 } 00497 00498 void KFileIconView::setIgnoreMaximumSize(bool ignoreSize) 00499 { 00500 d->ignoreMaximumSize = ignoreSize; 00501 } 00502 00503 void KFileIconView::updateIcons() 00504 { 00505 updateView( true ); 00506 arrangeItemsInGrid(); 00507 } 00508 00509 void KFileIconView::ensureItemVisible( const KFileItem *i ) 00510 { 00511 KFileIconViewItem *item = viewItem( i ); 00512 if ( item ) 00513 KIconView::ensureItemVisible( item ); 00514 } 00515 00516 void KFileIconView::slotSelectionChanged() 00517 { 00518 sig->highlightFile( 0L ); 00519 } 00520 00521 void KFileIconView::slotSmallColumns() 00522 { 00523 // setItemTextPos(), setArrangement(), setWordWrapIconText() and 00524 // setIconSize() all call arrangeItemsInGrid() :( Prevent this. 00525 d->noArrangement = true; // stop arrangeItemsInGrid()! 00526 00527 // Make sure to uncheck previews if selected 00528 if ( d->previews->isChecked() ) 00529 { 00530 stopPreview(); 00531 d->previews->setChecked( false ); 00532 } 00533 setGridX( -1 ); 00534 setMaxItemWidth( 300 ); 00535 setItemTextPos( Right ); 00536 setArrangement( TopToBottom ); 00537 setWordWrapIconText( false ); 00538 setSpacing( 0 ); 00539 00540 d->noArrangement = false; // now we can arrange 00541 setIconSize( KIcon::SizeSmall ); 00542 } 00543 00544 void KFileIconView::slotLargeRows() 00545 { 00546 // setItemTextPos(), setArrangement(), setWordWrapIconText() and 00547 // setIconSize() all call arrangeItemsInGrid() :( Prevent this. 00548 d->noArrangement = true; // stop arrangeItemsInGrid()! 00549 00550 setGridX( KGlobal::iconLoader()->currentSize( KIcon::Desktop ) + 50 ); 00551 setItemTextPos( Bottom ); 00552 setArrangement( LeftToRight ); 00553 setWordWrapIconText( true ); 00554 setSpacing( 5 ); // default in QIconView 00555 00556 d->noArrangement = false; // now we can arrange 00557 setIconSize( KIcon::SizeMedium ); 00558 } 00559 00560 void KFileIconView::stopPreview() 00561 { 00562 if ( d->job ) { 00563 d->job->kill(); 00564 d->job = 0L; 00565 } 00566 } 00567 00568 void KFileIconView::slotPreviewsToggled( bool on ) 00569 { 00570 if ( on ) 00571 showPreviews(); 00572 else { 00573 stopPreview(); 00574 slotLargeRows(); 00575 } 00576 } 00577 00578 void KFileIconView::showPreviews() 00579 { 00580 if ( d->previewMimeTypes.isEmpty() ) 00581 d->previewMimeTypes = KIO::PreviewJob::supportedMimeTypes(); 00582 00583 stopPreview(); 00584 d->previews->setChecked( true ); 00585 00586 if ( !d->largeRows->isChecked() ) { 00587 d->largeRows->setChecked( true ); 00588 slotLargeRows(); // also sets the icon size and updates the grid 00589 } 00590 else { 00591 updateIcons(); 00592 } 00593 00594 d->job = KIO::filePreview(*items(), d->previewIconSize,d->previewIconSize); 00595 d->job->setIgnoreMaximumSize(d->ignoreMaximumSize); 00596 00597 connect( d->job, SIGNAL( result( KIO::Job * )), 00598 this, SLOT( slotPreviewResult( KIO::Job * ))); 00599 connect( d->job, SIGNAL( gotPreview( const KFileItem*, const QPixmap& )), 00600 SLOT( gotPreview( const KFileItem*, const QPixmap& ) )); 00601 // connect( d->job, SIGNAL( failed( const KFileItem* )), 00602 // this, SLOT( slotFailed( const KFileItem* ) )); 00603 } 00604 00605 void KFileIconView::slotPreviewResult( KIO::Job *job ) 00606 { 00607 if ( job == d->job ) 00608 d->job = 0L; 00609 } 00610 00611 void KFileIconView::gotPreview( const KFileItem *item, const QPixmap& pix ) 00612 { 00613 KFileIconViewItem *it = viewItem( item ); 00614 if ( it ) 00615 if( item->overlays() & KIcon::HiddenOverlay ) 00616 { 00617 QPixmap p( pix ); 00618 00619 KIconEffect::semiTransparent( p ); 00620 it->setPixmap( p ); 00621 } 00622 else 00623 it->setPixmap( pix ); 00624 } 00625 00626 bool KFileIconView::canPreview( const KFileItem *item ) const 00627 { 00628 QStringList::Iterator it = d->previewMimeTypes.begin(); 00629 QRegExp r; 00630 r.setWildcard( true ); 00631 00632 for ( ; it != d->previewMimeTypes.end(); ++it ) { 00633 QString type = *it; 00634 // the "mimetype" can be "image/*" 00635 if ( type.at( type.length() - 1 ) == '*' ) { 00636 r.setPattern( type ); 00637 if ( r.search( item->mimetype() ) != -1 ) 00638 return true; 00639 } 00640 else 00641 if ( item->mimetype() == type ) 00642 return true; 00643 } 00644 00645 return false; 00646 } 00647 00648 KFileItem * KFileIconView::firstFileItem() const 00649 { 00650 KFileIconViewItem *item = static_cast<KFileIconViewItem*>( firstItem() ); 00651 if ( item ) 00652 return item->fileInfo(); 00653 return 0L; 00654 } 00655 00656 KFileItem * KFileIconView::nextItem( const KFileItem *fileItem ) const 00657 { 00658 if ( fileItem ) { 00659 KFileIconViewItem *item = viewItem( fileItem ); 00660 if ( item && item->nextItem() ) 00661 return ((KFileIconViewItem*) item->nextItem())->fileInfo(); 00662 } 00663 return 0L; 00664 } 00665 00666 KFileItem * KFileIconView::prevItem( const KFileItem *fileItem ) const 00667 { 00668 if ( fileItem ) { 00669 KFileIconViewItem *item = viewItem( fileItem ); 00670 if ( item && item->prevItem() ) 00671 return ((KFileIconViewItem*) item->prevItem())->fileInfo(); 00672 } 00673 return 0L; 00674 } 00675 00676 void KFileIconView::setSorting( QDir::SortSpec spec ) 00677 { 00678 KFileView::setSorting( spec ); 00679 KFileItemListIterator it( *items() ); 00680 00681 KFileItem *item; 00682 00683 if ( spec & QDir::Time ) { 00684 for ( ; (item = it.current()); ++it ) 00685 // warning, time_t is often signed -> cast it 00686 viewItem(item)->setKey( sortingKey( (unsigned long)item->time( KIO::UDS_MODIFICATION_TIME ), item->isDir(), spec )); 00687 } 00688 00689 else if ( spec & QDir::Size ) { 00690 for ( ; (item = it.current()); ++it ) 00691 viewItem(item)->setKey( sortingKey( item->size(), item->isDir(), 00692 spec )); 00693 } 00694 else { // Name or Unsorted 00695 for ( ; (item = it.current()); ++it ) 00696 viewItem(item)->setKey( sortingKey( item->text(), item->isDir(), 00697 spec )); 00698 } 00699 00700 KIconView::setSorting( true, !isReversed() ); 00701 sort( !isReversed() ); 00702 } 00703 00704 // 00705 // mimetype determination on demand 00706 // 00707 void KFileIconView::mimeTypeDeterminationFinished() 00708 { 00709 // anything to do? 00710 } 00711 00712 void KFileIconView::determineIcon( KFileIconViewItem *item ) 00713 { 00714 (void) item->fileInfo()->determineMimeType(); 00715 updateView( item->fileInfo() ); 00716 } 00717 00718 void KFileIconView::listingCompleted() 00719 { 00720 arrangeItemsInGrid(); 00721 00722 // QIconView doesn't set the current item automatically, so we have to do 00723 // that. We don't want to emit selectionChanged() tho. 00724 if ( !currentItem() ) { 00725 bool block = signalsBlocked(); 00726 blockSignals( true ); 00727 QIconViewItem *item = viewItem( firstFileItem() ); 00728 KIconView::setCurrentItem( item ); 00729 KIconView::setSelected( item, false ); 00730 blockSignals( block ); 00731 } 00732 00733 m_resolver->start( d->previews->isChecked() ? 0 : 10 ); 00734 } 00735 00736 // need to remove our tooltip, eventually 00737 bool KFileIconView::eventFilter( QObject *o, QEvent *e ) 00738 { 00739 if ( o == viewport() || o == this ) { 00740 int type = e->type(); 00741 if ( type == QEvent::Leave || 00742 type == QEvent::FocusOut ) 00743 removeToolTip(); 00744 } 00745 00746 return KIconView::eventFilter( o, e ); 00747 } 00748 00750 00751 // ### workaround for Qt3 Bug 00752 void KFileIconView::showEvent( QShowEvent *e ) 00753 { 00754 KIconView::showEvent( e ); 00755 } 00756 00757 00758 void KFileIconView::initItem( KFileIconViewItem *item, const KFileItem *i, 00759 bool updateTextAndPixmap ) 00760 { 00761 if ( d->previews->isChecked() && canPreview( i ) ) 00762 item->setPixmapSize( QSize( d->previewIconSize, d->previewIconSize ) ); 00763 00764 if ( updateTextAndPixmap ) 00765 { 00766 // this causes a repaint of the item, which we want to avoid during 00767 // directory listing, when all items are created. We want to paint all 00768 // items at once, not every single item in that case. 00769 item->setText( i->text() , false, false ); 00770 item->setPixmap( i->pixmap( myIconSize ) ); 00771 } 00772 00773 // see also setSorting() 00774 QDir::SortSpec spec = KFileView::sorting(); 00775 00776 if ( spec & QDir::Time ) 00777 // warning, time_t is often signed -> cast it 00778 item->setKey( sortingKey( (unsigned long) i->time( KIO::UDS_MODIFICATION_TIME ), 00779 i->isDir(), spec )); 00780 else if ( spec & QDir::Size ) 00781 item->setKey( sortingKey( i->size(), i->isDir(), spec )); 00782 00783 else // Name or Unsorted 00784 item->setKey( sortingKey( i->text(), i->isDir(), spec )); 00785 00786 //qDebug("** key for: %s: %s", i->text().latin1(), item->key().latin1()); 00787 00788 if ( d->previews->isChecked() ) 00789 d->previewTimer.start( 10, true ); 00790 } 00791 00792 void KFileIconView::arrangeItemsInGrid( bool update ) 00793 { 00794 if ( d->noArrangement ) 00795 return; 00796 00797 KIconView::arrangeItemsInGrid( update ); 00798 } 00799 00800 void KFileIconView::zoomIn() 00801 { 00802 setPreviewSize( d->previewIconSize + 30 ); 00803 } 00804 00805 void KFileIconView::zoomOut() 00806 { 00807 setPreviewSize( d->previewIconSize - 30 ); 00808 } 00809 00810 QDragObject *KFileIconView::dragObject() 00811 { 00812 // create a list of the URL:s that we want to drag 00813 KURL::List urls; 00814 KFileItemListIterator it( * KFileView::selectedItems() ); 00815 for ( ; it.current(); ++it ){ 00816 urls.append( (*it)->url() ); 00817 } 00818 QPixmap pixmap; 00819 if( urls.count() > 1 ) 00820 pixmap = DesktopIcon( "kmultiple", iconSize() ); 00821 if( pixmap.isNull() ) 00822 pixmap = currentFileItem()->pixmap( iconSize() ); 00823 00824 QPoint hotspot; 00825 hotspot.setX( pixmap.width() / 2 ); 00826 hotspot.setY( pixmap.height() / 2 ); 00827 QDragObject* myDragObject = new KURLDrag( urls, widget() ); 00828 myDragObject->setPixmap( pixmap, hotspot ); 00829 return myDragObject; 00830 } 00831 00832 void KFileIconView::slotAutoOpen() 00833 { 00834 d->autoOpenTimer.stop(); 00835 if( !d->dropItem ) 00836 return; 00837 00838 KFileItem *fileItem = d->dropItem->fileInfo(); 00839 if (!fileItem) 00840 return; 00841 00842 if( fileItem->isFile() ) 00843 return; 00844 00845 if ( fileItem->isDir() || fileItem->isLink()) 00846 sig->activate( fileItem ); 00847 } 00848 00849 bool KFileIconView::acceptDrag(QDropEvent* e) const 00850 { 00851 return KURLDrag::canDecode( e ) && 00852 (e->source()!=const_cast<KFileIconView*>(this)) && 00853 ( e->action() == QDropEvent::Copy 00854 || e->action() == QDropEvent::Move 00855 || e->action() == QDropEvent::Link ); 00856 } 00857 00858 void KFileIconView::contentsDragEnterEvent( QDragEnterEvent *e ) 00859 { 00860 if ( ! acceptDrag( e ) ) { // can we decode this ? 00861 e->ignore(); // No 00862 return; 00863 } 00864 e->acceptAction(); // Yes 00865 00866 if ((dropOptions() & AutoOpenDirs) == 0) 00867 return; 00868 00869 KFileIconViewItem *item = dynamic_cast<KFileIconViewItem*>(findItem( contentsToViewport( e->pos() ) )); 00870 if ( item ) { // are we over an item ? 00871 d->dropItem = item; 00872 d->autoOpenTimer.start( autoOpenDelay() ); // restart timer 00873 } 00874 else 00875 { 00876 d->dropItem = 0; 00877 d->autoOpenTimer.stop(); 00878 } 00879 } 00880 00881 void KFileIconView::contentsDragMoveEvent( QDragMoveEvent *e ) 00882 { 00883 if ( ! acceptDrag( e ) ) { // can we decode this ? 00884 e->ignore(); // No 00885 return; 00886 } 00887 e->acceptAction(); // Yes 00888 00889 if ((dropOptions() & AutoOpenDirs) == 0) 00890 return; 00891 00892 KFileIconViewItem *item = dynamic_cast<KFileIconViewItem*>(findItem( contentsToViewport( e->pos() ) )); 00893 if ( item ) { // are we over an item ? 00894 if (d->dropItem != item) 00895 { 00896 d->dropItem = item; 00897 d->autoOpenTimer.start( autoOpenDelay() ); // restart timer 00898 } 00899 } 00900 else 00901 { 00902 d->dropItem = 0; 00903 d->autoOpenTimer.stop(); 00904 } 00905 } 00906 00907 void KFileIconView::contentsDragLeaveEvent( QDragLeaveEvent * ) 00908 { 00909 d->dropItem = 0; 00910 d->autoOpenTimer.stop(); 00911 } 00912 00913 void KFileIconView::contentsDropEvent( QDropEvent *e ) 00914 { 00915 d->dropItem = 0; 00916 d->autoOpenTimer.stop(); 00917 00918 if ( ! acceptDrag( e ) ) { // can we decode this ? 00919 e->ignore(); // No 00920 return; 00921 } 00922 e->acceptAction(); // Yes 00923 00924 KFileIconViewItem *item = dynamic_cast<KFileIconViewItem*>(findItem( contentsToViewport( e->pos() ) )); 00925 KFileItem * fileItem = 0; 00926 if (item) 00927 fileItem = item->fileInfo(); 00928 00929 emit dropped(e, fileItem); 00930 00931 KURL::List urls; 00932 if (KURLDrag::decode( e, urls ) && !urls.isEmpty()) 00933 { 00934 emit dropped(e, urls, fileItem ? fileItem->url() : KURL()); 00935 sig->dropURLs(fileItem, e, urls); 00936 } 00937 } 00938 00939 void KFileIconView::virtual_hook( int id, void* data ) 00940 { KIconView::virtual_hook( id, data ); 00941 KFileView::virtual_hook( id, data ); } 00942 00943 #include "kfileiconview.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 14 00:20:24 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003