qvalidatedlineedit.h raw

   1  // Copyright (c) 2011-2022 The Limenka 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  #ifndef LIMENKA_QT_QVALIDATEDLINEEDIT_H
   6  #define LIMENKA_QT_QVALIDATEDLINEEDIT_H
   7  
   8  #include <QLineEdit>
   9  
  10  /** Line edit that can be marked as "invalid" to show input validation feedback. When marked as invalid,
  11     it will get a red background until it is focused.
  12   */
  13  class QValidatedLineEdit : public QLineEdit
  14  {
  15      Q_OBJECT
  16  
  17  public:
  18      explicit QValidatedLineEdit(QWidget *parent);
  19      ~QValidatedLineEdit();
  20      void clear();
  21      void setCheckValidator(const QValidator *v);
  22      bool isValid();
  23      void setWarningValidator(const QValidator *);
  24      bool hasWarning() const;
  25  
  26  protected:
  27      void focusInEvent(QFocusEvent *evt) override;
  28      void focusOutEvent(QFocusEvent *evt) override;
  29  
  30  private:
  31      bool valid{true};
  32      const QValidator* checkValidator{nullptr};
  33      bool m_has_warning{false};
  34      const QValidator *m_warning_validator{nullptr};
  35  
  36  public Q_SLOTS:
  37      void setText(const QString&);
  38      void setValid(bool valid, bool with_warning=false, const std::vector<int>&error_locations=std::vector<int>());
  39      void setEnabled(bool enabled);
  40  
  41  Q_SIGNALS:
  42      void validationDidChange(QValidatedLineEdit *validatedLineEdit);
  43  
  44  private Q_SLOTS:
  45      void markValid();
  46      void checkValidity();
  47      bool checkWarning() const;
  48  };
  49  
  50  #endif // LIMENKA_QT_QVALIDATEDLINEEDIT_H
  51