libkonq Library API Documentation

kfileivi.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1999, 2000, 2001, 2002 David Faure <faure@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 as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kfileivi.h"
00021 #include "kivdirectoryoverlay.h"
00022 #include "konq_iconviewwidget.h"
00023 #include "konq_operations.h"
00024 
00025 #include <qpainter.h>
00026 
00027 #include <kurldrag.h>
00028 #include <kiconeffect.h>
00029 #include <kfileitem.h>
00030 #include <kdebug.h>
00031 
00032 #undef Bool
00033 
00037 struct KFileIVI::Private
00038 {
00039     QIconSet icons; // Icon states (cached to prevent re-applying icon effects
00040                     // every time)
00041     QPixmap  thumb; // Raw unprocessed thumbnail
00042     QString m_animatedIcon; // Name of animation
00043     bool m_animated;        // Animation currently running ?
00044         KIVDirectoryOverlay* m_directoryOverlay;
00045         QPixmap m_overlay;
00046         QString m_overlayName;
00047 };
00048 
00049 KFileIVI::KFileIVI( KonqIconViewWidget *iconview, KFileItem* fileitem, int size )
00050     : KIconViewItem( iconview, fileitem->text(),
00051                      fileitem->pixmap( size, KIcon::DefaultState ) ),
00052     m_size( size ), m_state( KIcon::DefaultState ),
00053     m_bDisabled( false ), m_bThumbnail( false ), m_fileitem( fileitem )
00054 {
00055     setDropEnabled( S_ISDIR( m_fileitem->mode() ) );
00056     d = new KFileIVI::Private;
00057 
00058     // Cache entry for the icon effects
00059     d->icons.reset( *pixmap(), QIconSet::Large );
00060     d->m_animated = false;
00061 
00062     // iconName() requires the mimetype to be known
00063     if ( fileitem->isMimeTypeKnown() )
00064     {
00065         QString icon = fileitem->iconName();
00066         if ( !icon.isEmpty() )
00067             setMouseOverAnimation( icon );
00068         else
00069             setMouseOverAnimation( "unknown" );
00070     }
00071     d->m_directoryOverlay = 0;
00072 }
00073 
00074 KFileIVI::~KFileIVI()
00075 {
00076     delete d->m_directoryOverlay;
00077     delete d;
00078 }
00079 
00080 void KFileIVI::invalidateThumb( int state, bool redraw )
00081 {
00082     QIconSet::Mode mode;
00083     switch( state )
00084     {
00085         case KIcon::DisabledState:
00086             mode = QIconSet::Disabled;
00087             break;
00088         case KIcon::ActiveState:
00089             mode = QIconSet::Active;
00090             break;
00091         case KIcon::DefaultState:
00092         default:
00093             mode = QIconSet::Normal;
00094             break;
00095     }
00096     d->icons = QIconSet();
00097     d->icons.setPixmap( KGlobal::iconLoader()->iconEffect()->
00098                         apply( d->thumb, KIcon::Desktop, state ),
00099                         QIconSet::Large, mode );
00100     m_state = state;
00101 
00102     QIconViewItem::setPixmap( d->icons.pixmap( QIconSet::Large, mode ),
00103                               false, redraw );
00104 }
00105 
00106 void KFileIVI::setIcon( int size, int state, bool recalc, bool redraw )
00107 {
00108     m_size = size;
00109     m_bThumbnail = false;
00110     if ( m_bDisabled )
00111       m_state = KIcon::DisabledState;
00112     else
00113       m_state = state;
00114     
00115     if ( d->m_overlayName.isNull() )
00116         d->m_overlay = QPixmap();
00117     else {
00118         int halfSize;
00119         if (m_size == 0) {
00120             halfSize = IconSize(KIcon::Desktop) / 2;
00121         } else {
00122             halfSize = m_size / 2;
00123         }
00124         d->m_overlay = DesktopIcon(d->m_overlayName, halfSize);
00125     }
00126       
00127     setPixmapDirect(m_fileitem->pixmap( m_size, m_state ) , recalc, redraw );
00128 }
00129 
00130 void KFileIVI::setOverlay( const QString& iconName )
00131 {
00132     d->m_overlayName = iconName;
00133    
00134     refreshIcon(true);
00135 }
00136 
00137 KIVDirectoryOverlay* KFileIVI::setShowDirectoryOverlay( bool show )
00138 {
00139     if ( !m_fileitem->isDir() || m_fileitem->iconName() != "folder" ) return 0;
00140 
00141         if (show) {
00142         if (!d->m_directoryOverlay) d->m_directoryOverlay = new KIVDirectoryOverlay(this);
00143         return d->m_directoryOverlay;
00144         } else {
00145         if (d->m_directoryOverlay) {
00146                     delete d->m_directoryOverlay;
00147             d->m_directoryOverlay = 0;
00148                 }
00149         setOverlay(QString());
00150         return 0;
00151         }
00152 }
00153 
00154 bool KFileIVI::showDirectoryOverlay(  )
00155 {
00156     return (bool)d->m_directoryOverlay;
00157 }
00158 
00159 void KFileIVI::setPixmapDirect( const QPixmap& pixmap, bool recalc, bool redraw )
00160 {
00161     QIconSet::Mode mode;
00162     switch( m_state )
00163     {
00164         case KIcon::DisabledState:
00165             mode = QIconSet::Disabled;
00166             break;
00167         case KIcon::ActiveState:
00168             mode = QIconSet::Active;
00169             break;
00170         case KIcon::DefaultState:
00171         default:
00172             mode = QIconSet::Normal;
00173             break;
00174     }
00175 
00176     // We cannot just reset() the iconset here, because setIcon can be
00177     // called with any state and not just normal state. So we just
00178     // create a dummy empty iconset as base object.
00179     d->icons = QIconSet();
00180     d->icons.setPixmap( pixmap, QIconSet::Large, mode );
00181     QIconViewItem::setPixmap( d->icons.pixmap( QIconSet::Large, mode ),
00182                               recalc, redraw );
00183 }
00184 
00185 void KFileIVI::setDisabled( bool disabled )
00186 {
00187     if ( m_bDisabled != disabled && !isThumbnail() )
00188     {
00189         m_bDisabled = disabled;
00190         bool active = ( m_state == KIcon::ActiveState );
00191         m_state = m_bDisabled ? KIcon::DisabledState : ( active ? KIcon::ActiveState : KIcon::DefaultState );
00192         QIconViewItem::setPixmap( m_fileitem->pixmap( m_size, m_state ), false, true );
00193     }
00194 }
00195 
00196 void KFileIVI::setThumbnailPixmap( const QPixmap & pixmap )
00197 {
00198     m_bThumbnail = true;
00199     d->thumb = pixmap;
00200     // QIconSet::reset() doesn't seem to clear the other generated pixmaps,
00201     // so we just create a blank QIconSet here
00202     d->icons = QIconSet();
00203     d->icons.setPixmap( KGlobal::iconLoader()->iconEffect()->
00204                     apply( pixmap, KIcon::Desktop, KIcon::DefaultState ),
00205                     QIconSet::Large, QIconSet::Normal );
00206 
00207     m_state = KIcon::DefaultState;
00208 
00209     // Recalc when setting this pixmap!
00210     QIconViewItem::setPixmap( d->icons.pixmap( QIconSet::Large,
00211                               QIconSet::Normal ), true );
00212 }
00213 
00214 void KFileIVI::setActive( bool active )
00215 {
00216     if ( active )
00217         setEffect( KIcon::ActiveState );
00218     else
00219         setEffect( m_bDisabled ? KIcon::DisabledState : KIcon::DefaultState );
00220 }
00221 
00222 void KFileIVI::setEffect( int state )
00223 {
00224     QIconSet::Mode mode;
00225     switch( state )
00226     {
00227         case KIcon::DisabledState:
00228             mode = QIconSet::Disabled;
00229             break;
00230         case KIcon::ActiveState:
00231             mode = QIconSet::Active;
00232             break;
00233         case KIcon::DefaultState:
00234         default:
00235             mode = QIconSet::Normal;
00236             break;
00237     }
00238     // Do not update if the fingerprint is identical (prevents flicker)!
00239 
00240     KIconEffect *effect = KGlobal::iconLoader()->iconEffect();
00241 
00242     bool haveEffect = effect->hasEffect( KIcon::Desktop, m_state ) !=
00243                       effect->hasEffect( KIcon::Desktop, state );
00244 
00245                 //kdDebug(1203) << "desktop;defaultstate=" <<
00246                 //      effect->fingerprint(KIcon::Desktop, KIcon::DefaultState) <<
00247                 //      endl;
00248                 //kdDebug(1203) << "desktop;activestate=" <<
00249                 //      effect->fingerprint(KIcon::Desktop, KIcon::ActiveState) <<
00250                 //      endl;
00251 
00252     if( haveEffect &&
00253         effect->fingerprint( KIcon::Desktop, m_state ) !=
00254         effect->fingerprint( KIcon::Desktop, state ) )
00255     {
00256         // Effects on are not applied until they are first accessed to
00257         // save memory. Do this now when needed
00258         if( m_bThumbnail )
00259         {
00260             if( d->icons.isGenerated( QIconSet::Large, mode ) )
00261                 d->icons.setPixmap( effect->apply( d->thumb, KIcon::Desktop, state ),
00262                                     QIconSet::Large, mode );
00263         }
00264         else
00265         {
00266             if( d->icons.isGenerated( QIconSet::Large, mode ) )
00267                 d->icons.setPixmap( m_fileitem->pixmap( m_size, state ),
00268                                     QIconSet::Large, mode );
00269         }
00270         QIconViewItem::setPixmap( d->icons.pixmap( QIconSet::Large, mode ) );
00271     }
00272     m_state = state;
00273 }
00274 
00275 void KFileIVI::refreshIcon( bool redraw )
00276 {
00277     if (!isThumbnail())
00278         setIcon( m_size, m_state, true, redraw );
00279 }
00280 
00281 void KFileIVI::invalidateThumbnail()
00282 {
00283     d->thumb = QPixmap();
00284 }
00285 
00286 bool KFileIVI::isThumbnailInvalid() const
00287 {
00288     return d->thumb.isNull();
00289 }
00290 
00291 bool KFileIVI::acceptDrop( const QMimeSource *mime ) const
00292 {
00293     if ( mime->provides( "text/uri-list" ) ) // We're dragging URLs
00294     {
00295         if ( m_fileitem->acceptsDrops() ) // Directory, executables, ...
00296             return true;
00297         
00298         // Use cache
00299         KURL::List uris = ( static_cast<KonqIconViewWidget*>(iconView()) )->dragURLs();
00300 
00301         // Check if we want to drop something on itself
00302         // (Nothing will happen, but it's a convenient way to move icons)
00303         KURL::List::Iterator it = uris.begin();
00304         for ( ; it != uris.end() ; it++ )
00305         {
00306             if ( m_fileitem->url().cmp( *it, true /*ignore trailing slashes*/ ) )
00307                 return true;
00308         }
00309     }
00310     return QIconViewItem::acceptDrop( mime );
00311 }
00312 
00313 void KFileIVI::setKey( const QString &key )
00314 {
00315     QString theKey = key;
00316 
00317     QVariant sortDirProp = iconView()->property( "sortDirectoriesFirst" );
00318 
00319     if ( S_ISDIR( m_fileitem->mode() ) && ( !sortDirProp.isValid() || ( sortDirProp.type() == QVariant::Bool && sortDirProp.toBool() ) ) )
00320       theKey.prepend( iconView()->sortDirection() ? '0' : '1' );
00321     else
00322       theKey.prepend( iconView()->sortDirection() ? '1' : '0' );
00323 
00324     QIconViewItem::setKey( theKey );
00325 }
00326 
00327 void KFileIVI::dropped( QDropEvent *e, const QValueList<QIconDragItem> & )
00328 {
00329     KonqOperations::doDrop( item(), item()->url(), e, iconView() );
00330 }
00331 
00332 void KFileIVI::returnPressed()
00333 {
00334     m_fileitem->run();
00335 }
00336 
00337 void KFileIVI::paintItem( QPainter *p, const QColorGroup &c )
00338 {
00339     QColorGroup cg( c );
00340     cg.setColor( QColorGroup::Text, static_cast<KonqIconViewWidget*>(iconView())->itemColor() );
00341     if ( m_fileitem->isLink() )
00342     {
00343         QFont f( p->font() );
00344         f.setItalic( TRUE );
00345         p->setFont( f );
00346     }
00347 
00348     //*** TEMPORARY CODE - MUST BE MADE CONFIGURABLE FIRST - Martijn
00349     // SET UNDERLINE ON HOVER ONLY
00350     /*if ( ( ( KonqIconViewWidget* ) iconView() )->m_pActiveItem == this )
00351     {
00352         QFont f( p->font() );
00353         f.setUnderline( TRUE );
00354         p->setFont( f );
00355     }*/
00356 
00357     KIconViewItem::paintItem( p, cg );
00358 
00359     if ( !d->m_overlay.isNull() ) {
00360         QRect rect = pixmapRect(true);
00361         p->drawPixmap(x() + rect.x() , y() + pixmapRect().height() - d->m_overlay.height(), d->m_overlay);
00362     }
00363 }
00364 
00365 bool KFileIVI::move( int x, int y )
00366 {
00367     if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
00368         if ( x < 5 )
00369             x = 5;
00370         if ( x > iconView()->viewport()->width() - ( width() + 5 ) )
00371             x = iconView()->viewport()->width() - ( width() + 5 );
00372         if ( y < 5 )
00373             y = 5;
00374         if ( y > iconView()->viewport()->height() - ( height() + 5 ) )
00375             y = iconView()->viewport()->height() - ( height() + 5 );
00376     }
00377     return QIconViewItem::move( x, y );
00378 }
00379 
00380 bool KFileIVI::hasAnimation() const
00381 {
00382     return !d->m_animatedIcon.isEmpty() && !m_bThumbnail;
00383 }
00384 
00385 void KFileIVI::setMouseOverAnimation( const QString& movieFileName )
00386 {
00387     if ( !movieFileName.isEmpty() )
00388     {
00389         //kdDebug(1203) << "KIconViewItem::setMouseOverAnimation " << movieFileName << endl;
00390         d->m_animatedIcon = movieFileName;
00391     }
00392 }
00393 
00394 QString KFileIVI::mouseOverAnimation() const
00395 {
00396     return d->m_animatedIcon;
00397 }
00398 
00399 bool KFileIVI::isAnimated() const
00400 {
00401     return d->m_animated;
00402 }
00403 
00404 void KFileIVI::setAnimated( bool a )
00405 {
00406     d->m_animated = a;
00407 }
00408 
00409 /* vim: set noet sw=4 ts=8 softtabstop=4: */
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Thu Jan 29 23:03:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001