]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.h
Build with QT_NO_KEYWORDS
[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 {
28 DiscardRemovedIndex,
29 AdjustRemovedIndex
30 };
31
32 public:
33 enum SelectionMode {
34 Select,
35 Deselect,
36 Toggle
37 };
38
39 explicit KItemListSelectionManager(QObject* parent = nullptr);
40 ~KItemListSelectionManager() override;
41
42 void setCurrentItem(int current);
43 int currentItem() const;
44
45 void setSelectedItems(const KItemSet& items);
46 KItemSet selectedItems() const;
47 bool isSelected(int index) const;
48 bool hasSelection() const;
49
50 void setSelected(int index, int count = 1, SelectionMode mode = Select);
51 /**
52 * Equivalent to:
53 * clearSelection();
54 * setSelected(index, count);
55 * but emitting once only selectionChanged signal
56 */
57 void replaceSelection(int index, int count = 1);
58 void clearSelection();
59
60 void beginAnchoredSelection(int anchor);
61 void endAnchoredSelection();
62 bool isAnchoredSelectionActive() const;
63
64 KItemModelBase* model() const;
65
66 Q_SIGNALS:
67 void currentChanged(int current, int previous);
68 void selectionChanged(const KItemSet& current, const KItemSet& previous);
69
70 private:
71 void setModel(KItemModelBase* model);
72 void itemsInserted(const KItemRangeList& itemRanges);
73 void itemsRemoved(const KItemRangeList& itemRanges);
74 void itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes);
75
76
77 /**
78 * Helper method for itemsRemoved. Returns the changed index after removing
79 * the given range. If the index is part of the range, -1 will be returned.
80 */
81 int indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges, const RangesRemovingBehaviour behaviour) const;
82
83 private:
84 int m_currentItem;
85 int m_anchorItem;
86 KItemSet m_selectedItems;
87 bool m_isAnchoredSelectionActive;
88
89 KItemModelBase* m_model;
90
91 friend class KItemListController; // Calls setModel()
92 friend class KItemListView; // Calls itemsInserted(), itemsRemoved() and itemsMoved()
93 friend class KItemListSelectionManagerTest;
94 };
95
96 #endif