AlignToolbarSection.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "ViewportAlign.h"
  11. #include <QToolButton>
  12. #include <QObject>
  13. AlignToolbarSection::AlignToolbarSection()
  14. {
  15. }
  16. void AlignToolbarSection::SetIsEnabled(bool enabled)
  17. {
  18. for (auto button : m_buttons)
  19. {
  20. button->setEnabled(enabled);
  21. }
  22. }
  23. void AlignToolbarSection::SetIsVisible(bool visible)
  24. {
  25. m_separator->setVisible(visible);
  26. for (auto button : m_buttons)
  27. {
  28. button->setVisible(visible);
  29. }
  30. }
  31. void AlignToolbarSection::AddButtons(QToolBar* parent)
  32. {
  33. m_separator = parent->addSeparator();
  34. AddButton(parent, ViewportAlign::AlignType::VerticalTop, "AlignVTop", "Align Top Edges");
  35. AddButton(parent, ViewportAlign::AlignType::VerticalCenter, "AlignVCenter", "Align Centers Vertically");
  36. AddButton(parent, ViewportAlign::AlignType::VerticalBottom, "AlignVBottom", "Align Bottom Edges");
  37. AddButton(parent, ViewportAlign::AlignType::HorizontalLeft, "AlignHLeft", "Align Left Edges");
  38. AddButton(parent, ViewportAlign::AlignType::HorizontalCenter, "AlignHCenter", "Align Centers Horizontally");
  39. AddButton(parent, ViewportAlign::AlignType::HorizontalRight, "AlignHRight", "Align Right Edges");
  40. }
  41. void AlignToolbarSection::AddButton(QToolBar* parent, ViewportAlign::AlignType alignType, const QString& iconName, const QString& toolTip)
  42. {
  43. EditorWindow* editorWindow = static_cast<EditorWindow*>(parent->parent());
  44. // Setup the icon
  45. QString iconImageDefault = QString(":/Icons/%1Default.png").arg(iconName);
  46. QIcon icon(iconImageDefault);
  47. // Create a ToolButton to put in the parent toolbar
  48. QToolButton* button = new QToolButton(parent);
  49. button->setIcon(icon);
  50. button->setToolTip(toolTip);
  51. button->setContentsMargins(0, 0, 0, 0);
  52. // Connect it up to call the align operation
  53. QObject::connect(button,
  54. &QPushButton::clicked,
  55. [ editorWindow, alignType ]([[maybe_unused]] bool checked)
  56. {
  57. ViewportAlign::AlignSelectedElements(editorWindow, alignType);
  58. });
  59. // Add the button to the parent tool bar
  60. QAction* buttonAction = parent->addWidget(button);
  61. //! we store the QActions rather than the QToolButtons since hiding and showing will not work on the QToolButtons
  62. m_buttons.push_back(buttonAction);
  63. }