]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.h
GIT_SILENT Update Appstream for new release
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifndef KITEMLISTSELECTIONMANAGER_H
10 #define KITEMLISTSELECTIONMANAGER_H
11
12 #include "dolphin_export.h"
13 #include "kitemviews/kitemmodelbase.h"
14 #include "kitemviews/kitemset.h"
15
16 #include <QObject>
17
18 class KItemModelBase;
19
20 /**
21 * @brief Allows to select and deselect items of a KItemListView.
22 */
23 class DOLPHIN_EXPORT KItemListSelectionManager : public QObject
24 {
25 Q_OBJECT
26
27 enum RangesRemovingBehaviour { DiscardRemovedIndex, AdjustRemovedIndex };
28
29 public:
30 enum SelectionMode { Select, Deselect, Toggle };
31
32 explicit KItemListSelectionManager(QObject *parent = nullptr);
33 ~KItemListSelectionManager() override;
34
35 void setCurrentItem(int current);
36 int currentItem() const;
37
38 void setSelectedItems(const KItemSet &items);
39 KItemSet selectedItems() const;
40 bool isSelected(int index) const;
41 bool hasSelection() const;
42
43 void setSelected(int index, int count = 1, SelectionMode mode = Select);
44 /**
45 * Equivalent to:
46 * clearSelection();
47 * setSelected(index, count);
48 * but emitting once only selectionChanged signal
49 */
50 void replaceSelection(int index, int count = 1);
51 void clearSelection();
52
53 void beginAnchoredSelection(int anchor);
54 void endAnchoredSelection();
55 bool isAnchoredSelectionActive() const;
56
57 KItemModelBase *model() const;
58
59 Q_SIGNALS:
60 void currentChanged(int current, int previous);
61 void selectionChanged(const KItemSet &current, const KItemSet &previous);
62
63 private:
64 void setModel(KItemModelBase *model);
65 void itemsInserted(const KItemRangeList &itemRanges);
66 void itemsRemoved(const KItemRangeList &itemRanges);
67 void itemsMoved(const KItemRange &itemRange, const QList<int> &movedToIndexes);
68
69 /**
70 * Helper method for itemsRemoved. Returns the changed index after removing
71 * the given range. If the index is part of the range, -1 will be returned.
72 */
73 int indexAfterRangesRemoving(int index, const KItemRangeList &itemRanges, const RangesRemovingBehaviour behaviour) const;
74
75 private:
76 int m_currentItem;
77 int m_anchorItem;
78 KItemSet m_selectedItems;
79 bool m_isAnchoredSelectionActive;
80
81 KItemModelBase *m_model;
82
83 friend class KItemListController; // Calls setModel()
84 friend class KItemListView; // Calls itemsInserted(), itemsRemoved() and itemsMoved()
85 friend class KItemListSelectionManagerTest;
86 };
87
88 #endif