QSimpleAudioControlListWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <QSimpleAudioControlListWidget.h>
  9. #include <ACEEnums.h>
  10. #include <AudioControlsEditorPlugin.h>
  11. #include <IAudioSystemEditor.h>
  12. #include <QAudioControlEditorIcons.h>
  13. #include <QApplication>
  14. #include <QMimeData>
  15. namespace AudioControls
  16. {
  17. //-------------------------------------------------------------------------------------------//
  18. class QMiddlewareControlItem
  19. : public QTreeWidgetItem
  20. {
  21. public:
  22. bool operator< (const QTreeWidgetItem& other) const override
  23. {
  24. TImplControlType type = data(0, eMDR_TYPE).toUInt();
  25. TImplControlType otherType = other.data(0, eMDR_TYPE).toUInt();
  26. if (type == otherType)
  27. {
  28. return text(0) < other.text(0);
  29. }
  30. return type < otherType;
  31. }
  32. };
  33. //-------------------------------------------------------------------------------------------//
  34. QSimpleAudioControlListWidget::QSimpleAudioControlListWidget(QWidget* parent)
  35. : QTreeWidget(parent)
  36. , m_connectedColor(QColor(0x99, 0x99, 0x99))
  37. , m_disconnectedColor(QColor(0xf3, 0x81, 0x1d))
  38. , m_localizedColor(QColor(0x42, 0x85, 0xf4))
  39. {
  40. CATLControlsModel* pATLModel = CAudioControlsEditorPlugin::GetATLModel();
  41. if (pATLModel)
  42. {
  43. pATLModel->AddListener(this);
  44. }
  45. }
  46. //-------------------------------------------------------------------------------------------//
  47. QSimpleAudioControlListWidget::~QSimpleAudioControlListWidget()
  48. {
  49. CATLControlsModel* pATLModel = CAudioControlsEditorPlugin::GetATLModel();
  50. if (pATLModel)
  51. {
  52. pATLModel->RemoveListener(this);
  53. }
  54. }
  55. //-------------------------------------------------------------------------------------------//
  56. void QSimpleAudioControlListWidget::UpdateModel()
  57. {
  58. setSortingEnabled(false);
  59. Refresh(true);
  60. sortByColumn(0, Qt::AscendingOrder);
  61. setSortingEnabled(true);
  62. }
  63. //-------------------------------------------------------------------------------------------//
  64. void QSimpleAudioControlListWidget::LoadControls()
  65. {
  66. clear();
  67. AudioControls::IAudioSystemEditor* pAudioSystemEditorImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
  68. if (pAudioSystemEditorImpl)
  69. {
  70. IAudioSystemControl* pControl = pAudioSystemEditorImpl->GetRoot();
  71. if (pControl)
  72. {
  73. LoadControl(pControl, invisibleRootItem());
  74. }
  75. }
  76. }
  77. //-------------------------------------------------------------------------------------------//
  78. void QSimpleAudioControlListWidget::LoadControl(IAudioSystemControl* pControl, QTreeWidgetItem* pRoot)
  79. {
  80. if (pControl)
  81. {
  82. size_t size = pControl->ChildCount();
  83. for (size_t i = 0; i < size; ++i)
  84. {
  85. IAudioSystemControl* pChild = pControl->GetChildAt(i);
  86. if (pChild && !pChild->IsPlaceholder())
  87. {
  88. QTreeWidgetItem* pItem = InsertControl(pChild, pRoot);
  89. if (pItem)
  90. {
  91. LoadControl(pChild, pItem);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. //-------------------------------------------------------------------------------------------//
  98. void QSimpleAudioControlListWidget::UpdateControl(IAudioSystemControl* pControl)
  99. {
  100. if (pControl)
  101. {
  102. QTreeWidgetItem* pItem = GetItem(pControl->GetId(), pControl->IsLocalized());
  103. if (pItem)
  104. {
  105. InitItemData(pItem, pControl);
  106. }
  107. }
  108. }
  109. //-------------------------------------------------------------------------------------------//
  110. QTreeWidgetItem* QSimpleAudioControlListWidget::InsertControl(IAudioSystemControl* pControl, QTreeWidgetItem* pRoot)
  111. {
  112. if (pRoot && pControl)
  113. {
  114. QTreeWidgetItem* pItem = new QMiddlewareControlItem();
  115. pItem->setText(0, QString(pControl->GetName().c_str()));
  116. InitItemData(pItem, pControl);
  117. pRoot->addChild(pItem);
  118. return pItem;
  119. }
  120. return nullptr;
  121. }
  122. //-------------------------------------------------------------------------------------------//
  123. QTreeWidgetItem* QSimpleAudioControlListWidget::GetItem(CID id, bool bLocalized)
  124. {
  125. QTreeWidgetItemIterator it(this);
  126. while (*it)
  127. {
  128. QTreeWidgetItem* item = *it;
  129. if (GetItemId(item) == id && IsLocalized(item) == bLocalized)
  130. {
  131. return item;
  132. }
  133. ++it;
  134. }
  135. return nullptr;
  136. }
  137. //-------------------------------------------------------------------------------------------//
  138. TImplControlType QSimpleAudioControlListWidget::GetControlType(QTreeWidgetItem* item)
  139. {
  140. if (item)
  141. {
  142. return (TImplControlType) item->data(0, eMDR_TYPE).toUInt();
  143. }
  144. return AUDIO_IMPL_INVALID_TYPE;
  145. }
  146. //-------------------------------------------------------------------------------------------//
  147. CID QSimpleAudioControlListWidget::GetItemId(QTreeWidgetItem* item)
  148. {
  149. if (item)
  150. {
  151. return static_cast<CID>(item->data(0, eMDR_ID).toUInt());
  152. }
  153. return ACE_INVALID_CID;
  154. }
  155. //-------------------------------------------------------------------------------------------//
  156. bool QSimpleAudioControlListWidget::IsLocalized(QTreeWidgetItem* item)
  157. {
  158. return item->data(0, eMDR_LOCALIZED).toBool();
  159. }
  160. //-------------------------------------------------------------------------------------------//
  161. bool QSimpleAudioControlListWidget::IsConnected(QTreeWidgetItem* item)
  162. {
  163. if (item)
  164. {
  165. return item->data(0, eMDR_CONNECTED).toBool();
  166. }
  167. return false;
  168. }
  169. //-------------------------------------------------------------------------------------------//
  170. void QSimpleAudioControlListWidget::InitItem(QTreeWidgetItem* pItem)
  171. {
  172. IAudioSystemEditor* pAudioSystemEditorImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
  173. if (pItem && pAudioSystemEditorImpl)
  174. {
  175. TImplControlType type = GetControlType(pItem);
  176. EACEControlType compatibleType = pAudioSystemEditorImpl->ImplTypeToATLType(type);
  177. QIcon icon(QString(pAudioSystemEditorImpl->GetTypeIcon(type).data()));
  178. icon.addFile(QString(pAudioSystemEditorImpl->GetTypeIconSelected(type).data()), QSize(), QIcon::Selected);
  179. pItem->setIcon(0, icon);
  180. pItem->setFlags(pItem->flags() & ~Qt::ItemIsDropEnabled);
  181. if (compatibleType != eACET_NUM_TYPES)
  182. {
  183. pItem->setFlags(pItem->flags() | Qt::ItemIsDragEnabled);
  184. if (pItem->data(0, eMDR_LOCALIZED).toBool())
  185. {
  186. pItem->setToolTip(0, tr("Localized control"));
  187. pItem->setForeground(0, m_localizedColor);
  188. }
  189. else
  190. {
  191. if (IsConnected(pItem))
  192. {
  193. pItem->setToolTip(0, tr("Connected control"));
  194. pItem->setForeground(0, m_connectedColor);
  195. }
  196. else
  197. {
  198. pItem->setToolTip(0, tr("Unassigned control"));
  199. pItem->setForeground(0, m_disconnectedColor);
  200. }
  201. }
  202. }
  203. else
  204. {
  205. pItem->setFlags(pItem->flags() & ~Qt::ItemIsDragEnabled);
  206. }
  207. if (compatibleType == EACEControlType::eACET_SWITCH_STATE)
  208. {
  209. IAudioSystemControl* pControl = pAudioSystemEditorImpl->GetControl(GetItemId(pItem));
  210. if (pControl && !pControl->IsLocalized())
  211. {
  212. size_t nConnect = 0;
  213. for (int i = 0; i < pControl->GetParent()->ChildCount(); ++i)
  214. {
  215. IAudioSystemControl* child = pControl->GetParent()->GetChildAt(i);
  216. if (child && child->IsConnected())
  217. {
  218. ++nConnect;
  219. }
  220. }
  221. QTreeWidgetItem* pParentItem = GetItem(pControl->GetParent()->GetId(), pControl->GetParent()->IsLocalized());
  222. if (pParentItem)
  223. {
  224. if (nConnect > 0 && nConnect == pControl->GetParent()->ChildCount())
  225. {
  226. pParentItem->setForeground(0, m_connectedColor);
  227. }
  228. else
  229. {
  230. pParentItem->setForeground(0, m_disconnectedColor);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. //-------------------------------------------------------------------------------------------//
  238. void QSimpleAudioControlListWidget::InitItemData(QTreeWidgetItem* pItem, IAudioSystemControl* pControl)
  239. {
  240. if (pItem && pControl)
  241. {
  242. pItem->setData(0, eMDR_ID, pControl->GetId());
  243. if (pControl->GetId() != ACE_INVALID_CID)
  244. {
  245. pItem->setData(0, eMDR_TYPE, pControl->GetType());
  246. pItem->setData(0, eMDR_LOCALIZED, pControl->IsLocalized());
  247. pItem->setData(0, eMDR_CONNECTED, pControl->IsConnected());
  248. }
  249. InitItem(pItem);
  250. }
  251. }
  252. //-------------------------------------------------------------------------------------------//
  253. ControlList QSimpleAudioControlListWidget::GetSelectedIds()
  254. {
  255. ControlList ids;
  256. QList<QTreeWidgetItem*> selected = selectedItems();
  257. int size = selected.length();
  258. for (int i = 0; i < size; ++i)
  259. {
  260. ids.push_back(GetItemId(selected[i]));
  261. }
  262. return ids;
  263. }
  264. //-------------------------------------------------------------------------------------------//
  265. void QSimpleAudioControlListWidget::Refresh(bool reload)
  266. {
  267. IAudioSystemEditor* pAudioSystemEditorImpl = CAudioControlsEditorPlugin::GetAudioSystemEditorImpl();
  268. if (pAudioSystemEditorImpl)
  269. {
  270. // store the currently selected control to select it again
  271. ControlList ids = GetSelectedIds();
  272. if (reload)
  273. {
  274. LoadControls();
  275. }
  276. QTreeWidgetItemIterator it(this);
  277. while (*it)
  278. {
  279. InitItem(*it);
  280. ++it;
  281. }
  282. // select the control that was previously selected
  283. size_t size = ids.size();
  284. for (size_t i = 0; i < size; ++i)
  285. {
  286. QTreeWidgetItem* pItem = GetItem(ids[i], false);
  287. if (pItem)
  288. {
  289. pItem->setSelected(true);
  290. setCurrentItem(pItem, 0);
  291. scrollToItem(pItem);
  292. }
  293. }
  294. }
  295. }
  296. //-------------------------------------------------------------------------------------------//
  297. void QSimpleAudioControlListWidget::OnConnectionAdded([[maybe_unused]] CATLControl* pControl, IAudioSystemControl* pMiddlewareControl)
  298. {
  299. UpdateControl(pMiddlewareControl);
  300. }
  301. //-------------------------------------------------------------------------------------------//
  302. void QSimpleAudioControlListWidget::OnConnectionRemoved([[maybe_unused]] CATLControl* pControl, IAudioSystemControl* pMiddlewareControl)
  303. {
  304. UpdateControl(pMiddlewareControl);
  305. }
  306. } // namespace AudioControls
  307. #include <Source/Editor/moc_QSimpleAudioControlListWidget.cpp>