ModeToolbar.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "EditorCommon.h"
  9. #include "AlignToolbarSection.h"
  10. ModeToolbar::ModeToolbar(EditorWindow* parent)
  11. : QToolBar("Mode Toolbar", parent)
  12. , m_group(nullptr)
  13. , m_previousAction(nullptr)
  14. , m_alignToolbarSection(new AlignToolbarSection)
  15. {
  16. setObjectName("ModeToolbar"); // needed to save state
  17. setFloatable(false);
  18. AddModes(parent);
  19. m_alignToolbarSection->AddButtons(this);
  20. parent->addToolBar(this);
  21. }
  22. void ModeToolbar::SetCheckedItem(int index)
  23. {
  24. if (m_group)
  25. {
  26. for (auto action : m_group->actions())
  27. {
  28. if (action->data().toInt() == index)
  29. {
  30. m_previousAction->setChecked(false);
  31. m_previousAction = action;
  32. m_previousAction->setChecked(true);
  33. return;
  34. }
  35. }
  36. }
  37. }
  38. void ModeToolbar::AddModes(EditorWindow* parent)
  39. {
  40. m_group = new QActionGroup(this);
  41. int i = 0;
  42. for (const auto m : ViewportInteraction::InteractionMode())
  43. {
  44. int key = (Qt::Key_1 + i++);
  45. int mode = static_cast<int>(m);
  46. QString nodeName = ViewportHelpers::InteractionModeToString(mode);
  47. QString iconImageDefault = QString(":/Icons/Mode%1Default.png").arg(nodeName);
  48. QIcon icon(iconImageDefault);
  49. QAction* action = new QAction(icon,
  50. (QString("%1 (%2)").arg(ViewportHelpers::InteractionModeToString(mode), QString(static_cast<char>(key)))),
  51. this);
  52. action->setData(mode);
  53. action->setShortcut(QKeySequence(key));
  54. action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
  55. action->setCheckable(true); // Give it the behavior of a toggle button.
  56. QObject::connect(action,
  57. &QAction::triggered,
  58. this,
  59. [ this, parent, action ]([[maybe_unused]] bool checked)
  60. {
  61. if (m_previousAction == action)
  62. {
  63. // Nothing to do.
  64. return;
  65. }
  66. parent->GetViewport()->GetViewportInteraction()->SetMode((ViewportInteraction::InteractionMode)action->data().toInt());
  67. m_previousAction = action;
  68. });
  69. m_group->addAction(action);
  70. }
  71. // Give it the behavior of radio buttons.
  72. m_group->setExclusive(true);
  73. // Set the first action as the default.
  74. m_previousAction = m_group->actions().constFirst();
  75. m_previousAction->setChecked(true);
  76. addActions(m_group->actions());
  77. }
  78. #include <moc_ModeToolbar.cpp>