NameDialog.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "NameDialog.hpp"
  15. #include <OBSApp.hpp>
  16. #include <QCheckBox>
  17. #include <QDialogButtonBox>
  18. #include <QLabel>
  19. #include <QLineEdit>
  20. #include <QVBoxLayout>
  21. #include "moc_NameDialog.cpp"
  22. NameDialog::NameDialog(QWidget *parent) : QDialog(parent)
  23. {
  24. installEventFilter(CreateShortcutFilter());
  25. setModal(true);
  26. setWindowModality(Qt::WindowModality::WindowModal);
  27. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  28. setFixedWidth(555);
  29. setMinimumHeight(100);
  30. QVBoxLayout *layout = new QVBoxLayout;
  31. setLayout(layout);
  32. label = new QLabel(this);
  33. layout->addWidget(label);
  34. label->setText("Set Text");
  35. userText = new QLineEdit(this);
  36. layout->addWidget(userText);
  37. checkbox = new QCheckBox(this);
  38. layout->addWidget(checkbox);
  39. QDialogButtonBox *buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  40. layout->addWidget(buttonbox);
  41. buttonbox->setCenterButtons(true);
  42. userText->setFocus();
  43. connect(buttonbox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  44. connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  45. }
  46. static bool IsWhitespace(char ch)
  47. {
  48. return ch == ' ' || ch == '\t';
  49. }
  50. static void CleanWhitespace(std::string &str)
  51. {
  52. while (str.size() && IsWhitespace(str.back()))
  53. str.erase(str.end() - 1);
  54. while (str.size() && IsWhitespace(str.front()))
  55. str.erase(str.begin());
  56. }
  57. bool NameDialog::AskForName(QWidget *parent, const QString &title, const QString &text, std::string &userTextInput,
  58. const QString &placeHolder, int maxSize)
  59. {
  60. if (maxSize <= 0 || maxSize > 32767)
  61. maxSize = 170;
  62. NameDialog dialog(parent);
  63. dialog.setWindowTitle(title);
  64. dialog.checkbox->setHidden(true);
  65. dialog.label->setText(text);
  66. dialog.userText->setMaxLength(maxSize);
  67. dialog.userText->setText(placeHolder);
  68. dialog.userText->selectAll();
  69. if (dialog.exec() != DialogCode::Accepted) {
  70. return false;
  71. }
  72. userTextInput = dialog.userText->text().toUtf8().constData();
  73. CleanWhitespace(userTextInput);
  74. return true;
  75. }
  76. bool NameDialog::AskForNameWithOption(QWidget *parent, const QString &title, const QString &text,
  77. std::string &userTextInput, const QString &optionLabel, bool &optionChecked,
  78. const QString &placeHolder)
  79. {
  80. NameDialog dialog(parent);
  81. dialog.setWindowTitle(title);
  82. dialog.label->setText(text);
  83. dialog.userText->setMaxLength(170);
  84. dialog.userText->setText(placeHolder);
  85. dialog.checkbox->setText(optionLabel);
  86. dialog.checkbox->setChecked(optionChecked);
  87. if (dialog.exec() != DialogCode::Accepted) {
  88. return false;
  89. }
  90. userTextInput = dialog.userText->text().toUtf8().constData();
  91. CleanWhitespace(userTextInput);
  92. optionChecked = dialog.checkbox->isChecked();
  93. return true;
  94. }