3
0

SelectionProxyModel.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <AzQtComponents/Utilities/SelectionProxyModel.h>
  9. #include <QAbstractProxyModel>
  10. namespace AzQtComponents
  11. {
  12. SelectionProxyModel::SelectionProxyModel(QItemSelectionModel* sourceSelectionModel, QAbstractProxyModel* proxyModel, QObject* parent)
  13. : QItemSelectionModel(proxyModel, parent)
  14. , m_sourceSelectionModel(sourceSelectionModel)
  15. {
  16. connect(sourceSelectionModel, &QItemSelectionModel::selectionChanged, this, &SelectionProxyModel::OnSourceSelectionChanged);
  17. connect(sourceSelectionModel, &QItemSelectionModel::currentChanged, this, &SelectionProxyModel::OnSourceSelectionCurrentChanged);
  18. connect(proxyModel, &QAbstractItemModel::rowsInserted, this, &SelectionProxyModel::OnProxyModelRowsInserted);
  19. connect(this, &QItemSelectionModel::selectionChanged, this, &SelectionProxyModel::OnProxySelectionChanged);
  20. // Find the chain of proxy models
  21. QAbstractProxyModel* sourceProxyModel = proxyModel;
  22. while (sourceProxyModel)
  23. {
  24. m_proxyModels.push_back(sourceProxyModel);
  25. sourceProxyModel = qobject_cast<QAbstractProxyModel*>(sourceProxyModel->sourceModel());
  26. }
  27. const QItemSelection currentSelection = mapFromSource(m_sourceSelectionModel->selection());
  28. QItemSelectionModel::select(currentSelection, QItemSelectionModel::ClearAndSelect);
  29. const QModelIndex currentModelIndex = mapFromSource(m_sourceSelectionModel->currentIndex());
  30. QItemSelectionModel::setCurrentIndex(currentModelIndex, QItemSelectionModel::ClearAndSelect);
  31. }
  32. void SelectionProxyModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
  33. {
  34. const QModelIndex sourcetIndex = mapToSource(index);
  35. m_sourceSelectionModel->setCurrentIndex(sourcetIndex, command);
  36. }
  37. void SelectionProxyModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
  38. {
  39. const QModelIndex sourceIndex = mapToSource(index);
  40. m_sourceSelectionModel->select(sourceIndex, command);
  41. }
  42. void SelectionProxyModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
  43. {
  44. const QItemSelection sourceSelection = mapToSource(selection);
  45. m_sourceSelectionModel->select(sourceSelection, command);
  46. }
  47. void SelectionProxyModel::clear()
  48. {
  49. m_sourceSelectionModel->clear();
  50. }
  51. void SelectionProxyModel::reset()
  52. {
  53. m_sourceSelectionModel->reset();
  54. }
  55. void SelectionProxyModel::clearCurrentIndex()
  56. {
  57. m_sourceSelectionModel->clearCurrentIndex();
  58. }
  59. void SelectionProxyModel::OnSourceSelectionCurrentChanged(const QModelIndex& current, [[maybe_unused]] const QModelIndex& previous)
  60. {
  61. QModelIndex targetCurrent = mapFromSource(current);
  62. QItemSelectionModel::setCurrentIndex(targetCurrent, QItemSelectionModel::NoUpdate);
  63. }
  64. void SelectionProxyModel::OnSourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
  65. {
  66. QItemSelection targetSelected = mapFromSource(selected);
  67. QItemSelection targetDeselected = mapFromSource(deselected);
  68. QItemSelectionModel::select(targetSelected, QItemSelectionModel::Select);
  69. QItemSelectionModel::select(targetDeselected, QItemSelectionModel::Deselect);
  70. }
  71. void SelectionProxyModel::OnProxySelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
  72. {
  73. const QItemSelection sourceSelected = mapToSource(selected);
  74. const QItemSelection sourceDeselected = mapToSource(deselected);
  75. // Disconnect from the selectionChanged signal in the source model to prevent recursion. We could also block the signals
  76. // of the source selection model, but someone else may be connected to its signals and expect to get an update.
  77. disconnect(m_sourceSelectionModel, &QItemSelectionModel::selectionChanged, this, &SelectionProxyModel::OnSourceSelectionChanged);
  78. if (selected.empty() && deselected.empty())
  79. {
  80. // Force the signal to fire
  81. emit m_sourceSelectionModel->selectionChanged({}, {});
  82. }
  83. else
  84. {
  85. m_sourceSelectionModel->select(sourceSelected, QItemSelectionModel::Select);
  86. m_sourceSelectionModel->select(sourceDeselected, QItemSelectionModel::Deselect);
  87. }
  88. connect(m_sourceSelectionModel, &QItemSelectionModel::selectionChanged, this, &SelectionProxyModel::OnSourceSelectionChanged);
  89. }
  90. void SelectionProxyModel::OnProxyModelRowsInserted([[maybe_unused]] const QModelIndex& parent, [[maybe_unused]] int first, [[maybe_unused]] int last)
  91. {
  92. QModelIndex sourceIndex = m_sourceSelectionModel->currentIndex();
  93. QModelIndex targetIndex = mapFromSource(sourceIndex);
  94. if (targetIndex != currentIndex())
  95. {
  96. QItemSelectionModel::setCurrentIndex(targetIndex, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
  97. }
  98. QItemSelection sourceSelection = m_sourceSelectionModel->selection();
  99. QItemSelection targetSelection = mapFromSource(sourceSelection);
  100. if (targetSelection != selection())
  101. {
  102. QItemSelectionModel::select(targetSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
  103. }
  104. }
  105. QModelIndex SelectionProxyModel::mapFromSource(const QModelIndex& sourceIndex)
  106. {
  107. QModelIndex mappedIndex = sourceIndex;
  108. for (QVector<QAbstractProxyModel*>::const_reverse_iterator itProxy = m_proxyModels.rbegin(); itProxy != m_proxyModels.rend(); ++itProxy)
  109. {
  110. mappedIndex = (*itProxy)->mapFromSource(mappedIndex);
  111. }
  112. return mappedIndex;
  113. }
  114. QItemSelection SelectionProxyModel::mapFromSource(const QItemSelection& sourceSelection)
  115. {
  116. QItemSelection mappedSelection = sourceSelection;
  117. for (QVector<QAbstractProxyModel*>::const_reverse_iterator itProxy = m_proxyModels.rbegin(); itProxy != m_proxyModels.rend(); ++itProxy)
  118. {
  119. mappedSelection = (*itProxy)->mapSelectionFromSource(mappedSelection);
  120. }
  121. return mappedSelection;
  122. }
  123. QModelIndex SelectionProxyModel::mapToSource(const QModelIndex& targetIndex)
  124. {
  125. QModelIndex mappedIndex = targetIndex;
  126. for (QVector<QAbstractProxyModel*>::const_iterator itProxy = m_proxyModels.begin(); itProxy != m_proxyModels.end(); ++itProxy)
  127. {
  128. mappedIndex = (*itProxy)->mapToSource(mappedIndex);
  129. }
  130. return mappedIndex;
  131. }
  132. QItemSelection SelectionProxyModel::mapToSource(const QItemSelection& targetSelection)
  133. {
  134. QItemSelection mappedSelection = targetSelection;
  135. for (QVector<QAbstractProxyModel*>::const_iterator itProxy = m_proxyModels.begin(); itProxy != m_proxyModels.end(); ++itProxy)
  136. {
  137. mappedSelection = (*itProxy)->mapSelectionToSource(mappedSelection);
  138. }
  139. return mappedSelection;
  140. }
  141. } // namespace AzQtComponents