23 #include <tqfontmetrics.h>
24 #include <tqkeycode.h>
26 #include <tqpainter.h>
30 #include <tqtooltip.h>
33 #include <kapplication.h>
35 #include <kfileitem.h>
36 #include <kiconeffect.h>
37 #include <kglobalsettings.h>
39 #include <kio/previewjob.h>
41 #include "kfileiconview.h"
42 #include "config-kfile.h"
44 #define DEFAULT_PREVIEW_SIZE 60
45 #define DEFAULT_SHOW_PREVIEWS false
46 #define DEFAULT_VIEW_MODE "SmallColumns"
48 KFileIconViewItem::~KFileIconViewItem()
50 fileInfo()->removeExtraData( iconView() );
53 class KFileIconView::KFileIconViewPrivate
61 noArrangement =
false;
62 ignoreMaximumSize =
false;
63 smallColumns =
new KRadioAction( i18n(
"Small Icons"), 0, TQT_TQOBJECT(parent),
64 TQT_SLOT( slotSmallColumns() ),
68 largeRows =
new KRadioAction( i18n(
"Large Icons"), 0, TQT_TQOBJECT(parent),
69 TQT_SLOT( slotLargeRows() ),
73 smallColumns->setExclusiveGroup(TQString::fromLatin1(
"IconView mode"));
74 largeRows->setExclusiveGroup(TQString::fromLatin1(
"IconView mode"));
76 previews =
new KToggleAction( i18n(
"Thumbnail Previews"), 0,
79 zoomIn = KStdAction::zoomIn( TQT_TQOBJECT(parent), TQT_SLOT( zoomIn() ),
81 zoomOut = KStdAction::zoomOut( TQT_TQOBJECT(parent), TQT_SLOT( zoomOut() ),
84 previews->setGroup(
"previews");
85 zoomIn->setGroup(
"previews");
86 zoomOut->setGroup(
"previews");
88 connect( previews, TQT_SIGNAL( toggled(
bool )),
89 parent, TQT_SLOT( slotPreviewsToggled(
bool )));
91 connect( &previewTimer, TQT_SIGNAL( timeout() ),
93 connect( &autoOpenTimer, TQT_SIGNAL( timeout() ),
94 parent, TQT_SLOT( slotAutoOpen() ));
97 ~KFileIconViewPrivate() {
102 KRadioAction *smallColumns, *largeRows;
103 KAction *zoomIn, *zoomOut;
104 KToggleAction *previews;
105 KIO::PreviewJob *job;
107 TQTimer previewTimer;
108 TQTimer autoOpenTimer;
109 TQStringList previewMimeTypes;
111 bool noArrangement :1;
112 bool ignoreMaximumSize :1;
115 KFileIconView::KFileIconView(TQWidget *parent,
const char *name)
118 d =
new KFileIconViewPrivate(
this );
120 setViewName( i18n(
"Icon View") );
123 setResizeMode( Adjust );
124 setMaxItemWidth( 300 );
125 setWordWrapIconText(
false );
126 setArrangement( TopToBottom );
127 setAutoArrange(
true );
128 setItemsMovable(
false );
130 KIconView::setSorting(
true );
133 setShowToolTips(
false );
135 d->smallColumns->setChecked(
true );
137 connect(
this, TQT_SIGNAL( returnPressed(TQIconViewItem *) ),
138 TQT_SLOT( slotActivate( TQIconViewItem *) ) );
141 connect(
this, TQT_SIGNAL( clicked(TQIconViewItem *,
const TQPoint&) ),
142 TQT_SLOT( selected( TQIconViewItem *) ) );
143 connect(
this, TQT_SIGNAL( doubleClicked(TQIconViewItem *,
const TQPoint&) ),
144 TQT_SLOT( slotActivate( TQIconViewItem *) ) );
146 connect(
this, TQT_SIGNAL( onItem( TQIconViewItem * ) ),
147 TQT_SLOT( showToolTip( TQIconViewItem * ) ) );
148 connect(
this, TQT_SIGNAL( onViewport() ),
149 TQT_SLOT( removeToolTip() ) );
150 connect(
this, TQT_SIGNAL( contextMenuRequested(TQIconViewItem*,
const TQPoint&)),
151 TQT_SLOT( slotActivateMenu( TQIconViewItem*,
const TQPoint& ) ) );
153 KFile::SelectionMode sm = KFileView::selectionMode();
156 TQIconView::setSelectionMode( TQIconView::Multi );
158 case KFile::Extended:
159 TQIconView::setSelectionMode( TQIconView::Extended );
161 case KFile::NoSelection:
162 TQIconView::setSelectionMode( TQIconView::NoSelection );
166 TQIconView::setSelectionMode( TQIconView::Single );
170 if ( sm == KFile::Multi || sm == KFile::Extended )
172 TQT_SLOT( slotSelectionChanged() ));
175 TQT_SLOT( highlighted( TQIconViewItem * )));
177 viewport()->installEventFilter(
this );
180 m_resolver =
new KMimeTypeResolver<KFileIconViewItem,KFileIconView>(
this);
183 KFileIconView::~KFileIconView()
190 void KFileIconView::readConfig( KConfig *kc,
const TQString& group )
192 TQString gr = group.isEmpty() ? TQString(
"KFileIconView") : group;
193 KConfigGroupSaver cs( kc, gr );
194 TQString small = TQString::fromLatin1(
"SmallColumns");
195 d->previewIconSize = kc->readNumEntry(
"Preview Size", DEFAULT_PREVIEW_SIZE );
196 d->previews->setChecked( kc->readBoolEntry(
"ShowPreviews", DEFAULT_SHOW_PREVIEWS ) );
198 if ( kc->readEntry(
"ViewMode", DEFAULT_VIEW_MODE ) == small ) {
199 d->smallColumns->setChecked(
true );
203 d->largeRows->setChecked(
true );
207 if ( d->previews->isChecked() )
211 void KFileIconView::writeConfig( KConfig *kc,
const TQString& group )
213 TQString gr = group.isEmpty() ? TQString(
"KFileIconView") : group;
214 KConfigGroupSaver cs( kc, gr );
216 TQString viewMode = d->smallColumns->isChecked() ?
217 TQString::fromLatin1(
"SmallColumns") :
218 TQString::fromLatin1(
"LargeRows");
219 if(!kc->hasDefault(
"ViewMode" ) && viewMode == DEFAULT_VIEW_MODE )
220 kc->revertToDefault(
"ViewMode" );
222 kc->writeEntry(
"ViewMode", viewMode );
224 int previewsIconSize = d->previewIconSize;
225 if(!kc->hasDefault(
"Preview Size" ) && previewsIconSize == DEFAULT_PREVIEW_SIZE )
226 kc->revertToDefault(
"Preview Size" );
228 kc->writeEntry(
"Preview Size", previewsIconSize );
231 if(!kc->hasDefault(
"ShowPreviews" ) && showPreviews == DEFAULT_SHOW_PREVIEWS )
232 kc->revertToDefault(
"ShowPreviews" );
234 kc->writeEntry(
"ShowPreviews", showPreviews );
237 void KFileIconView::removeToolTip()
243 void KFileIconView::showToolTip( TQIconViewItem *item )
251 int w = maxItemWidth() - ( itemTextPos() == TQIconView::Bottom ? 0 :
252 item->pixmapRect().width() ) - 4;
253 if ( fontMetrics().width( item->text() ) >= w ) {
254 toolTip =
new TQLabel( TQString::fromLatin1(
" %1 ").arg(item->text()), 0,
256 (WFlags)(WStyle_StaysOnTop | WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM) );
257 toolTip->setFrameStyle( TQFrame::Plain | TQFrame::Box );
258 toolTip->setLineWidth( 1 );
259 toolTip->setAlignment( AlignLeft | AlignTop );
260 toolTip->move( TQCursor::pos() + TQPoint( 14, 14 ) );
261 toolTip->adjustSize();
262 TQRect screen = TQApplication::desktop()->screenGeometry(
263 TQApplication::desktop()->screenNumber(TQCursor::pos()));
264 if (toolTip->x()+toolTip->width() > screen.right()) {
265 toolTip->move(toolTip->x()+screen.right()-toolTip->x()-toolTip->width(), toolTip->y());
267 if (toolTip->y()+toolTip->height() > screen.bottom()) {
268 toolTip->move(toolTip->x(), screen.bottom()-toolTip->y()-toolTip->height()+toolTip->y());
270 toolTip->setFont( TQToolTip::font() );
271 toolTip->setPalette( TQToolTip::palette(),
true );
276 void KFileIconView::slotActivateMenu( TQIconViewItem* item,
const TQPoint& pos )
279 sig->activateMenu( 0, pos );
283 sig->activateMenu( i->
fileInfo(), pos );
289 KIconView::hideEvent( e );
294 KIconView::keyPressEvent( e );
297 if ( (e->state() & ControlButton) &&
298 (e->key() == Key_Return || e->key() == Key_Enter) )
306 KIconView::setSelected( item, enable,
true );
311 if (KFileView::selectionMode() == KFile::NoSelection ||
312 KFileView::selectionMode() == KFile::Single)
315 KIconView::selectAll(
true );
320 KIconView::clearSelection();
325 KIconView::invertSelection();
330 m_resolver->m_lstPendingMimeIconItems.clear();
340 TQIconView* qview =
static_cast<TQIconView*
>( this );
343 qview->setUpdatesEnabled(
false );
345 initItem( item, i,
true );
346 qview->setUpdatesEnabled(
true );
348 if ( !i->isMimeTypeKnown() )
349 m_resolver->m_lstPendingMimeIconItems.append( item );
351 i->setExtraData(
this, item );
354 void KFileIconView::slotActivate( TQIconViewItem *item )
363 void KFileIconView::selected( TQIconViewItem *item )
365 if ( !item || (KApplication::keyboardMouseState() & (ShiftButton | ControlButton)) != 0 )
368 if ( KGlobalSettings::singleClick() ) {
379 KIconView::setCurrentItem( it );
391 void KFileIconView::highlighted( TQIconViewItem *item )
400 void KFileIconView::setSelectionMode( KFile::SelectionMode sm )
402 disconnect( TQT_SIGNAL( selectionChanged() ),
this );
403 disconnect( TQT_SIGNAL( selectionChanged( TQIconViewItem * )),
this );
405 KFileView::setSelectionMode( sm );
406 switch ( KFileView::selectionMode() ) {
408 TQIconView::setSelectionMode( TQIconView::Multi );
410 case KFile::Extended:
411 TQIconView::setSelectionMode( TQIconView::Extended );
413 case KFile::NoSelection:
414 TQIconView::setSelectionMode( TQIconView::NoSelection );
418 TQIconView::setSelectionMode( TQIconView::Single );
422 if ( sm == KFile::Multi || sm == KFile::Extended )
423 connect(
this, TQT_SIGNAL( selectionChanged() ),
424 TQT_SLOT( slotSelectionChanged() ));
426 connect(
this, TQT_SIGNAL( selectionChanged( TQIconViewItem * )),
427 TQT_SLOT( highlighted( TQIconViewItem * )));
433 return (item && item->isSelected());
444 if ( d->previews->isChecked() ) {
445 if ( canPreview( item->
fileInfo() ) )
446 item->setPixmapSize( TQSize( d->previewIconSize, d->previewIconSize ) );
450 if ( !item->pixmapSize().isNull() )
451 item->setPixmapSize( TQSize( 0, 0 ) );
454 item->setPixmap( (item->
fileInfo())->pixmap( myIconSize ),
true, false );
456 }
while ( item != 0L );
464 initItem( item, i,
true );
473 d->job->removeItem( i );
476 m_resolver->m_lstPendingMimeIconItems.remove( item );
493 d->previewIconSize = size;
494 if ( d->previews->isChecked() )
500 d->ignoreMaximumSize = ignoreSize;
503 void KFileIconView::updateIcons()
513 KIconView::ensureItemVisible( item );
516 void KFileIconView::slotSelectionChanged()
521 void KFileIconView::slotSmallColumns()
525 d->noArrangement =
true;
528 if ( d->previews->isChecked() )
531 d->previews->setChecked(
false );
534 setMaxItemWidth( 300 );
535 setItemTextPos( Right );
536 setArrangement( TopToBottom );
537 setWordWrapIconText(
false );
540 d->noArrangement =
false;
544 void KFileIconView::slotLargeRows()
548 d->noArrangement =
true;
550 setGridX( KGlobal::iconLoader()->currentSize( KIcon::Desktop ) + 50 );
551 setItemTextPos( Bottom );
552 setArrangement( LeftToRight );
553 setWordWrapIconText(
true );
556 d->noArrangement =
false;
560 void KFileIconView::stopPreview()
568 void KFileIconView::slotPreviewsToggled(
bool on )
580 if ( d->previewMimeTypes.isEmpty() )
581 d->previewMimeTypes = KIO::PreviewJob::supportedMimeTypes();
584 d->previews->setChecked(
true );
586 if ( !d->largeRows->isChecked() ) {
587 d->largeRows->setChecked(
true );
594 d->job = KIO::filePreview(*
items(), d->previewIconSize,d->previewIconSize);
595 d->job->setIgnoreMaximumSize(d->ignoreMaximumSize);
597 connect( d->job, TQT_SIGNAL( result( KIO::Job * )),
598 this, TQT_SLOT( slotPreviewResult( KIO::Job * )));
599 connect( d->job, TQT_SIGNAL( gotPreview(
const KFileItem*,
const TQPixmap& )),
600 TQT_SLOT( gotPreview(
const KFileItem*,
const TQPixmap& ) ));
605 void KFileIconView::slotPreviewResult( KIO::Job *job )
611 void KFileIconView::gotPreview(
const KFileItem *item,
const TQPixmap& pix )
615 if( item->overlays() & KIcon::HiddenOverlay )
619 KIconEffect::semiTransparent( p );
623 it->setPixmap( pix );
628 bool KFileIconView::canPreview(
const KFileItem *item )
const
630 TQStringList::Iterator it = d->previewMimeTypes.begin();
632 r.setWildcard(
true );
634 for ( ; it != d->previewMimeTypes.end(); ++it ) {
637 if ( type.at( type.length() - 1 ) ==
'*' ) {
638 r.setPattern( type );
639 if ( r.search( item->mimetype() ) != -1 )
643 if ( item->mimetype() == type )
650 KFileItem * KFileIconView::firstFileItem()
const
658 KFileItem * KFileIconView::nextItem(
const KFileItem *fileItem )
const
662 if ( item && item->nextItem() )
668 KFileItem * KFileIconView::prevItem(
const KFileItem *fileItem )
const
672 if ( item && item->prevItem() )
681 KFileItemListIterator it( *
items() );
685 if ( spec & TQDir::Time ) {
686 for ( ; (item = it.current()); ++it )
688 viewItem(item)->setKey(
sortingKey( (
unsigned long)item->time( KIO::UDS_MODIFICATION_TIME ), item->isDir(), spec ));
691 else if ( spec & TQDir::Size ) {
692 for ( ; (item = it.current()); ++it )
693 viewItem(item)->setKey(
sortingKey( item->size(), item->isDir(),
697 for ( ; (item = it.current()); ++it )
698 viewItem(item)->setKey(
sortingKey( item->text(), item->isDir(),
709 void KFileIconView::mimeTypeDeterminationFinished()
716 (void) item->
fileInfo()->determineMimeType();
726 if ( !currentItem() ) {
727 bool block = signalsBlocked();
728 blockSignals(
true );
729 TQIconViewItem *item = viewItem( firstFileItem() );
730 KIconView::setCurrentItem( item );
731 KIconView::setSelected( item,
false );
732 blockSignals( block );
735 m_resolver->start( d->previews->isChecked() ? 0 : 10 );
739 bool KFileIconView::eventFilter( TQObject *o, TQEvent *e )
741 if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(viewport()) || TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(
this) ) {
742 int type = e->type();
743 if ( type == TQEvent::Leave ||
744 type == TQEvent::FocusOut )
748 return KIconView::eventFilter( o, e );
754 void KFileIconView::showEvent( TQShowEvent *e )
756 KIconView::showEvent( e );
761 bool updateTextAndPixmap )
763 if ( d->previews->isChecked() && canPreview( i ) )
764 item->setPixmapSize( TQSize( d->previewIconSize, d->previewIconSize ) );
766 if ( updateTextAndPixmap )
771 item->setText( i->text() ,
false, false );
772 item->setPixmap( i->pixmap( myIconSize ) );
778 if ( spec & TQDir::Time )
780 item->setKey(
sortingKey( (
unsigned long) i->time( KIO::UDS_MODIFICATION_TIME ),
782 else if ( spec & TQDir::Size )
783 item->setKey(
sortingKey( i->size(), i->isDir(), spec ));
786 item->setKey(
sortingKey( i->text(), i->isDir(), spec ));
790 if ( d->previews->isChecked() )
791 d->previewTimer.start( 10,
true );
796 if ( d->noArrangement )
799 KIconView::arrangeItemsInGrid( update );
802 void KFileIconView::zoomIn()
807 void KFileIconView::zoomOut()
812 TQDragObject *KFileIconView::dragObject()
817 for ( ; it.current(); ++it ){
818 urls.append( (*it)->url() );
821 if( urls.count() > 1 )
822 pixmap = DesktopIcon(
"kmultiple",
iconSize() );
823 if( pixmap.isNull() )
827 hotspot.setX( pixmap.width() / 2 );
828 hotspot.setY( pixmap.height() / 2 );
829 TQDragObject* myDragObject =
new KURLDrag( urls,
widget() );
830 myDragObject->setPixmap( pixmap, hotspot );
834 void KFileIconView::slotAutoOpen()
836 d->autoOpenTimer.stop();
840 KFileItem *fileItem = d->dropItem->fileInfo();
844 if( fileItem->isFile() )
847 if ( fileItem->isDir() || fileItem->isLink())
851 bool KFileIconView::acceptDrag(TQDropEvent* e)
const
853 return KURLDrag::canDecode( e ) &&
855 ( e->action() == TQDropEvent::Copy
856 || e->action() == TQDropEvent::Move
857 || e->action() == TQDropEvent::Link );
860 void KFileIconView::contentsDragEnterEvent( TQDragEnterEvent *e )
862 if ( ! acceptDrag( e ) ) {
874 d->autoOpenTimer.start( autoOpenDelay() );
879 d->autoOpenTimer.stop();
883 void KFileIconView::contentsDragMoveEvent( TQDragMoveEvent *e )
885 if ( ! acceptDrag( e ) ) {
896 if (d->dropItem != item)
899 d->autoOpenTimer.start( autoOpenDelay() );
905 d->autoOpenTimer.stop();
909 void KFileIconView::contentsDragLeaveEvent( TQDragLeaveEvent * )
912 d->autoOpenTimer.stop();
915 void KFileIconView::contentsDropEvent( TQDropEvent *e )
918 d->autoOpenTimer.stop();
920 if ( ! acceptDrag( e ) ) {
927 KFileItem * fileItem = 0;
934 if (KURLDrag::decode( e, urls ) && !urls.isEmpty())
936 emit
dropped(e, urls, fileItem ? fileItem->url() : KURL());
937 sig->dropURLs(fileItem, e, urls);
941 void KFileIconView::virtual_hook(
int id,
void* data )
942 { KIconView::virtual_hook(
id, data );
943 KFileView::virtual_hook(
id, data ); }
945 #include "kfileiconview.moc"
void activate(const KFileItem *item)
Call this method when an item is selected (depends on single click / double click configuration)...
An item for the iconview, that has a reference to its corresponding KFileItem.
bool onlyDoubleClickSelectsFiles() const
virtual void listingCompleted()
This hook is called when all items of the currently listed directory are listed and inserted into the...
virtual TQWidget * widget()
a pure virtual function to get a TQWidget, that can be added to other widgets.
virtual bool isSelected(const KFileItem *i) const
virtual void hideEvent(TQHideEvent *)
Reimplemented to remove an eventual tooltip.
const KFileItemList * items() const
virtual void setSelected(const KFileItem *, bool)
Tells the view that it should highlight the item.
void highlightFile(const KFileItem *i)
emits the highlighted signal for item.
virtual void insertItem(KFileItem *i)
The derived view must implement this function to add the file in the widget.
virtual KActionCollection * actionCollection() const
This class defines an interface to all file views.
virtual void arrangeItemsInGrid(bool updated=true)
Reimplemented for performance reasons.
virtual void clearView()
pure virtual function, that should be implemented to clear the view.
TQDir::SortSpec sorting() const
Returns the sorting order of the internal list.
const KFileItemList * selectedItems() const
bool isReversed() const
Tells whether the current items are in reversed order (shortcut to sorting() & TQDir::Reversed).
virtual void invertSelection()
Inverts the current selection, i.e.
void setIconSize(int size)
Sets the size of the icons to show.
void setPreviewSize(int size)
Sets the size of the previews.
virtual void removeItem(const KFileItem *)
Removes an item from the list; has to be implemented by the view.
int dropOptions()
Returns the DND options in effect.
virtual void clearSelection()
Clears any selection, unhighlights everything.
virtual KFileItem * currentFileItem() const
virtual void keyPressEvent(TQKeyEvent *)
Reimplemented to not let TQIconView eat return-key events.
virtual void removeItem(const KFileItem *item)
Removes an item from the list; has to be implemented by the view.
void selectionChanged()
Emitted when the user hilights one or more files in multiselection mode.
virtual void setCurrentItem(const KFileItem *)
Reimplement this to set item the current item in the view, e.g.
KFileItem * fileInfo() const
virtual void setSorting(TQDir::SortSpec sort)
Sets the sorting order of the view.
void ensureItemVisible(const KFileItem *)
pure virtual function, that should be implemented to make item i visible, i.e.
void setMode(KFile::Mode m)
Convenient overload of the other setMode(unsigned int) method.
virtual void selectAll()
Selects all items.
virtual void insertItem(KFileItem *i)
The derived view must implement this function to add the file in the widget.
void showPreviews()
Starts loading previews for all files shown and shows them.
static TQString sortingKey(const TQString &value, bool isDir, int sortSpec)
This method calculates a TQString from the given parameters, that is suitable for sorting with e...
An icon-view capable of showing KFileItem's.
void setIgnoreMaximumSize(bool ignoreSize=true)
Disables the "Maximum file size" configuration option for previews.
virtual void updateView(bool)
does a repaint of the view.
virtual void setSorting(TQDir::SortSpec sort)
Sets the sorting order of the view.
void dropped(TQDropEvent *event, KFileItem *fileItem)
The user dropped something.