software_keyboard.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <mutex>
  6. #include <QDialogButtonBox>
  7. #include <QFont>
  8. #include <QLabel>
  9. #include <QLineEdit>
  10. #include <QVBoxLayout>
  11. #include "core/hle/lock.h"
  12. #include "yuzu/applets/software_keyboard.h"
  13. #include "yuzu/main.h"
  14. QtSoftwareKeyboardValidator::QtSoftwareKeyboardValidator(
  15. Core::Frontend::SoftwareKeyboardParameters parameters)
  16. : parameters(std::move(parameters)) {}
  17. QValidator::State QtSoftwareKeyboardValidator::validate(QString& input, int& pos) const {
  18. if (input.size() > parameters.max_length)
  19. return Invalid;
  20. if (parameters.disable_space && input.contains(' '))
  21. return Invalid;
  22. if (parameters.disable_address && input.contains('@'))
  23. return Invalid;
  24. if (parameters.disable_percent && input.contains('%'))
  25. return Invalid;
  26. if (parameters.disable_slash && (input.contains('/') || input.contains('\\')))
  27. return Invalid;
  28. if (parameters.disable_number &&
  29. std::any_of(input.begin(), input.end(), [](QChar c) { return c.isDigit(); })) {
  30. return Invalid;
  31. }
  32. if (parameters.disable_download_code &&
  33. std::any_of(input.begin(), input.end(), [](QChar c) { return c == 'O' || c == 'I'; })) {
  34. return Invalid;
  35. }
  36. return Acceptable;
  37. }
  38. QtSoftwareKeyboardDialog::QtSoftwareKeyboardDialog(
  39. QWidget* parent, Core::Frontend::SoftwareKeyboardParameters parameters_)
  40. : QDialog(parent), parameters(std::move(parameters_)) {
  41. layout = new QVBoxLayout;
  42. header_label = new QLabel(QString::fromStdU16String(parameters.header_text));
  43. header_label->setFont({header_label->font().family(), 11, QFont::Bold});
  44. if (header_label->text().isEmpty())
  45. header_label->setText(tr("Enter text:"));
  46. sub_label = new QLabel(QString::fromStdU16String(parameters.sub_text));
  47. sub_label->setFont({sub_label->font().family(), sub_label->font().pointSize(),
  48. sub_label->font().weight(), true});
  49. sub_label->setHidden(parameters.sub_text.empty());
  50. guide_label = new QLabel(QString::fromStdU16String(parameters.guide_text));
  51. guide_label->setHidden(parameters.guide_text.empty());
  52. length_label = new QLabel(QStringLiteral("0/%1").arg(parameters.max_length));
  53. length_label->setAlignment(Qt::AlignRight);
  54. length_label->setFont({length_label->font().family(), 8});
  55. line_edit = new QLineEdit;
  56. line_edit->setValidator(new QtSoftwareKeyboardValidator(parameters));
  57. line_edit->setMaxLength(static_cast<int>(parameters.max_length));
  58. line_edit->setText(QString::fromStdU16String(parameters.initial_text));
  59. line_edit->setCursorPosition(
  60. parameters.cursor_at_beginning ? 0 : static_cast<int>(parameters.initial_text.size()));
  61. line_edit->setEchoMode(parameters.password ? QLineEdit::Password : QLineEdit::Normal);
  62. connect(line_edit, &QLineEdit::textChanged, this, [this](const QString& text) {
  63. length_label->setText(QStringLiteral("%1/%2").arg(text.size()).arg(parameters.max_length));
  64. });
  65. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel);
  66. if (parameters.submit_text.empty()) {
  67. buttons->addButton(QDialogButtonBox::Ok);
  68. } else {
  69. buttons->addButton(QString::fromStdU16String(parameters.submit_text),
  70. QDialogButtonBox::AcceptRole);
  71. }
  72. connect(buttons, &QDialogButtonBox::accepted, this, &QtSoftwareKeyboardDialog::accept);
  73. connect(buttons, &QDialogButtonBox::rejected, this, &QtSoftwareKeyboardDialog::reject);
  74. layout->addWidget(header_label);
  75. layout->addWidget(sub_label);
  76. layout->addWidget(guide_label);
  77. layout->addWidget(length_label);
  78. layout->addWidget(line_edit);
  79. layout->addWidget(buttons);
  80. setLayout(layout);
  81. setWindowTitle(tr("Software Keyboard"));
  82. }
  83. QtSoftwareKeyboardDialog::~QtSoftwareKeyboardDialog() = default;
  84. void QtSoftwareKeyboardDialog::accept() {
  85. ok = true;
  86. text = line_edit->text().toStdU16String();
  87. QDialog::accept();
  88. }
  89. void QtSoftwareKeyboardDialog::reject() {
  90. ok = false;
  91. text.clear();
  92. QDialog::reject();
  93. }
  94. std::u16string QtSoftwareKeyboardDialog::GetText() const {
  95. return text;
  96. }
  97. bool QtSoftwareKeyboardDialog::GetStatus() const {
  98. return ok;
  99. }
  100. QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& main_window) {
  101. connect(this, &QtSoftwareKeyboard::MainWindowGetText, &main_window,
  102. &GMainWindow::SoftwareKeyboardGetText, Qt::QueuedConnection);
  103. connect(this, &QtSoftwareKeyboard::MainWindowTextCheckDialog, &main_window,
  104. &GMainWindow::SoftwareKeyboardInvokeCheckDialog, Qt::BlockingQueuedConnection);
  105. connect(&main_window, &GMainWindow::SoftwareKeyboardFinishedText, this,
  106. &QtSoftwareKeyboard::MainWindowFinishedText, Qt::QueuedConnection);
  107. }
  108. QtSoftwareKeyboard::~QtSoftwareKeyboard() = default;
  109. void QtSoftwareKeyboard::RequestText(std::function<void(std::optional<std::u16string>)> out,
  110. Core::Frontend::SoftwareKeyboardParameters parameters) const {
  111. text_output = std::move(out);
  112. emit MainWindowGetText(parameters);
  113. }
  114. void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message,
  115. std::function<void()> finished_check) const {
  116. this->finished_check = std::move(finished_check);
  117. emit MainWindowTextCheckDialog(error_message);
  118. }
  119. void QtSoftwareKeyboard::MainWindowFinishedText(std::optional<std::u16string> text) {
  120. // Acquire the HLE mutex
  121. std::lock_guard lock{HLE::g_hle_lock};
  122. text_output(text);
  123. }
  124. void QtSoftwareKeyboard::MainWindowFinishedCheckDialog() {
  125. // Acquire the HLE mutex
  126. std::lock_guard lock{HLE::g_hle_lock};
  127. finished_check();
  128. }