ComboBoxNodePropertyDisplay.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <QGraphicsProxyWidget>
  9. #include <QGraphicsSceneDragDropEvent>
  10. #include <QGraphicsView>
  11. #include <QMenu>
  12. #include <QMimeData>
  13. #include <QSignalBlocker>
  14. #include <Components/NodePropertyDisplays/ComboBoxNodePropertyDisplay.h>
  15. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  16. #include <GraphCanvas/Components/Slots/SlotBus.h>
  17. #include <GraphCanvas/Components/VisualBus.h>
  18. #include <GraphCanvas/Utils/ConversionUtils.h>
  19. #include <GraphCanvas/Widgets/NodePropertyBus.h>
  20. #include <Widgets/GraphCanvasLabel.h>
  21. #include <GraphCanvas/Utils/QtDrawingUtils.h>
  22. namespace GraphCanvas
  23. {
  24. ////////////////////////////////
  25. // ComboBoxNodePropertyDisplay
  26. ////////////////////////////////
  27. ComboBoxNodePropertyDisplay::ComboBoxNodePropertyDisplay(ComboBoxDataInterface* dataInterface)
  28. : NodePropertyDisplay(dataInterface)
  29. , m_valueDirty(false)
  30. , m_menuDisplayDirty(true)
  31. , m_dataInterface(dataInterface)
  32. , m_comboBox(nullptr)
  33. , m_proxyWidget(nullptr)
  34. , m_dataTypeOutlineEnabled(true)
  35. {
  36. m_disabledLabel = aznew GraphCanvasLabel();
  37. m_displayLabel = aznew GraphCanvasLabel();
  38. }
  39. void ComboBoxNodePropertyDisplay::ShowContextMenu(const QPoint& pos)
  40. {
  41. if (m_comboBox)
  42. {
  43. m_dataInterface->OnShowContextMenu(m_comboBox, pos);
  44. }
  45. else
  46. {
  47. AZ_Error("GraphCanvas", false, "m_propertyEntityIdCtrl doesn't exist!");
  48. }
  49. }
  50. ComboBoxNodePropertyDisplay::~ComboBoxNodePropertyDisplay()
  51. {
  52. CleanupProxyWidget();
  53. delete m_disabledLabel;
  54. delete m_displayLabel;
  55. delete m_dataInterface;
  56. }
  57. void ComboBoxNodePropertyDisplay::SetDataTypeOutlineEnabled(bool dataTypeOutlineEnabled)
  58. {
  59. if (m_dataTypeOutlineEnabled != dataTypeOutlineEnabled)
  60. {
  61. m_dataTypeOutlineEnabled = dataTypeOutlineEnabled;
  62. if (GetSlotId().IsValid())
  63. {
  64. UpdateOutlineColor();
  65. }
  66. }
  67. }
  68. void ComboBoxNodePropertyDisplay::RefreshStyle()
  69. {
  70. m_disabledLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisabledLabelStyle("entityId").c_str());
  71. AZStd::string styleName = NodePropertyDisplay::CreateDisplayLabelStyle("entityId");
  72. m_displayLabel->SetSceneStyle(GetSceneId(), styleName.c_str());
  73. QSizeF minimumSize = m_displayLabel->minimumSize();
  74. QSizeF maximumSize = m_displayLabel->maximumSize();
  75. if (m_comboBox)
  76. {
  77. m_comboBox->setMinimumSize(aznumeric_cast<int>(minimumSize.width()), aznumeric_cast<int>(minimumSize.height()));
  78. m_comboBox->setMaximumSize(aznumeric_cast<int>(maximumSize.width()), aznumeric_cast<int>(maximumSize.height()));
  79. }
  80. UpdateOutlineColor();
  81. }
  82. void ComboBoxNodePropertyDisplay::UpdateDisplay()
  83. {
  84. const QString& displayValue = m_dataInterface->GetDisplayString();
  85. if (m_comboBox)
  86. {
  87. QModelIndex selectedIndex = m_dataInterface->GetAssignedIndex();
  88. {
  89. QSignalBlocker signalBlocker(m_comboBox);
  90. m_comboBox->SetSelectedIndex(selectedIndex);
  91. m_valueDirty = false;
  92. }
  93. }
  94. QString displayLabel = displayValue;
  95. if (displayLabel.isEmpty())
  96. {
  97. displayLabel = "<None>";
  98. }
  99. m_displayLabel->SetLabel(displayLabel.toUtf8().data());
  100. if (m_proxyWidget)
  101. {
  102. m_proxyWidget->update();
  103. }
  104. }
  105. QGraphicsLayoutItem* ComboBoxNodePropertyDisplay::GetDisabledGraphicsLayoutItem()
  106. {
  107. CleanupProxyWidget();
  108. return m_disabledLabel;
  109. }
  110. QGraphicsLayoutItem* ComboBoxNodePropertyDisplay::GetDisplayGraphicsLayoutItem()
  111. {
  112. CleanupProxyWidget();
  113. return m_displayLabel;
  114. }
  115. QGraphicsLayoutItem* ComboBoxNodePropertyDisplay::GetEditableGraphicsLayoutItem()
  116. {
  117. SetupProxyWidget();
  118. return m_proxyWidget;
  119. }
  120. void ComboBoxNodePropertyDisplay::OnPositionChanged(const AZ::EntityId& /*targetEntity*/, const AZ::Vector2& /*position*/)
  121. {
  122. GraphId graphId;
  123. SceneMemberRequestBus::EventResult(graphId, GetNodeId(), &SceneMemberRequests::GetScene);
  124. ViewId viewId;
  125. SceneRequestBus::EventResult(viewId, graphId, &SceneRequests::GetViewId);
  126. UpdateMenuDisplay(viewId);
  127. }
  128. void ComboBoxNodePropertyDisplay::OnZoomChanged(qreal /*zoomLevel*/)
  129. {
  130. const ViewId* viewId = ViewNotificationBus::GetCurrentBusId();
  131. if (viewId)
  132. {
  133. UpdateMenuDisplay((*viewId));
  134. }
  135. }
  136. void ComboBoxNodePropertyDisplay::OnDisplayTypeChanged(const AZ::Uuid& /*dataTypes*/, const AZStd::vector<AZ::Uuid>& /*containerTypes*/)
  137. {
  138. UpdateOutlineColor();
  139. }
  140. void ComboBoxNodePropertyDisplay::OnDragDropStateStateChanged(const DragDropState& dragState)
  141. {
  142. Styling::StyleHelper& styleHelper = m_displayLabel->GetStyleHelper();
  143. UpdateStyleForDragDrop(dragState, styleHelper);
  144. m_displayLabel->update();
  145. }
  146. void ComboBoxNodePropertyDisplay::UpdateOutlineColor()
  147. {
  148. if (!m_dataTypeOutlineEnabled)
  149. {
  150. return;
  151. }
  152. DataValueType valueType = DataValueType::Unknown;
  153. DataSlotRequests* dataSlotRequests = DataSlotRequestBus::FindFirstHandler(GetSlotId());
  154. if (dataSlotRequests)
  155. {
  156. valueType = dataSlotRequests->GetDataValueType();
  157. bool updatedOutline = false;
  158. QBrush outlineBrush;
  159. if (valueType == DataValueType::Container)
  160. {
  161. size_t typeCount = dataSlotRequests->GetContainedTypesCount();
  162. if (typeCount != 0)
  163. {
  164. updatedOutline = true;
  165. AZStd::vector< const Styling::StyleHelper* > containerColorPalettes;
  166. containerColorPalettes.reserve(typeCount);
  167. for (size_t i = 0; i < typeCount; ++i)
  168. {
  169. const Styling::StyleHelper* colorPalette = dataSlotRequests->GetContainedTypeColorPalette(i);
  170. if (colorPalette)
  171. {
  172. containerColorPalettes.emplace_back(colorPalette);
  173. }
  174. }
  175. QLinearGradient penGradient;
  176. QLinearGradient fillGradient;
  177. if (!containerColorPalettes.empty())
  178. {
  179. QtDrawingUtils::GenerateGradients(containerColorPalettes, m_displayLabel->GetDisplayedSize(), penGradient, fillGradient);
  180. }
  181. m_displayLabel->SetBorderColorOverride(QBrush(penGradient));
  182. }
  183. }
  184. if (!updatedOutline)
  185. {
  186. const Styling::StyleHelper* colorPalette = dataSlotRequests->GetDataColorPalette();
  187. if (colorPalette)
  188. {
  189. QColor color = colorPalette->GetColor(GraphCanvas::Styling::Attribute::LineColor);
  190. m_displayLabel->SetBorderColorOverride(QBrush(color));
  191. }
  192. else
  193. {
  194. m_displayLabel->ClearBorderColorOverride();
  195. if (m_comboBox)
  196. {
  197. m_comboBox->ClearOutlineColor();
  198. }
  199. }
  200. }
  201. }
  202. }
  203. void ComboBoxNodePropertyDisplay::OnSlotIdSet()
  204. {
  205. UpdateOutlineColor();
  206. }
  207. void ComboBoxNodePropertyDisplay::EditStart()
  208. {
  209. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::LockEditState, this);
  210. TryAndSelectNode();
  211. }
  212. void ComboBoxNodePropertyDisplay::SubmitValue()
  213. {
  214. if (!m_valueDirty)
  215. {
  216. return;
  217. }
  218. m_valueDirty = false;
  219. if (m_comboBox)
  220. {
  221. QModelIndex index = m_comboBox->GetSelectedIndex();
  222. m_dataInterface->AssignIndex(index);
  223. }
  224. else
  225. {
  226. AZ_Error("GraphCanvas", false, "m_comboBox doesn't exist!");
  227. }
  228. UpdateDisplay();
  229. }
  230. void ComboBoxNodePropertyDisplay::EditFinished()
  231. {
  232. SubmitValue();
  233. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::UnlockEditState, this);
  234. }
  235. void ComboBoxNodePropertyDisplay::SetupProxyWidget()
  236. {
  237. if (!m_comboBox)
  238. {
  239. m_proxyWidget = new QGraphicsProxyWidget();
  240. m_proxyWidget->setFlag(QGraphicsItem::ItemIsFocusable, true);
  241. m_proxyWidget->setFocusPolicy(Qt::StrongFocus);
  242. m_comboBox = aznew GraphCanvasComboBox(m_dataInterface->GetItemInterface());
  243. m_comboBox->setContextMenuPolicy(Qt::CustomContextMenu);
  244. QObject::connect(m_comboBox, &AzToolsFramework::PropertyEntityIdCtrl::customContextMenuRequested, [this](const QPoint& pos) { ShowContextMenu(pos); });
  245. QObject::connect(m_comboBox, &GraphCanvasComboBox::SelectedIndexChanged, [this](const QModelIndex& /*index*/) { m_valueDirty = true; });
  246. QObject::connect(m_comboBox, &GraphCanvasComboBox::OnFocusIn, [this]() { EditStart(); });
  247. QObject::connect(m_comboBox, &GraphCanvasComboBox::OnFocusOut, [this]() { EditFinished(); });
  248. QObject::connect(m_comboBox, &GraphCanvasComboBox::OnUserActionComplete, [this]() { SubmitValue(); });
  249. QObject::connect(m_comboBox, &GraphCanvasComboBox::OnMenuAboutToDisplay, [this]() { OnMenuAboutToDisplay(); });
  250. m_proxyWidget->setWidget(m_comboBox);
  251. RegisterShortcutDispatcher(m_comboBox);
  252. UpdateDisplay();
  253. RefreshStyle();
  254. GraphId graphId;
  255. SceneMemberRequestBus::EventResult(graphId, GetNodeId(), &SceneMemberRequests::GetScene);
  256. ViewId viewId;
  257. SceneRequestBus::EventResult(viewId, graphId, &SceneRequests::GetViewId);
  258. m_comboBox->RegisterViewId(viewId);
  259. m_comboBox->SetSelectedIndex(m_dataInterface->GetAssignedIndex());
  260. m_valueDirty = false;
  261. m_menuDisplayDirty = true;
  262. ViewNotificationBus::Handler::BusConnect(viewId);
  263. GeometryNotificationBus::Handler::BusConnect(GetNodeId());
  264. }
  265. }
  266. void ComboBoxNodePropertyDisplay::CleanupProxyWidget()
  267. {
  268. if (m_comboBox)
  269. {
  270. UnregisterShortcutDispatcher(m_comboBox);
  271. delete m_comboBox; // NB: this implicitly deletes m_proxy widget
  272. m_comboBox = nullptr;
  273. m_proxyWidget = nullptr;
  274. m_menuDisplayDirty = false;
  275. ViewNotificationBus::Handler::BusDisconnect();
  276. GeometryNotificationBus::Handler::BusDisconnect(GetNodeId());
  277. }
  278. }
  279. void ComboBoxNodePropertyDisplay::OnMenuAboutToDisplay()
  280. {
  281. if (m_menuDisplayDirty)
  282. {
  283. GraphId graphId;
  284. SceneMemberRequestBus::EventResult(graphId, GetNodeId(), &SceneMemberRequests::GetScene);
  285. ViewId viewId;
  286. SceneRequestBus::EventResult(viewId, graphId, &SceneRequests::GetViewId);
  287. const bool forceUpdate = true;
  288. UpdateMenuDisplay(viewId, forceUpdate);
  289. m_menuDisplayDirty = false;
  290. }
  291. }
  292. void ComboBoxNodePropertyDisplay::UpdateMenuDisplay(const ViewId& viewId, bool forceUpdate)
  293. {
  294. if (m_proxyWidget && m_comboBox && (m_comboBox->IsMenuVisible() || forceUpdate))
  295. {
  296. QPointF scenePoint = m_proxyWidget->mapToScene(QPoint(0, aznumeric_cast<int>(m_proxyWidget->size().height())));
  297. QPointF widthPoint = m_proxyWidget->mapToScene(QPoint(aznumeric_cast<int>(m_proxyWidget->size().width()), aznumeric_cast<int>(m_proxyWidget->size().height())));
  298. AZ::Vector2 globalPoint;
  299. ViewRequestBus::EventResult(globalPoint, viewId, &ViewRequests::MapToGlobal, ConversionUtils::QPointToVector(scenePoint));
  300. AZ::Vector2 globalWidthPoint;
  301. ViewRequestBus::EventResult(globalWidthPoint, viewId, &ViewRequests::MapToGlobal, ConversionUtils::QPointToVector(widthPoint));
  302. m_comboBox->SetAnchorPoint(ConversionUtils::AZToQPoint(globalPoint).toPoint());
  303. qreal menuWidth = globalPoint.GetDistance(globalWidthPoint);
  304. m_comboBox->SetMenuWidth(menuWidth);
  305. }
  306. else
  307. {
  308. m_menuDisplayDirty = true;
  309. }
  310. }
  311. }