forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathqvalidatedlineedit.cpp
More file actions
136 lines (115 loc) · 2.84 KB
/
qvalidatedlineedit.cpp
File metadata and controls
136 lines (115 loc) · 2.84 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2009-2020 The Bitcoin Core developers
// Copyright (c) 2014-2025 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/qvalidatedlineedit.h>
#include <qt/digibyteaddressvalidator.h>
#include <qt/guiconstants.h>
#include <QStyle>
QValidatedLineEdit::QValidatedLineEdit(QWidget* parent)
: QLineEdit(parent)
{
connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
}
void QValidatedLineEdit::setText(const QString& text)
{
QLineEdit::setText(text);
checkValidity();
}
void QValidatedLineEdit::setValid(bool _valid)
{
if(_valid == this->valid)
{
return;
}
if(_valid)
{
setStyleSheet("");
setProperty("invalid", false);
}
else
{
// Don't use inline styles - let CSS handle the styling
setProperty("invalid", true);
}
this->valid = _valid;
// Force style update
style()->unpolish(this);
style()->polish(this);
}
void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
{
// Clear invalid flag on focus
setValid(true);
QLineEdit::focusInEvent(evt);
}
void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
{
checkValidity();
QLineEdit::focusOutEvent(evt);
}
void QValidatedLineEdit::markValid()
{
// As long as a user is typing ensure we display state as valid
setValid(true);
}
void QValidatedLineEdit::clear()
{
setValid(true);
QLineEdit::clear();
}
void QValidatedLineEdit::setEnabled(bool enabled)
{
if (!enabled)
{
// A disabled QValidatedLineEdit should be marked valid
setValid(true);
}
else
{
// Recheck validity when QValidatedLineEdit gets enabled
checkValidity();
}
QLineEdit::setEnabled(enabled);
}
void QValidatedLineEdit::checkValidity()
{
if (text().isEmpty())
{
setValid(true);
}
else if (hasAcceptableInput())
{
setValid(true);
// Check contents on focus out
if (checkValidator)
{
QString address = text();
int pos = 0;
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
setValid(true);
else
setValid(false);
}
}
else
setValid(false);
Q_EMIT validationDidChange(this);
}
void QValidatedLineEdit::setCheckValidator(const QValidator *v)
{
checkValidator = v;
checkValidity();
}
bool QValidatedLineEdit::isValid()
{
// use checkValidator in case the QValidatedLineEdit is disabled
if (checkValidator)
{
QString address = text();
int pos = 0;
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
return true;
}
return valid;
}