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

libkonq

  • libkonq
tdefileivi.cpp
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 "tdefileivi.h"
21 #include "kivdirectoryoverlay.h"
22 #include "kivfreespaceoverlay.h"
23 #include "konq_iconviewwidget.h"
24 #include "konq_operations.h"
25 #include "konq_settings.h"
26 
27 #include <tqpainter.h>
28 
29 #include <kurldrag.h>
30 #include <kiconeffect.h>
31 #include <tdefileitem.h>
32 #include <kdebug.h>
33 #include <krun.h>
34 #include <kservice.h>
35 
36 #undef Bool
37 
41 struct KFileIVI::Private
42 {
43  TQIconSet icons; // Icon states (cached to prevent re-applying icon effects
44  // every time)
45  TQPixmap thumb; // Raw unprocessed thumbnail
46  TQString m_animatedIcon; // Name of animation
47  bool m_animated; // Animation currently running ?
48  KIVDirectoryOverlay* m_directoryOverlay;
49  KIVFreeSpaceOverlay* m_freeSpaceOverlay;
50  TQPixmap m_overlay;
51  TQString m_overlayName;
52  int m_progress;
53 };
54 
55 KFileIVI::KFileIVI( KonqIconViewWidget *iconview, KFileItem* fileitem, int size )
56  : TDEIconViewItem( iconview, fileitem->text() ),
57  m_size( size ), m_state( TDEIcon::DefaultState ),
58  m_bDisabled( false ), m_bThumbnail( false ), m_fileitem( fileitem )
59 {
60  d = new KFileIVI::Private;
61 
62  updatePixmapSize();
63  setPixmap( m_fileitem->pixmap( m_size, m_state ) );
64  setDropEnabled( S_ISDIR( m_fileitem->mode() ) );
65 
66  // Cache entry for the icon effects
67  d->icons.reset( *pixmap(), TQIconSet::Large );
68  d->m_animated = false;
69 
70  // iconName() requires the mimetype to be known
71  if ( fileitem->isMimeTypeKnown() )
72  {
73  TQString icon = fileitem->iconName();
74  if ( !icon.isEmpty() )
75  setMouseOverAnimation( icon );
76  else
77  setMouseOverAnimation( "unknown" );
78  }
79  d->m_progress = -1;
80  d->m_directoryOverlay = 0;
81  d->m_freeSpaceOverlay = 0;
82 }
83 
84 KFileIVI::~KFileIVI()
85 {
86  delete d->m_directoryOverlay;
87  delete d->m_freeSpaceOverlay;
88  delete d;
89 }
90 
91 void KFileIVI::invalidateThumb( int state, bool redraw )
92 {
93  TQIconSet::Mode mode;
94  switch( state )
95  {
96  case TDEIcon::DisabledState:
97  mode = TQIconSet::Disabled;
98  break;
99  case TDEIcon::ActiveState:
100  mode = TQIconSet::Active;
101  break;
102  case TDEIcon::DefaultState:
103  default:
104  mode = TQIconSet::Normal;
105  break;
106  }
107  d->icons = TQIconSet();
108  d->icons.setPixmap( TDEGlobal::iconLoader()->iconEffect()->
109  apply( d->thumb, TDEIcon::Desktop, state ),
110  TQIconSet::Large, mode );
111  m_state = state;
112 
113  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
114  false, redraw );
115 }
116 
117 void KFileIVI::setIcon( int size, int state, bool recalc, bool redraw )
118 {
119  m_size = size;
120  m_bThumbnail = false;
121  if ( m_bDisabled )
122  m_state = TDEIcon::DisabledState;
123  else
124  m_state = state;
125 
126  if ( d->m_overlayName.isNull() )
127  d->m_overlay = TQPixmap();
128  else {
129  int halfSize;
130  if (m_size == 0) {
131  halfSize = IconSize(TDEIcon::Desktop) / 2;
132  } else {
133  halfSize = m_size / 2;
134  }
135  d->m_overlay = DesktopIcon(d->m_overlayName, halfSize);
136  }
137 
138  setPixmapDirect(m_fileitem->pixmap( m_size, m_state ) , recalc, redraw );
139 }
140 
141 void KFileIVI::setOverlay( const TQString& iconName )
142 {
143  d->m_overlayName = iconName;
144 
145  refreshIcon(true);
146 }
147 
148 void KFileIVI::setOverlayProgressBar( const int progress )
149 {
150  d->m_progress = progress;
151 
152  refreshIcon(true);
153 }
154 
155 KIVDirectoryOverlay* KFileIVI::setShowDirectoryOverlay( bool show )
156 {
157  if ( !m_fileitem->isDir() || m_fileitem->iconName() != "folder" )
158  return 0;
159 
160  if (show) {
161  if (!d->m_directoryOverlay)
162  d->m_directoryOverlay = new KIVDirectoryOverlay(this);
163  return d->m_directoryOverlay;
164  } else {
165  delete d->m_directoryOverlay;
166  d->m_directoryOverlay = 0;
167  setOverlay(TQString());
168  return 0;
169  }
170 }
171 
172 bool KFileIVI::showDirectoryOverlay( )
173 {
174  return (bool)d->m_directoryOverlay;
175 }
176 
177 KIVFreeSpaceOverlay* KFileIVI::setShowFreeSpaceOverlay( bool show )
178 {
179  if ( !m_fileitem->mimetype().startsWith("media/") ) {
180  return 0;
181  }
182 
183  if (show) {
184  if (!d->m_freeSpaceOverlay)
185  d->m_freeSpaceOverlay = new KIVFreeSpaceOverlay(this);
186  return d->m_freeSpaceOverlay;
187  } else {
188  delete d->m_freeSpaceOverlay;
189  d->m_freeSpaceOverlay = 0;
190  setOverlayProgressBar(-1);
191  return 0;
192  }
193 }
194 
195 bool KFileIVI::showFreeSpaceOverlay( )
196 {
197  return (bool)d->m_freeSpaceOverlay;
198 }
199 
200 void KFileIVI::setPixmapDirect( const TQPixmap& pixmap, bool recalc, bool redraw )
201 {
202  TQIconSet::Mode mode;
203  switch( m_state )
204  {
205  case TDEIcon::DisabledState:
206  mode = TQIconSet::Disabled;
207  break;
208  case TDEIcon::ActiveState:
209  mode = TQIconSet::Active;
210  break;
211  case TDEIcon::DefaultState:
212  default:
213  mode = TQIconSet::Normal;
214  break;
215  }
216 
217  // We cannot just reset() the iconset here, because setIcon can be
218  // called with any state and not just normal state. So we just
219  // create a dummy empty iconset as base object.
220  d->icons = TQIconSet();
221  d->icons.setPixmap( pixmap, TQIconSet::Large, mode );
222 
223  updatePixmapSize();
224  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
225  recalc, redraw );
226 }
227 
228 void KFileIVI::setDisabled( bool disabled )
229 {
230  if ( m_bDisabled != disabled )
231  {
232  m_bDisabled = disabled;
233  bool active = ( m_state == TDEIcon::ActiveState );
234  setEffect( m_bDisabled ? TDEIcon::DisabledState :
235  ( active ? TDEIcon::ActiveState : TDEIcon::DefaultState ) );
236  }
237 }
238 
239 void KFileIVI::setThumbnailPixmap( const TQPixmap & pixmap )
240 {
241  m_bThumbnail = true;
242  d->thumb = pixmap;
243  // TQIconSet::reset() doesn't seem to clear the other generated pixmaps,
244  // so we just create a blank TQIconSet here
245  d->icons = TQIconSet();
246  d->icons.setPixmap( TDEGlobal::iconLoader()->iconEffect()->
247  apply( pixmap, TDEIcon::Desktop, TDEIcon::DefaultState ),
248  TQIconSet::Large, TQIconSet::Normal );
249 
250  m_state = TDEIcon::DefaultState;
251 
252  // Recalc when setting this pixmap!
253  updatePixmapSize();
254  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large,
255  TQIconSet::Normal ), true );
256 }
257 
258 void KFileIVI::setActive( bool active )
259 {
260  if ( active )
261  setEffect( TDEIcon::ActiveState );
262  else
263  setEffect( m_bDisabled ? TDEIcon::DisabledState : TDEIcon::DefaultState );
264 }
265 
266 void KFileIVI::setEffect( int state )
267 {
268  TQIconSet::Mode mode;
269  switch( state )
270  {
271  case TDEIcon::DisabledState:
272  mode = TQIconSet::Disabled;
273  break;
274  case TDEIcon::ActiveState:
275  mode = TQIconSet::Active;
276  break;
277  case TDEIcon::DefaultState:
278  default:
279  mode = TQIconSet::Normal;
280  break;
281  }
282  // Do not update if the fingerprint is identical (prevents flicker)!
283 
284  TDEIconEffect *effect = TDEGlobal::iconLoader()->iconEffect();
285 
286  bool haveEffect = effect->hasEffect( TDEIcon::Desktop, m_state ) !=
287  effect->hasEffect( TDEIcon::Desktop, state );
288 
289  //kdDebug(1203) << "desktop;defaultstate=" <<
290  // effect->fingerprint(TDEIcon::Desktop, TDEIcon::DefaultState) <<
291  // endl;
292  //kdDebug(1203) << "desktop;activestate=" <<
293  // effect->fingerprint(TDEIcon::Desktop, TDEIcon::ActiveState) <<
294  // endl;
295 
296  if( haveEffect &&
297  effect->fingerprint( TDEIcon::Desktop, m_state ) !=
298  effect->fingerprint( TDEIcon::Desktop, state ) )
299  {
300  // Effects on are not applied until they are first accessed to
301  // save memory. Do this now when needed
302  if( m_bThumbnail )
303  {
304  if( d->icons.isGenerated( TQIconSet::Large, mode ) )
305  d->icons.setPixmap( effect->apply( d->thumb, TDEIcon::Desktop, state ),
306  TQIconSet::Large, mode );
307  }
308  else
309  {
310  if( d->icons.isGenerated( TQIconSet::Large, mode ) )
311  d->icons.setPixmap( m_fileitem->pixmap( m_size, state ),
312  TQIconSet::Large, mode );
313  }
314  TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ) );
315  }
316  m_state = state;
317 }
318 
319 void KFileIVI::refreshIcon( bool redraw )
320 {
321  if (!isThumbnail()) {
322  setIcon( m_size, m_state, true, redraw );
323  }
324 }
325 
326 void KFileIVI::invalidateThumbnail()
327 {
328  d->thumb = TQPixmap();
329 }
330 
331 bool KFileIVI::isThumbnailInvalid() const
332 {
333  return d->thumb.isNull();
334 }
335 
336 bool KFileIVI::acceptDrop( const TQMimeSource *mime ) const
337 {
338  if ( mime->provides( "text/uri-list" ) ) // We're dragging URLs
339  {
340  if ( m_fileitem->acceptsDrops() ) // Directory, executables, ...
341  return true;
342 
343  // Use cache
344  KURL::List uris = ( static_cast<KonqIconViewWidget*>(iconView()) )->dragURLs();
345 
346  // Check if we want to drop something on itself
347  // (Nothing will happen, but it's a convenient way to move icons)
348  KURL::List::Iterator it = uris.begin();
349  for ( ; it != uris.end() ; it++ )
350  {
351  if ( m_fileitem->url().equals( *it, true /*ignore trailing slashes*/ ) )
352  return true;
353  }
354  }
355  return TQIconViewItem::acceptDrop( mime );
356 }
357 
358 void KFileIVI::setKey( const TQString &key )
359 {
360  TQString theKey = key;
361 
362  TQVariant sortDirProp = iconView()->property( "sortDirectoriesFirst" );
363 
364  bool isdir = ( S_ISDIR( m_fileitem->mode() ) && ( !sortDirProp.isValid() || ( sortDirProp.type() == TQVariant::Bool && sortDirProp.toBool() ) ) );
365 
366  // The order is: .dir (0), dir (1), .file (2), file (3)
367  int sortChar = isdir ? 1 : 3;
368  if ( m_fileitem->text()[0] == '.' )
369  --sortChar;
370 
371  if ( !iconView()->sortDirection() ) // reverse sorting
372  sortChar = 3 - sortChar;
373 
374  theKey.prepend( TQChar( sortChar + '0' ) );
375 
376  TQIconViewItem::setKey( theKey );
377 }
378 
379 void KFileIVI::dropped( TQDropEvent *e, const TQValueList<TQIconDragItem> & )
380 {
381  KonqOperations::doDrop( item(), item()->url(), e, iconView() );
382 }
383 
384 void KFileIVI::returnPressed()
385 {
386  if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
387  KURL url = m_fileitem->url();
388  if (url.protocol() == "media") {
389  (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
390  }
391  else {
392  // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
393  // Symlink resolution must only happen on the desktop though (#63014)
394  if ( m_fileitem->isLink() && url.isLocalFile() ) {
395  url = KURL( url, m_fileitem->linkDest() );
396  }
397 
398  (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
399  }
400  }
401  else {
402  m_fileitem->run();
403  }
404 }
405 
406 
407 void KFileIVI::paintItem( TQPainter *p, const TQColorGroup &c )
408 {
409  TQColorGroup cg = updateColors(c);
410  paintFontUpdate( p );
411 
412  //*** TEMPORARY CODE - MUST BE MADE CONFIGURABLE FIRST - Martijn
413  // SET UNDERLINE ON HOVER ONLY
414  /*if ( ( ( KonqIconViewWidget* ) iconView() )->m_pActiveItem == this )
415  {
416  TQFont f( p->font() );
417  f.setUnderline( TRUE );
418  p->setFont( f );
419  }*/
420 
421  TDEIconViewItem::paintItem( p, cg );
422  paintOverlay(p);
423  paintOverlayProgressBar(p);
424 
425 }
426 
427 void KFileIVI::paintOverlay( TQPainter *p ) const
428 {
429  if ( !d->m_overlay.isNull() ) {
430  TQRect rect = pixmapRect(true);
431  p->drawPixmap(x() + rect.x() , y() + pixmapRect().height() - d->m_overlay.height(), d->m_overlay);
432  }
433 }
434 
435 void KFileIVI::paintOverlayProgressBar( TQPainter *p ) const
436 {
437  if (d->m_progress != -1) {
438 // // Pie chart
439 // TQRect rect = pixmapRect(true);
440 // rect.setX(x() + rect.x());
441 // rect.setY(y() + rect.y() + ((pixmapRect().height()*3)/4));
442 // rect.setWidth(pixmapRect().width()/4);
443 // rect.setHeight(pixmapRect().height()/4);
444 //
445 // p->save();
446 //
447 // p->setPen(TQPen::NoPen);
448 // p->setBrush(TQt::red);
449 // p->drawEllipse(rect);
450 // p->setBrush(TQt::green);
451 // p->drawPie(rect, 1440, (((100-d->m_progress)*5760)/100));
452 
453 // // Horizontal progress bar
454 // TQRect rect = pixmapRect(true);
455 // int verticalOffset = 0;
456 // int usedBarWidth = ((d->m_progress*pixmapRect().width())/100);
457 // int endPosition = x() + rect.x() + usedBarWidth;
458 //
459 // p->save();
460 //
461 // p->setPen(TQPen::NoPen);
462 // p->setBrush(TQt::red);
463 // p->drawRect(TQRect(x() + rect.x(), y() + rect.y() + (pixmapRect().height() - verticalOffset), usedBarWidth, 1));
464 // p->setBrush(TQt::green);
465 // p->drawRect(TQRect(endPosition, y() + rect.y() + (pixmapRect().height() - verticalOffset), pixmapRect().width() - usedBarWidth, 1));
466 
467  // Vertical progress bar
468  TQRect rect = pixmapRect(true);
469  int horizontalOffset = 0;
470  int usedBarHeight = (((100-d->m_progress)*pixmapRect().height())/100);
471  int endPosition = y() + rect.y() + usedBarHeight;
472 
473  p->save();
474 
475  p->setPen(TQPen::NoPen);
476  p->setBrush(TQt::green);
477  p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), y() + rect.y(), 1, usedBarHeight));
478  p->setBrush(TQt::red);
479  p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), endPosition, 1, pixmapRect().height() - usedBarHeight));
480 
481  p->restore();
482  }
483 }
484 
485 void KFileIVI::paintFontUpdate( TQPainter *p ) const
486 {
487  if ( m_fileitem->isLink() )
488  {
489  TQFont f( p->font() );
490  f.setItalic( TRUE );
491  p->setFont( f );
492  }
493 }
494 
495 TQColorGroup KFileIVI::updateColors( const TQColorGroup &c ) const
496 {
497  TQColorGroup cg( c );
498  cg.setColor( TQColorGroup::Text, static_cast<KonqIconViewWidget*>(iconView())->itemColor() );
499  return cg;
500 }
501 
502 bool KFileIVI::move( int x, int y )
503 {
504  if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
505  if ( x < 5 )
506  x = 5;
507  if ( x > iconView()->viewport()->width() - ( width() + 5 ) )
508  x = iconView()->viewport()->width() - ( width() + 5 );
509  if ( y < 5 )
510  y = 5;
511  if ( y > iconView()->viewport()->height() - ( height() + 5 ) )
512  y = iconView()->viewport()->height() - ( height() + 5 );
513  }
514  return TQIconViewItem::move( x, y );
515 }
516 
517 bool KFileIVI::hasAnimation() const
518 {
519  return !d->m_animatedIcon.isEmpty() && !m_bThumbnail;
520 }
521 
522 void KFileIVI::setMouseOverAnimation( const TQString& movieFileName )
523 {
524  if ( !movieFileName.isEmpty() )
525  {
526  //kdDebug(1203) << "TDEIconViewItem::setMouseOverAnimation " << movieFileName << endl;
527  d->m_animatedIcon = movieFileName;
528  }
529 }
530 
531 TQString KFileIVI::mouseOverAnimation() const
532 {
533  return d->m_animatedIcon;
534 }
535 
536 bool KFileIVI::isAnimated() const
537 {
538  return d->m_animated;
539 }
540 
541 void KFileIVI::setAnimated( bool a )
542 {
543  d->m_animated = a;
544 }
545 
546 int KFileIVI::compare( TQIconViewItem *i ) const
547 {
548  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>(iconView());
549  if ( view->caseInsensitiveSort() )
550  return key().localeAwareCompare( i->key() );
551  else
552  return view->m_pSettings->caseSensitiveCompare( key(), i->key() );
553 }
554 
555 void KFileIVI::updatePixmapSize()
556 {
557  int size = m_size ? m_size :
558  TDEGlobal::iconLoader()->currentSize( TDEIcon::Desktop );
559 
560  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>( iconView() );
561 
562  bool mimeDetermined = false;
563  if ( m_fileitem->isMimeTypeKnown() ) {
564  mimeDetermined = true;
565  }
566 
567  if (mimeDetermined) {
568  bool changed = false;
569  if ( view && view->canPreview( item() ) ) {
570  int previewSize = view->previewIconSize( size );
571  if (previewSize != size) {
572  setPixmapSize( TQSize( previewSize, previewSize ) );
573  changed = true;
574  }
575  }
576  else {
577  TQSize pixSize = TQSize( size, size );
578  if ( pixSize != pixmapSize() ) {
579  setPixmapSize( pixSize );
580  changed = true;
581  }
582  }
583  if (changed) {
584  view->adjustItems();
585  }
586  }
587  else {
588  TQSize pixSize = TQSize( size, size );
589  if ( pixSize != pixmapSize() ) {
590  setPixmapSize( pixSize );
591  }
592  }
593 }
594 
595 void KFileIVI::mimeTypeAndIconDetermined()
596 {
597  updatePixmapSize();
598 }

libkonq

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

libkonq

Skip menu "libkonq"
  • kate
  • libkonq
  • twin
  •   lib
Generated for libkonq by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.