main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "Almond.h"
  2. #include <QtWidgets/QApplication>
  3. #include <QPixmap>
  4. #include <QDesktopWidget>
  5. #include <QSplashScreen>
  6. #include <QMovie>
  7. #include <QTimer>
  8. #include <QFuture>
  9. #include <cmath>
  10. #include <QtConcurrent>
  11. class AlmondSplashScreen : public QSplashScreen
  12. {
  13. private:
  14. float animOff = 0.0f;
  15. QTimer animUpdate;
  16. volatile bool updated = true;
  17. public:
  18. AlmondSplashScreen(QPixmap splash) :
  19. QSplashScreen{ splash },
  20. animUpdate{ this }
  21. {
  22. animUpdate.start(10);
  23. //loading.start();
  24. //this->add(loading);
  25. //connect(&loading, &QMovie::updated, this, &AlmondSplashScreen::nextFrame);
  26. connect(&animUpdate, &QTimer::timeout, this, &AlmondSplashScreen::nextFrame);
  27. }
  28. ~AlmondSplashScreen(void)
  29. {
  30. animUpdate.stop();
  31. }
  32. void drawContents(QPainter* painter) override
  33. {
  34. QSplashScreen::drawContents(painter);
  35. drawAnimation(painter);
  36. updated = true;
  37. }
  38. void drawAnimation(QPainter* painter)
  39. {
  40. const auto minimum = [] (auto a, auto b, auto c) {
  41. return a < b ? (a < c ? a : c) : (b < c ? b : c);
  42. };
  43. int width = this->width();
  44. int height = this->height();
  45. int pieces = 7;
  46. float off = ::fmod(animOff, width / pieces);
  47. for (int i = 0; i < pieces; i++) {
  48. float x = off + i * width / pieces;
  49. float accelOff = 0;
  50. if (x < 160)
  51. accelOff = (160 - x) * (160 - x) / 160 ;
  52. else if (x > width - 160)
  53. accelOff = -(width - 160 - x) * (width - 160 - x) / 160 ;
  54. x -= accelOff;
  55. if (x < 0 || x > width)
  56. continue;
  57. float opacity = minimum(x, width - x, 130);
  58. QPen pen(QColor(255, 255, 255, int(opacity)));
  59. pen.setWidth(4);
  60. painter->setPen(pen);
  61. painter->drawEllipse(QRectF{ x, double(height - 40), 16, 16 });
  62. }
  63. }
  64. public slots:
  65. void nextFrame() //(const QRect& rect)
  66. {
  67. emit this->repaint();
  68. animOff += 3;
  69. }
  70. };
  71. int main(int argc, char *argv[])
  72. {
  73. QApplication a(argc, argv);
  74. QRect screenDim = a.desktop()->screenGeometry();
  75. int splashW = screenDim.width() * 2 / 11;
  76. QPixmap splashImg(":/splash/splash");
  77. QPixmap splashScaled = splashImg.scaled(splashW, splashW * splashImg.height() / splashImg.width());
  78. AlmondSplashScreen splash{ splashScaled };
  79. a.processEvents();
  80. splash.show();
  81. a.processEvents();
  82. /*for (int i = 0; i < 100; i++) {
  83. a.processEvents();
  84. system("sleep 0.03");
  85. }*/
  86. Almond w;
  87. a.processEvents();
  88. splash.finish(&w);
  89. a.processEvents();
  90. w.show();
  91. return a.exec();
  92. }