NameDialog.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. connect(buttonbox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  43. connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  44. }
  45. static bool IsWhitespace(char ch)
  46. {
  47. return ch == ' ' || ch == '\t';
  48. }
  49. static void CleanWhitespace(std::string &str)
  50. {
  51. while (str.size() && IsWhitespace(str.back()))
  52. str.erase(str.end() - 1);
  53. while (str.size() && IsWhitespace(str.front()))
  54. str.erase(str.begin());
  55. }
  56. bool NameDialog::AskForName(QWidget *parent, const QString &title, const QString &text, std::string &userTextInput,
  57. const QString &placeHolder, int maxSize)
  58. {
  59. if (maxSize <= 0 || maxSize > 32767)
  60. maxSize = 170;
  61. NameDialog dialog(parent);
  62. dialog.setWindowTitle(title);
  63. dialog.checkbox->setHidden(true);
  64. dialog.label->setText(text);
  65. dialog.userText->setMaxLength(maxSize);
  66. dialog.userText->setText(placeHolder);
  67. dialog.userText->selectAll();
  68. if (dialog.exec() != DialogCode::Accepted) {
  69. return false;
  70. }
  71. userTextInput = dialog.userText->text().toUtf8().constData();
  72. CleanWhitespace(userTextInput);
  73. return true;
  74. }
  75. bool NameDialog::AskForNameWithOption(QWidget *parent, const QString &title, const QString &text,
  76. std::string &userTextInput, const QString &optionLabel, bool &optionChecked,
  77. const QString &placeHolder)
  78. {
  79. NameDialog dialog(parent);
  80. dialog.setWindowTitle(title);
  81. dialog.label->setText(text);
  82. dialog.userText->setMaxLength(170);
  83. dialog.userText->setText(placeHolder);
  84. dialog.checkbox->setText(optionLabel);
  85. dialog.checkbox->setChecked(optionChecked);
  86. if (dialog.exec() != DialogCode::Accepted) {
  87. return false;
  88. }
  89. userTextInput = dialog.userText->text().toUtf8().constData();
  90. CleanWhitespace(userTextInput);
  91. optionChecked = dialog.checkbox->isChecked();
  92. return true;
  93. }