File: name-dialog.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (75 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "name-dialog.hpp"

#include <QVBoxLayout>
#include <QDialogButtonBox>

namespace advss {

NameDialog::NameDialog(QWidget *parent)
	: QDialog(parent),
	  _label(new QLabel(this)),
	  _userText(new QLineEdit(this))

{
	setModal(true);
	setWindowModality(Qt::WindowModality::WindowModal);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setFixedWidth(555);
	setMinimumHeight(100);
	auto layout = new QVBoxLayout;
	setLayout(layout);

	layout->addWidget(_label);
	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.size() && isWhitespace(str.back())) {
		str.erase(str.end() - 1);
	}
	while (str.size() && isWhitespace(str.front())) {
		str.erase(str.begin());
	}
}

bool NameDialog::AskForName(QWidget *parent, const QString &title,
			    const QString &prompt, std::string &userTextInput,
			    const QString &placeHolder, int maxSize, bool clean)
{
	if (maxSize <= 0 || maxSize > 32767) {
		maxSize = 170;
	}

	NameDialog dialog(parent);
	dialog.setWindowTitle(title);

	dialog._label->setVisible(!prompt.isEmpty());
	dialog._label->setText(prompt);
	dialog._userText->setMaxLength(maxSize);
	dialog._userText->setText(placeHolder);
	dialog._userText->selectAll();

	if (dialog.exec() != DialogCode::Accepted) {
		return false;
	}
	userTextInput = dialog._userText->text().toUtf8().constData();
	if (clean) {
		cleanWhitespace(userTextInput);
	}
	return true;
}

} // namespace advss