/src/appwidget.cpp

https://github.com/flaviotordini/minitube · C++ · 120 lines · 103 code · 17 blank · 0 comment · 7 complexity · c63abd5d685823591816acd11790b6ab MD5 · raw file

  1. #include "appwidget.h"
  2. #include "constants.h"
  3. #include "http.h"
  4. #ifdef APP_EXTRA
  5. #include "updatedialog.h"
  6. #endif
  7. AppsWidget::AppsWidget(QWidget *parent) : QWidget(parent) {
  8. const int padding = 30;
  9. QBoxLayout *layout = new QHBoxLayout(this);
  10. layout->setMargin(padding);
  11. layout->setSpacing(padding*2);
  12. layout->setAlignment(Qt::AlignCenter);
  13. #ifdef APP_MAC
  14. const QString ext = "dmg";
  15. #elif defined APP_WIN
  16. const QString ext = "exe";
  17. #else
  18. const QString ext = "deb";
  19. #endif
  20. #ifdef APP_MAC
  21. setupApp("Sofa", "sofa." + ext);
  22. #endif
  23. setupApp("Finetune", "finetune." + ext);
  24. setupApp("Musictube", "musictube." + ext);
  25. setupApp("Musique", "musique." + ext);
  26. }
  27. void AppsWidget::setupApp(const QString &name, const QString &code) {
  28. AppWidget *w = new AppWidget(name, code);
  29. layout()->addWidget(w);
  30. }
  31. void AppsWidget::paintEvent(QPaintEvent *e) {
  32. Q_UNUSED(e);
  33. QStyleOption o;
  34. o.initFrom(this);
  35. QPainter p(this);
  36. style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
  37. }
  38. AppWidget::AppWidget(const QString &name, const QString &code, QWidget *parent)
  39. : QWidget(parent), name(name), downloadButton(nullptr) {
  40. const QString unixName = code.left(code.lastIndexOf('.'));
  41. const QString baseUrl = QLatin1String("https://") + Constants::ORG_DOMAIN;
  42. const QString filesUrl = baseUrl + QLatin1String("/files/");
  43. url = filesUrl + unixName + QLatin1String("/") + code;
  44. webPage = baseUrl + QLatin1String("/") + unixName;
  45. QBoxLayout *layout = new QVBoxLayout(this);
  46. layout->setMargin(0);
  47. layout->setAlignment(Qt::AlignHCenter);
  48. icon = new QLabel();
  49. icon->setMinimumHeight(128);
  50. layout->addWidget(icon);
  51. QString pixelRatioString;
  52. if (devicePixelRatioF() > 1.0)
  53. pixelRatioString = '@' + QString::number(devicePixelRatio()) + 'x';
  54. const QString iconUrl = filesUrl + QLatin1String("products/") + unixName + pixelRatioString +
  55. QLatin1String(".png");
  56. QObject *reply = Http::instance().get(iconUrl);
  57. connect(reply, SIGNAL(data(QByteArray)), SLOT(iconDownloaded(QByteArray)));
  58. QLabel *appTitle = new QLabel(name);
  59. appTitle->setAlignment(Qt::AlignHCenter);
  60. layout->addWidget(appTitle);
  61. #ifdef APP_EXTRA
  62. #if !defined(APP_UBUNTU) && !defined(APP_MAC_STORE)
  63. downloadButton = new QPushButton(tr("Download"));
  64. downloadButton->setAttribute(Qt::WA_MacSmallSize);
  65. downloadButton->setCursor(Qt::ArrowCursor);
  66. QSizePolicy sp = downloadButton->sizePolicy();
  67. sp.setHorizontalPolicy(QSizePolicy::Fixed);
  68. sp.setRetainSizeWhenHidden(true);
  69. downloadButton->setSizePolicy(sp);
  70. connect(downloadButton, SIGNAL(clicked(bool)), SLOT(downloadApp()));
  71. layout->addWidget(downloadButton, Qt::AlignHCenter);
  72. layout->setAlignment(downloadButton, Qt::AlignHCenter);
  73. downloadButton->hide();
  74. #endif
  75. #endif
  76. setCursor(Qt::PointingHandCursor);
  77. }
  78. void AppWidget::enterEvent(QEvent *e) {
  79. Q_UNUSED(e);
  80. if (downloadButton) downloadButton->show();
  81. }
  82. void AppWidget::leaveEvent(QEvent *e) {
  83. Q_UNUSED(e);
  84. if (downloadButton) downloadButton->hide();
  85. }
  86. void AppWidget::mouseReleaseEvent(QMouseEvent *e) {
  87. if (e->button() == Qt::LeftButton) {
  88. QDesktopServices::openUrl(webPage);
  89. }
  90. }
  91. void AppWidget::iconDownloaded(const QByteArray &bytes) {
  92. QPixmap pixmap;
  93. pixmap.loadFromData(bytes, "PNG");
  94. icon->setPixmap(pixmap);
  95. }
  96. void AppWidget::downloadApp() {
  97. #ifdef APP_EXTRA
  98. if (!icon) return;
  99. UpdateDialog *dialog = new UpdateDialog(icon->pixmap(), name, QString(), url, this);
  100. dialog->downloadUpdate();
  101. dialog->show();
  102. #endif
  103. }