diff options
| author | 2018-11-09 20:13:15 -0500 | |
|---|---|---|
| committer | 2018-11-18 10:53:47 -0500 | |
| commit | 5454494adbfad3148e75b45653a255004ca989b3 (patch) | |
| tree | 6767a10a20d2ade594354c9d6918cf4948d3f5a8 /src | |
| parent | am/applets: Add connector between frontend and AM applet classes (diff) | |
| download | yuzu-5454494adbfad3148e75b45653a255004ca989b3.tar.gz yuzu-5454494adbfad3148e75b45653a255004ca989b3.tar.xz yuzu-5454494adbfad3148e75b45653a255004ca989b3.zip | |
qt/applets: Provide Qt frontend implementation of software keyboard
Implements all of the features of the keyboard, including length, default text, character validation, and UTF-16 character support.
Diffstat (limited to '')
| -rw-r--r-- | src/yuzu/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/yuzu/applets/software_keyboard.cpp | 107 | ||||
| -rw-r--r-- | src/yuzu/applets/software_keyboard.h | 62 |
3 files changed, 171 insertions, 0 deletions
diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index f9ca2948e..53c7f00d4 100644 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt | |||
| @@ -7,6 +7,8 @@ add_executable(yuzu | |||
| 7 | Info.plist | 7 | Info.plist |
| 8 | about_dialog.cpp | 8 | about_dialog.cpp |
| 9 | about_dialog.h | 9 | about_dialog.h |
| 10 | applets/software_keyboard.cpp | ||
| 11 | applets/software_keyboard.h | ||
| 10 | bootmanager.cpp | 12 | bootmanager.cpp |
| 11 | bootmanager.h | 13 | bootmanager.h |
| 12 | compatibility_list.cpp | 14 | compatibility_list.cpp |
diff --git a/src/yuzu/applets/software_keyboard.cpp b/src/yuzu/applets/software_keyboard.cpp new file mode 100644 index 000000000..9e1c59626 --- /dev/null +++ b/src/yuzu/applets/software_keyboard.cpp | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <locale> | ||
| 6 | #include <QDialogButtonBox> | ||
| 7 | #include <QFont> | ||
| 8 | #include <QLabel> | ||
| 9 | #include <QLineEdit> | ||
| 10 | #include <QVBoxLayout> | ||
| 11 | #include "common/logging/backend.h" | ||
| 12 | #include "yuzu/applets/software_keyboard.h" | ||
| 13 | |||
| 14 | QtSoftwareKeyboardValidator::QtSoftwareKeyboardValidator( | ||
| 15 | Frontend::SoftwareKeyboardApplet::Parameters parameters) | ||
| 16 | : parameters(std::move(parameters)) {} | ||
| 17 | |||
| 18 | QValidator::State QtSoftwareKeyboardValidator::validate(QString& input, int&) const { | ||
| 19 | if (input.size() > parameters.max_length) | ||
| 20 | return Invalid; | ||
| 21 | if (parameters.disable_space && input.contains(' ')) | ||
| 22 | return Invalid; | ||
| 23 | if (parameters.disable_address && input.contains('@')) | ||
| 24 | return Invalid; | ||
| 25 | if (parameters.disable_percent && input.contains('%')) | ||
| 26 | return Invalid; | ||
| 27 | if (parameters.disable_slash && (input.contains('/') || input.contains('\\'))) | ||
| 28 | return Invalid; | ||
| 29 | if (parameters.disable_number && | ||
| 30 | std::any_of(input.begin(), input.end(), [](QChar c) { return c.isDigit(); })) | ||
| 31 | return Invalid; | ||
| 32 | |||
| 33 | if (parameters.disable_download_code && | ||
| 34 | std::any_of(input.begin(), input.end(), [](QChar c) { return c == 'O' || c == 'I'; })) | ||
| 35 | return Invalid; | ||
| 36 | |||
| 37 | return Acceptable; | ||
| 38 | } | ||
| 39 | |||
| 40 | QtSoftwareKeyboardDialog::QtSoftwareKeyboardDialog( | ||
| 41 | QWidget* parent, Frontend::SoftwareKeyboardApplet::Parameters parameters_) | ||
| 42 | : QDialog(parent), parameters(std::move(parameters_)) { | ||
| 43 | layout = new QVBoxLayout; | ||
| 44 | |||
| 45 | header_label = new QLabel(QString::fromStdU16String(parameters.header_text)); | ||
| 46 | header_label->setFont({header_label->font().family(), 12, QFont::Bold}); | ||
| 47 | if (header_label->text().isEmpty()) | ||
| 48 | header_label->setText(tr("Enter text:")); | ||
| 49 | |||
| 50 | sub_label = new QLabel(QString::fromStdU16String(parameters.sub_text)); | ||
| 51 | sub_label->setFont({sub_label->font().family(), sub_label->font().pointSize(), | ||
| 52 | sub_label->font().weight(), true}); | ||
| 53 | sub_label->setHidden(parameters.sub_text.empty()); | ||
| 54 | |||
| 55 | guide_label = new QLabel(QString::fromStdU16String(parameters.guide_text)); | ||
| 56 | guide_label->setHidden(parameters.guide_text.empty()); | ||
| 57 | |||
| 58 | line_edit = new QLineEdit; | ||
| 59 | line_edit->setValidator(new QtSoftwareKeyboardValidator(parameters)); | ||
| 60 | line_edit->setMaxLength(static_cast<int>(parameters.max_length)); | ||
| 61 | line_edit->setText(QString::fromStdU16String(parameters.initial_text)); | ||
| 62 | line_edit->setCursorPosition( | ||
| 63 | parameters.cursor_at_beginning ? 0 : static_cast<int>(parameters.initial_text.size())); | ||
| 64 | line_edit->setEchoMode(parameters.password ? QLineEdit::Password : QLineEdit::Normal); | ||
| 65 | |||
| 66 | buttons = new QDialogButtonBox; | ||
| 67 | buttons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole); | ||
| 68 | buttons->addButton(parameters.submit_text.empty() | ||
| 69 | ? tr("OK") | ||
| 70 | : QString::fromStdU16String(parameters.submit_text), | ||
| 71 | QDialogButtonBox::AcceptRole); | ||
| 72 | |||
| 73 | connect(buttons, &QDialogButtonBox::accepted, this, &QtSoftwareKeyboardDialog::Submit); | ||
| 74 | connect(buttons, &QDialogButtonBox::rejected, this, &QtSoftwareKeyboardDialog::Reject); | ||
| 75 | layout->addWidget(header_label); | ||
| 76 | layout->addWidget(sub_label); | ||
| 77 | layout->addWidget(guide_label); | ||
| 78 | layout->addWidget(line_edit); | ||
| 79 | layout->addWidget(buttons); | ||
| 80 | setLayout(layout); | ||
| 81 | setWindowTitle("Software Keyboard"); | ||
| 82 | } | ||
| 83 | |||
| 84 | void QtSoftwareKeyboardDialog::Submit() { | ||
| 85 | ok = true; | ||
| 86 | text = line_edit->text().toStdU16String(); | ||
| 87 | accept(); | ||
| 88 | } | ||
| 89 | |||
| 90 | void QtSoftwareKeyboardDialog::Reject() { | ||
| 91 | ok = false; | ||
| 92 | text = Common::UTF8ToUTF16(""); | ||
| 93 | accept(); | ||
| 94 | } | ||
| 95 | |||
| 96 | QtSoftwareKeyboard::QtSoftwareKeyboard(QWidget& parent) : parent(parent) {} | ||
| 97 | |||
| 98 | bool QtSoftwareKeyboard::GetText(Parameters parameters, std::u16string& text) { | ||
| 99 | QtSoftwareKeyboardDialog dialog(&parent, parameters); | ||
| 100 | dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | | ||
| 101 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint); | ||
| 102 | dialog.setWindowModality(Qt::WindowModal); | ||
| 103 | dialog.exec(); | ||
| 104 | |||
| 105 | text = dialog.text; | ||
| 106 | return dialog.ok; | ||
| 107 | } | ||
diff --git a/src/yuzu/applets/software_keyboard.h b/src/yuzu/applets/software_keyboard.h new file mode 100644 index 000000000..2a18419db --- /dev/null +++ b/src/yuzu/applets/software_keyboard.h | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | #include <QDialog> | ||
| 7 | #include <QValidator> | ||
| 8 | #include "common/assert.h" | ||
| 9 | #include "core/frontend/applets/software_keyboard.h" | ||
| 10 | |||
| 11 | class QDialogButtonBox; | ||
| 12 | class QLabel; | ||
| 13 | class QLineEdit; | ||
| 14 | class QVBoxLayout; | ||
| 15 | class QtSoftwareKeyboard; | ||
| 16 | |||
| 17 | class QtSoftwareKeyboardValidator final : public QValidator { | ||
| 18 | public: | ||
| 19 | explicit QtSoftwareKeyboardValidator(Frontend::SoftwareKeyboardApplet::Parameters parameters); | ||
| 20 | State validate(QString&, int&) const override; | ||
| 21 | |||
| 22 | private: | ||
| 23 | Frontend::SoftwareKeyboardApplet::Parameters parameters; | ||
| 24 | }; | ||
| 25 | |||
| 26 | class QtSoftwareKeyboardDialog final : public QDialog { | ||
| 27 | Q_OBJECT | ||
| 28 | |||
| 29 | public: | ||
| 30 | QtSoftwareKeyboardDialog(QWidget* parent, | ||
| 31 | Frontend::SoftwareKeyboardApplet::Parameters parameters); | ||
| 32 | void Submit(); | ||
| 33 | void Reject(); | ||
| 34 | |||
| 35 | private: | ||
| 36 | bool ok = false; | ||
| 37 | std::u16string text; | ||
| 38 | |||
| 39 | QDialogButtonBox* buttons; | ||
| 40 | QLabel* header_label; | ||
| 41 | QLabel* sub_label; | ||
| 42 | QLabel* guide_label; | ||
| 43 | QLineEdit* line_edit; | ||
| 44 | QVBoxLayout* layout; | ||
| 45 | |||
| 46 | Frontend::SoftwareKeyboardApplet::Parameters parameters; | ||
| 47 | |||
| 48 | friend class QtSoftwareKeyboard; | ||
| 49 | }; | ||
| 50 | |||
| 51 | class QtSoftwareKeyboard final : public QObject, public Frontend::SoftwareKeyboardApplet { | ||
| 52 | public: | ||
| 53 | explicit QtSoftwareKeyboard(QWidget& parent); | ||
| 54 | bool GetText(Parameters parameters, std::u16string& text) override; | ||
| 55 | |||
| 56 | ~QtSoftwareKeyboard() { | ||
| 57 | UNREACHABLE(); | ||
| 58 | } | ||
| 59 | |||
| 60 | private: | ||
| 61 | QWidget& parent; | ||
| 62 | }; | ||