# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 KIO_EXPORT 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 KIO_EXPORT 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:
    00125     KMimeTypeResolver( Parent * parent )
    00126         : m_parent(parent),
    00127           m_helper( new KMimeTypeResolverHelper(this, parent->scrollWidget())),
    00128           m_delayNonVisibleIcons(10)
    00129     {}
    00130
    00131     virtual ~KMimeTypeResolver() {
    00132         delete m_helper;
    00133     }
    00134
    00141     void start( uint delayNonVisibleIcons = 10 )
    00142     {
    00143         m_helper->start( 0, true /* single shot */ );
    00144         m_delayNonVisibleIcons = delayNonVisibleIcons;
    00145     }
    00146
    00152     QPtrList<IconItem> m_lstPendingMimeIconItems;
    00153
    00157     virtual void slotViewportAdjusted();
    00158
    00162     virtual void slotProcessMimeIcons();
    00163
    00164 private:
    00171     IconItem * findVisibleIcon();
    00172
    00173     Parent * m_parent;
    00174     KMimeTypeResolverHelper *m_helper;
    00175     uint m_delayNonVisibleIcons;
    00176 };
    00177
    00178 // The main slot
    00179 template<class IconItem, class Parent>
    00180 inline void KMimeTypeResolver<IconItem, Parent>::slotProcessMimeIcons()
    00181 {
    00182     //kdDebug(1203) << "KMimeTypeResolver::slotProcessMimeIcons() "
    00183     //              << m_lstPendingMimeIconItems.count() << endl;
    00184     IconItem * item = 0L;
    00185     int nextDelay = 0;
    00186
    00187     if ( m_lstPendingMimeIconItems.count() > 0 )
    00188     {
    00189         // We only find mimetypes for icons that are visible. When more
    00190         // of our viewport is exposed, we'll get a signal and then get
    00191         // the mimetypes for the newly visible icons. (Rikkus)
    00192         item = findVisibleIcon();
    00193     }
    00194
    00195     // No more visible items.
    00196     if (0 == item)
    00197     {
    00198         // Do the unvisible ones, then, but with a bigger delay, if so configured
    00199         if ( m_lstPendingMimeIconItems.count() > 0 )
    00200         {
    00201             item = m_lstPendingMimeIconItems.first();
    00202             nextDelay = m_delayNonVisibleIcons;
    00203         }
    00204         else
    00205         {
    00206             m_parent->mimeTypeDeterminationFinished();
    00207             return;
    00208         }
    00209     }
    00210
    00211     m_parent->determineIcon(item);
    00212     m_lstPendingMimeIconItems.remove(item);
    00213     m_helper->start( nextDelay, true /* single shot */ );
    00214 }
    00215
    00216 template<class IconItem, class Parent>
    00217 inline void KMimeTypeResolver<IconItem, Parent>::slotViewportAdjusted()
    00218 {
    00219     if (m_lstPendingMimeIconItems.isEmpty()) return;
    00220     IconItem * item = findVisibleIcon();
    00221     if (item)
    00222     {
    00223         m_parent->determineIcon( item );
    00224         m_lstPendingMimeIconItems.remove(item);
    00225         m_helper->start( 0, true /* single shot */ );
    00226     }
    00227 }
    00228
    00229 template<class IconItem, class Parent>
    00230 inline IconItem * KMimeTypeResolver<IconItem, Parent>::findVisibleIcon()
    00231 {
    00232     // Find an icon that's visible and whose mimetype we don't know.
    00233
    00234     QPtrListIterator<IconItem> it(m_lstPendingMimeIconItems);
    00235     if ( m_lstPendingMimeIconItems.count()<20) // for few items, it's faster to not bother
    00236         return m_lstPendingMimeIconItems.first();
    00237
    00238     QScrollView * view = m_parent->scrollWidget();
    00239     QRect visibleContentsRect
    00240         (
    00241             view->viewportToContents(QPoint(0, 0)),
    00242             view->viewportToContents
    00243             (
    00244                 QPoint(view->visibleWidth(), view->visibleHeight())
    00245                 )
    00246             );
    00247
    00248     for (; it.current(); ++it)
    00249         if (visibleContentsRect.intersects(it.current()->rect()))
    00250             return it.current();
    00251
    00252     return 0L;
    00253 }
    00254
    00255 #endif

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:00 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003