qtmaterialprogress.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "qtmaterialprogress.h"
  2. #include "qtmaterialprogress_p.h"
  3. #include <QPropertyAnimation>
  4. #include <QPainter>
  5. #include <QPainterPath>
  6. #include "qtmaterialprogress_internal.h"
  7. #include "lib/qtmaterialstyle.h"
  8. /*!
  9. * \class QtMaterialProgressPrivate
  10. * \internal
  11. */
  12. QtMaterialProgressPrivate::QtMaterialProgressPrivate(QtMaterialProgress *q)
  13. : q_ptr(q)
  14. {
  15. }
  16. QtMaterialProgressPrivate::~QtMaterialProgressPrivate()
  17. {
  18. }
  19. void QtMaterialProgressPrivate::init()
  20. {
  21. Q_Q(QtMaterialProgress);
  22. delegate = new QtMaterialProgressDelegate(q);
  23. progressType = Material::IndeterminateProgress;
  24. useThemeColors = true;
  25. QPropertyAnimation *animation;
  26. animation = new QPropertyAnimation(q);
  27. animation->setPropertyName("offset");
  28. animation->setTargetObject(delegate);
  29. animation->setStartValue(0);
  30. animation->setEndValue(1);
  31. animation->setDuration(1000);
  32. animation->setLoopCount(-1);
  33. animation->start();
  34. }
  35. /*!
  36. * \class QtMaterialProgress
  37. */
  38. QtMaterialProgress::QtMaterialProgress(QWidget *parent)
  39. : QProgressBar(parent),
  40. d_ptr(new QtMaterialProgressPrivate(this))
  41. {
  42. d_func()->init();
  43. }
  44. QtMaterialProgress::~QtMaterialProgress()
  45. {
  46. }
  47. void QtMaterialProgress::setProgressType(Material::ProgressType type)
  48. {
  49. Q_D(QtMaterialProgress);
  50. d->progressType = type;
  51. update();
  52. }
  53. Material::ProgressType QtMaterialProgress::progressType() const
  54. {
  55. Q_D(const QtMaterialProgress);
  56. return d->progressType;
  57. }
  58. void QtMaterialProgress::setUseThemeColors(bool state)
  59. {
  60. Q_D(QtMaterialProgress);
  61. if (d->useThemeColors == state) {
  62. return;
  63. }
  64. d->useThemeColors = state;
  65. update();
  66. }
  67. bool QtMaterialProgress::useThemeColors() const
  68. {
  69. Q_D(const QtMaterialProgress);
  70. return d->useThemeColors;
  71. }
  72. void QtMaterialProgress::setProgressColor(const QColor &color)
  73. {
  74. Q_D(QtMaterialProgress);
  75. d->progressColor = color;
  76. MATERIAL_DISABLE_THEME_COLORS
  77. update();
  78. }
  79. QColor QtMaterialProgress::progressColor() const
  80. {
  81. Q_D(const QtMaterialProgress);
  82. if (d->useThemeColors || !d->progressColor.isValid()) {
  83. return QtMaterialStyle::instance().themeColor("primary1");
  84. } else {
  85. return d->progressColor;
  86. }
  87. }
  88. void QtMaterialProgress::setBackgroundColor(const QColor &color)
  89. {
  90. Q_D(QtMaterialProgress);
  91. d->backgroundColor = color;
  92. MATERIAL_DISABLE_THEME_COLORS
  93. update();
  94. }
  95. QColor QtMaterialProgress::backgroundColor() const
  96. {
  97. Q_D(const QtMaterialProgress);
  98. if (d->useThemeColors || !d->backgroundColor.isValid()) {
  99. return QtMaterialStyle::instance().themeColor("border");
  100. } else {
  101. return d->backgroundColor;
  102. }
  103. }
  104. /*!
  105. * \reimp
  106. */
  107. void QtMaterialProgress::paintEvent(QPaintEvent *event)
  108. {
  109. Q_UNUSED(event)
  110. Q_D(QtMaterialProgress);
  111. QPainter painter(this);
  112. painter.setRenderHint(QPainter::Antialiasing);
  113. QBrush brush;
  114. brush.setStyle(Qt::SolidPattern);
  115. brush.setColor(isEnabled() ? backgroundColor()
  116. : QtMaterialStyle::instance().themeColor("disabled"));
  117. painter.setBrush(brush);
  118. painter.setPen(Qt::NoPen);
  119. QPainterPath path;
  120. path.addRoundedRect(0, height()/2-3, width(), 6, 3, 3);
  121. painter.setClipPath(path);
  122. painter.drawRect(0, 0, width(), height());
  123. if (isEnabled())
  124. {
  125. brush.setColor(progressColor());
  126. painter.setBrush(brush);
  127. if (Material::IndeterminateProgress == d->progressType) {
  128. painter.drawRect(d->delegate->offset()*width()*2-width(), 0, width(), height());
  129. } else {
  130. qreal p = static_cast<qreal>(width())*(value()-minimum())/(maximum()-minimum());
  131. painter.drawRect(0, 0, p, height());
  132. }
  133. }
  134. }