File indexing completed on 2024-04-28 05:45:07

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * Based on the Itemviews NG project from Trolltech Labs
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KITEMLISTSELECTIONMANAGER_H
0010 #define KITEMLISTSELECTIONMANAGER_H
0011 
0012 #include "dolphin_export.h"
0013 #include "kitemviews/kitemmodelbase.h"
0014 #include "kitemviews/kitemset.h"
0015 
0016 #include <QObject>
0017 
0018 class KItemModelBase;
0019 
0020 /**
0021  * @brief Allows to select and deselect items of a KItemListView.
0022  */
0023 class DOLPHIN_EXPORT KItemListSelectionManager : public QObject
0024 {
0025     Q_OBJECT
0026 
0027     enum RangesRemovingBehaviour { DiscardRemovedIndex, AdjustRemovedIndex };
0028 
0029 public:
0030     enum SelectionMode { Select, Deselect, Toggle };
0031 
0032     explicit KItemListSelectionManager(QObject *parent = nullptr);
0033     ~KItemListSelectionManager() override;
0034 
0035     void setCurrentItem(int current);
0036     int currentItem() const;
0037 
0038     void setSelectedItems(const KItemSet &items);
0039     KItemSet selectedItems() const;
0040     bool isSelected(int index) const;
0041     bool hasSelection() const;
0042 
0043     void setSelected(int index, int count = 1, SelectionMode mode = Select);
0044     /**
0045      * Equivalent to:
0046      * clearSelection();
0047      * setSelected(index, count);
0048      * but emitting once only selectionChanged signal
0049      */
0050     void replaceSelection(int index, int count = 1);
0051     void clearSelection();
0052 
0053     void beginAnchoredSelection(int anchor);
0054     void endAnchoredSelection();
0055     bool isAnchoredSelectionActive() const;
0056 
0057     KItemModelBase *model() const;
0058 
0059 Q_SIGNALS:
0060     void currentChanged(int current, int previous);
0061     void selectionChanged(const KItemSet &current, const KItemSet &previous);
0062 
0063 private:
0064     void setModel(KItemModelBase *model);
0065     void itemsInserted(const KItemRangeList &itemRanges);
0066     void itemsRemoved(const KItemRangeList &itemRanges);
0067     void itemsMoved(const KItemRange &itemRange, const QList<int> &movedToIndexes);
0068 
0069     /**
0070      * Helper method for itemsRemoved. Returns the changed index after removing
0071      * the given range. If the index is part of the range, -1 will be returned.
0072      */
0073     int indexAfterRangesRemoving(int index, const KItemRangeList &itemRanges, const RangesRemovingBehaviour behaviour) const;
0074 
0075 private:
0076     int m_currentItem;
0077     int m_anchorItem;
0078     KItemSet m_selectedItems;
0079     bool m_isAnchoredSelectionActive;
0080 
0081     KItemModelBase *m_model;
0082 
0083     friend class KItemListController; // Calls setModel()
0084     friend class KItemListView; // Calls itemsInserted(), itemsRemoved() and itemsMoved()
0085     friend class KItemListSelectionManagerTest;
0086 };
0087 
0088 #endif