main.cpp 2.6 KB

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