qtmaterialtextfield_internal.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "qtmaterialtextfield_internal.h"
  2. #include <QPropertyAnimation>
  3. #include <QEventTransition>
  4. #include <QPainter>
  5. #include "qtmaterialtextfield.h"
  6. /*!
  7. * \class QtMaterialTextFieldStateMachine
  8. * \internal
  9. */
  10. QtMaterialTextFieldStateMachine::QtMaterialTextFieldStateMachine(QtMaterialTextField *parent)
  11. : QStateMachine(parent),
  12. m_textField(parent),
  13. m_normalState(new QState),
  14. m_focusedState(new QState),
  15. m_label(0),
  16. m_offsetAnimation(0),
  17. m_colorAnimation(0),
  18. m_progress(0.0)
  19. {
  20. Q_ASSERT(parent);
  21. addState(m_normalState);
  22. addState(m_focusedState);
  23. setInitialState(m_normalState);
  24. QEventTransition *transition;
  25. QPropertyAnimation *animation;
  26. transition = new QEventTransition(parent, QEvent::FocusIn);
  27. transition->setTargetState(m_focusedState);
  28. m_normalState->addTransition(transition);
  29. animation = new QPropertyAnimation(this, "progress", this);
  30. animation->setEasingCurve(QEasingCurve::InCubic);
  31. animation->setDuration(310);
  32. transition->addAnimation(animation);
  33. transition = new QEventTransition(parent, QEvent::FocusOut);
  34. transition->setTargetState(m_normalState);
  35. m_focusedState->addTransition(transition);
  36. animation = new QPropertyAnimation(this, "progress", this);
  37. animation->setEasingCurve(QEasingCurve::OutCubic);
  38. animation->setDuration(310);
  39. transition->addAnimation(animation);
  40. m_normalState->assignProperty(this, "progress", 0);
  41. m_focusedState->assignProperty(this, "progress", 1);
  42. setupProperties();
  43. connect(m_textField, SIGNAL(textChanged(QString)), this, SLOT(setupProperties()));
  44. }
  45. QtMaterialTextFieldStateMachine::~QtMaterialTextFieldStateMachine()
  46. {
  47. }
  48. void QtMaterialTextFieldStateMachine::setLabel(QtMaterialTextFieldLabel *label)
  49. {
  50. if (m_label) {
  51. delete m_label;
  52. }
  53. if (m_offsetAnimation) {
  54. removeDefaultAnimation(m_offsetAnimation);
  55. delete m_offsetAnimation;
  56. }
  57. if (m_colorAnimation) {
  58. removeDefaultAnimation(m_colorAnimation);
  59. delete m_colorAnimation;
  60. }
  61. m_label = label;
  62. if (m_label)
  63. {
  64. m_offsetAnimation = new QPropertyAnimation(m_label, "offset", this);
  65. m_offsetAnimation->setDuration(210);
  66. m_offsetAnimation->setEasingCurve(QEasingCurve::OutCubic);
  67. addDefaultAnimation(m_offsetAnimation);
  68. m_colorAnimation = new QPropertyAnimation(m_label, "color", this);
  69. m_colorAnimation->setDuration(210);
  70. addDefaultAnimation(m_colorAnimation);
  71. }
  72. setupProperties();
  73. }
  74. void QtMaterialTextFieldStateMachine::setupProperties()
  75. {
  76. if (m_label)
  77. {
  78. const int m = m_textField->textMargins().top();
  79. if (m_textField->text().isEmpty()) {
  80. m_normalState->assignProperty(m_label, "offset", QPointF(0, 26));
  81. } else {
  82. m_normalState->assignProperty(m_label, "offset", QPointF(0, 0-m));
  83. }
  84. m_focusedState->assignProperty(m_label, "offset", QPointF(0, 0-m));
  85. m_focusedState->assignProperty(m_label, "color", m_textField->inkColor());
  86. m_normalState->assignProperty(m_label, "color", m_textField->labelColor());
  87. if (0 != m_label->offset().y() && !m_textField->text().isEmpty()) {
  88. m_label->setOffset(QPointF(0, 0-m));
  89. } else if (!m_textField->hasFocus() && m_label->offset().y() <= 0 && m_textField->text().isEmpty()) {
  90. m_label->setOffset(QPointF(0, 26));
  91. }
  92. }
  93. m_textField->update();
  94. }
  95. /*!
  96. * \class QtMaterialTextFieldLabel
  97. * \internal
  98. */
  99. QtMaterialTextFieldLabel::QtMaterialTextFieldLabel(QtMaterialTextField *parent)
  100. : QWidget(parent),
  101. m_textField(parent),
  102. m_scale(1),
  103. m_posX(0),
  104. m_posY(26),
  105. m_color(parent->labelColor())
  106. {
  107. Q_ASSERT(parent);
  108. QFont font("Roboto", parent->labelFontSize(), QFont::Medium);
  109. font.setLetterSpacing(QFont::PercentageSpacing, 102);
  110. setFont(font);
  111. }
  112. QtMaterialTextFieldLabel::~QtMaterialTextFieldLabel()
  113. {
  114. }
  115. /*!
  116. * \reimp
  117. */
  118. void QtMaterialTextFieldLabel::paintEvent(QPaintEvent *event)
  119. {
  120. Q_UNUSED(event)
  121. if (!m_textField->hasLabel()) {
  122. return;
  123. }
  124. QPainter painter(this);
  125. painter.setRenderHint(QPainter::Antialiasing);
  126. painter.scale(m_scale, m_scale);
  127. painter.setPen(m_color);
  128. painter.setOpacity(1);
  129. QPointF pos(2+m_posX, height()-36+m_posY);
  130. painter.drawText(pos.x(), pos.y(), m_textField->label());
  131. }