newswidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "gui\newswidget.h"
  2. #include "ui_newswidget.h"
  3. #include "networkdownloader.h"
  4. #include "legacyapp.h"
  5. #include <QtConcurrent/QtConcurrent>
  6. #include <QLabel>
  7. NewsWidget::NewsWidget(LegacyApp *_app, QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::NewsWidget),
  10. app(_app), news_downloader(this)
  11. {
  12. ui->setupUi(this);
  13. ui->test_news_piece->hide();
  14. news_downloader.targetBytearray = &news_data;
  15. news_downloader.setUrl(QUrl("http://translate.lotros.ru/groupware/launcher_news"));
  16. connect(&news_update_timer, &QTimer::timeout, &news_downloader, &NetworkDownloader::start);
  17. connect(&news_downloader, &NetworkDownloader::downloadFinished, this, &NewsWidget::updateNews);
  18. emit news_downloader.start();
  19. news_update_timer.setInterval(1000 * 60); // 10 minutes;
  20. news_update_timer.start();
  21. }
  22. NewsWidget::~NewsWidget()
  23. {
  24. news_update_timer.stop();
  25. delete ui;
  26. }
  27. void NewsWidget::updateNews()
  28. {
  29. if (!qApp)
  30. return;
  31. if (news_data.size() == 0) {
  32. constructNewsPiece(0, "Не могу загрузить новости", "Загрузка новостей не удалась. Чтобы просмотреть новости, перейдите на <a href='http://translate.lotros.ru/news'>http://translate.lotros.ru/news</a>", "http://translate.lotros.ru/news");
  33. return;
  34. }
  35. QStringList news_list = QString(news_data).split(":::");
  36. news_data.clear();
  37. for (int i = 0; i < news_list.size(); i++) {
  38. QStringList news_piece = news_list[i].split("|");
  39. QString img_src = news_piece[0];
  40. QString news_title = news_piece[1];
  41. QString news_desrc = news_piece[2];
  42. QString news_src = news_piece[3];
  43. constructNewsPiece(i, news_title, news_desrc, news_src);
  44. QtConcurrent::run([i, this, img_src](){
  45. NetworkDownloader img_dwnld;
  46. QByteArray img;
  47. img_dwnld.setUrl(QUrl(img_src));
  48. img_dwnld.targetBytearray = &img;
  49. img_dwnld.start();
  50. img_dwnld.waitForDownloaded();
  51. QPixmap img_pixmap;
  52. img_pixmap.loadFromData(img);
  53. setImgToNewsPiece(i, img_pixmap);
  54. });
  55. }
  56. QSpacerItem* verticalSpacer = new QSpacerItem(20, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
  57. ui->news_layout->addItem(verticalSpacer, news_list.size(), 0);
  58. }
  59. void NewsWidget::constructNewsPiece(int piece_id, QString title, QString text, QString news_src)
  60. {
  61. QWidget* old_piece = findChild<QWidget*>("news_piece_" + QString::number(piece_id));
  62. if (old_piece) {
  63. ui->news_layout->removeWidget(old_piece);
  64. old_piece->deleteLater();
  65. }
  66. QWidget* news_piece = new QWidget(this);
  67. news_piece->setObjectName(QStringLiteral("news_piece_") + QString::number(piece_id));
  68. QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
  69. sizePolicy.setHorizontalStretch(0);
  70. sizePolicy.setVerticalStretch(0);
  71. sizePolicy.setHeightForWidth(news_piece->sizePolicy().hasHeightForWidth());
  72. news_piece->setSizePolicy(sizePolicy);
  73. news_piece->setStyleSheet(QLatin1String("QWidget{\n"
  74. "border-radius: 20px;\n"
  75. "background-color: rgba(30, 0, 0, 100);\n"
  76. "color: white;\n"
  77. "}\n"
  78. "\n"
  79. "QLabel{\n"
  80. "border-radius: 2px;\n"
  81. "background-color:rgba(0,0,0,0);\n"
  82. "}"));
  83. QGridLayout* news_piece_layout = new QGridLayout(news_piece);
  84. news_piece_layout->setObjectName(QStringLiteral("news_piece_layout"));
  85. news_piece_layout->setContentsMargins(11, 11, 11, 11);
  86. news_piece_layout->setSpacing(15);
  87. QLabel* iconLabel = new QLabel(news_piece);
  88. iconLabel->setObjectName(QStringLiteral("news_icon_") + QString::number(piece_id));
  89. QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Fixed);
  90. sizePolicy1.setHorizontalStretch(0);
  91. sizePolicy1.setVerticalStretch(0);
  92. sizePolicy1.setHeightForWidth(iconLabel->sizePolicy().hasHeightForWidth());
  93. iconLabel->setSizePolicy(sizePolicy1);
  94. iconLabel->setMinimumSize(QSize(60, 60));
  95. iconLabel->setMaximumSize(QSize(60, 60));
  96. iconLabel->setPixmap(QPixmap(QString::fromUtf8(":/assets/appicon.ico")).scaled(60, 60));
  97. iconLabel->setStyleSheet("border-radius: 10px;");
  98. news_piece_layout->addWidget(iconLabel, 0, 0, 2, 1);
  99. QLabel* titleLabel = new QLabel(news_piece);
  100. titleLabel->setObjectName(QStringLiteral("titleLabel_title"));
  101. sizePolicy.setHeightForWidth(titleLabel->sizePolicy().hasHeightForWidth());
  102. titleLabel->setSizePolicy(sizePolicy);
  103. QFont font;
  104. font.setFamily(QStringLiteral("Trajan Pro 3"));
  105. font.setPixelSize(12);
  106. font.setBold(false);
  107. font.setUnderline(false);
  108. titleLabel->setFont(font);
  109. titleLabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
  110. titleLabel->setWordWrap(true);
  111. titleLabel->setText("<a style=\"color: #cfa644; text-decoration:none;\" href = '" + news_src + "'>" + title + "</a>");
  112. titleLabel->setOpenExternalLinks(true);
  113. news_piece_layout->addWidget(titleLabel, 0, 1, 1, 1);
  114. QLabel* contentLabel = new QLabel(news_piece);
  115. contentLabel->setObjectName(QStringLiteral("contentLabel_common"));
  116. contentLabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
  117. contentLabel->setWordWrap(true);
  118. contentLabel->setText(text);
  119. contentLabel->setSizePolicy(sizePolicy);
  120. news_piece_layout->addWidget(contentLabel, 1, 1, 1, 1);
  121. app->window.changeFontSizeRecursive(app->properties.value("settings/ui_scale", 100).toInt(), news_piece);
  122. ui->news_layout->addWidget(news_piece, piece_id, 0);
  123. }
  124. void NewsWidget::setImgToNewsPiece(int piece_id, QPixmap img)
  125. {
  126. QLabel* old_piece = findChild<QLabel*>("news_icon_" + QString::number(piece_id));
  127. if (!old_piece)
  128. return;
  129. old_piece->setPixmap(img.scaled(60, 60));
  130. }