qtmaterialtextfield.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "qtmaterialtextfield.h"
  2. #include "qtmaterialtextfield_p.h"
  3. #include <QtWidgets/QApplication>
  4. #include <QPainter>
  5. #include "qtmaterialtextfield_internal.h"
  6. #include "lib/qtmaterialstyle.h"
  7. #include <QDebug>
  8. /*!
  9. * \class QtMaterialTextFieldPrivate
  10. * \internal
  11. */
  12. QtMaterialTextFieldPrivate::QtMaterialTextFieldPrivate(QtMaterialTextField *q)
  13. : q_ptr(q)
  14. {
  15. }
  16. QtMaterialTextFieldPrivate::~QtMaterialTextFieldPrivate()
  17. {
  18. }
  19. void QtMaterialTextFieldPrivate::init()
  20. {
  21. Q_Q(QtMaterialTextField);
  22. stateMachine = new QtMaterialTextFieldStateMachine(q);
  23. label = 0;
  24. labelFontSize = 9.5;
  25. showLabel = false;
  26. showInputLine = true;
  27. useThemeColors = true;
  28. q->setFrame(false);
  29. q->setStyle(&QtMaterialStyle::instance());
  30. q->setAttribute(Qt::WA_Hover);
  31. q->setMouseTracking(true);
  32. q->setTextMargins(0, 2, 0, 4);
  33. q->setFont(QFont("Roboto", 11, QFont::Normal));
  34. stateMachine->start();
  35. QCoreApplication::processEvents();
  36. }
  37. /*!
  38. * \class QtMaterialTextField
  39. */
  40. QtMaterialTextField::QtMaterialTextField(QWidget *parent)
  41. : QLineEdit(parent),
  42. d_ptr(new QtMaterialTextFieldPrivate(this))
  43. {
  44. d_func()->init();
  45. }
  46. QtMaterialTextField::~QtMaterialTextField()
  47. {
  48. }
  49. void QtMaterialTextField::setUseThemeColors(bool value)
  50. {
  51. Q_D(QtMaterialTextField);
  52. if (d->useThemeColors == value) {
  53. return;
  54. }
  55. d->useThemeColors = value;
  56. d->stateMachine->setupProperties();
  57. }
  58. bool QtMaterialTextField::useThemeColors() const
  59. {
  60. Q_D(const QtMaterialTextField);
  61. return d->useThemeColors;
  62. }
  63. void QtMaterialTextField::setShowLabel(bool value)
  64. {
  65. Q_D(QtMaterialTextField);
  66. if (d->showLabel == value) {
  67. return;
  68. }
  69. d->showLabel = value;
  70. if (!d->label && value) {
  71. d->label = new QtMaterialTextFieldLabel(this);
  72. d->stateMachine->setLabel(d->label);
  73. }
  74. if (value) {
  75. setContentsMargins(0, 23, 0, 0);
  76. } else {
  77. setContentsMargins(0, 0, 0, 0);
  78. }
  79. }
  80. bool QtMaterialTextField::hasLabel() const
  81. {
  82. Q_D(const QtMaterialTextField);
  83. return d->showLabel;
  84. }
  85. void QtMaterialTextField::setLabelFontSize(qreal size)
  86. {
  87. Q_D(QtMaterialTextField);
  88. d->labelFontSize = size;
  89. if (d->label)
  90. {
  91. QFont font(d->label->font());
  92. font.setPointSizeF(size);
  93. d->label->setFont(font);
  94. d->label->update();
  95. }
  96. }
  97. qreal QtMaterialTextField::labelFontSize() const
  98. {
  99. Q_D(const QtMaterialTextField);
  100. return d->labelFontSize;
  101. }
  102. void QtMaterialTextField::setLabel(const QString &label)
  103. {
  104. Q_D(QtMaterialTextField);
  105. d->labelString = label;
  106. setShowLabel(true);
  107. d->label->update();
  108. }
  109. QString QtMaterialTextField::label() const
  110. {
  111. Q_D(const QtMaterialTextField);
  112. return d->labelString;
  113. }
  114. void QtMaterialTextField::setTextColor(const QColor &color)
  115. {
  116. Q_D(QtMaterialTextField);
  117. d->textColor = color;
  118. setStyleSheet(QString("QLineEdit { color: %1; }").arg(color.name()));
  119. MATERIAL_DISABLE_THEME_COLORS
  120. d->stateMachine->setupProperties();
  121. }
  122. QColor QtMaterialTextField::textColor() const
  123. {
  124. Q_D(const QtMaterialTextField);
  125. if (d->useThemeColors || !d->textColor.isValid()) {
  126. return QtMaterialStyle::instance().themeColor("text");
  127. } else {
  128. return d->textColor;
  129. }
  130. }
  131. void QtMaterialTextField::setLabelColor(const QColor &color)
  132. {
  133. Q_D(QtMaterialTextField);
  134. d->labelColor = color;
  135. MATERIAL_DISABLE_THEME_COLORS
  136. d->stateMachine->setupProperties();
  137. }
  138. QColor QtMaterialTextField::labelColor() const
  139. {
  140. Q_D(const QtMaterialTextField);
  141. if (d->useThemeColors || !d->labelColor.isValid()) {
  142. return QtMaterialStyle::instance().themeColor("accent3");
  143. } else {
  144. return d->labelColor;
  145. }
  146. }
  147. void QtMaterialTextField::setInkColor(const QColor &color)
  148. {
  149. Q_D(QtMaterialTextField);
  150. d->inkColor = color;
  151. MATERIAL_DISABLE_THEME_COLORS
  152. d->stateMachine->setupProperties();
  153. }
  154. QColor QtMaterialTextField::inkColor() const
  155. {
  156. Q_D(const QtMaterialTextField);
  157. if (d->useThemeColors || !d->inkColor.isValid()) {
  158. return QtMaterialStyle::instance().themeColor("primary1");
  159. } else {
  160. return d->inkColor;
  161. }
  162. }
  163. void QtMaterialTextField::setInputLineColor(const QColor &color)
  164. {
  165. Q_D(QtMaterialTextField);
  166. d->inputLineColor = color;
  167. MATERIAL_DISABLE_THEME_COLORS
  168. d->stateMachine->setupProperties();
  169. }
  170. QColor QtMaterialTextField::inputLineColor() const
  171. {
  172. Q_D(const QtMaterialTextField);
  173. if (d->useThemeColors || !d->inputLineColor.isValid()) {
  174. return QtMaterialStyle::instance().themeColor("border");
  175. } else {
  176. return d->inputLineColor;
  177. }
  178. }
  179. void QtMaterialTextField::setShowInputLine(bool value)
  180. {
  181. Q_D(QtMaterialTextField);
  182. if (d->showInputLine == value) {
  183. return;
  184. }
  185. d->showInputLine = value;
  186. update();
  187. }
  188. bool QtMaterialTextField::hasInputLine() const
  189. {
  190. Q_D(const QtMaterialTextField);
  191. return d->showInputLine;
  192. }
  193. QtMaterialTextField::QtMaterialTextField(QtMaterialTextFieldPrivate &d, QWidget *parent)
  194. : QLineEdit(parent),
  195. d_ptr(&d)
  196. {
  197. d_func()->init();
  198. }
  199. /*!
  200. * \reimp
  201. */
  202. bool QtMaterialTextField::event(QEvent *event)
  203. {
  204. Q_D(QtMaterialTextField);
  205. switch (event->type())
  206. {
  207. case QEvent::Resize:
  208. case QEvent::Move: {
  209. if (d->label) {
  210. d->label->setGeometry(rect());
  211. }
  212. }
  213. default:
  214. break;
  215. }
  216. return QLineEdit::event(event);
  217. }
  218. /*!
  219. * \reimp
  220. */
  221. void QtMaterialTextField::paintEvent(QPaintEvent *event)
  222. {
  223. Q_D(QtMaterialTextField);
  224. QLineEdit::paintEvent(event);
  225. QPainter painter(this);
  226. const qreal progress = d->stateMachine->progress();
  227. if (text().isEmpty() && progress < 1)
  228. {
  229. painter.setOpacity(1-progress);
  230. painter.fillRect(rect(), parentWidget()->palette().color(backgroundRole()));
  231. }
  232. const int y = height()-1;
  233. const int wd = width()-5;
  234. if (d->showInputLine)
  235. {
  236. QPen pen;
  237. pen.setWidth(1);
  238. pen.setColor(inputLineColor());
  239. if (!isEnabled())
  240. pen.setStyle(Qt::DashLine);
  241. painter.setPen(pen);
  242. painter.setOpacity(1);
  243. painter.drawLine(QLineF(2.5, y, wd, y));
  244. QBrush brush;
  245. brush.setStyle(Qt::SolidPattern);
  246. brush.setColor(inkColor());
  247. if (progress > 0)
  248. {
  249. painter.setPen(Qt::NoPen);
  250. painter.setBrush(brush);
  251. const int w = (1-progress)*static_cast<qreal>(wd/2);
  252. painter.drawRect(w+2.5, height()-2, wd-w*2, 2);
  253. }
  254. }
  255. }