Bitcoin Core  22.99.0
P2P Digital Currency
qvalidatedlineedit.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
8 #include <qt/guiconstants.h>
9 
11  QLineEdit(parent),
12  valid(true),
13  checkValidator(nullptr)
14 {
15  connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
16 }
17 
18 void QValidatedLineEdit::setText(const QString& text)
19 {
20  QLineEdit::setText(text);
21  checkValidity();
22 }
23 
25 {
26  if(_valid == this->valid)
27  {
28  return;
29  }
30 
31  if(_valid)
32  {
33  setStyleSheet("");
34  }
35  else
36  {
37  setStyleSheet("QValidatedLineEdit { " STYLE_INVALID "}");
38  }
39  this->valid = _valid;
40 }
41 
42 void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
43 {
44  // Clear invalid flag on focus
45  setValid(true);
46 
47  QLineEdit::focusInEvent(evt);
48 }
49 
50 void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
51 {
52  checkValidity();
53 
54  QLineEdit::focusOutEvent(evt);
55 }
56 
58 {
59  // As long as a user is typing ensure we display state as valid
60  setValid(true);
61 }
62 
64 {
65  setValid(true);
66  QLineEdit::clear();
67 }
68 
70 {
71  if (!enabled)
72  {
73  // A disabled QValidatedLineEdit should be marked valid
74  setValid(true);
75  }
76  else
77  {
78  // Recheck validity when QValidatedLineEdit gets enabled
79  checkValidity();
80  }
81 
82  QLineEdit::setEnabled(enabled);
83 }
84 
86 {
87  if (text().isEmpty())
88  {
89  setValid(true);
90  }
91  else if (hasAcceptableInput())
92  {
93  setValid(true);
94 
95  // Check contents on focus out
96  if (checkValidator)
97  {
98  QString address = text();
99  int pos = 0;
100  if (checkValidator->validate(address, pos) == QValidator::Acceptable)
101  setValid(true);
102  else
103  setValid(false);
104  }
105  }
106  else
107  setValid(false);
108 
109  Q_EMIT validationDidChange(this);
110 }
111 
112 void QValidatedLineEdit::setCheckValidator(const QValidator *v)
113 {
114  checkValidator = v;
115  checkValidity();
116 }
117 
119 {
120  // use checkValidator in case the QValidatedLineEdit is disabled
121  if (checkValidator)
122  {
123  QString address = text();
124  int pos = 0;
125  if (checkValidator->validate(address, pos) == QValidator::Acceptable)
126  return true;
127  }
128 
129  return valid;
130 }
QValidatedLineEdit::setCheckValidator
void setCheckValidator(const QValidator *v)
Definition: qvalidatedlineedit.cpp:112
QValidatedLineEdit::isValid
bool isValid()
Definition: qvalidatedlineedit.cpp:118
QValidatedLineEdit::focusInEvent
void focusInEvent(QFocusEvent *evt) override
Definition: qvalidatedlineedit.cpp:42
QValidatedLineEdit::setText
void setText(const QString &)
Definition: qvalidatedlineedit.cpp:18
QValidatedLineEdit::markValid
void markValid()
Definition: qvalidatedlineedit.cpp:57
QValidatedLineEdit::setEnabled
void setEnabled(bool enabled)
Definition: qvalidatedlineedit.cpp:69
qvalidatedlineedit.h
QValidatedLineEdit::validationDidChange
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
guiconstants.h
QValidatedLineEdit::setValid
void setValid(bool valid)
Definition: qvalidatedlineedit.cpp:24
bitcoinaddressvalidator.h
QValidatedLineEdit::checkValidator
const QValidator * checkValidator
Definition: qvalidatedlineedit.h:29
QValidatedLineEdit::QValidatedLineEdit
QValidatedLineEdit(QWidget *parent)
Definition: qvalidatedlineedit.cpp:10
QValidatedLineEdit::clear
void clear()
Definition: qvalidatedlineedit.cpp:63
STYLE_INVALID
#define STYLE_INVALID
Definition: guiconstants.h:28
QValidatedLineEdit::checkValidity
void checkValidity()
Definition: qvalidatedlineedit.cpp:85
QValidatedLineEdit::focusOutEvent
void focusOutEvent(QFocusEvent *evt) override
Definition: qvalidatedlineedit.cpp:50
QValidatedLineEdit::valid
bool valid
Definition: qvalidatedlineedit.h:28