FormOptionsWidget.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <AzQtComponents/Components/Widgets/CheckBox.h>
  9. #include <FormOptionsWidget.h>
  10. #include <QCheckBox>
  11. #include <QFrame>
  12. #include <QHash>
  13. #include <QHBoxLayout>
  14. #include <QLabel>
  15. #include <QWidget>
  16. #include <QVBoxLayout>
  17. namespace O3DE::ProjectManager
  18. {
  19. FormOptionsWidget::FormOptionsWidget(const QString& labelText,
  20. const QStringList& options,
  21. const QString& allOptionsText,
  22. const int optionItemSpacing,
  23. QWidget* parent)
  24. : QWidget(parent)
  25. {
  26. setObjectName("formOptionsWidget");
  27. QVBoxLayout* mainLayout = new QVBoxLayout();
  28. mainLayout->setAlignment(Qt::AlignTop);
  29. {
  30. m_optionFrame = new QFrame(this);
  31. m_optionFrame->setObjectName("formOptionsFrame");
  32. QHBoxLayout* optionFrameLayout = new QHBoxLayout();
  33. {
  34. QVBoxLayout* fieldLayout = new QVBoxLayout();
  35. QLabel* label = new QLabel(labelText, this);
  36. fieldLayout->addWidget(label);
  37. QHBoxLayout* optionLayout = new QHBoxLayout();
  38. //Add the options
  39. for(const QString& option : options)
  40. {
  41. QCheckBox* optionCheckBox = new QCheckBox(option);
  42. optionLayout->addWidget(optionCheckBox);
  43. connect(optionCheckBox, &QCheckBox::clicked, this, [=](bool checked){
  44. if(checked)
  45. {
  46. Enable(option);
  47. }
  48. else
  49. {
  50. Disable(option);
  51. }
  52. });
  53. m_options.insert(option, optionCheckBox);
  54. optionLayout->addSpacing(optionItemSpacing);
  55. }
  56. //Add the platform option toggle
  57. m_allOptionsToggle = new QCheckBox(allOptionsText);
  58. connect(m_allOptionsToggle, &QCheckBox::clicked, this, [=](bool checked){
  59. if(checked)
  60. {
  61. EnableAll();
  62. }
  63. else
  64. {
  65. Clear();
  66. }
  67. });
  68. AzQtComponents::CheckBox::applyToggleSwitchStyle(m_allOptionsToggle);
  69. optionLayout->addWidget(m_allOptionsToggle);
  70. optionLayout->addStretch();
  71. fieldLayout->addLayout(optionLayout);
  72. optionFrameLayout->addLayout(fieldLayout);
  73. }
  74. m_optionFrame->setLayout(optionFrameLayout);
  75. mainLayout->addWidget(m_optionFrame);
  76. }
  77. setLayout(mainLayout);
  78. }
  79. void FormOptionsWidget::Enable(const QString& option)
  80. {
  81. if(m_options.contains(option))
  82. {
  83. auto checkbox = m_options.value(option);
  84. checkbox->setChecked(true);
  85. if(GetCheckedCount() == m_options.values().count())
  86. {
  87. m_allOptionsToggle->setChecked(true);
  88. }
  89. }
  90. }
  91. void FormOptionsWidget::Enable(const QStringList& options)
  92. {
  93. for(const QString& option : options)
  94. {
  95. Enable(option);
  96. }
  97. }
  98. void FormOptionsWidget::Disable(const QString& option)
  99. {
  100. if(m_options.contains(option))
  101. {
  102. auto checkbox = m_options.value(option);
  103. m_allOptionsToggle->setChecked(false);
  104. checkbox->setChecked(false);
  105. }
  106. }
  107. void FormOptionsWidget::Disable(const QStringList& options)
  108. {
  109. for (const QString& option : options)
  110. {
  111. Disable(option);
  112. }
  113. }
  114. void FormOptionsWidget::EnableAll()
  115. {
  116. for(const auto& checkbox : m_options.values())
  117. {
  118. checkbox->setChecked(true);
  119. }
  120. m_allOptionsToggle->setChecked(true);
  121. }
  122. void FormOptionsWidget::Clear()
  123. {
  124. for(const auto& checkbox : m_options.values())
  125. {
  126. checkbox->setChecked(false);
  127. }
  128. m_allOptionsToggle->setChecked(false);
  129. }
  130. QStringList FormOptionsWidget::GetOptions() const
  131. {
  132. if(m_allOptionsToggle->isChecked())
  133. {
  134. return QStringList(m_options.keys());
  135. }
  136. QStringList options;
  137. for(const QString& option : m_options.keys())
  138. {
  139. auto checkbox = m_options.value(option);
  140. if(checkbox->isChecked())
  141. {
  142. options << option;
  143. }
  144. }
  145. return options;
  146. }
  147. int FormOptionsWidget::GetCheckedCount() const
  148. {
  149. int count = 0;
  150. for(const auto& checkbox : m_options.values())
  151. {
  152. count += static_cast<int>(checkbox->isChecked());
  153. }
  154. return count;
  155. }
  156. } // namespace O3DE::ProjectManager