-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInputDialogValidator.cpp
More file actions
executable file
·98 lines (70 loc) · 2.31 KB
/
InputDialogValidator.cpp
File metadata and controls
executable file
·98 lines (70 loc) · 2.31 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "InputDialogValidator.h"
#include <QDialogButtonBox>
#include <QRegExpValidator>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QLabel>
InputDialogValidator::InputDialogValidator(QWidget *parent, Qt::WindowFlags flags) : QDialog(parent)
{
this->setAttribute(Qt::WA_QuitOnClose, false);
if(flags!=0) setWindowFlags(flags);
QVBoxLayout *l=new QVBoxLayout(this);
label=new QLabel(this);
// Security Fix : Whitelisted characters digits and symbols
QRegExp regExp("^[a-zA-Z0-9-_]+$");
//regExp=QRegExp("^[a-zA-Z0-9-_]{0,12}$");
validator=new QRegExpValidator(regExp);
text=new QLineEdit(this);
text->setValidator(validator);
connect(text, SIGNAL(textChanged(QString)), this, SLOT(checkValid(QString)));
buttonBox=new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal, this);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
l->addWidget(label);
l->addWidget(text);
l->addWidget(buttonBox);
}
InputDialogValidator::~InputDialogValidator()
{
}
void InputDialogValidator::setTitle(const QString &title){
setWindowTitle(title);
}
void InputDialogValidator::setLabelText(const QString &label){
this->label->setText(label);
}
void InputDialogValidator::setText(const QString &text)
{
this->text->setText(text);
}
void InputDialogValidator::setRegExp(const QRegExp ®Exp){
validator->setRegExp(regExp);
checkValid(text->text());
}
QString InputDialogValidator::getLabelText(){
return label->text();
}
QString InputDialogValidator::getText(){
return text->text();
}
void InputDialogValidator::checkValid(const QString &text)
{
QString _text=QString(text);
int pos=0;
bool valid=validator->validate(_text, pos)==QRegExpValidator::Acceptable;
buttonBox->button(QDialogButtonBox::Ok);
buttonBox->setEnabled(valid);
}
QString InputDialogValidator::getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp ®Exp, bool *ok, Qt::WindowFlags flags)
{
InputDialogValidator *r = new InputDialogValidator(parent, flags);
r->setTitle(title);
r->setLabelText(label);
r->setText(text);
r->setRegExp(regExp);
*ok = r->exec()==QDialog::Accepted;
if(*ok)
return r->getText();
else
return QString();
}