TrackViewNewSequenceDialog.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "EditorDefs.h"
  9. #include "TrackViewNewSequenceDialog.h"
  10. // Qt
  11. #include <QValidator>
  12. #include <QPushButton>
  13. // CryCommon
  14. #include <CryCommon/Maestro/Types/SequenceType.h>
  15. // Editor
  16. #include "TrackView/TrackViewSequenceManager.h"
  17. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  18. #include <ui_TrackViewNewSequenceDialog.h>
  19. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  20. namespace
  21. {
  22. struct seqTypeComboPair
  23. {
  24. const char* name;
  25. SequenceType type;
  26. };
  27. }
  28. class CTVNewSequenceDialogValidator : public QValidator
  29. {
  30. public:
  31. CTVNewSequenceDialogValidator(QObject* parent)
  32. : QValidator(parent)
  33. {
  34. m_parentDialog = qobject_cast<CTVNewSequenceDialog*>(parent);
  35. }
  36. QValidator::State validate(QString& input, [[maybe_unused]] int& pos) const override
  37. {
  38. constexpr int MaxInputLength = 160;
  39. SetEnabled(true);
  40. SetToolTip("");
  41. if (input.isEmpty())
  42. {
  43. return QValidator::Acceptable; // Allow further editing
  44. }
  45. if (input.contains('/'))
  46. {
  47. SetToolTip("A sequence name cannot contain a '/' character");
  48. return QValidator::Invalid; // Undo this change
  49. }
  50. if (input.length() > MaxInputLength)
  51. {
  52. SetToolTip(QString("A sequence name cannot exceed %1 characters").arg(MaxInputLength).toStdString().c_str());
  53. return QValidator::Invalid; // Undo this change
  54. }
  55. bool isValid = true;
  56. if (input == LIGHT_ANIMATION_SET_NAME)
  57. {
  58. SetToolTip("The sequence name " LIGHT_ANIMATION_SET_NAME " is reserved.\nPlease choose a different name");
  59. isValid = false;
  60. }
  61. else
  62. {
  63. for (unsigned int k = 0; k < GetIEditor()->GetSequenceManager()->GetCount(); ++k)
  64. {
  65. CTrackViewSequence* pSequence = GetIEditor()->GetSequenceManager()->GetSequenceByIndex(k);
  66. const QString fullname = QString::fromUtf8(pSequence->GetName().c_str());
  67. if (fullname.compare(input, Qt::CaseInsensitive) == 0)
  68. {
  69. isValid = false;
  70. SetToolTip("Sequence with this name already exists");
  71. break;
  72. }
  73. }
  74. }
  75. SetEnabled(isValid); // Disable OK button if input is invalid.
  76. return QValidator::Acceptable; // Accept the change and allow further name editing even if input is invalid.
  77. }
  78. private:
  79. void SetEnabled(bool enable) const
  80. {
  81. m_parentDialog->ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
  82. }
  83. void SetToolTip(const char* toolTipText) const
  84. {
  85. m_parentDialog->ui->NAME->setToolTip(toolTipText);
  86. }
  87. const CTVNewSequenceDialog* m_parentDialog;
  88. };
  89. // TrackViewNewSequenceDialog dialog
  90. CTVNewSequenceDialog::CTVNewSequenceDialog(QWidget* parent)
  91. : QDialog(parent)
  92. , ui(new Ui::CTVNewSequenceDialog)
  93. , m_inputFocusSet(false)
  94. , m_validator(new CTVNewSequenceDialogValidator(this))
  95. {
  96. ui->setupUi(this);
  97. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &CTVNewSequenceDialog::OnOK);
  98. connect(ui->NAME, &QLineEdit::returnPressed, this, &CTVNewSequenceDialog::OnOK);
  99. ui->NAME->setValidator(m_validator);
  100. setWindowTitle("Add New Sequence");
  101. OnInitDialog();
  102. }
  103. CTVNewSequenceDialog::~CTVNewSequenceDialog()
  104. {
  105. }
  106. void CTVNewSequenceDialog::OnInitDialog()
  107. {
  108. }
  109. void CTVNewSequenceDialog::OnOK()
  110. {
  111. m_sequenceType = SequenceType::SequenceComponent;
  112. m_sequenceName = ui->NAME->text();
  113. accept();
  114. }
  115. void CTVNewSequenceDialog::showEvent(QShowEvent* event)
  116. {
  117. if (!m_inputFocusSet)
  118. {
  119. ui->NAME->setFocus();
  120. m_inputFocusSet = true;
  121. }
  122. QDialog::showEvent(event);
  123. }
  124. #include <moc_TrackViewNewSequenceDialog.cpp>