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