2018-11-10 01:13:15 +00:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-11-11 21:39:25 +00:00
|
|
|
#include <algorithm>
|
2018-11-17 17:18:03 +00:00
|
|
|
#include <mutex>
|
2018-11-10 01:13:15 +00:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QVBoxLayout>
|
2018-11-17 17:18:03 +00:00
|
|
|
#include "core/hle/lock.h"
|
2018-11-10 01:13:15 +00:00
|
|
|
#include "yuzu/applets/software_keyboard.h"
|
2018-11-11 21:39:25 +00:00
|
|
|
#include "yuzu/main.h"
|
2018-11-10 01:13:15 +00:00
|
|
|
|
|
|
|
QtSoftwareKeyboardValidator::QtSoftwareKeyboardValidator(
|
2018-11-11 21:39:25 +00:00
|
|
|
Core::Frontend::SoftwareKeyboardParameters parameters)
|
2018-11-10 01:13:15 +00:00
|
|
|
: parameters(std::move(parameters)) {}
|
|
|
|
|
2018-11-11 21:39:25 +00:00
|
|
|
QValidator::State QtSoftwareKeyboardValidator::validate(QString& input, int& pos) const {
|
2018-11-10 01:13:15 +00:00
|
|
|
if (input.size() > parameters.max_length)
|
|
|
|
return Invalid;
|
|
|
|
if (parameters.disable_space && input.contains(' '))
|
|
|
|
return Invalid;
|
|
|
|
if (parameters.disable_address && input.contains('@'))
|
|
|
|
return Invalid;
|
|
|
|
if (parameters.disable_percent && input.contains('%'))
|
|
|
|
return Invalid;
|
|
|
|
if (parameters.disable_slash && (input.contains('/') || input.contains('\\')))
|
|
|
|
return Invalid;
|
|
|
|
if (parameters.disable_number &&
|
2018-11-11 21:39:25 +00:00
|
|
|
std::any_of(input.begin(), input.end(), [](QChar c) { return c.isDigit(); })) {
|
2018-11-10 01:13:15 +00:00
|
|
|
return Invalid;
|
2018-11-11 21:39:25 +00:00
|
|
|
}
|
2018-11-10 01:13:15 +00:00
|
|
|
|
|
|
|
if (parameters.disable_download_code &&
|
2018-11-11 21:39:25 +00:00
|
|
|
std::any_of(input.begin(), input.end(), [](QChar c) { return c == 'O' || c == 'I'; })) {
|
2018-11-10 01:13:15 +00:00
|
|
|
return Invalid;
|
2018-11-11 21:39:25 +00:00
|
|
|
}
|
2018-11-10 01:13:15 +00:00
|
|
|
|
|
|
|
return Acceptable;
|
|
|
|
}
|
|
|
|
|
|
|
|
QtSoftwareKeyboardDialog::QtSoftwareKeyboardDialog(
|
2018-11-11 21:39:25 +00:00
|
|
|
QWidget* parent, Core::Frontend::SoftwareKeyboardParameters parameters_)
|
2018-11-10 01:13:15 +00:00
|
|
|
: QDialog(parent), parameters(std::move(parameters_)) {
|
|
|
|
layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
header_label = new QLabel(QString::fromStdU16String(parameters.header_text));
|
|
|
|
header_label->setFont({header_label->font().family(), 12, QFont::Bold});
|
|
|
|
if (header_label->text().isEmpty())
|
|
|
|
header_label->setText(tr("Enter text:"));
|
|
|
|
|
|
|
|
sub_label = new QLabel(QString::fromStdU16String(parameters.sub_text));
|
|
|
|
sub_label->setFont({sub_label->font().family(), sub_label->font().pointSize(),
|
|
|
|
sub_label->font().weight(), true});
|
|
|
|
sub_label->setHidden(parameters.sub_text.empty());
|
|
|
|
|
|
|
|
guide_label = new QLabel(QString::fromStdU16String(parameters.guide_text));
|
|
|
|
guide_label->setHidden(parameters.guide_text.empty());
|
|
|
|
|
|
|
|
line_edit = new QLineEdit;
|
|
|
|
line_edit->setValidator(new QtSoftwareKeyboardValidator(parameters));
|
|
|
|
line_edit->setMaxLength(static_cast<int>(parameters.max_length));
|
|
|
|
line_edit->setText(QString::fromStdU16String(parameters.initial_text));
|
|
|
|
line_edit->setCursorPosition(
|
|
|
|
parameters.cursor_at_beginning ? 0 : static_cast<int>(parameters.initial_text.size()));
|
|
|
|
line_edit->setEchoMode(parameters.password ? QLineEdit::Password : QLineEdit::Normal);
|
|
|
|
|
|
|
|
buttons = new QDialogButtonBox;
|
|
|
|
buttons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
|
|
|
|
buttons->addButton(parameters.submit_text.empty()
|
|
|
|
? tr("OK")
|
|
|
|
: QString::fromStdU16String(parameters.submit_text),
|
|
|
|
QDialogButtonBox::AcceptRole);
|
|
|
|
|
|
|
|
connect(buttons, &QDialogButtonBox::accepted, this, &QtSoftwareKeyboardDialog::Submit);
|
|
|
|
connect(buttons, &QDialogButtonBox::rejected, this, &QtSoftwareKeyboardDialog::Reject);
|
|
|
|
layout->addWidget(header_label);
|
|
|
|
layout->addWidget(sub_label);
|
|
|
|
layout->addWidget(guide_label);
|
|
|
|
layout->addWidget(line_edit);
|
|
|
|
layout->addWidget(buttons);
|
|
|
|
setLayout(layout);
|
2018-11-11 21:39:25 +00:00
|
|
|
setWindowTitle(tr("Software Keyboard"));
|
2018-11-10 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 21:39:25 +00:00
|
|
|
QtSoftwareKeyboardDialog::~QtSoftwareKeyboardDialog() = default;
|
|
|
|
|
2018-11-10 01:13:15 +00:00
|
|
|
void QtSoftwareKeyboardDialog::Submit() {
|
|
|
|
ok = true;
|
|
|
|
text = line_edit->text().toStdU16String();
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSoftwareKeyboardDialog::Reject() {
|
|
|
|
ok = false;
|
2018-11-11 21:39:25 +00:00
|
|
|
text.clear();
|
2018-11-10 01:13:15 +00:00
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
2018-11-12 01:16:38 +00:00
|
|
|
std::u16string QtSoftwareKeyboardDialog::GetText() const {
|
2018-11-11 21:39:25 +00:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2018-11-12 01:16:38 +00:00
|
|
|
bool QtSoftwareKeyboardDialog::GetStatus() const {
|
2018-11-11 21:39:25 +00:00
|
|
|
return ok;
|
|
|
|
}
|
2018-11-10 01:13:15 +00:00
|
|
|
|
2018-11-12 16:08:09 +00:00
|
|
|
QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& main_window) {
|
|
|
|
connect(this, &QtSoftwareKeyboard::MainWindowGetText, &main_window,
|
|
|
|
&GMainWindow::SoftwareKeyboardGetText, Qt::QueuedConnection);
|
|
|
|
connect(this, &QtSoftwareKeyboard::MainWindowTextCheckDialog, &main_window,
|
|
|
|
&GMainWindow::SoftwareKeyboardInvokeCheckDialog, Qt::BlockingQueuedConnection);
|
|
|
|
connect(&main_window, &GMainWindow::SoftwareKeyboardFinishedText, this,
|
|
|
|
&QtSoftwareKeyboard::MainWindowFinishedText, Qt::QueuedConnection);
|
|
|
|
}
|
2018-11-11 21:39:25 +00:00
|
|
|
|
|
|
|
QtSoftwareKeyboard::~QtSoftwareKeyboard() = default;
|
|
|
|
|
2018-11-12 16:08:09 +00:00
|
|
|
void QtSoftwareKeyboard::RequestText(std::function<void(std::optional<std::u16string>)> out,
|
|
|
|
Core::Frontend::SoftwareKeyboardParameters parameters) const {
|
|
|
|
text_output = out;
|
|
|
|
emit MainWindowGetText(parameters);
|
2018-11-11 21:39:25 +00:00
|
|
|
}
|
2018-11-10 01:13:15 +00:00
|
|
|
|
2018-11-17 17:18:03 +00:00
|
|
|
void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message,
|
|
|
|
std::function<void()> finished_check) const {
|
|
|
|
this->finished_check = finished_check;
|
2018-11-12 16:08:09 +00:00
|
|
|
emit MainWindowTextCheckDialog(error_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSoftwareKeyboard::MainWindowFinishedText(std::optional<std::u16string> text) {
|
2018-11-17 17:18:03 +00:00
|
|
|
// Acquire the HLE mutex
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
|
2018-11-12 16:08:09 +00:00
|
|
|
text_output(text);
|
2018-11-10 01:13:15 +00:00
|
|
|
}
|
2018-11-17 17:18:03 +00:00
|
|
|
|
|
|
|
void QtSoftwareKeyboard::MainWindowFinishedCheckDialog() {
|
|
|
|
// Acquire the HLE mutex
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
|
|
|
|
finished_check();
|
|
|
|
}
|