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

libkonq

  • libkonq
tdefileivi.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 "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  // The user reasonably expects to be placed within the media:/ tree
390  // when opening a media device from the desktop
391  KService::Ptr service = KService::serviceByDesktopName("konqueror");
392  if (service) {
393  // HACK
394  // There doesn't seem to be a way to prevent KRun from resolving the URL to its
395  // local path, so simpy launch Konqueror with the correct arguments instead...
396  KRun::runCommand("konqueror " + url.url(), "konqueror", service->icon());
397  }
398  else {
399  (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
400  }
401  }
402  else {
403  // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
404  // Symlink resolution must only happen on the desktop though (#63014)
405  if ( m_fileitem->isLink() && url.isLocalFile() ) {
406  url = KURL( url, m_fileitem->linkDest() );
407  }
408 
409  (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
410  }
411  }
412  else {
413  m_fileitem->run();
414  }
415 }
416 
417 
418 void KFileIVI::paintItem( TQPainter *p, const TQColorGroup &c )
419 {
420  TQColorGroup cg = updateColors(c);
421  paintFontUpdate( p );
422 
423  //*** TEMPORARY CODE - MUST BE MADE CONFIGURABLE FIRST - Martijn
424  // SET UNDERLINE ON HOVER ONLY
425  /*if ( ( ( KonqIconViewWidget* ) iconView() )->m_pActiveItem == this )
426  {
427  TQFont f( p->font() );
428  f.setUnderline( TRUE );
429  p->setFont( f );
430  }*/
431 
432  TDEIconViewItem::paintItem( p, cg );
433  paintOverlay(p);
434  paintOverlayProgressBar(p);
435 
436 }
437 
438 void KFileIVI::paintOverlay( TQPainter *p ) const
439 {
440  if ( !d->m_overlay.isNull() ) {
441  TQRect rect = pixmapRect(true);
442  p->drawPixmap(x() + rect.x() , y() + pixmapRect().height() - d->m_overlay.height(), d->m_overlay);
443  }
444 }
445 
446 void KFileIVI::paintOverlayProgressBar( TQPainter *p ) const
447 {
448  if (d->m_progress != -1) {
449 // // Pie chart
450 // TQRect rect = pixmapRect(true);
451 // rect.setX(x() + rect.x());
452 // rect.setY(y() + rect.y() + ((pixmapRect().height()*3)/4));
453 // rect.setWidth(pixmapRect().width()/4);
454 // rect.setHeight(pixmapRect().height()/4);
455 //
456 // p->save();
457 //
458 // p->setPen(TQPen::NoPen);
459 // p->setBrush(TQt::red);
460 // p->drawEllipse(rect);
461 // p->setBrush(TQt::green);
462 // p->drawPie(rect, 1440, (((100-d->m_progress)*5760)/100));
463 
464 // // Horizontal progress bar
465 // TQRect rect = pixmapRect(true);
466 // int verticalOffset = 0;
467 // int usedBarWidth = ((d->m_progress*pixmapRect().width())/100);
468 // int endPosition = x() + rect.x() + usedBarWidth;
469 //
470 // p->save();
471 //
472 // p->setPen(TQPen::NoPen);
473 // p->setBrush(TQt::red);
474 // p->drawRect(TQRect(x() + rect.x(), y() + rect.y() + (pixmapRect().height() - verticalOffset), usedBarWidth, 1));
475 // p->setBrush(TQt::green);
476 // p->drawRect(TQRect(endPosition, y() + rect.y() + (pixmapRect().height() - verticalOffset), pixmapRect().width() - usedBarWidth, 1));
477 
478  // Vertical progress bar
479  TQRect rect = pixmapRect(true);
480  int horizontalOffset = 0;
481  int usedBarHeight = (((100-d->m_progress)*pixmapRect().height())/100);
482  int endPosition = y() + rect.y() + usedBarHeight;
483 
484  p->save();
485 
486  p->setPen(TQPen::NoPen);
487  p->setBrush(TQt::green);
488  p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), y() + rect.y(), 1, usedBarHeight));
489  p->setBrush(TQt::red);
490  p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), endPosition, 1, pixmapRect().height() - usedBarHeight));
491 
492  p->restore();
493  }
494 }
495 
496 void KFileIVI::paintFontUpdate( TQPainter *p ) const
497 {
498  if ( m_fileitem->isLink() )
499  {
500  TQFont f( p->font() );
501  f.setItalic( TRUE );
502  p->setFont( f );
503  }
504 }
505 
506 TQColorGroup KFileIVI::updateColors( const TQColorGroup &c ) const
507 {
508  TQColorGroup cg( c );
509  cg.setColor( TQColorGroup::Text, static_cast<KonqIconViewWidget*>(iconView())->itemColor() );
510  return cg;
511 }
512 
513 bool KFileIVI::move( int x, int y )
514 {
515  if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
516  if ( x < 5 )
517  x = 5;
518  if ( x > iconView()->viewport()->width() - ( width() + 5 ) )
519  x = iconView()->viewport()->width() - ( width() + 5 );
520  if ( y < 5 )
521  y = 5;
522  if ( y > iconView()->viewport()->height() - ( height() + 5 ) )
523  y = iconView()->viewport()->height() - ( height() + 5 );
524  }
525  return TQIconViewItem::move( x, y );
526 }
527 
528 bool KFileIVI::hasAnimation() const
529 {
530  return !d->m_animatedIcon.isEmpty() && !m_bThumbnail;
531 }
532 
533 void KFileIVI::setMouseOverAnimation( const TQString& movieFileName )
534 {
535  if ( !movieFileName.isEmpty() )
536  {
537  //kdDebug(1203) << "TDEIconViewItem::setMouseOverAnimation " << movieFileName << endl;
538  d->m_animatedIcon = movieFileName;
539  }
540 }
541 
542 TQString KFileIVI::mouseOverAnimation() const
543 {
544  return d->m_animatedIcon;
545 }
546 
547 bool KFileIVI::isAnimated() const
548 {
549  return d->m_animated;
550 }
551 
552 void KFileIVI::setAnimated( bool a )
553 {
554  d->m_animated = a;
555 }
556 
557 int KFileIVI::compare( TQIconViewItem *i ) const
558 {
559  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>(iconView());
560  if ( view->caseInsensitiveSort() )
561  return key().localeAwareCompare( i->key() );
562  else
563  return view->m_pSettings->caseSensitiveCompare( key(), i->key() );
564 }
565 
566 void KFileIVI::updatePixmapSize()
567 {
568  int size = m_size ? m_size :
569  TDEGlobal::iconLoader()->currentSize( TDEIcon::Desktop );
570 
571  KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>( iconView() );
572 
573  bool mimeDetermined = false;
574  if ( m_fileitem->isMimeTypeKnown() ) {
575  mimeDetermined = true;
576  }
577 
578  if (mimeDetermined) {
579  bool changed = false;
580  if ( view && view->canPreview( item() ) ) {
581  int previewSize = view->previewIconSize( size );
582  if (previewSize != size) {
583  setPixmapSize( TQSize( previewSize, previewSize ) );
584  changed = true;
585  }
586  }
587  else {
588  TQSize pixSize = TQSize( size, size );
589  if ( pixSize != pixmapSize() ) {
590  setPixmapSize( pixSize );
591  changed = true;
592  }
593  }
594  if (changed) {
595  view->adjustItems();
596  }
597  }
598  else {
599  TQSize pixSize = TQSize( size, size );
600  if ( pixSize != pixmapSize() ) {
601  setPixmapSize( pixSize );
602  }
603  }
604 }
605 
606 void KFileIVI::mimeTypeAndIconDetermined()
607 {
608  updatePixmapSize();
609 }
610 
611 /* 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
  • libkonq
  • twin
  •   lib
Generated for libkonq by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.