• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kio
kmimetyperesolver.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000, 2006 David Faure <faure@kde.org>
3  Copyright (C) 2000 Rik Hemsley <rik@kde.org>
4  Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kmimetyperesolver.h"
22 #include <kdebug.h>
23 #include "defaultviewadapter_p.h"
24 #include <kdirmodel.h>
25 #include <kfileitem.h>
26 #include <QAbstractItemView>
27 #include <QAbstractProxyModel>
28 #include <QScrollBar>
29 #include <QTimer>
30 
31 class KMimeTypeResolverPrivate
32 {
33 public:
34  KMimeTypeResolverPrivate(KMimeTypeResolver *parent)
35  : q(parent),
36  m_delayForNonVisibleIcons(10), // TODO set me to 0 when image preview is enabled
37  m_noVisibleIcon(false)
38  {
39  m_timer.setSingleShot(true);
40  }
41 
42 
43  void _k_slotRowsInserted(const QModelIndex&,int,int);
44  void _k_slotViewportAdjusted();
45  void _k_slotProcessMimeIcons();
46 
47  void init();
48  QModelIndex findVisibleIcon();
49 
50  KMimeTypeResolver* q;
51  KAbstractViewAdapter* m_adapter;
52  QAbstractProxyModel* m_proxyModel;
53  KDirModel* m_dirModel;
54  int m_delayForNonVisibleIcons;
55  QList<QPersistentModelIndex> m_pendingIndexes;
56  QTimer m_timer;
57  // Set to true when findVisibleIcon found no visible index in m_pendingIndexes.
58  // This makes further calls to findVisibleIcon no-ops until this bool is reset to false.
59  bool m_noVisibleIcon;
60 };
61 
62 void KMimeTypeResolverPrivate::init()
63 {
64  QObject::connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
65  q, SLOT(_k_slotRowsInserted(QModelIndex,int,int)));
66  QObject::connect(&m_timer, SIGNAL(timeout()),
67  q, SLOT(_k_slotProcessMimeIcons()));
68  m_adapter->connect(KAbstractViewAdapter::ScrollBarValueChanged, q, SLOT(_k_slotViewportAdjusted()));
69 }
70 
71 QModelIndex KMimeTypeResolverPrivate::findVisibleIcon()
72 {
73  if (m_noVisibleIcon)
74  return QModelIndex();
75 
76  if (m_pendingIndexes.count() < 20) { // for few items, it's faster to not bother
77  //kDebug() << "Few items, returning first one";
78  return QModelIndex(m_pendingIndexes.first());
79  }
80 
81  const QRect visibleArea = m_adapter->visibleArea();
82  QList<QPersistentModelIndex>::const_iterator it = m_pendingIndexes.constBegin();
83  const QList<QPersistentModelIndex>::const_iterator end = m_pendingIndexes.constEnd();
84  bool layoutDone = true;
85  for ( ; it != end ; ++it ) {
86  const QModelIndex index = m_proxyModel ? m_proxyModel->mapFromSource(*it) : QModelIndex(*it);
87  const QRect rect = m_adapter->visualRect(index);
88  if (rect.isNull())
89  layoutDone = false;
90  else if (rect.intersects(visibleArea)) {
91  //kDebug() << "found item at " << rect << " in visibleArea " << visibleArea;
92  return QModelIndex(*it);
93  }
94  }
95 
96  if (layoutDone) {
97  //kDebug() << "no more visible icon found";
98  m_noVisibleIcon = true;
99  return QModelIndex();
100  } else {
101  // Return a random index if the layout is still ongoing
102  return QModelIndex(m_pendingIndexes.first());
103  }
104 }
105 
107 
108 KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, KDirModel* model)
109  : QObject(view), d(new KMimeTypeResolverPrivate(this))
110 {
111  d->m_adapter = new KIO::DefaultViewAdapter(view, this);
112  d->m_proxyModel = 0;
113  d->m_dirModel = model;
114  d->init();
115 }
116 
117 KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, QAbstractProxyModel* model)
118  : QObject(view), d(new KMimeTypeResolverPrivate(this))
119 {
120  d->m_adapter = new KIO::DefaultViewAdapter(view, this);
121  d->m_proxyModel = model;
122  d->m_dirModel = static_cast<KDirModel*>(model->sourceModel());
123  d->init();
124 }
125 
126 KMimeTypeResolver::KMimeTypeResolver(KAbstractViewAdapter* adapter)
127  : QObject(adapter), d(new KMimeTypeResolverPrivate(this))
128 {
129  QAbstractItemModel *model = adapter->model();
130  d->m_adapter = adapter;
131  d->m_proxyModel = qobject_cast<QAbstractProxyModel*>(model);
132  d->m_dirModel = d->m_proxyModel ? qobject_cast<KDirModel*>(d->m_proxyModel->sourceModel())
133  : qobject_cast<KDirModel*>(model);
134  Q_ASSERT(d->m_dirModel);
135  d->init();
136 }
137 
138 KMimeTypeResolver::~KMimeTypeResolver()
139 {
140  delete d;
141 }
142 
143 void KMimeTypeResolverPrivate::_k_slotProcessMimeIcons()
144 {
145  if (m_pendingIndexes.isEmpty()) {
146  // Finished
147  return;
148  }
149 
150  int nextDelay = 0;
151  QModelIndex index = findVisibleIcon();
152  if (index.isValid()) {
153  // Found a visible item.
154  const int numFound = m_pendingIndexes.removeAll(index);
155  Q_UNUSED(numFound); // prevent warning in release builds.
156  Q_ASSERT(numFound == 1);
157  } else {
158  // No more visible items.
159  // Do the unvisible ones, then, but with a bigger delay, if so configured
160  index = m_pendingIndexes.takeFirst();
161  nextDelay = m_delayForNonVisibleIcons;
162  }
163  KFileItem item = m_dirModel->itemForIndex(index);
164  if (!item.isNull()) { // check that item still exists
165  if (!item.isMimeTypeKnown()) { // check if someone did it meanwhile
166  item.determineMimeType();
167  m_dirModel->itemChanged(index);
168  }
169  }
170  m_timer.start(nextDelay); // singleshot
171 }
172 
173 void KMimeTypeResolverPrivate::_k_slotRowsInserted(const QModelIndex& parent, int first, int last)
174 {
175  KDirModel* model = m_dirModel;
176  for (int row = first; row <= last; ++row) {
177  QModelIndex idx = model->index(row, 0, parent);
178  KFileItem item = model->itemForIndex(idx);
179  if (!item.isMimeTypeKnown())
180  m_pendingIndexes.append(idx);
181  // TODO else if (item->isDir() && !item->isLocalFile() /*nor pseudo local...*/ &&
182  // TODO model->data(idx, ChildCountRole).toInt() == KDirModel::ChildCountUnknown)
183  // TODO d->m_pendingIndexes.append(idx);
184  }
185  m_noVisibleIcon = false;
186  m_timer.start(m_delayForNonVisibleIcons); // singleshot
187 }
188 
189 void KMimeTypeResolverPrivate::_k_slotViewportAdjusted()
190 {
191  m_noVisibleIcon = false;
192  m_timer.start(0);
193 }
194 
195 #include "kmimetyperesolver.moc"
QModelIndex
KFileItem::determineMimeType
KMimeType::Ptr determineMimeType() const
Returns the mimetype of the file item.
Definition: kfileitem.cpp:779
kdebug.h
QAbstractItemView
QAbstractProxyModel
KIO::DefaultViewAdapter
Implementation of the view adapter for the default case when an instance of QAbstractItemView is used...
Definition: defaultviewadapter_p.h:34
KMimeTypeResolver::KMimeTypeResolver
KMimeTypeResolver(QAbstractItemView *view, KDirModel *model)
The mimetype resolver is made a child of the view.
Definition: kmimetyperesolver.cpp:108
KAbstractViewAdapter
Definition: kabstractviewadapter_p.h:36
timeout
int timeout
KFileItem::isNull
bool isNull() const
Return true if default-constructed.
Definition: kfileitem.cpp:1714
QRect::intersects
bool intersects(const QRect &rectangle) const
KDirModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented from QAbstractItemModel. O(1)
Definition: kdirmodel.cpp:979
KDirModel::itemForIndex
KFileItem itemForIndex(const QModelIndex &index) const
Return the fileitem for a given index.
Definition: kdirmodel.cpp:943
QRect
QModelIndex::isValid
bool isValid() const
QTimer
QObject
QList< QPersistentModelIndex >
kmimetyperesolver.h
KMimeTypeResolver
This class implements the "delayed-mimetype-determination" feature, for directory views...
Definition: kmimetyperesolver.h:45
QRect::isNull
bool isNull() const
kdirmodel.h
QAbstractProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
defaultviewadapter_p.h
KAbstractViewAdapter::ScrollBarValueChanged
Definition: kabstractviewadapter_p.h:40
KMimeTypeResolver::~KMimeTypeResolver
~KMimeTypeResolver()
Definition: kmimetyperesolver.cpp:138
QAbstractItemModel
KFileItem::isMimeTypeKnown
bool isMimeTypeKnown() const
Definition: kfileitem.cpp:804
QList::constBegin
const_iterator constBegin() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KAbstractViewAdapter::model
virtual QAbstractItemModel * model() const =0
end
const KShortcut & end()
KDirModel
A model for a KIO-based directory tree.
Definition: kdirmodel.h:48
kfileitem.h
KFileItem
A KFileItem is a generic class to handle a file, local or remote.
Definition: kfileitem.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal