Bitcoin ABC  0.24.7
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  Q_EMIT validationDidChange(this);
29 }
30 
31 void QValidatedLineEdit::focusInEvent(QFocusEvent *evt) {
32  // Clear invalid flag on focus
33  setValid(true);
34 
35  QLineEdit::focusInEvent(evt);
36 }
37 
38 void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt) {
39  checkValidity();
40 
41  QLineEdit::focusOutEvent(evt);
42 }
43 
45  // As long as a user is typing ensure we display state as valid
46  setValid(true);
47 }
48 
50  setValid(true);
51  QLineEdit::clear();
52 }
53 
54 void QValidatedLineEdit::setEnabled(bool enabled) {
55  if (!enabled) {
56  // A disabled QValidatedLineEdit should be marked valid
57  setValid(true);
58  } else {
59  // Recheck validity when QValidatedLineEdit gets enabled
60  checkValidity();
61  }
62 
63  QLineEdit::setEnabled(enabled);
64 }
65 
67  if (text().isEmpty()) {
68  setValid(true);
69  } else if (hasAcceptableInput()) {
70  setValid(true);
71 
72  // Check contents on focus out
73  if (checkValidator) {
74  QString address = text();
75  int pos = 0;
76  if (checkValidator->validate(address, pos) ==
77  QValidator::Acceptable) {
78  setValid(true);
79  } else {
80  setValid(false);
81  }
82  }
83  } else {
84  setValid(false);
85  }
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 }
QValidatedLineEdit::setCheckValidator
void setCheckValidator(const QValidator *v)
Definition: qvalidatedlineedit.cpp:88
QValidatedLineEdit::isValid
bool isValid()
Definition: qvalidatedlineedit.cpp:92
QValidatedLineEdit::focusInEvent
void focusInEvent(QFocusEvent *evt) override
Definition: qvalidatedlineedit.cpp:31
QValidatedLineEdit::markValid
void markValid()
Definition: qvalidatedlineedit.cpp:44
QValidatedLineEdit::setEnabled
void setEnabled(bool enabled)
Definition: qvalidatedlineedit.cpp:54
qvalidatedlineedit.h
QValidatedLineEdit::validationDidChange
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
guiconstants.h
QValidatedLineEdit::setValid
void setValid(bool valid)
Definition: qvalidatedlineedit.cpp:16
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:49
STYLE_INVALID
#define STYLE_INVALID
Definition: guiconstants.h:22
QValidatedLineEdit::checkValidity
void checkValidity()
Definition: qvalidatedlineedit.cpp:66
QValidatedLineEdit::focusOutEvent
void focusOutEvent(QFocusEvent *evt) override
Definition: qvalidatedlineedit.cpp:38
QValidatedLineEdit::valid
bool valid
Definition: qvalidatedlineedit.h:28