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