diff options
| author | 2021-06-09 11:55:36 -0700 | |
|---|---|---|
| committer | 2021-06-09 11:55:36 -0700 | |
| commit | 75a4ac12c6e4c68de147165527155da8e4e96595 (patch) | |
| tree | 549f3ebe7f0e07e7dcedeec9abc709f2515d8087 /src | |
| parent | Merge pull request #6435 from lioncash/nodisc2 (diff) | |
| parent | limitable_input_dialog: Implement character limiter (diff) | |
| download | yuzu-75a4ac12c6e4c68de147165527155da8e4e96595.tar.gz yuzu-75a4ac12c6e4c68de147165527155da8e4e96595.tar.xz yuzu-75a4ac12c6e4c68de147165527155da8e4e96595.zip | |
Merge pull request #6413 from Kewlan/limitable_input_dialog_limit
limitable_input_dialog: Implement character limiter
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuzu/configuration/configure_input_player.cpp | 3 | ||||
| -rw-r--r-- | src/yuzu/util/limitable_input_dialog.cpp | 38 | ||||
| -rw-r--r-- | src/yuzu/util/limitable_input_dialog.h | 12 |
3 files changed, 48 insertions, 5 deletions
diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index ab3512810..d5d624b96 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp | |||
| @@ -1395,7 +1395,8 @@ void ConfigureInputPlayer::keyPressEvent(QKeyEvent* event) { | |||
| 1395 | 1395 | ||
| 1396 | void ConfigureInputPlayer::CreateProfile() { | 1396 | void ConfigureInputPlayer::CreateProfile() { |
| 1397 | const auto profile_name = | 1397 | const auto profile_name = |
| 1398 | LimitableInputDialog::GetText(this, tr("New Profile"), tr("Enter a profile name:"), 1, 20); | 1398 | LimitableInputDialog::GetText(this, tr("New Profile"), tr("Enter a profile name:"), 1, 20, |
| 1399 | LimitableInputDialog::InputLimiter::Filesystem); | ||
| 1399 | 1400 | ||
| 1400 | if (profile_name.isEmpty()) { | 1401 | if (profile_name.isEmpty()) { |
| 1401 | return; | 1402 | return; |
diff --git a/src/yuzu/util/limitable_input_dialog.cpp b/src/yuzu/util/limitable_input_dialog.cpp index edd78e579..6fea41f95 100644 --- a/src/yuzu/util/limitable_input_dialog.cpp +++ b/src/yuzu/util/limitable_input_dialog.cpp | |||
| @@ -21,11 +21,13 @@ void LimitableInputDialog::CreateUI() { | |||
| 21 | 21 | ||
| 22 | text_label = new QLabel(this); | 22 | text_label = new QLabel(this); |
| 23 | text_entry = new QLineEdit(this); | 23 | text_entry = new QLineEdit(this); |
| 24 | text_label_invalid = new QLabel(this); | ||
| 24 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); | 25 | buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); |
| 25 | 26 | ||
| 26 | auto* const layout = new QVBoxLayout; | 27 | auto* const layout = new QVBoxLayout; |
| 27 | layout->addWidget(text_label); | 28 | layout->addWidget(text_label); |
| 28 | layout->addWidget(text_entry); | 29 | layout->addWidget(text_entry); |
| 30 | layout->addWidget(text_label_invalid); | ||
| 29 | layout->addWidget(buttons); | 31 | layout->addWidget(buttons); |
| 30 | 32 | ||
| 31 | setLayout(layout); | 33 | setLayout(layout); |
| @@ -37,18 +39,36 @@ void LimitableInputDialog::ConnectEvents() { | |||
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | QString LimitableInputDialog::GetText(QWidget* parent, const QString& title, const QString& text, | 41 | QString LimitableInputDialog::GetText(QWidget* parent, const QString& title, const QString& text, |
| 40 | int min_character_limit, int max_character_limit) { | 42 | int min_character_limit, int max_character_limit, |
| 43 | InputLimiter limit_type) { | ||
| 41 | Q_ASSERT(min_character_limit <= max_character_limit); | 44 | Q_ASSERT(min_character_limit <= max_character_limit); |
| 42 | 45 | ||
| 43 | LimitableInputDialog dialog{parent}; | 46 | LimitableInputDialog dialog{parent}; |
| 44 | dialog.setWindowTitle(title); | 47 | dialog.setWindowTitle(title); |
| 45 | dialog.text_label->setText(text); | 48 | dialog.text_label->setText(text); |
| 46 | dialog.text_entry->setMaxLength(max_character_limit); | 49 | dialog.text_entry->setMaxLength(max_character_limit); |
| 50 | dialog.text_label_invalid->show(); | ||
| 51 | |||
| 52 | switch (limit_type) { | ||
| 53 | case InputLimiter::Filesystem: | ||
| 54 | dialog.invalid_characters = QStringLiteral("<>:;\"/\\|,.!?*"); | ||
| 55 | break; | ||
| 56 | default: | ||
| 57 | dialog.invalid_characters.clear(); | ||
| 58 | dialog.text_label_invalid->hide(); | ||
| 59 | break; | ||
| 60 | } | ||
| 61 | dialog.text_label_invalid->setText( | ||
| 62 | tr("The text can't contain any of the following characters:\n%1") | ||
| 63 | .arg(dialog.invalid_characters)); | ||
| 47 | 64 | ||
| 48 | auto* const ok_button = dialog.buttons->button(QDialogButtonBox::Ok); | 65 | auto* const ok_button = dialog.buttons->button(QDialogButtonBox::Ok); |
| 49 | ok_button->setEnabled(false); | 66 | ok_button->setEnabled(false); |
| 50 | connect(dialog.text_entry, &QLineEdit::textEdited, [&](const QString& new_text) { | 67 | connect(dialog.text_entry, &QLineEdit::textEdited, [&] { |
| 51 | ok_button->setEnabled(new_text.length() >= min_character_limit); | 68 | if (!dialog.invalid_characters.isEmpty()) { |
| 69 | dialog.RemoveInvalidCharacters(); | ||
| 70 | } | ||
| 71 | ok_button->setEnabled(dialog.text_entry->text().length() >= min_character_limit); | ||
| 52 | }); | 72 | }); |
| 53 | 73 | ||
| 54 | if (dialog.exec() != QDialog::Accepted) { | 74 | if (dialog.exec() != QDialog::Accepted) { |
| @@ -57,3 +77,15 @@ QString LimitableInputDialog::GetText(QWidget* parent, const QString& title, con | |||
| 57 | 77 | ||
| 58 | return dialog.text_entry->text(); | 78 | return dialog.text_entry->text(); |
| 59 | } | 79 | } |
| 80 | |||
| 81 | void LimitableInputDialog::RemoveInvalidCharacters() { | ||
| 82 | auto cpos = text_entry->cursorPosition(); | ||
| 83 | for (int i = 0; i < text_entry->text().length(); i++) { | ||
| 84 | if (invalid_characters.contains(text_entry->text().at(i))) { | ||
| 85 | text_entry->setText(text_entry->text().remove(i, 1)); | ||
| 86 | i--; | ||
| 87 | cpos--; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | text_entry->setCursorPosition(cpos); | ||
| 91 | } | ||
diff --git a/src/yuzu/util/limitable_input_dialog.h b/src/yuzu/util/limitable_input_dialog.h index 164ad7301..a8e31098b 100644 --- a/src/yuzu/util/limitable_input_dialog.h +++ b/src/yuzu/util/limitable_input_dialog.h | |||
| @@ -18,14 +18,24 @@ public: | |||
| 18 | explicit LimitableInputDialog(QWidget* parent = nullptr); | 18 | explicit LimitableInputDialog(QWidget* parent = nullptr); |
| 19 | ~LimitableInputDialog() override; | 19 | ~LimitableInputDialog() override; |
| 20 | 20 | ||
| 21 | enum class InputLimiter { | ||
| 22 | None, | ||
| 23 | Filesystem, | ||
| 24 | }; | ||
| 25 | |||
| 21 | static QString GetText(QWidget* parent, const QString& title, const QString& text, | 26 | static QString GetText(QWidget* parent, const QString& title, const QString& text, |
| 22 | int min_character_limit, int max_character_limit); | 27 | int min_character_limit, int max_character_limit, |
| 28 | InputLimiter limit_type = InputLimiter::None); | ||
| 23 | 29 | ||
| 24 | private: | 30 | private: |
| 25 | void CreateUI(); | 31 | void CreateUI(); |
| 26 | void ConnectEvents(); | 32 | void ConnectEvents(); |
| 27 | 33 | ||
| 34 | void RemoveInvalidCharacters(); | ||
| 35 | QString invalid_characters; | ||
| 36 | |||
| 28 | QLabel* text_label; | 37 | QLabel* text_label; |
| 29 | QLineEdit* text_entry; | 38 | QLineEdit* text_entry; |
| 39 | QLabel* text_label_invalid; | ||
| 30 | QDialogButtonBox* buttons; | 40 | QDialogButtonBox* buttons; |
| 31 | }; | 41 | }; |