window-namedialog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "moc_window-namedialog.cpp"
  15. #include "obs-app.hpp"
  16. #include <qt-wrappers.hpp>
  17. #include <QVBoxLayout>
  18. NameDialog::NameDialog(QWidget *parent) : QDialog(parent)
  19. {
  20. installEventFilter(CreateShortcutFilter());
  21. setModal(true);
  22. setWindowModality(Qt::WindowModality::WindowModal);
  23. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  24. setFixedWidth(555);
  25. setMinimumHeight(100);
  26. QVBoxLayout *layout = new QVBoxLayout;
  27. setLayout(layout);
  28. label = new QLabel(this);
  29. layout->addWidget(label);
  30. label->setText("Set Text");
  31. userText = new QLineEdit(this);
  32. layout->addWidget(userText);
  33. checkbox = new QCheckBox(this);
  34. layout->addWidget(checkbox);
  35. QDialogButtonBox *buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  36. layout->addWidget(buttonbox);
  37. buttonbox->setCenterButtons(true);
  38. connect(buttonbox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  39. connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  40. }
  41. static bool IsWhitespace(char ch)
  42. {
  43. return ch == ' ' || ch == '\t';
  44. }
  45. static void CleanWhitespace(std::string &str)
  46. {
  47. while (str.size() && IsWhitespace(str.back()))
  48. str.erase(str.end() - 1);
  49. while (str.size() && IsWhitespace(str.front()))
  50. str.erase(str.begin());
  51. }
  52. bool NameDialog::AskForName(QWidget *parent, const QString &title, const QString &text, std::string &userTextInput,
  53. const QString &placeHolder, int maxSize)
  54. {
  55. if (maxSize <= 0 || maxSize > 32767)
  56. maxSize = 170;
  57. NameDialog dialog(parent);
  58. dialog.setWindowTitle(title);
  59. dialog.checkbox->setHidden(true);
  60. dialog.label->setText(text);
  61. dialog.userText->setMaxLength(maxSize);
  62. dialog.userText->setText(placeHolder);
  63. dialog.userText->selectAll();
  64. if (dialog.exec() != DialogCode::Accepted) {
  65. return false;
  66. }
  67. userTextInput = dialog.userText->text().toUtf8().constData();
  68. CleanWhitespace(userTextInput);
  69. return true;
  70. }
  71. bool NameDialog::AskForNameWithOption(QWidget *parent, const QString &title, const QString &text,
  72. std::string &userTextInput, const QString &optionLabel, bool &optionChecked,
  73. const QString &placeHolder)
  74. {
  75. NameDialog dialog(parent);
  76. dialog.setWindowTitle(title);
  77. dialog.label->setText(text);
  78. dialog.userText->setMaxLength(170);
  79. dialog.userText->setText(placeHolder);
  80. dialog.checkbox->setText(optionLabel);
  81. dialog.checkbox->setChecked(optionChecked);
  82. if (dialog.exec() != DialogCode::Accepted) {
  83. return false;
  84. }
  85. userTextInput = dialog.userText->text().toUtf8().constData();
  86. CleanWhitespace(userTextInput);
  87. optionChecked = dialog.checkbox->isChecked();
  88. return true;
  89. }