KIO
kmimetyperesolver.cpp
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 #include "kmimetyperesolver.h"
00022 #include <kdirmodel.h>
00023 #include <kfileitem.h>
00024 #include <kdirlister.h>
00025 #include <QAbstractItemView>
00026 #include <QScrollBar>
00027 #include <QTimer>
00028
00029 class KMimeTypeResolverPrivate
00030 {
00031 public:
00032 KMimeTypeResolverPrivate()
00033 : m_delayForNonVisibleIcons(10),
00034 m_noVisibleIcon(false)
00035 {
00036 m_timer.setSingleShot(true);
00037 }
00038
00039 void _k_slotRowsInserted(const QModelIndex&,int,int);
00040 void _k_slotViewportAdjusted();
00041 void _k_slotProcessMimeIcons();
00042
00043 QModelIndex findVisibleIcon();
00044
00045 QAbstractItemView* m_view;
00046 KDirModel* m_dirModel;
00047 int m_delayForNonVisibleIcons;
00048 QList<QPersistentModelIndex> m_pendingIndexes;
00049 QTimer m_timer;
00050
00051
00052 bool m_noVisibleIcon;
00053 };
00054
00055
00056 QModelIndex KMimeTypeResolverPrivate::findVisibleIcon()
00057 {
00058 if (m_noVisibleIcon)
00059 return QModelIndex();
00060
00061 if (m_pendingIndexes.count() < 20) {
00062
00063 return QModelIndex(m_pendingIndexes.first());
00064 }
00065
00066 const QRect visibleArea = m_view->viewport()->rect();
00067 QList<QPersistentModelIndex>::const_iterator it = m_pendingIndexes.begin();
00068 const QList<QPersistentModelIndex>::const_iterator end = m_pendingIndexes.end();
00069 for ( ; it != end ; ++it ) {
00070 const QRect rect = m_view->visualRect(*it);
00071 if (rect.intersects(visibleArea)) {
00072
00073 return QModelIndex(*it);
00074 }
00075 }
00076
00077
00078 m_noVisibleIcon = true;
00079 return QModelIndex();
00080 }
00081
00083
00084 KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, KDirModel* model)
00085 : QObject(view), d(new KMimeTypeResolverPrivate)
00086 {
00087 d->m_view = view;
00088 d->m_dirModel = model;
00089 connect(d->m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
00090 this, SLOT(_k_slotRowsInserted(QModelIndex,int,int)));
00091 connect(&d->m_timer, SIGNAL(timeout()),
00092 this, SLOT(_k_slotProcessMimeIcons()));
00093 connect(d->m_view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
00094 this, SLOT(_k_slotViewportAdjusted()));
00095 connect(d->m_view->verticalScrollBar(), SIGNAL(valueChanged(int)),
00096 this, SLOT(_k_slotViewportAdjusted()));
00097 }
00098
00099 KMimeTypeResolver::~KMimeTypeResolver()
00100 {
00101 delete d;
00102 }
00103
00104 void KMimeTypeResolverPrivate::_k_slotProcessMimeIcons()
00105 {
00106 if (m_pendingIndexes.isEmpty()) {
00107
00108 return;
00109 }
00110
00111 int nextDelay = 0;
00112 bool isVisible = false;
00113 QModelIndex index = findVisibleIcon();
00114 if (index.isValid()) {
00115
00116 const int numFound = m_pendingIndexes.removeAll(index);
00117 Q_ASSERT(numFound == 1);
00118 isVisible = true;
00119 } else {
00120
00121
00122 index = m_pendingIndexes.takeFirst();
00123 nextDelay = m_delayForNonVisibleIcons;
00124 }
00125 KFileItem item = m_dirModel->itemForIndex(index);
00126 if (!item.isNull()) {
00127 if (!item.isMimeTypeKnown()) {
00128
00129 item.determineMimeType();
00130 if (isVisible) {
00131 m_dirModel->itemChanged(index);
00132 }
00133 }
00134 }
00135 m_timer.start(nextDelay);
00136 }
00137
00138 void KMimeTypeResolverPrivate::_k_slotRowsInserted(const QModelIndex& parent, int first, int last)
00139 {
00140 KDirModel* model = m_dirModel;
00141 for (int row = first; row <= last; ++row) {
00142 QModelIndex idx = model->index(row, 0, parent);
00143 KFileItem item = model->itemForIndex(idx);
00144 if (!item.isMimeTypeKnown())
00145 m_pendingIndexes.append(idx);
00146
00147
00148
00149 }
00150 m_noVisibleIcon = false;
00151 m_timer.start(m_delayForNonVisibleIcons);
00152 }
00153
00154 void KMimeTypeResolverPrivate::_k_slotViewportAdjusted()
00155 {
00156 m_noVisibleIcon = false;
00157 m_timer.start(0);
00158 }
00159
00160 #include "kmimetyperesolver.moc"