-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
name-dialog.cpp
53 lines (44 loc) · 1.4 KB
/
name-dialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "name-dialog.hpp"
#include <QVBoxLayout>
#include "obs-module.h"
NameDialog::NameDialog(QWidget *parent, const QString &title) : QDialog(parent)
{
setWindowTitle(title);
setModal(true);
setWindowModality(Qt::WindowModality::WindowModal);
setMinimumWidth(200);
setMinimumHeight(100);
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
userText = new QLineEdit(this);
layout->addWidget(userText);
QDialogButtonBox *buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
layout->addWidget(buttonbox);
buttonbox->setCenterButtons(true);
connect(buttonbox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonbox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
static bool IsWhitespace(char ch)
{
return ch == ' ' || ch == '\t';
}
static void CleanWhitespace(std::string &str)
{
while (!str.empty() && IsWhitespace(str.back()))
str.erase(str.end() - 1);
while (!str.empty() && IsWhitespace(str.front()))
str.erase(str.begin());
}
bool NameDialog::AskForName(QWidget *parent, const QString &title, std::string &name)
{
NameDialog dialog(parent, title);
dialog.userText->setMaxLength(170);
dialog.userText->setText(QString::fromUtf8(name.c_str()));
dialog.userText->selectAll();
if (dialog.exec() != DialogCode::Accepted) {
return false;
}
name = dialog.userText->text().toUtf8().constData();
CleanWhitespace(name);
return true;
}