]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.h
Avoid emitting twice twice selectionChanged when keyboard changes the selection,...
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #ifndef KITEMLISTSELECTIONMANAGER_H
24 #define KITEMLISTSELECTIONMANAGER_H
25
26 #include "dolphin_export.h"
27 #include "kitemviews/kitemmodelbase.h"
28 #include "kitemviews/kitemset.h"
29
30 #include <QObject>
31
32 class KItemModelBase;
33
34 /**
35 * @brief Allows to select and deselect items of a KItemListView.
36 */
37 class DOLPHIN_EXPORT KItemListSelectionManager : public QObject
38 {
39 Q_OBJECT
40
41 enum RangesRemovingBehaviour {
42 DiscardRemovedIndex,
43 AdjustRemovedIndex
44 };
45
46 public:
47 enum SelectionMode {
48 Select,
49 Deselect,
50 Toggle
51 };
52
53 explicit KItemListSelectionManager(QObject* parent = nullptr);
54 ~KItemListSelectionManager() override;
55
56 void setCurrentItem(int current);
57 int currentItem() const;
58
59 void setSelectedItems(const KItemSet& items);
60 KItemSet selectedItems() const;
61 bool isSelected(int index) const;
62 bool hasSelection() const;
63
64 void setSelected(int index, int count = 1, SelectionMode mode = Select);
65 /**
66 * Equivalent to:
67 * clearSelection();
68 * setSelected(index, count);
69 * but emitting once only selectionChanged signal
70 */
71 void replaceSelection(int index, int count = 1);
72 void clearSelection();
73
74 void beginAnchoredSelection(int anchor);
75 void endAnchoredSelection();
76 bool isAnchoredSelectionActive() const;
77
78 KItemModelBase* model() const;
79
80 signals:
81 void currentChanged(int current, int previous);
82 void selectionChanged(const KItemSet& current, const KItemSet& previous);
83
84 private:
85 void setModel(KItemModelBase* model);
86 void itemsInserted(const KItemRangeList& itemRanges);
87 void itemsRemoved(const KItemRangeList& itemRanges);
88 void itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes);
89
90
91 /**
92 * Helper method for itemsRemoved. Returns the changed index after removing
93 * the given range. If the index is part of the range, -1 will be returned.
94 */
95 int indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges, const RangesRemovingBehaviour behaviour) const;
96
97 private:
98 int m_currentItem;
99 int m_anchorItem;
100 KItemSet m_selectedItems;
101 bool m_isAnchoredSelectionActive;
102
103 KItemModelBase* m_model;
104
105 friend class KItemListController; // Calls setModel()
106 friend class KItemListView; // Calls itemsInserted(), itemsRemoved() and itemsMoved()
107 friend class KItemListSelectionManagerTest;
108 };
109
110 #endif