PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tiled/newsbutton.cpp

http://github.com/bjorn/tiled
C++ | 143 lines | 99 code | 25 blank | 19 comment | 5 complexity | ba956599a5bad80d740ea5a5df9dc74e MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-2.1, AGPL-1.0
  1. /*
  2. * newsbutton.cpp
  3. * Copyright 2018, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
  4. *
  5. * This file is part of Tiled.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "newsbutton.h"
  21. #include "newsfeed.h"
  22. #include "preferences.h"
  23. #include "utils.h"
  24. #include <QDesktopServices>
  25. #include <QMenu>
  26. #include <QPainter>
  27. #ifdef TILED_SNAPSHOT
  28. static const char newsArchiveUrl[] = "https://thorbjorn.itch.io/tiled/devlog";
  29. #else
  30. static const char newsArchiveUrl[] = "https://www.mapeditor.org/news";
  31. #endif
  32. namespace Tiled {
  33. NewsButton::NewsButton(QWidget *parent)
  34. : QToolButton(parent)
  35. , mReadIcon(QLatin1String("://images/16/mail-read-symbolic.png"))
  36. , mUnreadIcon(QLatin1String("://images/16/mail-unread-symbolic.png"))
  37. {
  38. const auto preferences = Preferences::instance();
  39. setVisible(preferences->displayNews());
  40. connect(preferences, &Preferences::displayNewsChanged, this, &NewsButton::setVisible);
  41. auto &feed = NewsFeed::instance();
  42. setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  43. setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  44. setAutoRaise(true);
  45. #ifdef TILED_SNAPSHOT
  46. setText(tr("Devlog"));
  47. #else
  48. setText(tr("News"));
  49. #endif
  50. setToolTip(feed.errorString());
  51. connect(&feed, &NewsFeed::refreshed,
  52. this, &NewsButton::refreshButton);
  53. connect(&feed, &NewsFeed::errorStringChanged,
  54. this, &NewsButton::setToolTip);
  55. connect(this, &QToolButton::pressed,
  56. this, &NewsButton::showNewsMenu);
  57. refreshButton();
  58. }
  59. void NewsButton::refreshButton()
  60. {
  61. auto &feed = NewsFeed::instance();
  62. auto unreadCount = feed.unreadCount();
  63. if (unreadCount > 0) {
  64. QPixmap numberPixmap(Utils::smallIconSize());
  65. numberPixmap.fill(Qt::transparent);
  66. QPainter painter(&numberPixmap);
  67. painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  68. painter.setBrush(QColor(250, 92, 92));
  69. painter.setPen(Qt::NoPen);
  70. painter.drawEllipse(numberPixmap.rect().adjusted(1, 1, -1, -1));
  71. auto font = painter.font();
  72. font.setBold(true);
  73. painter.setFont(font);
  74. painter.setBrush(Qt::white);
  75. painter.setPen(Qt::white);
  76. painter.drawText(numberPixmap.rect(), Qt::AlignCenter, unreadCount < 5 ? QString::number(unreadCount) :
  77. QString(QLatin1String("!")));
  78. setIcon(QIcon(numberPixmap));
  79. } else {
  80. setIcon(QIcon());
  81. }
  82. setEnabled(!feed.isEmpty());
  83. }
  84. void NewsButton::showNewsMenu()
  85. {
  86. auto newsFeedMenu = new QMenu;
  87. auto &feed = NewsFeed::instance();
  88. for (const NewsItem &newsItem : feed.newsItems()) {
  89. QAction *action = newsFeedMenu->addAction(newsItem.title, [=] {
  90. QDesktopServices::openUrl(newsItem.link);
  91. NewsFeed::instance().markRead(newsItem);
  92. });
  93. if (feed.isUnread(newsItem)) {
  94. QFont f = action->font();
  95. f.setBold(true);
  96. action->setFont(f);
  97. action->setIcon(mUnreadIcon);
  98. } else {
  99. action->setIcon(mReadIcon);
  100. }
  101. }
  102. newsFeedMenu->addSeparator();
  103. #ifdef TILED_SNAPSHOT
  104. QAction *action = newsFeedMenu->addAction(tr("View All Posts"));
  105. #else
  106. QAction *action = newsFeedMenu->addAction(tr("News Archive"));
  107. #endif
  108. connect(action, &QAction::triggered, [] {
  109. QDesktopServices::openUrl(QUrl(QLatin1String(newsArchiveUrl)));
  110. NewsFeed::instance().markAllRead();
  111. });
  112. auto size = newsFeedMenu->sizeHint();
  113. auto rect = QRect(mapToGlobal(QPoint(width() - size.width(), -size.height())), size);
  114. newsFeedMenu->setGeometry(rect);
  115. newsFeedMenu->exec();
  116. setDown(false);
  117. }
  118. } // namespace Tiled