TargetContextButton.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "TargetContextButton.hxx"
  9. #include <Source/LUA/moc_TargetContextButton.cpp>
  10. #include <Source/LUA/LUAEditorContextMessages.h>
  11. #include <Source/LUA/LUAEditorDebuggerMessages.h>
  12. #include <Source/LUA/LUATargetContextTrackerMessages.h>
  13. #include <QMenu>
  14. namespace LUA
  15. {
  16. TargetContextButton::TargetContextButton(QWidget* pParent)
  17. : QPushButton(pParent)
  18. {
  19. LUAEditor::Context_ControlManagement::Handler::BusConnect();
  20. AZStd::string context("Default");
  21. LUAEditor::LUATargetContextRequestMessages::Bus::Broadcast(
  22. &LUAEditor::LUATargetContextRequestMessages::Bus::Events::SetCurrentTargetContext, context);
  23. this->setText("Context: Default");
  24. QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Preferred);
  25. sizePolicy1.setHorizontalStretch(0);
  26. sizePolicy1.setVerticalStretch(0);
  27. sizePolicy1.setHeightForWidth(sizePolicy().hasHeightForWidth());
  28. setSizePolicy(sizePolicy1);
  29. setMinimumSize(QSize(128, 24));
  30. this->setToolTip(tr("Click to change context"));
  31. connect(this, SIGNAL(clicked()), this, SLOT(DoPopup()));
  32. }
  33. TargetContextButton::~TargetContextButton()
  34. {
  35. LUAEditor::Context_ControlManagement::Handler::BusDisconnect();
  36. }
  37. void TargetContextButton::DoPopup()
  38. {
  39. QMenu menu;
  40. AZStd::vector<AZStd::string> contexts;
  41. LUAEditor::LUATargetContextRequestMessages::Bus::BroadcastResult(
  42. contexts, &LUAEditor::LUATargetContextRequestMessages::Bus::Events::RequestTargetContexts);
  43. for (AZStd::vector<AZStd::string>::const_iterator it = contexts.begin(); it != contexts.end(); ++it)
  44. {
  45. QAction* targetAction = new QAction((*it).c_str(), this);
  46. targetAction->setProperty("context", (*it).c_str());
  47. menu.addAction(targetAction);
  48. }
  49. QAction* resultAction = menu.exec(QCursor::pos());
  50. if (resultAction)
  51. {
  52. AZStd::string context = resultAction->property("context").toString().toUtf8().data();
  53. this->setText("Context: None"); // prepare for failure
  54. LUAEditor::LUATargetContextRequestMessages::Bus::Broadcast(
  55. &LUAEditor::LUATargetContextRequestMessages::Bus::Events::SetCurrentTargetContext, context);
  56. }
  57. }
  58. void TargetContextButton::OnTargetContextPrepared(AZStd::string& contextName)
  59. {
  60. QString qstr = QString("Context: %1").arg(contextName.c_str());
  61. this->setText(qstr); // plan for success
  62. }
  63. TargetContextButtonAction::TargetContextButtonAction(QObject* pParent)
  64. : QWidgetAction(pParent)
  65. {
  66. }
  67. QWidget* TargetContextButtonAction::createWidget(QWidget* pParent)
  68. {
  69. return aznew TargetContextButton(pParent);
  70. }
  71. }