qBittorrent
articlelistwidget.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 Vladimir Golovnev <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "articlelistwidget.h"
30 
31 #include <QListWidgetItem>
32 
33 #include "base/global.h"
34 #include "base/rss/rss_article.h"
35 #include "base/rss/rss_item.h"
36 #include "gui/uithememanager.h"
37 
39  : QListWidget(parent)
40 {
41  setContextMenuPolicy(Qt::CustomContextMenu);
42  setSelectionMode(QAbstractItemView::ExtendedSelection);
43 
45 }
46 
47 RSS::Article *ArticleListWidget::getRSSArticle(QListWidgetItem *item) const
48 {
49  Q_ASSERT(item);
50  return reinterpret_cast<RSS::Article *>(item->data(Qt::UserRole).value<quintptr>());
51 }
52 
53 QListWidgetItem *ArticleListWidget::mapRSSArticle(RSS::Article *rssArticle) const
54 {
55  return m_rssArticleToListItemMapping.value(rssArticle);
56 }
57 
58 void ArticleListWidget::setRSSItem(RSS::Item *rssItem, bool unreadOnly)
59 {
60  // Clear the list first
61  clear();
63  if (m_rssItem)
64  m_rssItem->disconnect(this);
65 
66  m_unreadOnly = unreadOnly;
67  m_rssItem = rssItem;
68  if (m_rssItem)
69  {
73 
74  for (const auto article : asConst(rssItem->articles()))
75  {
76  if (!(m_unreadOnly && article->isRead()))
77  {
78  auto item = createItem(article);
79  addItem(item);
80  m_rssArticleToListItemMapping.insert(article, item);
81  }
82  }
83  }
84 
86 }
87 
89 {
90  if (!(m_unreadOnly && rssArticle->isRead()))
91  {
92  auto item = createItem(rssArticle);
93  insertItem(0, item);
94  m_rssArticleToListItemMapping.insert(rssArticle, item);
95  }
96 
98 }
99 
101 {
102  auto item = mapRSSArticle(rssArticle);
103  if (!item) return;
104 
105  const QColor defaultColor {palette().color(QPalette::Inactive, QPalette::WindowText)};
106  const QBrush foregroundBrush {UIThemeManager::instance()->getColor("RSS.ReadArticle", defaultColor)};
107  item->setData(Qt::ForegroundRole, foregroundBrush);
108  item->setData(Qt::DecorationRole, UIThemeManager::instance()->getIcon(QLatin1String("sphere")));
109 
110  checkInvariant();
111 }
112 
114 {
115  delete m_rssArticleToListItemMapping.take(rssArticle);
116  checkInvariant();
117 }
118 
120 {
121  Q_ASSERT(count() == m_rssArticleToListItemMapping.count());
122 }
123 
124 QListWidgetItem *ArticleListWidget::createItem(RSS::Article *article) const
125 {
126  Q_ASSERT(article);
127  auto *item = new QListWidgetItem;
128 
129  item->setData(Qt::DisplayRole, article->title());
130  item->setData(Qt::UserRole, reinterpret_cast<quintptr>(article));
131  if (article->isRead())
132  {
133  const QColor defaultColor {palette().color(QPalette::Inactive, QPalette::WindowText)};
134  const QBrush foregroundBrush {UIThemeManager::instance()->getColor("RSS.ReadArticle", defaultColor)};
135  item->setData(Qt::ForegroundRole, foregroundBrush);
136  item->setData(Qt::DecorationRole, UIThemeManager::instance()->getIcon(QLatin1String("sphere")));
137  }
138  else
139  {
140  const QColor defaultColor {palette().color(QPalette::Active, QPalette::Link)};
141  const QBrush foregroundBrush {UIThemeManager::instance()->getColor("RSS.UnreadArticle", defaultColor)};
142  item->setData(Qt::ForegroundRole, foregroundBrush);
143  item->setData(Qt::DecorationRole, UIThemeManager::instance()->getIcon(QLatin1String("sphere")));
144  }
145 
146  return item;
147 }
void handleArticleAdded(RSS::Article *rssArticle)
void checkInvariant() const
RSS::Item * m_rssItem
void setRSSItem(RSS::Item *rssItem, bool unreadOnly=false)
bool m_unreadOnly
QListWidgetItem * mapRSSArticle(RSS::Article *rssArticle) const
ArticleListWidget(QWidget *parent)
QHash< RSS::Article *, QListWidgetItem * > m_rssArticleToListItemMapping
QListWidgetItem * createItem(RSS::Article *article) const
void handleArticleRead(RSS::Article *rssArticle)
RSS::Article * getRSSArticle(QListWidgetItem *item) const
void handleArticleAboutToBeRemoved(RSS::Article *rssArticle)
Definition: rss_article.h:43
QString title() const
Definition: rss_article.cpp:92
bool isRead() const
void newArticle(Article *article)
void articleAboutToBeRemoved(Article *article)
virtual QList< Article * > articles() const =0
void articleRead(Article *article)
static UIThemeManager * instance()
QColor getColor(const QString &id, const QColor &defaultColor) const
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42