TrackViewNewSequenceDialog.cpp 4.1 KB

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