qtmaterialslider_internal.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "qtmaterialslider_internal.h"
  2. #include <QState>
  3. #include <QAbstractTransition>
  4. #include <QSignalTransition>
  5. #include <QEventTransition>
  6. #include <QPropertyAnimation>
  7. #include <QPainter>
  8. #include "qtmaterialslider.h"
  9. #include "lib/qtmaterialstyle.h"
  10. #include "lib/qtmaterialstatetransition.h"
  11. /*!
  12. * \class QtMaterialSliderStateMachine
  13. * \internal
  14. */
  15. QtMaterialSliderStateMachine::QtMaterialSliderStateMachine(
  16. QtMaterialSlider *slider,
  17. QtMaterialSliderThumb *thumb,
  18. QtMaterialSliderTrack *track)
  19. : QStateMachine(slider),
  20. m_slider(slider),
  21. m_thumb(thumb),
  22. m_track(track),
  23. m_topState(new QState(QState::ParallelStates)),
  24. m_fstState(new QState(m_topState)),
  25. m_sndState(new QState(m_topState)),
  26. m_inactiveState(new QState(m_fstState)),
  27. m_focusState(new QState(m_fstState)),
  28. m_slidingState(new QState(m_fstState)),
  29. m_pulseOutState(new QState(m_focusState)),
  30. m_pulseInState(new QState(m_focusState)),
  31. m_minState(new QState(m_sndState)),
  32. m_normalState(new QState(m_sndState))
  33. {
  34. addState(m_topState);
  35. setInitialState(m_topState);
  36. m_fstState->setInitialState(m_inactiveState);
  37. m_focusState->setInitialState(m_pulseOutState);
  38. m_inactiveState->assignProperty(thumb, "haloSize", 0);
  39. m_slidingState->assignProperty(thumb, "haloSize", 0);
  40. m_pulseOutState->assignProperty(thumb, "haloSize", 35);
  41. m_pulseInState->assignProperty(thumb, "haloSize", 28);
  42. m_inactiveState->assignProperty(thumb, "diameter", 11);
  43. m_focusState->assignProperty(thumb, "diameter", 11);
  44. m_slidingState->assignProperty(thumb, "diameter", 17);
  45. QAbstractTransition *transition;
  46. QtMaterialStateTransition *customTransition;
  47. QPropertyAnimation *animation;
  48. // Show halo on mouse enter
  49. customTransition = new QtMaterialStateTransition(SliderNoFocusMouseEnter);
  50. customTransition->setTargetState(m_focusState);
  51. animation = new QPropertyAnimation(thumb, "haloSize", this);
  52. animation->setEasingCurve(QEasingCurve::InOutSine);
  53. customTransition->addAnimation(animation);
  54. customTransition->addAnimation(new QPropertyAnimation(track, "fillColor", this));
  55. m_inactiveState->addTransition(customTransition);
  56. // Show halo on focus in
  57. transition = new QEventTransition(slider, QEvent::FocusIn);
  58. transition->setTargetState(m_focusState);
  59. animation = new QPropertyAnimation(thumb, "haloSize", this);
  60. animation->setEasingCurve(QEasingCurve::InOutSine);
  61. transition->addAnimation(animation);
  62. transition->addAnimation(new QPropertyAnimation(track, "fillColor", this));
  63. m_inactiveState->addTransition(transition);
  64. // Hide halo on focus out
  65. transition = new QEventTransition(slider, QEvent::FocusOut);
  66. transition->setTargetState(m_inactiveState);
  67. animation = new QPropertyAnimation(thumb, "haloSize", this);
  68. animation->setEasingCurve(QEasingCurve::InOutSine);
  69. transition->addAnimation(animation);
  70. transition->addAnimation(new QPropertyAnimation(track, "fillColor", this));
  71. m_focusState->addTransition(transition);
  72. // Hide halo on mouse leave, except if widget has focus
  73. customTransition = new QtMaterialStateTransition(SliderNoFocusMouseLeave);
  74. customTransition->setTargetState(m_inactiveState);
  75. animation = new QPropertyAnimation(thumb, "haloSize", this);
  76. animation->setEasingCurve(QEasingCurve::InOutSine);
  77. customTransition->addAnimation(animation);
  78. customTransition->addAnimation(new QPropertyAnimation(track, "fillColor", this));
  79. m_focusState->addTransition(customTransition);
  80. // Pulse in
  81. transition = new QSignalTransition(m_pulseOutState, SIGNAL(propertiesAssigned()));
  82. transition->setTargetState(m_pulseInState);
  83. animation = new QPropertyAnimation(thumb, "haloSize", this);
  84. animation->setEasingCurve(QEasingCurve::InOutSine);
  85. animation->setDuration(1000);
  86. transition->addAnimation(animation);
  87. m_pulseOutState->addTransition(transition);
  88. // Pulse out
  89. transition = new QSignalTransition(m_pulseInState, SIGNAL(propertiesAssigned()));
  90. transition->setTargetState(m_pulseOutState);
  91. animation = new QPropertyAnimation(thumb, "haloSize", this);
  92. animation->setEasingCurve(QEasingCurve::InOutSine);
  93. animation->setDuration(1000);
  94. transition->addAnimation(animation);
  95. m_pulseInState->addTransition(transition);
  96. // Slider pressed
  97. transition = new QSignalTransition(slider, SIGNAL(sliderPressed()));
  98. transition->setTargetState(m_slidingState);
  99. animation = new QPropertyAnimation(thumb, "diameter", this);
  100. animation->setDuration(70);
  101. transition->addAnimation(animation);
  102. animation = new QPropertyAnimation(thumb, "haloSize", this);
  103. animation->setEasingCurve(QEasingCurve::InOutSine);
  104. transition->addAnimation(animation);
  105. m_focusState->addTransition(transition);
  106. // Slider released
  107. transition = new QSignalTransition(slider, SIGNAL(sliderReleased()));
  108. transition->setTargetState(m_focusState);
  109. animation = new QPropertyAnimation(thumb, "diameter", this);
  110. animation->setDuration(70);
  111. transition->addAnimation(animation);
  112. animation = new QPropertyAnimation(thumb, "haloSize", this);
  113. animation->setEasingCurve(QEasingCurve::InOutSine);
  114. transition->addAnimation(animation);
  115. m_slidingState->addTransition(transition);
  116. // Min. value transitions
  117. m_minState->assignProperty(thumb, "borderWidth", 2);
  118. m_normalState->assignProperty(thumb, "borderWidth", 0);
  119. m_sndState->setInitialState(m_minState);
  120. customTransition = new QtMaterialStateTransition(SliderChangedFromMinimum);
  121. customTransition->setTargetState(m_normalState);
  122. animation = new QPropertyAnimation(thumb, "fillColor", this);
  123. animation->setDuration(200);
  124. customTransition->addAnimation(animation);
  125. animation = new QPropertyAnimation(thumb, "haloColor", this);
  126. animation->setDuration(300);
  127. customTransition->addAnimation(animation);
  128. animation = new QPropertyAnimation(thumb, "borderColor", this);
  129. animation->setDuration(200);
  130. customTransition->addAnimation(animation);
  131. animation = new QPropertyAnimation(thumb, "borderWidth", this);
  132. animation->setDuration(200);
  133. customTransition->addAnimation(animation);
  134. m_minState->addTransition(customTransition);
  135. customTransition = new QtMaterialStateTransition(SliderChangedToMinimum);
  136. customTransition->setTargetState(m_minState);
  137. animation = new QPropertyAnimation(thumb, "fillColor", this);
  138. animation->setDuration(200);
  139. customTransition->addAnimation(animation);
  140. animation = new QPropertyAnimation(thumb, "haloColor", this);
  141. animation->setDuration(300);
  142. customTransition->addAnimation(animation);
  143. animation = new QPropertyAnimation(thumb, "borderColor", this);
  144. animation->setDuration(200);
  145. customTransition->addAnimation(animation);
  146. animation = new QPropertyAnimation(thumb, "borderWidth", this);
  147. animation->setDuration(200);
  148. customTransition->addAnimation(animation);
  149. m_normalState->addTransition(customTransition);
  150. setupProperties();
  151. }
  152. QtMaterialSliderStateMachine::~QtMaterialSliderStateMachine()
  153. {
  154. }
  155. void QtMaterialSliderStateMachine::setupProperties()
  156. {
  157. QColor trackColor = m_slider->trackColor();
  158. QColor thumbColor = m_slider->thumbColor();
  159. m_inactiveState->assignProperty(m_track, "fillColor", trackColor.lighter(130));
  160. m_slidingState->assignProperty(m_track, "fillColor", trackColor);
  161. m_focusState->assignProperty(m_track, "fillColor", trackColor);
  162. QColor holeColor = m_slider->palette().color(QPalette::Base);
  163. if (m_slider->parentWidget()) {
  164. holeColor = m_slider->parentWidget()->palette().color(m_slider->backgroundRole());
  165. }
  166. m_minState->assignProperty(m_thumb, "fillColor", holeColor);
  167. m_minState->assignProperty(m_thumb, "haloColor", trackColor);
  168. m_minState->assignProperty(m_thumb, "borderColor", trackColor);
  169. m_normalState->assignProperty(m_thumb, "fillColor", thumbColor);
  170. m_normalState->assignProperty(m_thumb, "haloColor", thumbColor);
  171. m_normalState->assignProperty(m_thumb, "borderColor", thumbColor);
  172. m_slider->update();
  173. }
  174. /*!
  175. * \class QtMaterialSliderThumb
  176. * \internal
  177. */
  178. QtMaterialSliderThumb::QtMaterialSliderThumb(QtMaterialSlider *slider)
  179. : QtMaterialOverlayWidget(slider->parentWidget()),
  180. m_slider(slider),
  181. m_diameter(11),
  182. m_borderWidth(2),
  183. m_haloSize(0),
  184. m_offset(0)
  185. {
  186. slider->installEventFilter(this);
  187. setAttribute(Qt::WA_TransparentForMouseEvents, true);
  188. }
  189. QtMaterialSliderThumb::~QtMaterialSliderThumb()
  190. {
  191. }
  192. bool QtMaterialSliderThumb::eventFilter(QObject *obj, QEvent *event)
  193. {
  194. if (QEvent::ParentChange == event->type()) {
  195. setParent(m_slider->parentWidget());
  196. }
  197. return QtMaterialOverlayWidget::eventFilter(obj, event);
  198. }
  199. void QtMaterialSliderThumb::paintEvent(QPaintEvent *event)
  200. {
  201. Q_UNUSED(event)
  202. QPainter painter(this);
  203. painter.setRenderHint(QPainter::Antialiasing);
  204. // Halo
  205. QBrush brush;
  206. brush.setStyle(Qt::SolidPattern);
  207. brush.setColor(m_haloColor);
  208. painter.setBrush(brush);
  209. painter.setPen(Qt::NoPen);
  210. QPointF disp = Qt::Horizontal == m_slider->orientation()
  211. ? QPointF(QT_MATERIAL_SLIDER_MARGIN + m_offset, m_slider->height()/2)
  212. : QPointF(m_slider->width()/2, QT_MATERIAL_SLIDER_MARGIN + m_offset);
  213. QRectF halo((m_slider->pos() - QPointF(m_haloSize, m_haloSize)/2) + disp,
  214. QSizeF(m_haloSize, m_haloSize));
  215. painter.setOpacity(0.15);
  216. painter.drawEllipse(halo);
  217. // Knob
  218. const bool isMin = m_slider->value() == m_slider->minimum();
  219. brush.setColor(m_slider->isEnabled()
  220. ? m_fillColor
  221. : m_slider->disabledColor());
  222. painter.setBrush(!m_slider->isEnabled() && isMin
  223. ? Qt::NoBrush
  224. : brush);
  225. if (m_slider->isEnabled() || isMin) {
  226. QPen pen;
  227. pen.setColor(m_borderColor);
  228. pen.setWidthF((isMin && !m_slider->isEnabled()) ? 1.7 : m_borderWidth);
  229. painter.setPen(pen);
  230. } else {
  231. painter.setPen(Qt::NoPen);
  232. }
  233. QRectF geometry = Qt::Horizontal == m_slider->orientation()
  234. ? QRectF(m_offset, m_slider->height()/2 - QT_MATERIAL_SLIDER_MARGIN,
  235. QT_MATERIAL_SLIDER_MARGIN*2, QT_MATERIAL_SLIDER_MARGIN*2).translated(m_slider->pos())
  236. : QRectF(m_slider->width()/2 - QT_MATERIAL_SLIDER_MARGIN, m_offset,
  237. QT_MATERIAL_SLIDER_MARGIN*2, QT_MATERIAL_SLIDER_MARGIN*2).translated(m_slider->pos());
  238. qreal s = m_slider->isEnabled() ? m_diameter : 7;
  239. QRectF thumb(0, 0, s, s);
  240. thumb.moveCenter(geometry.center());
  241. painter.setOpacity(1);
  242. painter.drawEllipse(thumb);
  243. }
  244. /*!
  245. * \class QtMaterialSliderTrack
  246. * \internal
  247. */
  248. QtMaterialSliderTrack::QtMaterialSliderTrack(QtMaterialSliderThumb *thumb, QtMaterialSlider *slider)
  249. : QtMaterialOverlayWidget(slider->parentWidget()),
  250. m_slider(slider),
  251. m_thumb(thumb),
  252. m_trackWidth(2)
  253. {
  254. slider->installEventFilter(this);
  255. setAttribute(Qt::WA_TransparentForMouseEvents, true);
  256. connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(update()));
  257. }
  258. QtMaterialSliderTrack::~QtMaterialSliderTrack()
  259. {
  260. }
  261. bool QtMaterialSliderTrack::eventFilter(QObject *obj, QEvent *event)
  262. {
  263. if (QEvent::ParentChange == event->type()) {
  264. setParent(m_slider->parentWidget());
  265. }
  266. return QtMaterialOverlayWidget::eventFilter(obj, event);
  267. }
  268. void QtMaterialSliderTrack::paintEvent(QPaintEvent *event)
  269. {
  270. Q_UNUSED(event)
  271. QPainter painter(this);
  272. painter.setRenderHint(QPainter::Antialiasing);
  273. QBrush fg;
  274. fg.setStyle(Qt::SolidPattern);
  275. fg.setColor(m_slider->isEnabled() ? m_slider->thumbColor()
  276. : m_slider->disabledColor());
  277. QBrush bg;
  278. bg.setStyle(Qt::SolidPattern);
  279. bg.setColor(m_slider->isEnabled() ? m_fillColor
  280. : m_slider->disabledColor());
  281. qreal offset = m_thumb->offset();
  282. if (Qt::Horizontal == m_slider->orientation()) {
  283. painter.translate(m_slider->x() + QT_MATERIAL_SLIDER_MARGIN,
  284. m_slider->y() + m_slider->height()/2
  285. - static_cast<qreal>(m_trackWidth)/2);
  286. } else {
  287. painter.translate(m_slider->x() + m_slider->width()/2
  288. - static_cast<qreal>(m_trackWidth)/2,
  289. m_slider->y() + QT_MATERIAL_SLIDER_MARGIN);
  290. }
  291. QRectF geometry = Qt::Horizontal == m_slider->orientation()
  292. ? QRectF(0, 0, m_slider->width() - QT_MATERIAL_SLIDER_MARGIN*2, m_trackWidth)
  293. : QRectF(0, 0, m_trackWidth, m_slider->height() - QT_MATERIAL_SLIDER_MARGIN*2);
  294. QRectF bgRect;
  295. QRectF fgRect;
  296. if (Qt::Horizontal == m_slider->orientation()) {
  297. fgRect = QRectF(0, 0, offset, m_trackWidth);
  298. bgRect = QRectF(offset, 0, m_slider->width(), m_trackWidth).intersected(geometry);
  299. } else {
  300. fgRect = QRectF(0, 0, m_trackWidth, offset);
  301. bgRect = QRectF(0, offset, m_trackWidth, m_slider->height()).intersected(geometry);
  302. }
  303. if (!m_slider->isEnabled()) {
  304. fgRect = fgRect.width() < 9 ? QRectF() : fgRect.adjusted(0, 0, -6, 0);
  305. bgRect = bgRect.width() < 9 ? QRectF() : bgRect.adjusted(6, 0, 0, 0);
  306. }
  307. if (m_slider->invertedAppearance()) {
  308. qSwap(bgRect, fgRect);
  309. }
  310. painter.fillRect(bgRect, bg);
  311. painter.fillRect(fgRect, fg);
  312. }