]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.h
Ported KIcon to QIcon
[dolphin.git] / src / kitemviews / kfileitemmodel.h
index ef9dc98b9a58554f99b4afa0c64b639804ea7215..62a283d33539f17afb053a641a1521bd606642d0 100644 (file)
@@ -27,6 +27,7 @@
 #include <kitemviews/private/kfileitemmodelfilter.h>
 
 #include <QHash>
+#include <QSet>
 
 class KFileItemModelDirLister;
 class QTimer;
@@ -99,7 +100,7 @@ public:
     bool showDirectoriesOnly() const;
 
     /** @reimp */
-    virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
+    virtual QMimeData* createMimeData(const KItemSet& indexes) const;
 
     /** @reimp */
     virtual int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const;
@@ -129,14 +130,14 @@ public:
 
     /**
      * @return The index for the file-item \a item. -1 is returned if no file-item
-     *         is found or if the file-item is null. The runtime
+     *         is found or if the file-item is null. The amortized runtime
      *         complexity of this call is O(1).
      */
     int index(const KFileItem& item) const;
 
     /**
      * @return The index for the URL \a url. -1 is returned if no file-item
-     *         is found. The runtime complexity of this call is O(1).
+     *         is found. The amortized runtime complexity of this call is O(1).
      */
     int index(const KUrl& url) const;
 
@@ -186,14 +187,14 @@ public:
     {   QByteArray role;
         QString translation;
         QString group;
-        bool requiresNepomuk;
+        bool requiresBaloo;
         bool requiresIndexer;
     };
 
     /**
      * @return Provides static information for all available roles that
      *         are supported by KFileItemModel. Some roles can only be
-     *         determined if Nepomuk is enabled and/or the Nepomuk
+     *         determined if Baloo is enabled and/or the Baloo
      *         indexing is enabled.
      */
     static QList<RoleInfo> rolesInformation();
@@ -272,11 +273,10 @@ private slots:
 
     void slotCompleted();
     void slotCanceled();
-    void slotNewItems(const KFileItemList& items);
+    void slotItemsAdded(const KUrl& directoryUrl, const KFileItemList& items);
     void slotItemsDeleted(const KFileItemList& items);
     void slotRefreshItems(const QList<QPair<KFileItem, KFileItem> >& items);
     void slotClear();
-    void slotClear(const KUrl& url);
     void slotNaturalSortingChanged();
 
     void dispatchPendingItemsToInsert();
@@ -286,7 +286,7 @@ private:
         // User visible roles:
         NoRole, NameRole, SizeRole, DateRole, PermissionsRole, OwnerRole,
         GroupRole, TypeRole, DestinationRole, PathRole,
-        // User visible roles available with Nepomuk:
+        // User visible roles available with Baloo:
         CommentRole, TagsRole, RatingRole, ImageSizeRole, OrientationRole,
         WordCountRole, LineCountRole, ArtistRole, AlbumRole, DurationRole, TrackRole,
         CopiedFromRole,
@@ -303,8 +303,13 @@ private:
         ItemData* parent;
     };
 
-    void insertItems(const KFileItemList& items);
-    void removeItems(const KFileItemList& items);
+    enum RemoveItemsBehavior {
+        KeepItemData,
+        DeleteItemData
+    };
+
+    void insertItems(QList<ItemData*>& items);
+    void removeItems(const KItemRangeList& itemRanges, RemoveItemsBehavior behavior);
 
     /**
      * Helper method for insertItems() and removeItems(): Creates
@@ -312,10 +317,26 @@ private:
      * Note that the ItemData instances are created dynamically and
      * must be deleted by the caller.
      */
-    QList<ItemData*> createItemDataList(const KFileItemList& items) const;
+    QList<ItemData*> createItemDataList(const KUrl& parentUrl, const KFileItemList& items) const;
+
+    /**
+     * Prepares the items for sorting. Normally, the hash 'values' in ItemData is filled
+     * lazily to save time and memory, but for some sort roles, it is expected that the
+     * sort role data is stored in 'values'.
+     */
+    void prepareItemsForSorting(QList<ItemData*>& itemDataList);
+
+    static int expandedParentsCount(const ItemData* data);
 
     void removeExpandedItems();
 
+    /**
+     * This function is called by setData() and slotRefreshItems(). It emits
+     * the itemsChanged() signal, checks if the sort order is still correct,
+     * and starts m_resortAllItemsTimer if that is not the case.
+     */
+    void emitItemsChangedAndTriggerResorting(const KItemRangeList& itemRanges, const QSet<QByteArray>& changedRoles);
+
     /**
      * Resets all values from m_requestRole to false.
      */
@@ -333,7 +354,13 @@ private:
      */
     QByteArray roleForType(RoleType roleType) const;
 
-    QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const;
+    QHash<QByteArray, QVariant> retrieveData(const KFileItem& item, const ItemData* parent) const;
+
+    /**
+     * @return True if \a a has a KFileItem whose text is 'less than' the one
+     *         of \a b according to QString::operator<(const QString&).
+     */
+    static bool nameLessThan(const ItemData* a, const ItemData* b);
 
     /**
      * @return True if the item-data \a a should be ordered before the item-data
@@ -341,6 +368,12 @@ private:
      */
     bool lessThan(const ItemData* a, const ItemData* b) const;
 
+    /**
+     * Sorts the items between \a begin and \a end using the comparison
+     * function lessThan().
+     */
+    void sort(QList<ItemData*>::iterator begin, QList<ItemData*>::iterator end) const;
+
     /**
      * Helper method for lessThan() and expandedParentsCountCompare(): Compares
      * the passed item-data using m_sortRole as criteria. Both items must
@@ -350,22 +383,6 @@ private:
 
     int stringCompare(const QString& a, const QString& b) const;
 
-    /**
-     * Compares the expansion level of both items. The "expansion level" is defined
-     * by the number of parent directories. However simply comparing just the numbers
-     * is not sufficient, it is also important to check the hierarchy for having
-     * a correct order like shown in a tree.
-     */
-    int expandedParentsCountCompare(const ItemData* a, const ItemData* b) const;
-
-    /**
-     * Helper method for expandedParentsCountCompare().
-     */
-    QString subPath(const KFileItem& item,
-                    const QString& itemPath,
-                    int start,
-                    bool* isDir) const;
-
     bool useMaximumUpdateInterval() const;
 
     QList<QPair<int, QVariant> > nameRoleGroups() const;
@@ -385,11 +402,6 @@ private:
      */
     bool isChildItem(int index) const;
 
-    /**
-     * @return Recursive list of child items that have \a item as upper most parent.
-     */
-    KFileItemList childItems(const KFileItem& item) const;
-
     /**
      * Is invoked by KFileItemModelRolesUpdater and results in emitting the
      * sortProgress signal with a percent-value of the progress.
@@ -401,6 +413,12 @@ private:
      */
     void applyFilters();
 
+    /**
+     * Removes filtered items whose expanded parents have been deleted
+     * or collapsed via setExpanded(parentIndex, false).
+     */
+    void removeFilteredChildren(const KItemRangeList& parents);
+
     /**
      * Maps the QByteArray-roles to RoleTypes and provides translation- and
      * group-contexts.
@@ -413,7 +431,7 @@ private:
         const char* const roleTranslation;
         const char* const groupTranslationContext;
         const char* const groupTranslation;
-        const bool requiresNepomuk;
+        const bool requiresBaloo;
         const bool requiresIndexer;
     };
 
@@ -428,6 +446,17 @@ private:
      */
     static void determineMimeTypes(const KFileItemList& items, int timeout);
 
+    /**
+     * @return Returns a copy of \a value that is implicitly shared
+     * with other users to save memory.
+     */
+    static QByteArray sharedValue(const QByteArray& value);
+
+    /**
+     * Checks if the model's internal data structures are consistent.
+     */
+    bool isConsistent() const;
+
 private:
     KFileItemModelDirLister* m_dirLister;
 
@@ -440,52 +469,53 @@ private:
     Qt::CaseSensitivity m_caseSensitivity;
 
     QList<ItemData*> m_itemData;
-    QHash<KUrl, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
+
+    // m_items is a cache for the method index(const KUrl&). If it contains N
+    // entries, it is guaranteed that these correspond to the first N items in
+    // the model, i.e., that (for every i between 0 and N - 1)
+    // m_items.value(fileItem(i).url()) == i
+    mutable QHash<KUrl, int> m_items;
 
     KFileItemModelFilter m_filter;
-    QSet<KFileItem> m_filteredItems; // Items that got hidden by KFileItemModel::setNameFilter()
+    QHash<KFileItem, ItemData*> m_filteredItems; // Items that got hidden by KFileItemModel::setNameFilter()
 
     bool m_requestRole[RolesCount];
 
     QTimer* m_maximumUpdateIntervalTimer;
     QTimer* m_resortAllItemsTimer;
-    KFileItemList m_pendingItemsToInsert;
+    QList<ItemData*> m_pendingItemsToInsert;
 
     // Cache for KFileItemModel::groups()
     mutable QList<QPair<int, QVariant> > m_groups;
 
-    // Stores the smallest expansion level of the root-URL. Is required to calculate
-    // the "expandedParentsCount" role in an efficient way. A value < 0 indicates a
-    // special meaning:
-    enum ExpandedParentsCountRootTypes
-    {
-        // m_expandedParentsCountRoot is uninitialized and must be determined by checking
-        // the root URL from the KDirLister.
-        UninitializedExpandedParentsCountRoot = -1,
-        // All items should be forced to get an expanded parents count of 0 even if they
-        // represent child items. This is useful for slaves that provide no parent items
-        // for child items like e.g. the search IO slaves.
-        ForceExpandedParentsCountRoot = -2
-    };
-    mutable int m_expandedParentsCountRoot;
-
-    // Stores the URLs of the expanded directories.
-    QSet<KUrl> m_expandedDirs;
+    // Stores the URLs (key: target url, value: url) of the expanded directories.
+    QHash<KUrl, KUrl> m_expandedDirs;
 
     // URLs that must be expanded. The expanding is initially triggered in setExpanded()
     // and done step after step in slotCompleted().
     QSet<KUrl> m_urlsToExpand;
 
-    friend class KFileItemModelSortAlgorithm;  // Accesses lessThan() method
+    friend class KFileItemModelLessThan;       // Accesses lessThan() method
     friend class KFileItemModelRolesUpdater;   // Accesses emitSortProgress() method
     friend class KFileItemModelTest;           // For unit testing
+    friend class KFileItemModelBenchmark;      // For unit testing
     friend class KFileItemListViewTest;        // For unit testing
     friend class DolphinPart;                  // Accesses m_dirLister
 };
 
+inline bool KFileItemModel::nameLessThan(const ItemData* a, const ItemData* b)
+{
+    return a->item.text() < b->item.text();
+}
+
+
 inline bool KFileItemModel::isChildItem(int index) const
 {
-    return m_requestRole[ExpandedParentsCountRole] && m_itemData.at(index)->values.value("expandedParentsCount").toInt() > 0;
+    if (m_itemData.at(index)->parent) {
+        return true;
+    } else {
+        return false;
+    }
 }
 
 #endif