FormLineEditWidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <FormLineEditWidget.h>
  9. #include <AzQtComponents/Components/StyledLineEdit.h>
  10. #include <AzQtComponents/Components/Widgets/LineEdit.h>
  11. #include <QVBoxLayout>
  12. #include <QHBoxLayout>
  13. #include <QLineEdit>
  14. #include <QLabel>
  15. #include <QMovie>
  16. #include <QFrame>
  17. #include <QValidator>
  18. #include <QStyle>
  19. namespace O3DE::ProjectManager
  20. {
  21. FormLineEditWidget::FormLineEditWidget(
  22. const QString& labelText,
  23. const QString& valueText,
  24. const QString& placeholderText,
  25. const QString& errorText,
  26. QWidget* parent)
  27. : QWidget(parent)
  28. {
  29. setObjectName("formLineEditWidget");
  30. m_mainLayout = new QVBoxLayout();
  31. m_mainLayout->setAlignment(Qt::AlignTop);
  32. {
  33. m_frame = new QFrame(this);
  34. m_frame->setObjectName("formFrame");
  35. // use a horizontal box layout so buttons can be added to the right of the field
  36. m_frameLayout = new QHBoxLayout();
  37. {
  38. QVBoxLayout* fieldLayout = new QVBoxLayout();
  39. QLabel* label = new QLabel(labelText, this);
  40. fieldLayout->addWidget(label);
  41. m_lineEdit = new AzQtComponents::StyledLineEdit(this);
  42. m_lineEdit->setFlavor(AzQtComponents::StyledLineEdit::Question);
  43. AzQtComponents::LineEdit::setErrorIconEnabled(m_lineEdit, false);
  44. m_lineEdit->setText(valueText);
  45. m_lineEdit->setPlaceholderText(placeholderText);
  46. connect(m_lineEdit, &AzQtComponents::StyledLineEdit::flavorChanged, this, &FormLineEditWidget::flavorChanged);
  47. connect(m_lineEdit, &AzQtComponents::StyledLineEdit::onFocus, this, &FormLineEditWidget::onFocus);
  48. connect(m_lineEdit, &AzQtComponents::StyledLineEdit::onFocusOut, this, &FormLineEditWidget::onFocusOut);
  49. m_lineEdit->setFrame(false);
  50. fieldLayout->addWidget(m_lineEdit);
  51. m_frameLayout->addLayout(fieldLayout);
  52. QWidget* emptyWidget = new QWidget(this);
  53. m_frameLayout->addWidget(emptyWidget);
  54. m_processingSpinnerMovie = new QMovie(":/in_progress.gif");
  55. m_processingSpinner = new QLabel(this);
  56. m_processingSpinner->setScaledContents(true);
  57. m_processingSpinner->setMaximumSize(32, 32);
  58. m_processingSpinner->setMovie(m_processingSpinnerMovie);
  59. m_frameLayout->addWidget(m_processingSpinner);
  60. m_validationErrorIcon = new QLabel(this);
  61. m_validationErrorIcon->setPixmap(QIcon(":/error.svg").pixmap(32, 32));
  62. m_frameLayout->addWidget(m_validationErrorIcon);
  63. m_validationSuccessIcon = new QLabel(this);
  64. m_validationSuccessIcon->setPixmap(QIcon(":/checkmark.svg").pixmap(32, 32));
  65. m_frameLayout->addWidget(m_validationSuccessIcon);
  66. SetValidationState(ValidationState::NotValidating);
  67. }
  68. m_frame->setLayout(m_frameLayout);
  69. m_mainLayout->addWidget(m_frame);
  70. m_errorLabel = new QLabel(this);
  71. m_errorLabel->setObjectName("formErrorLabel");
  72. m_errorLabel->setText(errorText);
  73. m_errorLabel->setVisible(false);
  74. m_mainLayout->addWidget(m_errorLabel);
  75. }
  76. setLayout(m_mainLayout);
  77. }
  78. FormLineEditWidget::FormLineEditWidget(const QString& labelText, const QString& valueText, QWidget* parent)
  79. : FormLineEditWidget(labelText, valueText, "", "", parent)
  80. {
  81. }
  82. void FormLineEditWidget::setErrorLabelText(const QString& labelText)
  83. {
  84. m_errorLabel->setText(labelText);
  85. }
  86. void FormLineEditWidget::setErrorLabelVisible(bool visible)
  87. {
  88. m_errorLabel->setVisible(visible);
  89. m_frame->setProperty("Valid", !visible);
  90. refreshStyle();
  91. }
  92. QLineEdit* FormLineEditWidget::lineEdit() const
  93. {
  94. return m_lineEdit;
  95. }
  96. void FormLineEditWidget::flavorChanged()
  97. {
  98. if (m_lineEdit->flavor() == AzQtComponents::StyledLineEdit::Flavor::Invalid)
  99. {
  100. m_frame->setProperty("Valid", false);
  101. m_errorLabel->setVisible(true);
  102. }
  103. else
  104. {
  105. m_frame->setProperty("Valid", true);
  106. m_errorLabel->setVisible(false);
  107. }
  108. refreshStyle();
  109. }
  110. void FormLineEditWidget::onFocus()
  111. {
  112. m_frame->setProperty("Focus", true);
  113. refreshStyle();
  114. }
  115. void FormLineEditWidget::onFocusOut()
  116. {
  117. m_frame->setProperty("Focus", false);
  118. refreshStyle();
  119. }
  120. void FormLineEditWidget::refreshStyle()
  121. {
  122. // we must unpolish/polish every child after changing a property
  123. // or else they won't use the correct stylesheet selector
  124. for (auto child : findChildren<QWidget*>())
  125. {
  126. child->style()->unpolish(child);
  127. child->style()->polish(child);
  128. }
  129. }
  130. void FormLineEditWidget::setText(const QString& text)
  131. {
  132. m_lineEdit->setText(text);
  133. }
  134. void FormLineEditWidget::SetValidationState(ValidationState validationState)
  135. {
  136. switch (validationState)
  137. {
  138. case ValidationState::Validating:
  139. m_processingSpinnerMovie->start();
  140. m_processingSpinner->setVisible(true);
  141. m_validationErrorIcon->setVisible(false);
  142. m_validationSuccessIcon->setVisible(false);
  143. break;
  144. case ValidationState::ValidationSuccess:
  145. m_processingSpinnerMovie->stop();
  146. m_processingSpinner->setVisible(false);
  147. m_validationErrorIcon->setVisible(false);
  148. m_validationSuccessIcon->setVisible(true);
  149. break;
  150. case ValidationState::ValidationFailed:
  151. m_processingSpinnerMovie->stop();
  152. m_processingSpinner->setVisible(false);
  153. m_validationErrorIcon->setVisible(true);
  154. m_validationSuccessIcon->setVisible(false);
  155. break;
  156. case ValidationState::NotValidating:
  157. default:
  158. m_processingSpinnerMovie->stop();
  159. m_processingSpinner->setVisible(false);
  160. m_validationErrorIcon->setVisible(false);
  161. m_validationSuccessIcon->setVisible(false);
  162. break;
  163. }
  164. }
  165. void FormLineEditWidget::mousePressEvent([[maybe_unused]] QMouseEvent* event)
  166. {
  167. m_lineEdit->setFocus();
  168. }
  169. } // namespace O3DE::ProjectManager