limitable_input_dialog.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <QDialogButtonBox>
  4. #include <QLabel>
  5. #include <QLineEdit>
  6. #include <QPushButton>
  7. #include <QVBoxLayout>
  8. #include "yuzu/util/limitable_input_dialog.h"
  9. LimitableInputDialog::LimitableInputDialog(QWidget* parent) : QDialog{parent} {
  10. CreateUI();
  11. ConnectEvents();
  12. }
  13. LimitableInputDialog::~LimitableInputDialog() = default;
  14. void LimitableInputDialog::CreateUI() {
  15. text_label = new QLabel(this);
  16. text_entry = new QLineEdit(this);
  17. text_label_invalid = new QLabel(this);
  18. buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
  19. auto* const layout = new QVBoxLayout;
  20. layout->addWidget(text_label);
  21. layout->addWidget(text_entry);
  22. layout->addWidget(text_label_invalid);
  23. layout->addWidget(buttons);
  24. setLayout(layout);
  25. }
  26. void LimitableInputDialog::ConnectEvents() {
  27. connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  28. connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
  29. }
  30. QString LimitableInputDialog::GetText(QWidget* parent, const QString& title, const QString& text,
  31. int min_character_limit, int max_character_limit,
  32. InputLimiter limit_type) {
  33. Q_ASSERT(min_character_limit <= max_character_limit);
  34. LimitableInputDialog dialog{parent};
  35. dialog.setWindowTitle(title);
  36. dialog.text_label->setText(text);
  37. dialog.text_entry->setMaxLength(max_character_limit);
  38. dialog.text_label_invalid->show();
  39. switch (limit_type) {
  40. case InputLimiter::Filesystem:
  41. dialog.invalid_characters = QStringLiteral("<>:;\"/\\|,.!?*");
  42. break;
  43. default:
  44. dialog.invalid_characters.clear();
  45. dialog.text_label_invalid->hide();
  46. break;
  47. }
  48. dialog.text_label_invalid->setText(
  49. tr("The text can't contain any of the following characters:\n%1")
  50. .arg(dialog.invalid_characters));
  51. auto* const ok_button = dialog.buttons->button(QDialogButtonBox::Ok);
  52. ok_button->setEnabled(false);
  53. connect(dialog.text_entry, &QLineEdit::textEdited, [&] {
  54. if (!dialog.invalid_characters.isEmpty()) {
  55. dialog.RemoveInvalidCharacters();
  56. }
  57. ok_button->setEnabled(dialog.text_entry->text().length() >= min_character_limit);
  58. });
  59. if (dialog.exec() != QDialog::Accepted) {
  60. return {};
  61. }
  62. return dialog.text_entry->text();
  63. }
  64. void LimitableInputDialog::RemoveInvalidCharacters() {
  65. auto cpos = text_entry->cursorPosition();
  66. for (int i = 0; i < text_entry->text().length(); i++) {
  67. if (invalid_characters.contains(text_entry->text().at(i))) {
  68. text_entry->setText(text_entry->text().remove(i, 1));
  69. i--;
  70. cpos--;
  71. }
  72. }
  73. text_entry->setCursorPosition(cpos);
  74. }