LineEditValidatable.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/LineEditValidatable.h>
  10. namespace EMStudio
  11. {
  12. AZ_CLASS_ALLOCATOR_IMPL(LineEditValidatable, AZ::SystemAllocator)
  13. const QRegExp LineEditValidatable::s_defaultRegExp = QRegExp("(^[^{}\"%<>:\\\\/|?*]*$)");
  14. LineEditValidatable::LineEditValidatable(QWidget* parent, const QRegExp& regExp)
  15. : QLineEdit(parent)
  16. , m_validationExp(regExp)
  17. , m_lineValidator(m_validationExp, 0)
  18. {
  19. setValidator(&m_lineValidator);
  20. connect(this, &QLineEdit::textChanged, this, &LineEditValidatable::OnTextChanged);
  21. connect(this, &QLineEdit::editingFinished, this, &LineEditValidatable::OnEditingFinished);
  22. }
  23. void LineEditValidatable::SetPreviousText(const QString& previousText)
  24. {
  25. m_previousText = previousText;
  26. }
  27. QString LineEditValidatable::GetPreviousText() const
  28. {
  29. return m_previousText;
  30. }
  31. void LineEditValidatable::OnTextChanged()
  32. {
  33. if (IsValid())
  34. {
  35. setStyleSheet("");
  36. }
  37. else
  38. {
  39. setStyleSheet("border: 1px solid red;");
  40. }
  41. emit TextChanged();
  42. }
  43. void LineEditValidatable::OnEditingFinished()
  44. {
  45. if (IsValid() && m_previousText != text())
  46. {
  47. emit TextEditingFinished();
  48. }
  49. else
  50. {
  51. setText(m_previousText);
  52. }
  53. }
  54. bool LineEditValidatable::IsValid() const
  55. {
  56. if (m_validatorFunc)
  57. {
  58. return m_validatorFunc();
  59. }
  60. // Always return true in case the validator function is not callable and has not been set.
  61. return true;
  62. }
  63. void LineEditValidatable::focusInEvent([[maybe_unused]] QFocusEvent* event)
  64. {
  65. selectAll();
  66. }
  67. } // namespace EMStudio