qvalidatedlineedit.cpp raw

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