AnchorPresetsWidget.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "PresetButton.h"
  9. #include "AnchorPresets.h"
  10. #include "AnchorPresetsWidget.h"
  11. #include <AzToolsFramework/UI/PropertyEditor/PropertyQTConstants.h>
  12. #include <QGridLayout>
  13. #define UICANVASEDITOR_ANCHOR_ICON_PATH_DEFAULT(presetIndex) (QString(":/Icons/AnchorIcon%1Default.tif").arg(presetIndex, 2, 10, QChar('0')))
  14. #define UICANVASEDITOR_ANCHOR_ICON_PATH_HOVER(presetIndex) (QString(":/Icons/AnchorIcon%1Hover.tif").arg(presetIndex, 2, 10, QChar('0')))
  15. #define UICANVASEDITOR_ANCHOR_ICON_PATH_SELECTED(presetIndex) (QString(":/Icons/AnchorIcon%1Selected.tif").arg(presetIndex, 2, 10, QChar('0')))
  16. #define UICANVASEDITOR_ANCHOR_WIDGET_FIXED_SIZE (106)
  17. #define UICANVASEDITOR_ANCHOR_BUTTON_AND_ICON_FIXED_SIZE (20)
  18. AnchorPresetsWidget::AnchorPresetsWidget(int defaultPresetIndex,
  19. PresetChanger presetChanger,
  20. QWidget* parent)
  21. : QWidget(parent)
  22. , m_presetIndex(defaultPresetIndex)
  23. , m_buttons(AnchorPresets::PresetIndexCount, nullptr)
  24. {
  25. // The layout.
  26. QGridLayout* grid = new QGridLayout(this);
  27. grid->setContentsMargins(0, 0, 0, 0);
  28. grid->setSpacing(0);
  29. // Preset buttons.
  30. {
  31. for (int presetIndex = 0; presetIndex < AnchorPresets::PresetIndexCount; ++presetIndex)
  32. {
  33. QLayout* boxLayout = new QVBoxLayout();
  34. PresetButton* button = new PresetButton(UICANVASEDITOR_ANCHOR_ICON_PATH_DEFAULT(presetIndex),
  35. UICANVASEDITOR_ANCHOR_ICON_PATH_HOVER(presetIndex),
  36. UICANVASEDITOR_ANCHOR_ICON_PATH_SELECTED(presetIndex),
  37. QSize(UICANVASEDITOR_ANCHOR_BUTTON_AND_ICON_FIXED_SIZE, UICANVASEDITOR_ANCHOR_BUTTON_AND_ICON_FIXED_SIZE),
  38. "",
  39. [ this, presetChanger, presetIndex ]([[maybe_unused]] bool checked)
  40. {
  41. SetPresetSelection(presetIndex);
  42. presetChanger(presetIndex);
  43. },
  44. this);
  45. boxLayout->addWidget(button);
  46. boxLayout->setContentsMargins(2, 2, 2, 2);
  47. grid->addItem(boxLayout, (presetIndex / 4), (presetIndex % 4));
  48. m_buttons[ presetIndex ] = button;
  49. }
  50. }
  51. }
  52. void AnchorPresetsWidget::SetPresetSelection(int presetIndex)
  53. {
  54. if (m_presetIndex != -1)
  55. {
  56. // Clear the old selection.
  57. m_buttons[ m_presetIndex ]->setChecked(false);
  58. }
  59. if (presetIndex != -1)
  60. {
  61. // Set the new selection.
  62. m_buttons[ presetIndex ]->setChecked(true);
  63. }
  64. m_presetIndex = presetIndex;
  65. }
  66. void AnchorPresetsWidget::SetPresetButtonEnabledAt(int presetIndex, bool enabled)
  67. {
  68. if (presetIndex != -1)
  69. {
  70. // Set the new selection.
  71. m_buttons[presetIndex]->setEnabled(enabled);
  72. }
  73. }
  74. #include <moc_AnchorPresetsWidget.cpp>