window-namedialog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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 "window-namedialog.hpp"
  15. #include "qt-wrappers.hpp"
  16. #include "obs-app.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(
  36. QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  37. layout->addWidget(buttonbox);
  38. buttonbox->setCenterButtons(true);
  39. connect(buttonbox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  40. connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  41. }
  42. static bool IsWhitespace(char ch)
  43. {
  44. return ch == ' ' || ch == '\t';
  45. }
  46. static void CleanWhitespace(std::string &str)
  47. {
  48. while (str.size() && IsWhitespace(str.back()))
  49. str.erase(str.end() - 1);
  50. while (str.size() && IsWhitespace(str.front()))
  51. str.erase(str.begin());
  52. }
  53. bool NameDialog::AskForName(QWidget *parent, const QString &title,
  54. const QString &text, std::string &userTextInput,
  55. const QString &placeHolder, int maxSize)
  56. {
  57. if (maxSize <= 0 || maxSize > 32767)
  58. maxSize = 170;
  59. NameDialog dialog(parent);
  60. dialog.setWindowTitle(title);
  61. dialog.checkbox->setHidden(true);
  62. dialog.label->setText(text);
  63. dialog.userText->setMaxLength(maxSize);
  64. dialog.userText->setText(placeHolder);
  65. dialog.userText->selectAll();
  66. if (dialog.exec() != DialogCode::Accepted) {
  67. return false;
  68. }
  69. userTextInput = dialog.userText->text().toUtf8().constData();
  70. CleanWhitespace(userTextInput);
  71. return true;
  72. }
  73. bool NameDialog::AskForNameWithOption(QWidget *parent, const QString &title,
  74. const QString &text,
  75. std::string &userTextInput,
  76. const QString &optionLabel,
  77. 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. }