InputDialogValidatable.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <AzCore/Memory/SystemAllocator.h>
  9. #include <Editor/InputDialogValidatable.h>
  10. #include <QDialogButtonBox>
  11. #include <QKeyEvent>
  12. #include <QLabel>
  13. #include <QPushButton>
  14. #include <QVBoxLayout>
  15. namespace EMStudio
  16. {
  17. AZ_CLASS_ALLOCATOR_IMPL(InputDialogValidatable, AZ::SystemAllocator)
  18. InputDialogValidatable::InputDialogValidatable(QWidget* parent, const QString& labelText, const QRegExp regExp)
  19. : QDialog(parent)
  20. {
  21. QVBoxLayout* layout = new QVBoxLayout();
  22. layout->addWidget(new QLabel(labelText));
  23. m_lineEdit = aznew LineEditValidatable(this, regExp);
  24. layout->addWidget(m_lineEdit);
  25. m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  26. layout->addWidget(m_buttonBox);
  27. connect(m_buttonBox, &QDialogButtonBox::rejected, this, &InputDialogValidatable::reject);
  28. connect(m_buttonBox, &QDialogButtonBox::accepted, this, &InputDialogValidatable::OnAccepted);
  29. connect(m_lineEdit, &LineEditValidatable::TextChanged, this, [this]() {
  30. // Special case when using the validatable line edit for non-existing objects without a previous value.
  31. // This is needed because in case of an invalid input the text will be reset to the previous input.
  32. m_lineEdit->SetPreviousText(m_lineEdit->text());
  33. QPushButton* okButton = m_buttonBox->button(QDialogButtonBox::Ok);
  34. okButton->setEnabled(m_lineEdit->IsValid());
  35. });
  36. setLayout(layout);
  37. }
  38. void InputDialogValidatable::OnAccepted()
  39. {
  40. if (m_lineEdit->IsValid())
  41. {
  42. emit accept();
  43. }
  44. }
  45. void InputDialogValidatable::SetText(const QString& newText)
  46. {
  47. m_lineEdit->setText(newText);
  48. }
  49. QString InputDialogValidatable::GetText() const
  50. {
  51. return m_lineEdit->text();
  52. }
  53. void InputDialogValidatable::SetValidatorFunc(const AZStd::function<bool()>& func)
  54. {
  55. m_lineEdit->SetValidatorFunc(func);
  56. QPushButton* okButton = m_buttonBox->button(QDialogButtonBox::Ok);
  57. okButton->setEnabled(m_lineEdit->IsValid());
  58. }
  59. } // namespace EMStudio