Line data Source code
1 : // Copyright (c) 2011-2013 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 :
5 : #include "qvalidatedlineedit.h"
6 :
7 : #include "bitcoinaddressvalidator.h"
8 : #include "guiconstants.h"
9 :
10 0 : QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
11 : QLineEdit(parent),
12 : valid(true),
13 0 : checkValidator(0)
14 : {
15 0 : connect(this, SIGNAL(textChanged(QString)), this, SLOT(markValid()));
16 0 : }
17 :
18 0 : void QValidatedLineEdit::setValid(bool valid)
19 : {
20 0 : if(valid == this->valid)
21 : {
22 0 : return;
23 : }
24 :
25 0 : if(valid)
26 : {
27 0 : setStyleSheet("");
28 : }
29 : else
30 : {
31 0 : setStyleSheet(STYLE_INVALID);
32 : }
33 0 : this->valid = valid;
34 : }
35 :
36 0 : void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
37 : {
38 : // Clear invalid flag on focus
39 0 : setValid(true);
40 :
41 0 : QLineEdit::focusInEvent(evt);
42 0 : }
43 :
44 0 : void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
45 : {
46 0 : checkValidity();
47 :
48 0 : QLineEdit::focusOutEvent(evt);
49 0 : }
50 :
51 0 : void QValidatedLineEdit::markValid()
52 : {
53 : // As long as a user is typing ensure we display state as valid
54 0 : setValid(true);
55 0 : }
56 :
57 0 : void QValidatedLineEdit::clear()
58 : {
59 0 : setValid(true);
60 0 : QLineEdit::clear();
61 0 : }
62 :
63 0 : void QValidatedLineEdit::setEnabled(bool enabled)
64 : {
65 0 : if (!enabled)
66 : {
67 : // A disabled QValidatedLineEdit should be marked valid
68 0 : setValid(true);
69 : }
70 : else
71 : {
72 : // Recheck validity when QValidatedLineEdit gets enabled
73 0 : checkValidity();
74 : }
75 :
76 0 : QLineEdit::setEnabled(enabled);
77 0 : }
78 :
79 0 : void QValidatedLineEdit::checkValidity()
80 : {
81 0 : if (text().isEmpty())
82 : {
83 0 : setValid(true);
84 : }
85 0 : else if (hasAcceptableInput())
86 : {
87 0 : setValid(true);
88 :
89 : // Check contents on focus out
90 0 : if (checkValidator)
91 : {
92 0 : QString address = text();
93 0 : int pos = 0;
94 0 : if (checkValidator->validate(address, pos) == QValidator::Acceptable)
95 0 : setValid(true);
96 : else
97 0 : setValid(false);
98 : }
99 : }
100 : else
101 0 : setValid(false);
102 0 : }
103 :
104 0 : void QValidatedLineEdit::setCheckValidator(const QValidator *v)
105 : {
106 0 : checkValidator = v;
107 0 : }
|