software_keyboard.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <QDialogButtonBox>
  6. #include <QFont>
  7. #include <QLabel>
  8. #include <QLineEdit>
  9. #include <QVBoxLayout>
  10. #include "yuzu/applets/software_keyboard.h"
  11. #include "yuzu/main.h"
  12. QtSoftwareKeyboardValidator::QtSoftwareKeyboardValidator(
  13. Core::Frontend::SoftwareKeyboardParameters parameters)
  14. : parameters(std::move(parameters)) {}
  15. QValidator::State QtSoftwareKeyboardValidator::validate(QString& input, int& pos) const {
  16. if (input.size() > parameters.max_length)
  17. return Invalid;
  18. if (parameters.disable_space && input.contains(' '))
  19. return Invalid;
  20. if (parameters.disable_address && input.contains('@'))
  21. return Invalid;
  22. if (parameters.disable_percent && input.contains('%'))
  23. return Invalid;
  24. if (parameters.disable_slash && (input.contains('/') || input.contains('\\')))
  25. return Invalid;
  26. if (parameters.disable_number &&
  27. std::any_of(input.begin(), input.end(), [](QChar c) { return c.isDigit(); })) {
  28. return Invalid;
  29. }
  30. if (parameters.disable_download_code &&
  31. std::any_of(input.begin(), input.end(), [](QChar c) { return c == 'O' || c == 'I'; })) {
  32. return Invalid;
  33. }
  34. return Acceptable;
  35. }
  36. QtSoftwareKeyboardDialog::QtSoftwareKeyboardDialog(
  37. QWidget* parent, Core::Frontend::SoftwareKeyboardParameters parameters_)
  38. : QDialog(parent), parameters(std::move(parameters_)) {
  39. layout = new QVBoxLayout;
  40. header_label = new QLabel(QString::fromStdU16String(parameters.header_text));
  41. header_label->setFont({header_label->font().family(), 12, QFont::Bold});
  42. if (header_label->text().isEmpty())
  43. header_label->setText(tr("Enter text:"));
  44. sub_label = new QLabel(QString::fromStdU16String(parameters.sub_text));
  45. sub_label->setFont({sub_label->font().family(), sub_label->font().pointSize(),
  46. sub_label->font().weight(), true});
  47. sub_label->setHidden(parameters.sub_text.empty());
  48. guide_label = new QLabel(QString::fromStdU16String(parameters.guide_text));
  49. guide_label->setHidden(parameters.guide_text.empty());
  50. line_edit = new QLineEdit;
  51. line_edit->setValidator(new QtSoftwareKeyboardValidator(parameters));
  52. line_edit->setMaxLength(static_cast<int>(parameters.max_length));
  53. line_edit->setText(QString::fromStdU16String(parameters.initial_text));
  54. line_edit->setCursorPosition(
  55. parameters.cursor_at_beginning ? 0 : static_cast<int>(parameters.initial_text.size()));
  56. line_edit->setEchoMode(parameters.password ? QLineEdit::Password : QLineEdit::Normal);
  57. buttons = new QDialogButtonBox;
  58. buttons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
  59. buttons->addButton(parameters.submit_text.empty()
  60. ? tr("OK")
  61. : QString::fromStdU16String(parameters.submit_text),
  62. QDialogButtonBox::AcceptRole);
  63. connect(buttons, &QDialogButtonBox::accepted, this, &QtSoftwareKeyboardDialog::Submit);
  64. connect(buttons, &QDialogButtonBox::rejected, this, &QtSoftwareKeyboardDialog::Reject);
  65. layout->addWidget(header_label);
  66. layout->addWidget(sub_label);
  67. layout->addWidget(guide_label);
  68. layout->addWidget(line_edit);
  69. layout->addWidget(buttons);
  70. setLayout(layout);
  71. setWindowTitle(tr("Software Keyboard"));
  72. }
  73. QtSoftwareKeyboardDialog::~QtSoftwareKeyboardDialog() = default;
  74. void QtSoftwareKeyboardDialog::Submit() {
  75. ok = true;
  76. text = line_edit->text().toStdU16String();
  77. accept();
  78. }
  79. void QtSoftwareKeyboardDialog::Reject() {
  80. ok = false;
  81. text.clear();
  82. accept();
  83. }
  84. std::u16string QtSoftwareKeyboardDialog::GetText() const {
  85. return text;
  86. }
  87. bool QtSoftwareKeyboardDialog::GetStatus() const {
  88. return ok;
  89. }
  90. QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& parent) : main_window(parent) {}
  91. QtSoftwareKeyboard::~QtSoftwareKeyboard() = default;
  92. std::optional<std::u16string> QtSoftwareKeyboard::GetText(
  93. Core::Frontend::SoftwareKeyboardParameters parameters) const {
  94. std::optional<std::u16string> success;
  95. QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardGetText", Qt::BlockingQueuedConnection,
  96. Q_RETURN_ARG(std::optional<std::u16string>, success),
  97. Q_ARG(Core::Frontend::SoftwareKeyboardParameters, parameters));
  98. return success;
  99. }
  100. void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message) const {
  101. QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardInvokeCheckDialog",
  102. Qt::BlockingQueuedConnection, Q_ARG(std::u16string, error_message));
  103. }