2
0

TrackViewFBXImportPreviewDialog.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "EditorDefs.h"
  9. #include "TrackViewFBXImportPreviewDialog.h"
  10. // Qt
  11. #include <QAbstractListModel>
  12. #include <ui_TrackViewFBXImportPreviewDialog.h>
  13. class FBXImportModel
  14. : public QAbstractListModel
  15. {
  16. public:
  17. FBXImportModel(CTrackViewFBXImportPreviewDialog::TItemsMap& map, QObject* parent = nullptr)
  18. : QAbstractListModel(parent)
  19. , m_map(map)
  20. {
  21. }
  22. int rowCount(const QModelIndex& parent = QModelIndex()) const override
  23. {
  24. return parent.isValid() ? 0 : m_map.size();
  25. }
  26. Qt::ItemFlags flags(const QModelIndex& index) const override
  27. {
  28. return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
  29. }
  30. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
  31. {
  32. if (!index.isValid() || index.row() >= m_map.size())
  33. {
  34. return QVariant();
  35. }
  36. auto it = m_map.begin() + index.row();
  37. switch (role)
  38. {
  39. case Qt::DisplayRole:
  40. return it->name;
  41. case Qt::CheckStateRole:
  42. return it->checked ? Qt::Checked : Qt::Unchecked;
  43. default:
  44. return QVariant();
  45. }
  46. }
  47. bool setData(const QModelIndex& index, const QVariant& data, int role = Qt::EditRole) override
  48. {
  49. if (!index.isValid() || index.row() >= m_map.size())
  50. {
  51. return false;
  52. }
  53. auto it = m_map.begin() + index.row();
  54. switch (role)
  55. {
  56. case Qt::CheckStateRole:
  57. it->checked = (data.toInt() == Qt::Checked);
  58. emit dataChanged(index, index, QVector<int>(1, Qt::CheckStateRole));
  59. return true;
  60. default:
  61. return false;
  62. }
  63. }
  64. void setAllItemsChecked(bool checked)
  65. {
  66. if (m_map.empty())
  67. {
  68. return;
  69. }
  70. for (auto it : m_map)
  71. {
  72. it.checked = checked;
  73. }
  74. emit dataChanged(index(0, 0), index(m_map.size() - 1, 0), QVector<int>(1, Qt::CheckStateRole));
  75. }
  76. private:
  77. CTrackViewFBXImportPreviewDialog::TItemsMap& m_map;
  78. };
  79. //////////////////////////////////////////////////////////////////////////
  80. CTrackViewFBXImportPreviewDialog::CTrackViewFBXImportPreviewDialog(QWidget* parent)
  81. : QDialog(parent)
  82. , m_ui(new Ui::TrackViewFBXImportPreviewDialog)
  83. {
  84. m_ui->setupUi(this);
  85. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  86. setFixedSize(size());
  87. connect(m_ui->buttonSelectAll, &QPushButton::clicked, this, &CTrackViewFBXImportPreviewDialog::OnBnSelectAllClicked);
  88. connect(m_ui->buttonUnselectAll, &QPushButton::clicked, this, &CTrackViewFBXImportPreviewDialog::OnBnUnselectAllClicked);
  89. }
  90. CTrackViewFBXImportPreviewDialog::~CTrackViewFBXImportPreviewDialog()
  91. {
  92. }
  93. //////////////////////////////////////////////////////////////////////////
  94. int CTrackViewFBXImportPreviewDialog::exec()
  95. {
  96. m_ui->m_tree->setModel(new FBXImportModel(m_fBXItemNames, this));
  97. return QDialog::exec();
  98. }
  99. //////////////////////////////////////////////////////////////////////////
  100. void CTrackViewFBXImportPreviewDialog::AddTreeItem(const QString& objectName)
  101. {
  102. m_fBXItemNames.push_back(Item { objectName, true });
  103. }
  104. bool CTrackViewFBXImportPreviewDialog::IsObjectSelected(const QString& objectName) const
  105. {
  106. for (auto it : m_fBXItemNames)
  107. {
  108. if (it.name == objectName)
  109. {
  110. return it.checked;
  111. }
  112. }
  113. return false;
  114. };
  115. void CTrackViewFBXImportPreviewDialog::OnBnSelectAllClicked()
  116. {
  117. static_cast<FBXImportModel*>(m_ui->m_tree->model())->setAllItemsChecked(true);
  118. }
  119. void CTrackViewFBXImportPreviewDialog::OnBnUnselectAllClicked()
  120. {
  121. static_cast<FBXImportModel*>(m_ui->m_tree->model())->setAllItemsChecked(false);
  122. }
  123. #include <moc_TrackViewFBXImportPreviewDialog.cpp>