1
0

NameDialog.cpp 3.6 KB

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