Bitcoin ABC  0.22.12
P2P Digital Currency
qvalidatedlineedit.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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), valid(true), checkValidator(nullptr) {
12  connect(this, &QValidatedLineEdit::textChanged, this,
14 }
15 
16 void QValidatedLineEdit::setValid(bool _valid) {
17  if (_valid == this->valid) {
18  return;
19  }
20 
21  if (_valid) {
22  setStyleSheet("");
23  } else {
24  setStyleSheet(STYLE_INVALID);
25  }
26  this->valid = _valid;
27 }
28 
29 void QValidatedLineEdit::focusInEvent(QFocusEvent *evt) {
30  // Clear invalid flag on focus
31  setValid(true);
32 
33  QLineEdit::focusInEvent(evt);
34 }
35 
36 void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt) {
37  checkValidity();
38 
39  QLineEdit::focusOutEvent(evt);
40 }
41 
43  // As long as a user is typing ensure we display state as valid
44  setValid(true);
45 }
46 
48  setValid(true);
49  QLineEdit::clear();
50 }
51 
52 void QValidatedLineEdit::setEnabled(bool enabled) {
53  if (!enabled) {
54  // A disabled QValidatedLineEdit should be marked valid
55  setValid(true);
56  } else {
57  // Recheck validity when QValidatedLineEdit gets enabled
58  checkValidity();
59  }
60 
61  QLineEdit::setEnabled(enabled);
62 }
63 
65  if (text().isEmpty()) {
66  setValid(true);
67  } else if (hasAcceptableInput()) {
68  setValid(true);
69 
70  // Check contents on focus out
71  if (checkValidator) {
72  QString address = text();
73  int pos = 0;
74  if (checkValidator->validate(address, pos) ==
75  QValidator::Acceptable) {
76  setValid(true);
77  } else {
78  setValid(false);
79  }
80  }
81  } else {
82  setValid(false);
83  }
84 
85  Q_EMIT validationDidChange(this);
86 }
87 
88 void QValidatedLineEdit::setCheckValidator(const QValidator *v) {
89  checkValidator = v;
90 }
91 
93  // use checkValidator in case the QValidatedLineEdit is disabled
94  if (checkValidator) {
95  QString address = text();
96  int pos = 0;
97  if (checkValidator->validate(address, pos) == QValidator::Acceptable) {
98  return true;
99  }
100  }
101 
102  return valid;
103 }
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
#define STYLE_INVALID
Definition: guiconstants.h:22
void focusInEvent(QFocusEvent *evt) override
void focusOutEvent(QFocusEvent *evt) override
QValidatedLineEdit(QWidget *parent)
const QValidator * checkValidator
void setCheckValidator(const QValidator *v)
void setEnabled(bool enabled)
void setValid(bool valid)