/src/MEGASync/gui/PlanWidget.cpp

https://github.com/meganz/MEGAsync · C++ · 150 lines · 131 code · 17 blank · 2 comment · 7 complexity · a8e0d6210ea3fe521a885dded20b3f9d MD5 · raw file

  1. #include "PlanWidget.h"
  2. #include "ui_PlanWidget.h"
  3. #include "Utilities.h"
  4. #include <QDesktopServices>
  5. #include <QUrl>
  6. #include <QtCore>
  7. #include "Preferences.h"
  8. #include "megaapi.h"
  9. #if QT_VERSION >= 0x050000
  10. #include <QtConcurrent/QtConcurrent>
  11. #endif
  12. #define TOBYTES 1024 * 1024 * 1024
  13. PlanWidget::PlanWidget(PlanInfo data, QString userAgent, QWidget *parent) :
  14. QWidget(parent),
  15. ui(new Ui::PlanWidget)
  16. {
  17. details = data;
  18. this->userAgent = userAgent;
  19. ui->setupUi(this);
  20. //Create the overlay widget with transparent background
  21. //that will be shown over the Plans to manage clicked() events
  22. overlay = new QPushButton(this);
  23. overlay->setObjectName(QString::fromUtf8("bOverlay"));
  24. overlay->setStyleSheet(QString::fromAscii(
  25. "QPushButton#bOverlay:hover {border-image: url(://images/account_type_over.png);} "
  26. "QPushButton#bOverlay {border-radius: 3px; border: 1px solid; border-color: rgba(0, 0, 0, 0.1); border: none;} "
  27. "QPushButton#bOverlay:pressed {border-image: url(://images/account_type_press.png);}"));
  28. overlay->setCursor(Qt::PointingHandCursor);
  29. overlay->resize(this->size());
  30. connect(overlay, SIGNAL(clicked()), this, SLOT(onOverlayClicked()));
  31. updatePlanInfo();
  32. }
  33. void PlanWidget::onOverlayClicked()
  34. {
  35. QString url;
  36. switch (details.level)
  37. {
  38. case PRO_LITE:
  39. url = QString::fromUtf8("mega://#propay_4");
  40. break;
  41. case PRO_I:
  42. url = QString::fromUtf8("mega://#propay_1");
  43. break;
  44. case PRO_II:
  45. url = QString::fromUtf8("mega://#propay_2");
  46. break;
  47. case PRO_III:
  48. url = QString::fromUtf8("mega://#propay_3");
  49. break;
  50. case BUSINESS:
  51. url = QString::fromUtf8("mega://#registerb");
  52. break;
  53. default:
  54. url = QString::fromUtf8("mega://#pro");
  55. break;
  56. }
  57. Utilities::getPROurlWithParameters(url);
  58. QtConcurrent::run(QDesktopServices::openUrl, QUrl(url));
  59. }
  60. PlanWidget::~PlanWidget()
  61. {
  62. delete ui;
  63. }
  64. void PlanWidget::updatePlanInfo()
  65. {
  66. QString colorPrice;
  67. ui->lPeriod->setText(QString::fromUtf8("/%1").arg(tr("month")));
  68. switch (details.level)
  69. {
  70. case PRO_LITE:
  71. ui->lProPlan->setText(QString::fromUtf8("PRO LITE"));
  72. colorPrice = QString::fromUtf8("color: #ffa500;");
  73. break;
  74. case PRO_I:
  75. ui->lProPlan->setText(QString::fromUtf8("PRO I"));
  76. colorPrice = QString::fromUtf8("color: #ff333a;");
  77. break;
  78. case PRO_II:
  79. ui->lProPlan->setText(QString::fromUtf8("PRO II"));
  80. colorPrice = QString::fromUtf8("color: #ff333a;");
  81. break;
  82. case PRO_III:
  83. ui->lProPlan->setText(QString::fromUtf8("PRO III"));
  84. colorPrice = QString::fromUtf8("color: #ff333a;");
  85. break;
  86. case BUSINESS:
  87. ui->lProPlan->setText(QString::fromUtf8("BUSINESS"));
  88. colorPrice = QString::fromUtf8("color: #2BA6DE;");
  89. ui->lPeriod->setText(tr("per user %1").arg(ui->lPeriod->text()));
  90. break;
  91. default:
  92. ui->lProPlan->setText(QString::fromUtf8("PRO"));
  93. colorPrice = QString::fromUtf8("color: #2BA6DE;");
  94. break;
  95. }
  96. ui->lPrice->setText(QString::fromUtf8("<span style='font-family:\"Lato\"; font-size:48px; %1'>%2</span><span style='font-family:\"Lato\"; font-size: 26px; %1'>%3</span>")
  97. .arg(colorPrice)
  98. .arg(details.amount / 100)
  99. .arg(details.amount % 100 ? QString::fromUtf8(".%3 %4").arg(details.amount % 100).arg(details.currency) : details.currency));
  100. if (details.gbStorage == -1) //UNLIMITED
  101. {
  102. ui->lStorageInfo->setText(QString::fromUtf8("<span style=\'color:#333333; font-family: Lato; font-size: 15px; font-weight: 600; text-decoration:none;\'>")
  103. + tr("UNLIMITED") + QString::fromUtf8("</span>"));
  104. ui->lBandWidthInfo->setText(QString::fromUtf8("<span style='color:#666666; font-family: Lato; font-size: 13px; text-decoration:none;'>")
  105. + tr("Storage and transfers") + QString::fromUtf8("</span>"));
  106. }
  107. else
  108. {
  109. ui->lStorageInfo->setText(formatRichString(Utilities::getSizeString(details.gbStorage * TOBYTES), STORAGE));
  110. ui->lBandWidthInfo->setText(formatRichString(Utilities::getSizeString(details.gbTransfer * TOBYTES), BANDWIDTH));
  111. }
  112. }
  113. void PlanWidget::setPlanInfo(PlanInfo data)
  114. {
  115. details = data;
  116. updatePlanInfo();
  117. }
  118. QString PlanWidget::formatRichString(QString str, int type)
  119. {
  120. return QString::fromUtf8("<span style='color:#333333; font-family: Lato; font-size: 15px; font-weight: 600; text-decoration:none;'>%1 </span>"
  121. "<span style='color:#666666; font-family: Lato; font-size: 13px; text-decoration:none;'>%2</span>")
  122. .arg(str)
  123. .arg(type == STORAGE ? tr("Storage") : tr("Transfer"));
  124. }
  125. void PlanWidget::changeEvent(QEvent *event)
  126. {
  127. if (event->type() == QEvent::LanguageChange)
  128. {
  129. ui->retranslateUi(this);
  130. updatePlanInfo();
  131. }
  132. QWidget::changeEvent(event);
  133. }