X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/f23e9496f303995557b744c03178f5dbd9b35016..76a46fd9094b17eb99e8a42cca8562fdc0b3814c:/src/kitemviews/kitemlistselectionmanager.h diff --git a/src/kitemviews/kitemlistselectionmanager.h b/src/kitemviews/kitemlistselectionmanager.h index 5c8e84614..6b0435139 100644 --- a/src/kitemviews/kitemlistselectionmanager.h +++ b/src/kitemviews/kitemlistselectionmanager.h @@ -1,8 +1,7 @@ /*************************************************************************** * Copyright (C) 2011 by Peter Penz * * * - * Based on the Itemviews NG project from Trolltech Labs: * - * http://qt.gitorious.org/qt-labs/itemviews-ng * + * Based on the Itemviews NG project from Trolltech Labs * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -23,47 +22,88 @@ #ifndef KITEMLISTSELECTIONMANAGER_H #define KITEMLISTSELECTIONMANAGER_H -#include +#include "dolphin_export.h" +#include "kitemviews/kitemmodelbase.h" +#include "kitemviews/kitemset.h" #include class KItemModelBase; -class LIBDOLPHINPRIVATE_EXPORT KItemListSelectionManager : public QObject +/** + * @brief Allows to select and deselect items of a KItemListView. + */ +class DOLPHIN_EXPORT KItemListSelectionManager : public QObject { Q_OBJECT + enum RangesRemovingBehaviour { + DiscardRemovedIndex, + AdjustRemovedIndex + }; + public: enum SelectionMode { Select, Deselect, Toggle }; - - KItemListSelectionManager(QObject* parent = 0); - virtual ~KItemListSelectionManager(); + + explicit KItemListSelectionManager(QObject* parent = nullptr); + ~KItemListSelectionManager() override; void setCurrentItem(int current); int currentItem() const; - void setAnchorItem(int anchor); - int anchorItem() const; + void setSelectedItems(const KItemSet& items); + KItemSet selectedItems() const; + bool isSelected(int index) const; + bool hasSelection() const; + + void setSelected(int index, int count = 1, SelectionMode mode = Select); + /** + * Equivalent to: + * clearSelection(); + * setSelected(index, count); + * but emitting once only selectionChanged signal + */ + void replaceSelection(int index, int count = 1); + void clearSelection(); + + void beginAnchoredSelection(int anchor); + void endAnchoredSelection(); + bool isAnchoredSelectionActive() const; KItemModelBase* model() const; signals: void currentChanged(int current, int previous); - void anchorChanged(int anchor, int previous); + void selectionChanged(const KItemSet& current, const KItemSet& previous); -protected: +private: void setModel(KItemModelBase* model); + void itemsInserted(const KItemRangeList& itemRanges); + void itemsRemoved(const KItemRangeList& itemRanges); + void itemsMoved(const KItemRange& itemRange, const QList& movedToIndexes); + + + /** + * Helper method for itemsRemoved. Returns the changed index after removing + * the given range. If the index is part of the range, -1 will be returned. + */ + int indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges, const RangesRemovingBehaviour behaviour) const; private: int m_currentItem; int m_anchorItem; + KItemSet m_selectedItems; + bool m_isAnchoredSelectionActive; + KItemModelBase* m_model; - friend class KItemListController; + friend class KItemListController; // Calls setModel() + friend class KItemListView; // Calls itemsInserted(), itemsRemoved() and itemsMoved() + friend class KItemListSelectionManagerTest; }; #endif