masked_input_field.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. #include "ui/effects/animations.h"
  10. #include <QtWidgets/QLineEdit>
  11. #include <QtCore/QTimer>
  12. namespace style {
  13. struct InputField;
  14. } // namespace style
  15. namespace Ui {
  16. class PopupMenu;
  17. class MaskedInputField : public RpWidgetBase<QLineEdit> {
  18. Q_OBJECT
  19. using Parent = RpWidgetBase<QLineEdit>;
  20. public:
  21. MaskedInputField(
  22. QWidget *parent,
  23. const style::InputField &st,
  24. rpl::producer<QString> placeholder = nullptr,
  25. const QString &val = QString());
  26. void showError();
  27. void showErrorNoFocus();
  28. void hideError();
  29. QSize sizeHint() const override;
  30. QSize minimumSizeHint() const override;
  31. void customUpDown(bool isCustom);
  32. int borderAnimationStart() const;
  33. const QString &getLastText() const {
  34. return _oldtext;
  35. }
  36. void setPlaceholder(rpl::producer<QString> placeholder);
  37. void setPlaceholderHidden(bool forcePlaceholderHidden);
  38. void setDisplayFocused(bool focused);
  39. void finishAnimating();
  40. void setFocusFast() {
  41. setDisplayFocused(true);
  42. setFocus();
  43. }
  44. void setText(const QString &text) {
  45. QLineEdit::setText(text);
  46. startPlaceholderAnimation();
  47. }
  48. void clear() {
  49. QLineEdit::clear();
  50. startPlaceholderAnimation();
  51. }
  52. public Q_SLOTS:
  53. void onTextChange(const QString &text);
  54. void onCursorPositionChanged(int oldPosition, int position);
  55. void onTextEdited();
  56. void onTouchTimer();
  57. Q_SIGNALS:
  58. void changed();
  59. void cancelled();
  60. void submitted(Qt::KeyboardModifiers);
  61. void focused();
  62. void blurred();
  63. protected:
  64. QString getDisplayedText() const;
  65. void startBorderAnimation();
  66. void startPlaceholderAnimation();
  67. bool eventHook(QEvent *e) override;
  68. void touchEvent(QTouchEvent *e);
  69. void paintEvent(QPaintEvent *e) override;
  70. void focusInEvent(QFocusEvent *e) override;
  71. void focusOutEvent(QFocusEvent *e) override;
  72. void keyPressEvent(QKeyEvent *e) override;
  73. void resizeEvent(QResizeEvent *e) override;
  74. void contextMenuEvent(QContextMenuEvent *e) override;
  75. void inputMethodEvent(QInputMethodEvent *e) override;
  76. void mousePressEvent(QMouseEvent *e) override;
  77. void mouseReleaseEvent(QMouseEvent *e) override;
  78. void mouseMoveEvent(QMouseEvent *e) override;
  79. virtual void correctValue(
  80. const QString &was,
  81. int wasCursor,
  82. QString &now,
  83. int &nowCursor) {
  84. }
  85. void setCorrectedText(
  86. QString &now,
  87. int &nowCursor,
  88. const QString &newText,
  89. int newPos);
  90. virtual void paintAdditionalPlaceholder(QPainter &p) {
  91. }
  92. style::font phFont();
  93. void placeholderAdditionalPrepare(QPainter &p);
  94. QRect placeholderRect() const;
  95. void setTextMargins(const QMargins &mrg);
  96. const style::InputField &_st;
  97. private:
  98. void updatePalette();
  99. void refreshPlaceholder(const QString &text);
  100. void setErrorShown(bool error);
  101. void touchUpdate(QPoint globalPosition);
  102. void touchFinish();
  103. void setFocused(bool focused);
  104. int _maxLength = -1;
  105. bool _forcePlaceholderHidden = false;
  106. QString _oldtext;
  107. int _oldcursor = 0;
  108. QString _lastPreEditText;
  109. bool _undoAvailable = false;
  110. bool _redoAvailable = false;
  111. bool _customUpDown = false;
  112. rpl::variable<QString> _placeholderFull;
  113. QString _placeholder;
  114. Animations::Simple _a_placeholderShifted;
  115. bool _placeholderShifted = false;
  116. QPainterPath _placeholderPath;
  117. Animations::Simple _a_borderShown;
  118. int _borderAnimationStart = 0;
  119. Animations::Simple _a_borderOpacity;
  120. bool _borderVisible = false;
  121. Animations::Simple _a_focused;
  122. Animations::Simple _a_error;
  123. bool _focused = false;
  124. bool _error = false;
  125. style::margins _textMargins;
  126. QTimer _touchTimer;
  127. bool _touchPress = false;
  128. bool _touchRightButton = false;
  129. bool _touchMove = false;
  130. bool _mousePressedInTouch = false;
  131. QPoint _touchStart;
  132. base::unique_qptr<PopupMenu> _contextMenu;
  133. };
  134. } // namespace Ui