You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qvalidatedlineedit.cpp 2.5KB

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