• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libkonq
 

libkonq

  • libkonq
kfileivi.cc
1 /* This file is part of the KDE project
2  Copyright (C) 1999, 2000, 2001, 2002 David Faure <faure@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kfileivi.h"
21 #include "kivdirectoryoverlay.h"
22 #include "konq_iconviewwidget.h"
23 #include "konq_operations.h"
24 #include "konq_settings.h"
25 
26 #include <tqpainter.h>
27 
28 #include <kurldrag.h>
29 #include <kiconeffect.h>
30 #include <kfileitem.h>
31 #include <kdebug.h>
32 #include <krun.h>
33 
34 #undef Bool
35 
39 struct KFileIVI::Private
40 {
41  TQIconSet icons; // Icon states (cached to prevent re-applying icon effects
42  // every time)
43  TQPixmap thumb; // Raw unprocessed thumbnail
44  TQString m_animatedIcon; // Name of animation
45  bool m_animated; // Animation currently running ?
46  KIVDirectoryOverlay* m_directoryOverlay;
47  TQPixmap m_overlay;
48  TQString m_overlayName;
49 };
50 
51 KFileIVI::KFileIVI( KonqIconViewWidget *iconview, KFileItem* fileitem, int size )
52  : KIconViewItem( iconview, fileitem->text() ),
53  m_size( size ), m_state( KIcon::DefaultState ),
54  m_bDisabled( false ), m_bThumbnail( false ), m_fileitem( fileitem )
55 {
56  d = new KFileIVI::Private;
57 
58  updatePixmapSize();
59  setPixmap( m_fileitem->pixmap( m_size, m_state ) );
60  setDropEnabled( S_ISDIR( m_fileitem->mode() ) );
61 
62  // Cache entry for the icon effects
63  d->icons.reset( *pixmap(), TQIconSet::Large );
64  d->m_animated = false;
65 
66  // iconName() requires the mimetype to be known
67  if ( fileitem->isMimeTypeKnown() )
68  {
69  TQString icon = fileitem->iconName();
70  if ( !icon.isEmpty() )
71  setMouseOverAnimation( icon );
72  else
73  setMouseOverAnimation( "unknown" );
74  }
75  d->m_directoryOverlay = 0;
76 }
77 
78 KFileIVI::~KFileIVI()
79 {
80  delete d->m_directoryOverlay;
81  delete d;
82 }
83 
84 void KFileIVI::invalidateThumb( int state, bool redraw )
85 {
86  TQIconSet::Mode mode;
87  switch( state )
88  {
89  case KIcon::DisabledState:
90  mode = TQIconSet::Disabled;
91  break;
92  case KIcon::ActiveState:
93  mode = TQIconSet::Active;
94  break;
95  case KIcon::DefaultState:
96  default:
97  mode = TQIconSet::Normal;
98  break;
99  }
100  d->icons = TQIconSet();
101  d->icons.setPixmap( KGlobal::iconLoader()->iconEffect()->
102  apply( d->thumb, KIcon::Desktop, state ),
103  TQIconSet::Large, mode );
104  m_state = state;
105 
106  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
107  false, redraw );
108 }
109 
110 void KFileIVI::setIcon( int size, int state, bool recalc, bool redraw )
111 {
112  m_size = size;
113  m_bThumbnail = false;
114  if ( m_bDisabled )
115  m_state = KIcon::DisabledState;
116  else
117  m_state = state;
118 
119  if ( d->m_overlayName.isNull() )
120  d->m_overlay = TQPixmap();
121  else {
122  int halfSize;
123  if (m_size == 0) {
124  halfSize = IconSize(KIcon::Desktop) / 2;
125  } else {
126  halfSize = m_size / 2;
127  }
128  d->m_overlay = DesktopIcon(d->m_overlayName, halfSize);
129  }
130 
131  setPixmapDirect(m_fileitem->pixmap( m_size, m_state ) , recalc, redraw );
132 }
133 
134 void KFileIVI::setOverlay( const TQString& iconName )
135 {
136  d->m_overlayName = iconName;
137 
138  refreshIcon(true);
139 }
140 
141 KIVDirectoryOverlay* KFileIVI::setShowDirectoryOverlay( bool show )
142 {
143  if ( !m_fileitem->isDir() || m_fileitem->iconName() != "folder" )
144  return 0;
145 
146  if (show) {
147  if (!d->m_directoryOverlay)
148  d->m_directoryOverlay = new KIVDirectoryOverlay(this);
149  return d->m_directoryOverlay;
150  } else {
151  delete d->m_directoryOverlay;
152  d->m_directoryOverlay = 0;
153  setOverlay(TQString());
154  return 0;
155  }
156 }
157 
158 bool KFileIVI::showDirectoryOverlay( )
159 {
160  return (bool)d->m_directoryOverlay;
161 }
162 
163 void KFileIVI::setPixmapDirect( const TQPixmap& pixmap, bool recalc, bool redraw )
164 {
165  TQIconSet::Mode mode;
166  switch( m_state )
167  {
168  case KIcon::DisabledState:
169  mode = TQIconSet::Disabled;
170  break;
171  case KIcon::ActiveState:
172  mode = TQIconSet::Active;
173  break;
174  case KIcon::DefaultState:
175  default:
176  mode = TQIconSet::Normal;
177  break;
178  }
179 
180  // We cannot just reset() the iconset here, because setIcon can be
181  // called with any state and not just normal state. So we just
182  // create a dummy empty iconset as base object.
183  d->icons = TQIconSet();
184  d->icons.setPixmap( pixmap, TQIconSet::Large, mode );
185 
186  updatePixmapSize();
187  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
188  recalc, redraw );
189 }
190 
191 void KFileIVI::setDisabled( bool disabled )
192 {
193  if ( m_bDisabled != disabled )
194  {
195  m_bDisabled = disabled;
196  bool active = ( m_state == KIcon::ActiveState );
197  setEffect( m_bDisabled ? KIcon::DisabledState :
198  ( active ? KIcon::ActiveState : KIcon::DefaultState ) );
199  }
200 }
201 
202 void KFileIVI::setThumbnailPixmap( const TQPixmap & pixmap )
203 {
204  m_bThumbnail = true;
205  d->thumb = pixmap;
206  // TQIconSet::reset() doesn't seem to clear the other generated pixmaps,
207  // so we just create a blank TQIconSet here
208  d->icons = TQIconSet();
209  d->icons.setPixmap( KGlobal::iconLoader()->iconEffect()->
210  apply( pixmap, KIcon::Desktop, KIcon::DefaultState ),
211  TQIconSet::Large, TQIconSet::Normal );
212 
213  m_state = KIcon::DefaultState;
214 
215  // Recalc when setting this pixmap!
216  updatePixmapSize();
217  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large,
218  TQIconSet::Normal ), true );
219 }
220 
221 void KFileIVI::setActive( bool active )
222 {
223  if ( active )
224  setEffect( KIcon::ActiveState );
225  else
226  setEffect( m_bDisabled ? KIcon::DisabledState : KIcon::DefaultState );
227 }
228 
229 void KFileIVI::setEffect( int state )
230 {
231  TQIconSet::Mode mode;
232  switch( state )
233  {
234  case KIcon::DisabledState:
235  mode = TQIconSet::Disabled;
236  break;
237  case KIcon::ActiveState:
238  mode = TQIconSet::Active;
239  break;
240  case KIcon::DefaultState:
241  default:
242  mode = TQIconSet::Normal;
243  break;
244  }
245  // Do not update if the fingerprint is identical (prevents flicker)!
246 
247  KIconEffect *effect = KGlobal::iconLoader()->iconEffect();
248 
249  bool haveEffect = effect->hasEffect( KIcon::Desktop, m_state ) !=
250  effect->hasEffect( KIcon::Desktop, state );
251 
252  //kdDebug(1203) << "desktop;defaultstate=" <<
253  // effect->fingerprint(KIcon::Desktop, KIcon::DefaultState) <<
254  // endl;
255  //kdDebug(1203) << "desktop;activestate=" <<
256  // effect->fingerprint(KIcon::Desktop, KIcon::ActiveState) <<
257  // endl;
258 
259  if( haveEffect &&
260  effect->fingerprint( KIcon::Desktop, m_state ) !=
261  effect->fingerprint( KIcon::Desktop, state ) )
262  {
263  // Effects on are not applied until they are first accessed to
264  // save memory. Do this now when needed
265  if( m_bThumbnail )
266  {
267  if( d->icons.isGenerated( TQIconSet::Large, mode ) )
268  d->icons.setPixmap( effect->apply( d->thumb, KIcon::Desktop, state ),
269  TQIconSet::Large, mode );
270  }
271  else
272  {
273  if( d->icons.isGenerated( TQIconSet::Large, mode ) )
274  d->icons.setPixmap( m_fileitem->pixmap( m_size, state ),
275  TQIconSet::Large, mode );
276  }
277  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ) );
278  }
279  m_state = state;
280 }
281 
282 void KFileIVI::refreshIcon( bool redraw )
283 {
284  if (!isThumbnail())
285  setIcon( m_size, m_state, true, redraw );
286 }
287 
288 void KFileIVI::invalidateThumbnail()
289 {
290  d->thumb = TQPixmap();
291 }
292 
293 bool KFileIVI::isThumbnailInvalid() const
294 {
295  return d->thumb.isNull();
296 }
297 
298 bool KFileIVI::acceptDrop( const TQMimeSource *mime ) const
299 {
300  if ( mime->provides( "text/uri-list" ) ) // We're dragging URLs
301  {
302  if ( m_fileitem->acceptsDrops() ) // Directory, executables, ...
303  return true;
304 
305  // Use cache
306  KURL::List uris = ( static_cast<KonqIconViewWidget*>(iconView()) )->dragURLs();
307 
308  // Check if we want to drop something on itself
309  // (Nothing will happen, but it's a convenient way to move icons)
310  KURL::List::Iterator it = uris.begin();
311  for ( ; it != uris.end() ; it++ )
312  {
313  if ( m_fileitem->url().equals( *it, true /*ignore trailing slashes*/ ) )
314  return true;
315  }
316  }
317  return TQIconViewItem::acceptDrop( mime );
318 }
319 
320 void KFileIVI::setKey( const TQString &key )
321 {
322  TQString theKey = key;
323 
324  TQVariant sortDirProp = iconView()->property( "sortDirectoriesFirst" );
325 
326  bool isdir = ( S_ISDIR( m_fileitem->mode() ) && ( !sortDirProp.isValid() || ( sortDirProp.type() == TQVariant::Bool && sortDirProp.toBool() ) ) );
327 
328  // The order is: .dir (0), dir (1), .file (2), file (3)
329  int sortChar = isdir ? 1 : 3;
330  if ( m_fileitem->text()[0] == '.' )
331  --sortChar;
332 
333  if ( !iconView()->sortDirection() ) // reverse sorting
334  sortChar = 3 - sortChar;
335 
336  theKey.prepend( TQChar( sortChar + '0' ) );
337 
338  TQIconViewItem::setKey( theKey );
339 }
340 
341 void KFileIVI::dropped( TQDropEvent *e, const TQValueList<TQIconDragItem> & )
342 {
343  KonqOperations::doDrop( item(), item()->url(), e, iconView() );
344 }
345 
346 void KFileIVI::returnPressed()
347 {
348  if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
349  KURL url = m_fileitem->url();
350  // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
351  // Symlink resolution must only happen on the desktop though (#63014)
352  if ( m_fileitem->isLink() && url.isLocalFile() )
353  url = KURL( url, m_fileitem->linkDest() );
354 
355  (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
356  } else {
357  m_fileitem->run();
358  }
359 }
360 
361 
362 void KFileIVI::paintItem( TQPainter *p, const TQColorGroup &c )
363 {
364  TQColorGroup cg = updateColors(c);
365  paintFontUpdate( p );
366 
367  //*** TEMPORARY CODE - MUST BE MADE CONFIGURABLE FIRST - Martijn
368  // SET UNDERLINE ON HOVER ONLY
369  /*if ( ( ( KonqIconViewWidget* ) iconView() )->m_pActiveItem == this )
370  {
371  TQFont f( p->font() );
372  f.setUnderline( TRUE );
373  p->setFont( f );
374  }*/
375 
376  KIconViewItem::paintItem( p, cg );
377  paintOverlay(p);
378 
379 }
380 
381 void KFileIVI::paintOverlay( TQPainter *p ) const
382 {
383  if ( !d->m_overlay.isNull() ) {
384  TQRect rect = pixmapRect(true);
385  p->drawPixmap(x() + rect.x() , y() + pixmapRect().height() - d->m_overlay.height(), d->m_overlay);
386  }
387 }
388 
389 void KFileIVI::paintFontUpdate( TQPainter *p ) const
390 {
391  if ( m_fileitem->isLink() )
392  {
393  TQFont f( p->font() );
394  f.setItalic( TRUE );
395  p->setFont( f );
396  }
397 }
398 
399 TQColorGroup KFileIVI::updateColors( const TQColorGroup &c ) const
400 {
401  TQColorGroup cg( c );
402  cg.setColor( TQColorGroup::Text, static_cast<KonqIconViewWidget*>(iconView())->itemColor() );
403  return cg;
404 }
405 
406 bool KFileIVI::move( int x, int y )
407 {
408  if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
409  if ( x < 5 )
410  x = 5;
411  if ( x > iconView()->viewport()->width() - ( width() + 5 ) )
412  x = iconView()->viewport()->width() - ( width() + 5 );
413  if ( y < 5 )
414  y = 5;
415  if ( y > iconView()->viewport()->height() - ( height() + 5 ) )
416  y = iconView()->viewport()->height() - ( height() + 5 );
417  }
418  return TQIconViewItem::move( x, y );
419 }
420 
421 bool KFileIVI::hasAnimation() const
422 {
423  return !d->m_animatedIcon.isEmpty() && !m_bThumbnail;
424 }
425 
426 void KFileIVI::setMouseOverAnimation( const TQString& movieFileName )
427 {
428  if ( !movieFileName.isEmpty() )
429  {
430  //kdDebug(1203) << "KIconViewItem::setMouseOverAnimation " << movieFileName << endl;
431  d->m_animatedIcon = movieFileName;
432  }
433 }
434 
435 TQString KFileIVI::mouseOverAnimation() const
436 {
437  return d->m_animatedIcon;
438 }
439 
440 bool KFileIVI::isAnimated() const
441 {
442  return d->m_animated;
443 }
444 
445 void KFileIVI::setAnimated( bool a )
446 {
447  d->m_animated = a;
448 }
449 
450 int KFileIVI::compare( TQIconViewItem *i ) const
451 {
452  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>(iconView());
453  if ( view->caseInsensitiveSort() )
454  return key().localeAwareCompare( i->key() );
455  else
456  return view->m_pSettings->caseSensitiveCompare( key(), i->key() );
457 }
458 
459 void KFileIVI::updatePixmapSize()
460 {
461  int size = m_size ? m_size :
462  KGlobal::iconLoader()->currentSize( KIcon::Desktop );
463 
464  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>( iconView() );
465 
466  if ( view && view->canPreview( item() ) ) {
467  int previewSize = view->previewIconSize( size );
468  setPixmapSize( TQSize( previewSize, previewSize ) );
469  }
470  else {
471  TQSize pixSize = TQSize( size, size );
472  if ( pixSize != pixmapSize() )
473  setPixmapSize( pixSize );
474  }
475 }
476 
477 /* vim: set noet sw=4 ts=8 softtabstop=4: */

libkonq

Skip menu "libkonq"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libkonq

Skip menu "libkonq"
  • kate
  • kwin
  •   lib
  • libkonq
Generated for libkonq by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |