kio Library API Documentation

kmimetyperesolver.h

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003    Copyright (C) 2000 Rik Hemsley <rik@kde.org>
00004    Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #ifndef __kmimetyperesolver_h
00022 #define __kmimetyperesolver_h
00023 
00024 #include <qscrollview.h>
00025 #include <qptrlist.h>
00026 #include <qtimer.h>
00027 #include <kdebug.h>
00028 
00034 class KMimeTypeResolverBase
00035 {
00036 public:
00037     virtual void slotViewportAdjusted() = 0;
00038     virtual void slotProcessMimeIcons() = 0;
00039 protected:
00040     virtual void virtual_hook( int, void* ) {};
00041 };
00042 
00049 class KMimeTypeResolverHelper : public QObject
00050 {
00051     Q_OBJECT
00052     
00053 public:
00054     KMimeTypeResolverHelper( KMimeTypeResolverBase *resolver, 
00055                              QScrollView *view )
00056         : m_resolver( resolver ), 
00057           m_timer( new QTimer( this ) )
00058     {
00059         connect( m_timer, SIGNAL( timeout() ), SLOT( slotProcessMimeIcons() ));
00060         
00061         connect( view->horizontalScrollBar(), SIGNAL( sliderMoved(int) ),
00062                  SLOT( slotAdjust() ) );
00063         connect( view->verticalScrollBar(), SIGNAL( sliderMoved(int) ),
00064                  SLOT( slotAdjust() ) );
00065 
00066         view->viewport()->installEventFilter( this );
00067     }
00068     
00069     void start( int delay, bool singleShot )
00070     {
00071         m_timer->start( delay, singleShot );
00072     }
00073                    
00074 protected:
00075     virtual bool eventFilter( QObject *o, QEvent *e )
00076     {
00077         bool ret = QObject::eventFilter( o, e );
00078 
00079         if ( e->type() == QEvent::Resize )
00080             m_resolver->slotViewportAdjusted();
00081         
00082         return ret;
00083     }
00084     
00085 private slots:
00086     void slotProcessMimeIcons()
00087     {
00088         m_resolver->slotProcessMimeIcons();
00089     }
00090 
00091     void slotAdjust()
00092     {
00093         m_resolver->slotViewportAdjusted();
00094     }
00095     
00096 private:
00097     KMimeTypeResolverBase *m_resolver;
00098     QTimer *m_timer;
00099 };
00100 
00117 template<class IconItem, class Parent>
00118 class KMimeTypeResolver : public KMimeTypeResolverBase // if only this could be a QObject....
00119 {
00120 public:
00121     KMimeTypeResolver( Parent * parent )
00122         : m_parent(parent),
00123           m_helper( new KMimeTypeResolverHelper(this, parent->scrollWidget())),
00124           m_delayNonVisibleIcons(10)
00125     {}
00126 
00127     virtual ~KMimeTypeResolver() { 
00128         delete m_helper;
00129     }
00130 
00137     void start( uint delayNonVisibleIcons = 10 )
00138     {
00139         m_helper->start( 0, true /* single shot */ );
00140         m_delayNonVisibleIcons = delayNonVisibleIcons;
00141     }
00142 
00147     QPtrList<IconItem> m_lstPendingMimeIconItems;
00148 
00152     virtual void slotViewportAdjusted();
00153 
00157     virtual void slotProcessMimeIcons();
00158 
00159 private:
00166     IconItem * findVisibleIcon();
00167 
00168     Parent * m_parent;
00169     KMimeTypeResolverHelper *m_helper;
00170     uint m_delayNonVisibleIcons;
00171 };
00172 
00173 // The main slot
00174 template<class IconItem, class Parent>
00175 inline void KMimeTypeResolver<IconItem, Parent>::slotProcessMimeIcons()
00176 {
00177     //kdDebug(1203) << "KMimeTypeResolver::slotProcessMimeIcons() "
00178     //              << m_lstPendingMimeIconItems.count() << endl;
00179     IconItem * item = 0L;
00180     int nextDelay = 0;
00181 
00182     if ( m_lstPendingMimeIconItems.count() > 0 )
00183     {
00184         // We only find mimetypes for icons that are visible. When more
00185         // of our viewport is exposed, we'll get a signal and then get
00186         // the mimetypes for the newly visible icons. (Rikkus)
00187         item = findVisibleIcon();
00188     }
00189 
00190     // No more visible items.
00191     if (0 == item)
00192     {
00193         // Do the unvisible ones, then, but with a bigger delay, if so configured
00194         if ( m_lstPendingMimeIconItems.count() > 0 )
00195         {
00196             item = m_lstPendingMimeIconItems.first();
00197             nextDelay = m_delayNonVisibleIcons;
00198         }
00199         else
00200         {
00201             m_parent->mimeTypeDeterminationFinished();
00202             return;
00203         }
00204     }
00205 
00206     m_parent->determineIcon(item);
00207     m_lstPendingMimeIconItems.remove(item);
00208     m_helper->start( nextDelay, true /* single shot */ );
00209 }
00210 
00211 template<class IconItem, class Parent>
00212 inline void KMimeTypeResolver<IconItem, Parent>::slotViewportAdjusted()
00213 {
00214     if (m_lstPendingMimeIconItems.isEmpty()) return;
00215     IconItem * item = findVisibleIcon();
00216     if (item)
00217     {
00218         m_parent->determineIcon( item );
00219         m_lstPendingMimeIconItems.remove(item);
00220         m_helper->start( 0, true /* single shot */ );
00221     }
00222 }
00223 
00224 template<class IconItem, class Parent>
00225 inline IconItem * KMimeTypeResolver<IconItem, Parent>::findVisibleIcon()
00226 {
00227     // Find an icon that's visible and whose mimetype we don't know.
00228 
00229     QPtrListIterator<IconItem> it(m_lstPendingMimeIconItems);
00230     if ( m_lstPendingMimeIconItems.count()<20) // for few items, it's faster to not bother
00231         return m_lstPendingMimeIconItems.first();
00232 
00233     QScrollView * view = m_parent->scrollWidget();
00234     QRect visibleContentsRect
00235         (
00236             view->viewportToContents(QPoint(0, 0)),
00237             view->viewportToContents
00238             (
00239                 QPoint(view->visibleWidth(), view->visibleHeight())
00240                 )
00241             );
00242 
00243     for (; it.current(); ++it)
00244         if (visibleContentsRect.intersects(it.current()->rect()))
00245             return it.current();
00246 
00247     return 0L;
00248 }
00249 
00250 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:31 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001