JointSelectionWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <Editor/JointSelectionWidget.h>
  9. #include <QHeaderView>
  10. #include <QLabel>
  11. #include <QVBoxLayout>
  12. #include <QTreeView>
  13. namespace EMotionFX
  14. {
  15. JointSelectionWidget::JointSelectionWidget(QAbstractItemView::SelectionMode selectionMode, QWidget* parent)
  16. : QWidget(parent)
  17. {
  18. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  19. setLayout(mainLayout);
  20. m_noSelectionLabel = new QLabel("Select an actor instance", this);
  21. m_noSelectionLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  22. mainLayout->addWidget(m_noSelectionLabel, 0, Qt::AlignCenter);
  23. m_searchWidget = new AzQtComponents::FilteredSearchWidget(this);
  24. mainLayout->addWidget(m_searchWidget);
  25. m_skeletonModel = AZStd::make_unique<SkeletonModel>();
  26. m_treeView = new QTreeView(this);
  27. m_filterProxyModel = new SkeletonSortFilterProxyModel(m_skeletonModel.get(), &m_skeletonModel->GetSelectionModel(), m_treeView);
  28. m_filterProxyModel->setFilterKeyColumn(-1);
  29. m_filterProxyModel->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
  30. m_treeView->setModel(m_filterProxyModel);
  31. m_treeView->setSelectionModel(m_filterProxyModel->GetSelectionProxyModel());
  32. m_filterProxyModel->ConnectFilterWidget(m_searchWidget);
  33. m_treeView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
  34. m_treeView->setSelectionMode(selectionMode);
  35. m_treeView->setContextMenuPolicy(Qt::DefaultContextMenu);
  36. m_treeView->setExpandsOnDoubleClick(false);
  37. QHeaderView* header = m_treeView->header();
  38. header->setStretchLastSection(false);
  39. header->resizeSection(1, SkeletonModel::s_defaultIconSize);
  40. header->resizeSection(2, SkeletonModel::s_defaultIconSize);
  41. header->resizeSection(3, SkeletonModel::s_defaultIconSize);
  42. header->setSectionResizeMode(0, QHeaderView::Stretch);
  43. header->hide();
  44. connect(m_treeView, &QTreeView::doubleClicked, this, &JointSelectionWidget::OnItemDoubleClicked);
  45. // Connect after the tree view connected to the model.
  46. connect(m_skeletonModel.get(), &QAbstractItemModel::modelReset, this, &JointSelectionWidget::Reinit);
  47. connect(m_searchWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, &JointSelectionWidget::OnTextFilterChanged);
  48. connect(m_searchWidget, &AzQtComponents::FilteredSearchWidget::TypeFilterChanged, this, &JointSelectionWidget::OnTypeFilterChanged);
  49. mainLayout->addWidget(m_treeView);
  50. Reinit();
  51. }
  52. JointSelectionWidget::~JointSelectionWidget()
  53. {
  54. // Disconnecting Reinit slot to avoid getting called
  55. // by qt after the skeleton model calls Resets in its destructor.
  56. // By the time Reinit is called the skeleton model unique pointer
  57. // has been destroyed.
  58. disconnect(m_skeletonModel.get(), &QAbstractItemModel::modelReset, this, &JointSelectionWidget::Reinit);
  59. }
  60. void JointSelectionWidget::SetFilterState(const QString& category, const QString& displayName, bool enabled)
  61. {
  62. m_searchWidget->SetFilterState(category, displayName, enabled);
  63. }
  64. void JointSelectionWidget::HideIcons()
  65. {
  66. m_treeView->hideColumn(SkeletonModel::COLUMN_RAGDOLL_LIMIT);
  67. m_treeView->hideColumn(SkeletonModel::COLUMN_RAGDOLL_COLLIDERS);
  68. m_treeView->hideColumn(SkeletonModel::COLUMN_HITDETECTION_COLLIDERS);
  69. }
  70. void JointSelectionWidget::SelectByJointName(const AZStd::string& jointName, [[maybe_unused]] bool clearSelection)
  71. {
  72. AZStd::vector<AZStd::string> jointNames;
  73. jointNames.emplace_back(jointName);
  74. SelectByJointNames(jointNames);
  75. }
  76. void JointSelectionWidget::SelectByJointNames(const AZStd::vector<AZStd::string>& jointNames, bool clearSelection)
  77. {
  78. QItemSelectionModel& selectionModel = m_skeletonModel->GetSelectionModel();
  79. if (clearSelection)
  80. {
  81. selectionModel.clearSelection();
  82. }
  83. AZStd::string itemName;
  84. m_skeletonModel->ForEach([&jointNames, this, &selectionModel, &itemName](const QModelIndex& modelIndex)
  85. {
  86. itemName = this->m_skeletonModel->data(modelIndex, Qt::DisplayRole).toString().toUtf8().data();
  87. if (AZStd::find(jointNames.begin(), jointNames.end(), itemName) != jointNames.end())
  88. {
  89. selectionModel.select(modelIndex, QItemSelectionModel::Select | QItemSelectionModel::Rows);
  90. }
  91. });
  92. }
  93. AZStd::vector<AZStd::string> JointSelectionWidget::GetSelectedJointNames() const
  94. {
  95. AZStd::vector<AZStd::string> result;
  96. const QItemSelectionModel& selectionModel = m_skeletonModel->GetSelectionModel();
  97. const QModelIndexList selectedRows = selectionModel.selectedRows(SkeletonModel::COLUMN_NAME);
  98. for (const QModelIndex& modelIndex : selectedRows)
  99. {
  100. result.emplace_back(m_skeletonModel->data(modelIndex, Qt::DisplayRole).toString().toUtf8().data());
  101. }
  102. return result;
  103. }
  104. void JointSelectionWidget::Reinit()
  105. {
  106. ActorInstance* actorInstance = m_skeletonModel->GetActorInstance();
  107. if (actorInstance)
  108. {
  109. m_treeView->setVisible(true);
  110. m_searchWidget->setVisible(true);
  111. m_noSelectionLabel->setVisible(false);
  112. }
  113. else
  114. {
  115. m_treeView->setVisible(false);
  116. m_searchWidget->setVisible(false);
  117. m_noSelectionLabel->setVisible(true);
  118. }
  119. m_treeView->expandAll();
  120. }
  121. void JointSelectionWidget::OnTextFilterChanged([[maybe_unused]] const QString& text)
  122. {
  123. m_treeView->expandAll();
  124. }
  125. void JointSelectionWidget::OnTypeFilterChanged([[maybe_unused]] const AzQtComponents::SearchTypeFilterList& activeTypeFilters)
  126. {
  127. m_treeView->expandAll();
  128. }
  129. } // namespace EMotionFX