/dev/Code/Tools/News/NewsShared/Qt/ArticleView.cpp

https://github.com/aws/lumberyard · C++ · 157 lines · 120 code · 20 blank · 17 comment · 14 complexity · f3d6f44f0733d13545b10e70167fbd90 MD5 · raw file

  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <AzQtComponents/Components/ExtendedLabel.h>
  13. #include "ArticleView.h"
  14. #include "NewsShared/ResourceManagement/ArticleDescriptor.h"
  15. AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option") // 4251: 'QVariant::d': struct 'QVariant::Private' needs to have dll-interface to be used by clients of class 'QVariant'
  16. #include "NewsShared/Qt/ui_ArticleView.h"
  17. AZ_POP_DISABLE_WARNING
  18. #include "NewsShared/Qt/ui_PinnedArticleView.h"
  19. #include "NewsShared/ResourceManagement/ResourceManifest.h"
  20. #include "NewsShared/ResourceManagement/Resource.h"
  21. #include <QDesktopServices>
  22. #include <QUrl>
  23. using namespace News;
  24. ArticleView::ArticleView(
  25. QWidget* parent,
  26. const ArticleDescriptor& article,
  27. const ResourceManifest& manifest)
  28. : QWidget(parent)
  29. , m_pArticle(new ArticleDescriptor(article))
  30. , m_manifest(manifest)
  31. , m_icon(nullptr)
  32. {
  33. }
  34. void ArticleView::Update()
  35. {
  36. Q_ASSERT(m_widgetImageFrame && m_widgetTitle && m_widgetBody);
  37. auto pResource = m_manifest.FindById(m_pArticle->GetResource().GetId());
  38. m_pArticle.reset();
  39. if (pResource)
  40. {
  41. m_pArticle = QSharedPointer<ArticleDescriptor>(new ArticleDescriptor(*pResource));
  42. m_widgetTitle->setText(m_pArticle->GetTitle());
  43. m_widgetBody->setText(m_pArticle->GetBody());
  44. auto pImageResource = m_manifest.FindById(m_pArticle->GetImageId());
  45. if (pImageResource)
  46. {
  47. QPixmap pixmap;
  48. if (pixmap.loadFromData(pImageResource->GetData()))
  49. {
  50. if (!m_icon)
  51. {
  52. m_icon = new AzQtComponents::ExtendedLabel(this);
  53. m_icon->setStyleSheet("border: none;");
  54. m_icon->setAlignment(Qt::AlignCenter);
  55. static_cast<QVBoxLayout*>(
  56. m_widgetImageFrame->layout())->insertWidget(0, m_icon);
  57. connect(m_icon, &AzQtComponents::ExtendedLabel::clicked, this, &ArticleView::articleSelectedSlot);
  58. }
  59. m_icon->setPixmap(pixmap.scaled(m_widgetImageFrame->minimumWidth(),
  60. m_widgetImageFrame->minimumHeight(),
  61. Qt::KeepAspectRatioByExpanding));
  62. }
  63. else
  64. {
  65. RemoveIcon();
  66. }
  67. }
  68. else
  69. {
  70. RemoveIcon();
  71. }
  72. }
  73. }
  74. void ArticleView::mousePressEvent(QMouseEvent* event)
  75. {
  76. articleSelectedSlot();
  77. }
  78. void ArticleView::RemoveIcon()
  79. {
  80. if (m_icon)
  81. {
  82. delete m_icon;
  83. m_icon = nullptr;
  84. }
  85. }
  86. void ArticleView::linkActivatedSlot(const QString& link)
  87. {
  88. QDesktopServices::openUrl(QUrl(link));
  89. Q_EMIT linkActivatedSignal(link);
  90. }
  91. void ArticleView::articleSelectedSlot()
  92. {
  93. Q_EMIT articleSelectedSignal(m_pArticle->GetResource().GetId());
  94. }
  95. QSharedPointer<const ArticleDescriptor> ArticleView::GetArticle() const
  96. {
  97. return m_pArticle;
  98. }
  99. void ArticleView::SetupViewWidget(QFrame* widgetImageFrame, AzQtComponents::ExtendedLabel* widgetTitle, AzQtComponents::ExtendedLabel* widgetBody)
  100. {
  101. Q_ASSERT(m_widgetImageFrame == nullptr && m_widgetTitle == nullptr && m_widgetBody == nullptr);
  102. m_widgetImageFrame = widgetImageFrame;
  103. m_widgetTitle = widgetTitle;
  104. m_widgetBody = widgetBody;
  105. connect(m_widgetTitle, &QLabel::linkActivated, this, &ArticleView::linkActivatedSlot);
  106. connect(m_widgetBody, &QLabel::linkActivated, this, &ArticleView::linkActivatedSlot);
  107. connect(m_widgetTitle, &AzQtComponents::ExtendedLabel::clicked, this, &ArticleView::articleSelectedSlot);
  108. connect(m_widgetBody, &AzQtComponents::ExtendedLabel::clicked, this, &ArticleView::articleSelectedSlot);
  109. Q_ASSERT(m_widgetImageFrame && m_widgetTitle && m_widgetBody);
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////
  112. // ArticleViewDefaultWidget
  113. ////////////////////////////////////////////////////////////////////////////////////////////////////
  114. ArticleViewDefaultWidget::ArticleViewDefaultWidget(QWidget* parent,
  115. const ArticleDescriptor& article,
  116. const ResourceManifest& manifest)
  117. : ArticleView(parent, article, manifest)
  118. , m_ui(new Ui::ArticleViewWidget())
  119. {
  120. m_ui->setupUi(this);
  121. SetupViewWidget(m_ui->imageFrame, m_ui->titleLabel, m_ui->bodyLabel);
  122. Update();
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////////////////////////
  125. // ArticleViewPinnedWidget
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////
  127. ArticleViewPinnedWidget::ArticleViewPinnedWidget(QWidget* parent,
  128. const ArticleDescriptor& article,
  129. const ResourceManifest& manifest)
  130. : ArticleView(parent, article, manifest)
  131. , m_ui(new Ui::PinnedArticleViewWidget())
  132. {
  133. m_ui->setupUi(this);
  134. SetupViewWidget(m_ui->imageFrame, m_ui->titleLabel, m_ui->bodyLabel);
  135. Update();
  136. }
  137. #include "NewsShared/Qt/ArticleView.moc"