KDevelop API Documentation

kfilednddetailview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 *                        kfilednddetailview.cpp  -  description
00003 *                           -------------------
00004 *  begin                : Wed Nov 1 2000
00005 *  copyright            : (C) 2000 by Björn Sahlström
00006 *  email                : kbjorn@users.sourceforge.net
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010 *                                                                         *
00011 *   This program is free software; you can redistribute it and/or modify  *
00012 *   it under the terms of the GNU General Public License as published by  *
00013 *   the Free Software Foundation; either version 2 of the License, or     *
00014 *   (at your option) any later version.                                   *
00015 *                                                                         *
00016 ***************************************************************************/
00017 
00019 // Qt specific includes
00020 #include <qptrlist.h>
00021 #include <qapplication.h>
00023 // KDE specific includes
00024 #include <kfileitem.h>
00025 #include <kiconloader.h>
00026 #include <kdebug.h>
00028 // Application specific includes
00029 #include "kfilednddetailview.h"
00030 
00031 #ifndef AUTO_OPEN_TIME
00032 #define AUTO_OPEN_TIME
00033     static int autoOpenTime = 750;
00034 #endif
00035 //-----------------------------------------------
00036 KFileDnDDetailView::KFileDnDDetailView(QWidget *parent, const char *name )
00037     :   KFileDetailView(parent,name), m_autoOpenTimer( this ),
00038         m_autoOpenTime( autoOpenTime ), m_useAutoOpenTimer( true ),
00039         m_dropItem(0), m_dndEnabled( true )
00040 {
00041     setAutoUpdate( true );
00042     setDnDEnabled( true );
00043     useAutoOpenTimer( true );
00044 }
00045 //-----------------------------------------------
00046 KFileDnDDetailView::~KFileDnDDetailView(){
00047 }
00048 //-----------------------------------------------
00049 void KFileDnDDetailView::readConfig( KConfig* config, const QString& group ) {
00050     KConfigGroupSaver cs( config, group );
00051     bool dnd = config->readBoolEntry("DragAndDrop", true );
00052     setDnDEnabled( dnd );
00053     KFileDetailView::readConfig( config, group );
00054 }
00055 //-----------------------------------------------
00056 void KFileDnDDetailView::writeConfig( KConfig* config, const QString& group ) {
00057     KConfigGroupSaver cs( config, group );
00058     config->writeEntry("DragAndDrop", m_dndEnabled );
00059     KFileDetailView::writeConfig( config, group );
00060 }
00061 //-----------------------------------------------
00062 void KFileDnDDetailView::slotOpenFolder(){
00063     if( m_useAutoOpenTimer ) {
00064         m_autoOpenTimer.stop();
00065         if( !m_dropItem )
00066             return;
00067     }
00068     KFileItemListIterator it( * KFileView::items() );
00069     for( ; it.current() ;++it ){
00070         if( (*it)->name() == m_dropItem->text(0) ) {
00071             if( (*it)->isFile() )
00072                 return;
00073             else if( (*it)->isDir() || (*it)->isLink()) {
00074                 sig->activate( (*it) );
00075                 return;
00076             }
00077         }
00078     }
00079 }
00080 //-----------------------------------------------
00081 void KFileDnDDetailView::contentsDragEnterEvent( QDragEnterEvent *e ) {
00082 
00083     kdDebug () << "KFileDnDDetailView::contentsDragEnterEvent" << endl;
00084 
00085     if ( ! acceptDrag( e ) ) {
00086         e->accept( false );
00087         return;
00088     }
00089     e->acceptAction();
00090     QListViewItem *i = itemAt( contentsToViewport( e->pos() ) );
00091     if ( i && m_useAutoOpenTimer ) {
00092         m_dropItem = i;
00093         m_autoOpenTimer.start( m_autoOpenTime );
00094     }
00095 }
00096 //-----------------------------------------------
00097 void KFileDnDDetailView::contentsDragMoveEvent( QDragMoveEvent *e ) {
00098 
00099     kdDebug () << "KFileDnDDetailView::contentsDragMoveEvent" << endl;
00100 
00101     if ( ! acceptDrag( e ) ) {
00102         e->accept( false );
00103         return;
00104     }
00105     e->acceptAction();
00106     QListViewItem *i = itemAt( contentsToViewport(  e->pos() ) );
00107     if( ! m_useAutoOpenTimer )
00108         return;
00109     if ( i ) {
00110         if ( i != m_dropItem ) {
00111             m_autoOpenTimer.stop();
00112             m_dropItem = i;
00113             m_autoOpenTimer.start( m_autoOpenTime );
00114         }
00115     }
00116     else
00117         m_autoOpenTimer.stop();
00118 }
00119 //-----------------------------------------------
00120 void KFileDnDDetailView::contentsDragLeaveEvent( QDragLeaveEvent* ) {
00121 
00122     kdDebug () << "KFileDnDDetailView::contentsDragLeaveEvent" << endl;
00123 
00124     if( m_useAutoOpenTimer ) {
00125         m_autoOpenTimer.stop();
00126         m_dropItem = 0L;
00127     }
00128 }
00129 //-----------------------------------------------
00130 void KFileDnDDetailView::contentsDropEvent( QDropEvent* e ) {
00131 
00132     kdDebug () << "KFileDndDetailView::contentsDropEvent" << endl;
00133 
00134     if( m_useAutoOpenTimer ) {
00135         m_autoOpenTimer.stop();
00136         m_dropItem = 0L;
00137     }
00138     if( ! acceptDrag( e ) ) {
00139         e->acceptAction( false );
00140         return;
00141     }
00142     e->acceptAction();              
00143     // the drop was accepted so lets emit this to the outside world
00144     KURL::List urls;
00145     KURLDrag::decode( e, urls );
00146     emit dropped( e );
00147     emit dropped( this, e );
00148     emit dropped( this, urls );
00149 }
00150 //-----------------------------------------------
00151 void KFileDnDDetailView::startDrag(){
00152 
00153     kdDebug () << "KFileDnDDetailView::startDrag()" << endl;
00154 
00155     // create a list of the URL:s that we want to drag
00156     KURL::List urls;
00157     KFileItemListIterator it( * KFileView::selectedItems() );
00158     for ( ; it.current(); ++it ){
00159         urls.append( (*it)->url() );
00160     }
00161     QPixmap pixmap;
00162     if( urls.count() > 1 ){
00163         pixmap = DesktopIcon( "kmultiple", 16 );
00164     }
00165     if( pixmap.isNull() )
00166         pixmap = currentFileItem()->pixmap( 16 );   
00167     QPoint hotspot;
00168     hotspot.setX( pixmap.width() / 2 );
00169     hotspot.setY( pixmap.height() / 2 );
00170     m_dragObject = KURLDrag::newDrag( urls, widget() );
00171     m_dragObject->setPixmap( pixmap, hotspot );
00172     m_dragObject->drag();   // start the drag
00173 }
00174 //-----------------------------------------------
00175 QDragObject* KFileDnDDetailView::dragObject() const {
00176     return m_dragObject;
00177 }
00178 //-----------------------------------------------
00179 bool KFileDnDDetailView::acceptDrag(QDropEvent* e ) const {
00180     return KURLDrag::canDecode( e ) &&
00181                 ( e->action() == QDropEvent::Copy
00182                 || e->action() == QDropEvent::Move
00183                 || e->action() == QDropEvent::Link );
00184 }
00185 //-----------------------------------------------
00186 void KFileDnDDetailView::setAutoOpenTime( const int& time ){
00187     m_autoOpenTime = time;
00188     useAutoOpenTimer();
00189 }
00190 //-----------------------------------------------
00191 void KFileDnDDetailView::useAutoOpenTimer( bool use ){
00192     m_useAutoOpenTimer = use;
00193     if( use )
00194         connect( &m_autoOpenTimer, SIGNAL( timeout() ),this, SLOT( slotOpenFolder() ) );
00195     else {
00196         disconnect( &m_autoOpenTimer, SIGNAL( timeout() ),this, SLOT( slotOpenFolder() ) );
00197         m_dropItem = 0L;
00198         m_autoOpenTimer.stop();
00199     }
00200 }
00201 //-----------------------------------------------
00202 void KFileDnDDetailView::setDnDEnabled( bool useDnD ){
00203     m_dndEnabled = useDnD;
00204     setDragEnabled( useDnD );
00205     setDropVisualizer( useDnD );
00206     setAcceptDrops( useDnD );
00207     viewport()->setAcceptDrops( useDnD );
00208 }
00209 //-----------------------------------------------
00210 #ifndef NO_INCLUDE_MOCFILES
00211 #include "kfilednddetailview.moc"
00212 #endif
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003