QAudioControlTreeWidget.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <QAudioControlTreeWidget.h>
  9. #include <ACEEnums.h>
  10. #include <ACETypes.h>
  11. #include <AudioControlsEditorUndo.h>
  12. #include <QAudioControlEditorIcons.h>
  13. using namespace AudioControls;
  14. //-----------------------------------------------------------------------------------------------//
  15. QFolderItem::QFolderItem(const QString& sName)
  16. : QStandardItem(sName)
  17. {
  18. setIcon(GetFolderIcon());
  19. setData(eIT_FOLDER, eDR_TYPE);
  20. setData(ACE_INVALID_CID, eDR_ID);
  21. setFlags(flags() | Qt::ItemIsDropEnabled);
  22. setFlags(flags() | Qt::ItemIsDragEnabled);
  23. setData(false, eDR_MODIFIED);
  24. }
  25. //-----------------------------------------------------------------------------------------------//
  26. QAudioControlItem::QAudioControlItem(const QString& sName, CATLControl* pControl)
  27. : QStandardItem(sName)
  28. {
  29. setIcon(GetControlTypeIcon(pControl->GetType()));
  30. setData(eIT_AUDIO_CONTROL, eDR_TYPE);
  31. setData(pControl->GetId(), eDR_ID);
  32. EACEControlType eType = pControl->GetType();
  33. if (eType == eACET_SWITCH)
  34. {
  35. setFlags(flags() | Qt::ItemIsDropEnabled);
  36. setFlags(flags() | Qt::ItemIsDragEnabled);
  37. }
  38. else if (eType == eACET_SWITCH_STATE)
  39. {
  40. setFlags(flags() & ~Qt::ItemIsDropEnabled);
  41. setFlags(flags() & ~Qt::ItemIsDragEnabled);
  42. }
  43. else
  44. {
  45. setFlags(flags() & ~Qt::ItemIsDropEnabled);
  46. setFlags(flags() | Qt::ItemIsDragEnabled);
  47. }
  48. setData(false, eDR_MODIFIED);
  49. }
  50. //-----------------------------------------------------------------------------------------------//
  51. QAudioControlSortProxy::QAudioControlSortProxy(QObject* pParent /*= 0*/)
  52. : QSortFilterProxyModel(pParent)
  53. {
  54. }
  55. //-----------------------------------------------------------------------------------------------//
  56. bool QAudioControlSortProxy::setData(const QModelIndex& index, const QVariant& value, int role /* = Qt::EditRole */)
  57. {
  58. if (role == Qt::EditRole)
  59. {
  60. QString sInitialName = value.toString();
  61. if (sInitialName.isEmpty() || sInitialName.contains(" "))
  62. {
  63. // TODO: Prevent user from inputing spaces to name
  64. return false;
  65. }
  66. if (index.data(eDR_TYPE) == eIT_FOLDER)
  67. {
  68. // Validate that the new folder name is valid
  69. bool bFoundValidName = false;
  70. QString sOriginalName = index.data(Qt::DisplayRole).toString();
  71. QString sCandidateName = sInitialName;
  72. int nNumber = 1;
  73. while (!bFoundValidName)
  74. {
  75. bFoundValidName = true;
  76. int i = 0;
  77. QModelIndex sibiling = index.sibling(i, 0);
  78. while (sibiling.isValid())
  79. {
  80. QString sSibilingName = sibiling.data(Qt::DisplayRole).toString();
  81. if ((sibiling != index) && (sibiling.data(eDR_TYPE) == eIT_FOLDER) && (QString::compare(sCandidateName, sSibilingName, Qt::CaseInsensitive) == 0))
  82. {
  83. sCandidateName = sInitialName + "_" + QString::number(nNumber);
  84. ++nNumber;
  85. bFoundValidName = false;
  86. break;
  87. }
  88. ++i;
  89. sibiling = index.sibling(i, 0);
  90. }
  91. }
  92. if (sCandidateName != sOriginalName && !CUndo::IsSuspended())
  93. {
  94. if (QStandardItem* folderItem = GetStandardItemFromIndex(index); folderItem != nullptr)
  95. {
  96. CUndo undo("ATL Folder Rename");
  97. CUndo::Record(new CUndoFolderRename(folderItem));
  98. }
  99. }
  100. return QSortFilterProxyModel::setData(index, sCandidateName, role);
  101. }
  102. }
  103. return QSortFilterProxyModel::setData(index, value, role);
  104. }
  105. //-----------------------------------------------------------------------------------------------//
  106. QStandardItem* QAudioControlSortProxy::GetStandardItemFromIndex(const QModelIndex& index)
  107. {
  108. if (auto srcModel = reinterpret_cast<QATLTreeModel*>(sourceModel()); srcModel != nullptr)
  109. {
  110. if (auto abstractItem = srcModel->itemFromIndex(mapToSource(index)); abstractItem != nullptr)
  111. {
  112. return reinterpret_cast<QStandardItem*>(abstractItem);
  113. }
  114. }
  115. return nullptr;
  116. }
  117. //-----------------------------------------------------------------------------------------------//
  118. bool QAudioControlSortProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const
  119. {
  120. uint eLeftType = sourceModel()->data(left, eDR_TYPE).toUInt();
  121. uint eRightType = sourceModel()->data(right, eDR_TYPE).toUInt();
  122. if (eLeftType != eRightType)
  123. {
  124. return eLeftType > eRightType;
  125. }
  126. return left.data(Qt::DisplayRole).toString() > right.data(Qt::DisplayRole).toString();
  127. }
  128. //-----------------------------------------------------------------------------------------------//
  129. QAudioControlsTreeView::QAudioControlsTreeView(QWidget* pParent /*= 0*/)
  130. : QTreeView(pParent)
  131. {
  132. }
  133. //-----------------------------------------------------------------------------------------------//
  134. void QAudioControlsTreeView::scrollTo(const QModelIndex& index, ScrollHint hint /*= EnsureVisible*/)
  135. {
  136. // QTreeView::scrollTo() expands all the parent items but
  137. // it is disabled when handling a Drag&Drop event so have to do it manually
  138. if (state() != QAbstractItemView::NoState)
  139. {
  140. QModelIndex parent = index.parent();
  141. while (parent.isValid())
  142. {
  143. if (!isExpanded(parent))
  144. {
  145. expand(parent);
  146. }
  147. parent = parent.parent();
  148. }
  149. }
  150. QTreeView::scrollTo(index, hint);
  151. }
  152. //-----------------------------------------------------------------------------------------------//
  153. bool QAudioControlsTreeView::IsEditing()
  154. {
  155. return state() == QAbstractItemView::EditingState;
  156. }