main.cpp 2.7 KB

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