FormComboBoxWidget.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/StyledLineEdit.h>
  9. #include <AzQtComponents/Components/Widgets/LineEdit.h>
  10. #include <FormComboBoxWidget.h>
  11. #include <QEvent>
  12. #include <QFrame>
  13. #include <QHBoxLayout>
  14. #include <QLabel>
  15. #include <QLineEdit>
  16. #include <QStyle>
  17. #include <QVBoxLayout>
  18. #include <QValidator>
  19. #include <QComboBox>
  20. namespace O3DE::ProjectManager
  21. {
  22. FormComboBoxWidget::FormComboBoxWidget(const QString& labelText, const QStringList& items, QWidget* parent)
  23. : QWidget(parent)
  24. {
  25. setObjectName("formComboBoxWidget");
  26. QVBoxLayout* mainLayout = new QVBoxLayout();
  27. mainLayout->setAlignment(Qt::AlignTop);
  28. {
  29. m_frame = new QFrame(this);
  30. m_frame->setObjectName("formFrame");
  31. // use a horizontal box layout so buttons can be added to the right of the field
  32. m_frameLayout = new QHBoxLayout();
  33. {
  34. QVBoxLayout* fieldLayout = new QVBoxLayout();
  35. QLabel* label = new QLabel(labelText, this);
  36. fieldLayout->addWidget(label);
  37. m_comboBox = new QComboBox(this);
  38. m_comboBox->addItems(items);
  39. m_comboBox->setFrame(false);
  40. m_comboBox->installEventFilter(this);
  41. fieldLayout->addWidget(m_comboBox);
  42. m_frameLayout->addLayout(fieldLayout);
  43. }
  44. m_frame->setLayout(m_frameLayout);
  45. mainLayout->addWidget(m_frame);
  46. m_errorLabel = new QLabel(this);
  47. m_errorLabel->setObjectName("formErrorLabel");
  48. m_errorLabel->setVisible(false);
  49. mainLayout->addWidget(m_errorLabel);
  50. }
  51. setLayout(mainLayout);
  52. }
  53. QComboBox* FormComboBoxWidget::comboBox() const
  54. {
  55. return m_comboBox;
  56. }
  57. bool FormComboBoxWidget::eventFilter(QObject* object, QEvent* event)
  58. {
  59. if (object == m_comboBox)
  60. {
  61. if (event->type() == QEvent::FocusIn)
  62. {
  63. onFocus();
  64. }
  65. else if (event->type() == QEvent::FocusOut)
  66. {
  67. onFocusOut();
  68. }
  69. }
  70. return false;
  71. }
  72. void FormComboBoxWidget::setErrorLabelText(const QString& labelText)
  73. {
  74. m_errorLabel->setText(labelText);
  75. }
  76. void FormComboBoxWidget::setErrorLabelVisible(bool visible)
  77. {
  78. m_errorLabel->setVisible(visible);
  79. m_frame->setProperty("Valid", !visible);
  80. refreshStyle();
  81. }
  82. void FormComboBoxWidget::onFocus()
  83. {
  84. m_frame->setProperty("Focus", true);
  85. refreshStyle();
  86. }
  87. void FormComboBoxWidget::onFocusOut()
  88. {
  89. m_frame->setProperty("Focus", false);
  90. refreshStyle();
  91. }
  92. void FormComboBoxWidget::refreshStyle()
  93. {
  94. // we must unpolish/polish every child after changing a property
  95. // or else they won't use the correct stylesheet selector
  96. for (auto child : findChildren<QWidget*>())
  97. {
  98. child->style()->unpolish(child);
  99. child->style()->polish(child);
  100. }
  101. }
  102. void FormComboBoxWidget::mousePressEvent([[maybe_unused]] QMouseEvent* event)
  103. {
  104. m_comboBox->setFocus();
  105. }
  106. } // namespace O3DE::ProjectManager