File indexing completed on 2024-04-14 03:54:01

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2007, 2009 Rafael Fernández López <ereslibre@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KCATEGORIZEDVIEW_P_H
0009 #define KCATEGORIZEDVIEW_P_H
0010 
0011 #include "kcategorizedview.h"
0012 
0013 class KCategorizedSortFilterProxyModel;
0014 class KCategoryDrawer;
0015 class KCategoryDrawerV2;
0016 class KCategoryDrawerV3;
0017 
0018 /**
0019  * @internal
0020  */
0021 class KCategorizedViewPrivate
0022 {
0023 public:
0024     struct Block;
0025     struct Item;
0026 
0027     explicit KCategorizedViewPrivate(KCategorizedView *qq);
0028     ~KCategorizedViewPrivate();
0029 
0030     /**
0031      * @return whether this view has all required elements to be categorized.
0032      */
0033     bool isCategorized() const;
0034 
0035     /**
0036      * Wrapper that returns the view's QStyleOptionViewItem, in Qt5 using viewOptions(), and
0037      * in Qt6 using initViewItemOption().
0038      */
0039     QStyleOptionViewItem viewOpts();
0040 
0041     /**
0042      * @return the block rect for the representative @p representative.
0043      */
0044     QStyleOptionViewItem blockRect(const QModelIndex &representative);
0045 
0046     /**
0047      * Returns the first and last element that intersects with rect.
0048      *
0049      * @note see that here we cannot take out items between first and last (as we could
0050      *       do with the rubberband).
0051      *
0052      * Complexity: O(log(n)) where n is model()->rowCount().
0053      */
0054     std::pair<QModelIndex, QModelIndex> intersectingIndexesWithRect(const QRect &rect) const;
0055 
0056     /**
0057      * Returns the position of the block of @p category.
0058      *
0059      * Complexity: O(n) where n is the number of different categories when the block has been
0060      *             marked as in quarantine. O(1) the rest of the times (the vast majority).
0061      */
0062     QPoint blockPosition(const QString &category);
0063 
0064     /**
0065      * Returns the height of the block determined by @p category.
0066      */
0067     int blockHeight(const QString &category);
0068 
0069     /**
0070      * Returns the actual viewport width.
0071      */
0072     int viewportWidth() const;
0073 
0074     /**
0075      * Marks all elements as in quarantine.
0076      *
0077      * Complexity: O(n) where n is model()->rowCount().
0078      *
0079      * @warning this is an expensive operation
0080      */
0081     void regenerateAllElements();
0082 
0083     /**
0084      * Update internal information, and keep sync with the real information that the model contains.
0085      */
0086     void rowsInserted(const QModelIndex &parent, int start, int end);
0087 
0088     /**
0089      * Returns @p rect in viewport terms, taking in count horizontal and vertical offsets.
0090      */
0091     QRect mapToViewport(const QRect &rect) const;
0092 
0093     /**
0094      * Returns @p rect in absolute terms, converted from viewport position.
0095      */
0096     QRect mapFromViewport(const QRect &rect) const;
0097 
0098     /**
0099      * Returns the height of the highest element in last row. This is only applicable if there is
0100      * no grid set and uniformItemSizes is false.
0101      *
0102      * @param block in which block are we searching. Necessary to stop the search if we hit the
0103      *              first item in this block.
0104      */
0105     int highestElementInLastRow(const Block &block) const;
0106 
0107     /**
0108      * Returns whether the view has a valid grid size.
0109      */
0110     bool hasGrid() const;
0111 
0112     /**
0113      * Returns the category for the given index.
0114      */
0115     QString categoryForIndex(const QModelIndex &index) const;
0116 
0117     /**
0118      * Updates the visual rect for item when flow is LeftToRight.
0119      */
0120     void leftToRightVisualRect(const QModelIndex &index, Item &item, const Block &block, const QPoint &blockPos) const;
0121 
0122     /**
0123      * Updates the visual rect for item when flow is TopToBottom.
0124      * @note we only support viewMode == ListMode in this case.
0125      */
0126     void topToBottomVisualRect(const QModelIndex &index, Item &item, const Block &block, const QPoint &blockPos) const;
0127 
0128     /**
0129      * Called when expand or collapse has been clicked on the category drawer.
0130      */
0131     void _k_slotCollapseOrExpandClicked(QModelIndex);
0132 
0133     KCategorizedView *const q;
0134     KCategorizedSortFilterProxyModel *proxyModel = nullptr;
0135     KCategoryDrawer *categoryDrawer = nullptr;
0136     int categorySpacing = 0;
0137     bool alternatingBlockColors = false;
0138     bool collapsibleBlocks = false;
0139 
0140     Block *const hoveredBlock;
0141     QString hoveredCategory;
0142     QModelIndex hoveredIndex;
0143 
0144     QPoint pressedPosition;
0145     QRect rubberBandRect;
0146 
0147     QHash<QString, Block> blocks;
0148 };
0149 
0150 #endif // KCATEGORIZEDVIEW_P_H