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