SelectionProxyModel.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QtCore/QItemSelectionModel>
  11. #include <AzCore/std/containers/vector.h>
  12. #endif
  13. QT_FORWARD_DECLARE_CLASS(QAbstractProxyModel)
  14. namespace EMotionFX
  15. {
  16. // This class is a QItemSelectionModel that syncs through proxy models and maintains
  17. // selection. In Qt we can have a model being filtered/sorted by proxy models. If the
  18. // selection model is connected to the original model, the view needs a new selection
  19. // model that understands the filtering. This class does that conversion.
  20. // NOTE: this class does not support changing proxy models (anywhere in the chain).
  21. // The class will have to be recreated with the new proxy model.
  22. //
  23. class SelectionProxyModel
  24. : public QItemSelectionModel
  25. {
  26. Q_OBJECT // AUTOMOC
  27. public:
  28. SelectionProxyModel(QItemSelectionModel* sourceSelectionModel, QAbstractProxyModel* proxyModel, QObject* parent = nullptr);
  29. void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) override;
  30. void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) override;
  31. void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) override;
  32. void clear() override;
  33. void reset() override;
  34. void clearCurrentIndex() override;
  35. private slots:
  36. void OnSourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
  37. void OnSourceSelectionCurrentChanged(const QModelIndex& current, const QModelIndex& previous);
  38. void OnProxySelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
  39. void OnProxyModelRowsInserted(const QModelIndex& parent, int first, int last);
  40. private:
  41. QModelIndex mapFromSource(const QModelIndex& sourceIndex);
  42. QItemSelection mapFromSource(const QItemSelection& sourceSelection);
  43. QModelIndex mapToSource(const QModelIndex& targetIndex);
  44. QItemSelection mapToSource(const QItemSelection& targetSelection);
  45. // Contains the chain of proxy models that leads us to the real model. The outer-most proxy model
  46. // comes first and is followed by inner proxy models.
  47. AZStd::vector<QAbstractProxyModel*> m_proxyModels;
  48. QItemSelectionModel* m_sourceSelectionModel;
  49. };
  50. } // namespace EMotionFX