kio
kmimetyperesolver.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 );
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
00179 template<class IconItem, class Parent>
00180 inline void KMimeTypeResolver<IconItem, Parent>::slotProcessMimeIcons()
00181 {
00182
00183
00184 IconItem * item = 0L;
00185 int nextDelay = 0;
00186
00187 if ( m_lstPendingMimeIconItems.count() > 0 )
00188 {
00189
00190
00191
00192 item = findVisibleIcon();
00193 }
00194
00195
00196 if (0 == item)
00197 {
00198
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 );
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 );
00226 }
00227 }
00228
00229 template<class IconItem, class Parent>
00230 inline IconItem * KMimeTypeResolver<IconItem, Parent>::findVisibleIcon()
00231 {
00232
00233
00234 QPtrListIterator<IconItem> it(m_lstPendingMimeIconItems);
00235 if ( m_lstPendingMimeIconItems.count()<20)
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