LoadingUI.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "LoadingUI.h"
  2. #include <QApplication>
  3. #include <QScreen>
  4. #include <QMouseEvent>
  5. #include <qmath.h>
  6. #include <QPainter>
  7. #include <QPainter>
  8. BaseLoading::BaseLoading(QWidget *parent)
  9. : QWidget(parent),
  10. m_animationTimer(new QTimer(this)),
  11. m_animationStop(false),
  12. m_duration(1000),
  13. m_extendDuration(150)
  14. {
  15. connect(m_animationTimer,&QTimer::timeout,this,
  16. &BaseLoading::updateFrame);
  17. updateFrameRate();
  18. }
  19. void BaseLoading::setEasingCurve(const QEasingCurve &curve)
  20. {
  21. m_curve = curve;
  22. updateFrameValues();
  23. }
  24. void BaseLoading::setFrameRate(const int &frameRate)
  25. {
  26. m_frameRate = frameRate;
  27. updateFrameValues();
  28. }
  29. void BaseLoading::setDuration(const int &duration)
  30. {
  31. m_duration = duration;
  32. updateFrameValues();
  33. }
  34. void BaseLoading::setExtendDuration(const int &duration)
  35. {
  36. m_extendDuration = duration;
  37. }
  38. void BaseLoading::updateFrameData()
  39. {
  40. m_frameRects.clear();
  41. qreal refreshTime = 1000/m_frameRate*1.0;
  42. m_animationTimer->setInterval(refreshTime); // 刷新时间s
  43. int frameCount = m_duration / refreshTime;
  44. for (int i = 0; i < frameCount; ++i)
  45. m_frameRects.append(QRect());
  46. m_extendFrameCount = m_extendDuration / refreshTime;
  47. updateFrameRects();
  48. }
  49. void BaseLoading::start()
  50. {
  51. m_animationTimer->start();
  52. }
  53. void BaseLoading::stop()
  54. {
  55. m_animationStop = true;
  56. }
  57. void BaseLoading::updateFrameRate()
  58. {
  59. setFrameRate(qApp->primaryScreen()->refreshRate());
  60. }
  61. void BaseLoading::updateFrameValues()
  62. {
  63. m_frameValues.clear();
  64. int fCount = m_duration / refreshTime();
  65. qreal stepValue = 1.0 / fCount;
  66. for (int i = 0; i < fCount; ++i) {
  67. qreal progress = i*stepValue;
  68. m_frameValues.append(m_curve.valueForProgress(progress));
  69. }
  70. }
  71. /////////////////////////////////////
  72. LoadingHorizontal::LoadingHorizontal(QWidget *parent)
  73. : BaseLoading(parent)
  74. {
  75. m_currentFrame = 0;
  76. setItemCount(1);
  77. setItemLength(10);
  78. setBackgroundColor(QColor(Qt::white));
  79. auto curve = QEasingCurve(QEasingCurve::BezierSpline);
  80. curve.addCubicBezierSegment(QPointF(0.31f, 0.85f),QPointF(0.77f, 0.14f),QPointF(1,1));
  81. setEasingCurve(curve);
  82. connect(this,SIGNAL(loaddingStoped()),SLOT(animationStoped()));
  83. }
  84. void LoadingHorizontal::setItemCount(const int &count)
  85. {
  86. m_itemCount = count > 0 ? count : 1;
  87. m_itemColors.clear();
  88. for (int i = 0; i < count; ++i) {
  89. m_itemColors.append(QColor(Qt::black));
  90. }
  91. }
  92. void LoadingHorizontal::setItemColor(const int &index, const QColor &color)
  93. {
  94. if (index < 0) {
  95. for (int i = 0; i < m_itemCount; ++i) {
  96. m_itemColors[i] = color;
  97. }
  98. } else if (index < m_itemCount){
  99. m_itemColors[index] = color;
  100. }
  101. }
  102. void LoadingHorizontal::setItemColors(const QList<QColor> &colors)
  103. {
  104. for (int i = 0; i < colors.count(); ++i) {
  105. setItemColor(i,colors.at(i));
  106. }
  107. }
  108. void LoadingHorizontal::updateFrameRects()
  109. {
  110. int w = m_itemLength;
  111. int y = (height()-w) / 2;
  112. for (int i = 0; i < frameCount(); ++i) {
  113. int x = frameValue(i)*width();
  114. setFrameRect(i,QRect(x,y,w,w));
  115. }
  116. m_totalCount = frameCount()+extendFrameCount()*(m_itemCount-1);
  117. }
  118. void LoadingHorizontal::updateFrame()
  119. {
  120. m_currentFrame += 1;
  121. if (m_currentFrame >= m_totalCount) {
  122. m_currentFrame = 0;
  123. if (stopLoading()) {
  124. stopAnimationTimer();
  125. }
  126. }
  127. update();
  128. }
  129. void LoadingHorizontal::paintEvent(QPaintEvent *)
  130. {
  131. QPainter painter(this);
  132. painter.setRenderHint(QPainter::Antialiasing, true);
  133. if (!parentWidget()) {
  134. painter.fillRect(rect(),QBrush(m_bgColor));
  135. }
  136. if (isActive()) {
  137. for (int i = 0; i < m_itemColors.count(); ++i) {
  138. QPainterPath path;
  139. auto index = m_currentFrame - extendFrameCount()*i;
  140. if (index < 0 || index >= frameCount())
  141. continue;
  142. path.addEllipse(frameRect(index));
  143. painter.fillPath(path,QBrush(m_itemColors.at(i)));
  144. }
  145. }
  146. }
  147. void LoadingHorizontal::resizeEvent(QResizeEvent *)
  148. {
  149. updateFrameData();
  150. }
  151. void LoadingHorizontal::animationStoped()
  152. {
  153. m_currentFrame = 0;
  154. }
  155. /////////////////////////////////////////////////////
  156. LoadingCircle::LoadingCircle(QWidget *parent)
  157. : LoadingHorizontal(parent)
  158. {
  159. setCurrentFrame(0);
  160. }
  161. void LoadingCircle::updateFrameRects()
  162. {
  163. auto rectRadius = itemLength() / 2;
  164. int radius = (width() < height() ? width() : height()) / 2 - rectRadius;
  165. int cx = width() / 2;
  166. int cy = height() / 2;
  167. for (int i = 0; i < frameCount(); ++i) {
  168. qreal zeta = frameValue(i) * 2 * M_PI;
  169. int x = cx - qSin(zeta) * radius;
  170. int y = cy + qCos(zeta) * radius;
  171. setFrameRect(i,QRect(x-rectRadius,y-rectRadius,itemLength(),itemLength()));
  172. }
  173. setTotalCount(frameCount()+extendFrameCount()*(itemCount()-1));
  174. }
  175. void LoadingCircle::paintEvent(QPaintEvent *)
  176. {
  177. QPainter painter(this);
  178. painter.setRenderHint(QPainter::Antialiasing, true);
  179. if (!parentWidget()) {
  180. painter.fillRect(rect(),QBrush(backgroundColor()));
  181. }
  182. if (isActive()) {
  183. auto colors = itemColors();
  184. for (int i = 0; i < colors.count(); ++i) {
  185. QPainterPath path;
  186. auto index = currentFrame() - extendFrameCount()*i;
  187. if (index < 0 || index >= frameCount())
  188. continue;
  189. path.addEllipse(frameRect(index));
  190. painter.fillPath(path,QBrush(colors.at(i)));
  191. }
  192. }
  193. }
  194. LoadingUI::LoadingUI(QWidget* parent)
  195. : LoadingHorizontal(parent)
  196. {
  197. Q_UNUSED(parent);
  198. this->setWindowModality(Qt::ApplicationModal);
  199. this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
  200. this->setAttribute(Qt::WA_TranslucentBackground, true);
  201. this->resize(800, 40);
  202. this->show();
  203. this->setBackgroundColor(QColor(0x00, 0x00, 0x00, 0x10));
  204. this->setItemLength(30);
  205. this->setExtendDuration(240);
  206. this->setItemCount(5);
  207. this->setDuration(2500);
  208. QList<QColor> colors;
  209. colors << QColor("#99CCCC") << QColor("#FFCC99")
  210. << QColor("#FFCCCC") << QColor("#CCCCFF")
  211. << QColor("#CC9999");
  212. this->setItemColors(colors);
  213. // w.setItemColor(-1,QColor(Qt::white));
  214. this->updateFrameData();
  215. this->hide();
  216. }
  217. /// <summary>
  218. ///
  219. /// </summary>
  220. void LoadingUI::Show()
  221. {
  222. this->show();
  223. this->start();
  224. }
  225. /// <summary>
  226. ///
  227. /// </summary>
  228. void LoadingUI::Hide()
  229. {
  230. this->hide();
  231. this->stop();
  232. }