FormLineEditWidget.cpp 6.2 KB

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