Skip to content

Commit

Permalink
Follow project coding style. Issue qbittorrent#2192.
Browse files Browse the repository at this point in the history
--HG--
branch : magao-dev
  • Loading branch information
magao committed Feb 4, 2017
1 parent f89eb07 commit 58217f7
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 182 deletions.
320 changes: 168 additions & 152 deletions src/gui/rss/feedlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,203 +34,219 @@
#include "guiiconprovider.h"
#include "feedlistwidget.h"

FeedListWidget::FeedListWidget(QWidget *parent, const Rss::ManagerPtr& rssmanager)
FeedListWidget::FeedListWidget(QWidget *parent, const Rss::ManagerPtr &rssmanager)
: QTreeWidget(parent)
, m_rssManager(rssmanager)
, m_currentFeed(nullptr)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setDragDropMode(QAbstractItemView::InternalMove);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setColumnCount(1);
headerItem()->setText(0, tr("RSS feeds"));
m_unreadStickyItem = new QTreeWidgetItem(this);
m_unreadStickyItem->setText(0, tr("Unread") + QString::fromUtf8(" (") + QString::number(rssmanager->rootFolder()->unreadCount()) + QString(")"));
m_unreadStickyItem->setData(0,Qt::DecorationRole, GuiIconProvider::instance()->getIcon("mail-folder-inbox"));
itemAdded(m_unreadStickyItem, rssmanager->rootFolder());
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), SLOT(updateCurrentFeed(QTreeWidgetItem*)));
setCurrentItem(m_unreadStickyItem);
}

FeedListWidget::~FeedListWidget() {
delete m_unreadStickyItem;
}

void FeedListWidget::itemAdded(QTreeWidgetItem *item, const Rss::FilePtr& file) {
m_rssMapping[item] = file;
if (Rss::FeedPtr feed = qSharedPointerDynamicCast<Rss::Feed>(file)) {
m_feedsItems[feed->id()] = item;
}
}

void FeedListWidget::itemAboutToBeRemoved(QTreeWidgetItem *item) {
Rss::FilePtr file = m_rssMapping.take(item);
if (Rss::FeedPtr feed = qSharedPointerDynamicCast<Rss::Feed>(file)) {
m_feedsItems.remove(feed->id());
} else if (Rss::FolderPtr folder = qSharedPointerDynamicCast<Rss::Folder>(file)) {
Rss::FeedList feeds = folder->getAllFeeds();
foreach (const Rss::FeedPtr& feed, feeds) {
m_feedsItems.remove(feed->id());
setContextMenuPolicy(Qt::CustomContextMenu);
setDragDropMode(QAbstractItemView::InternalMove);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setColumnCount(1);
headerItem()->setText(0, tr("RSS feeds"));
m_unreadStickyItem = new QTreeWidgetItem(this);
m_unreadStickyItem->setText(0, tr("Unread") + QString::fromUtf8(" (") + QString::number(rssmanager->rootFolder()->unreadCount()) + QString(")"));
m_unreadStickyItem->setData(0,Qt::DecorationRole, GuiIconProvider::instance()->getIcon("mail-folder-inbox"));
itemAdded(m_unreadStickyItem, rssmanager->rootFolder());
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)), SLOT(updateCurrentFeed(QTreeWidgetItem *)));
setCurrentItem(m_unreadStickyItem);
}

FeedListWidget::~FeedListWidget()
{
delete m_unreadStickyItem;
}

void FeedListWidget::itemAdded(QTreeWidgetItem *item, const Rss::FilePtr &file)
{
m_rssMapping[item] = file;
if (Rss::FeedPtr feed = qSharedPointerDynamicCast<Rss::Feed>(file))
m_feedsItems[feed->id()] = item;
}

void FeedListWidget::itemAboutToBeRemoved(QTreeWidgetItem *item)
{
Rss::FilePtr file = m_rssMapping.take(item);
if (Rss::FeedPtr feed = qSharedPointerDynamicCast<Rss::Feed>(file)) {
m_feedsItems.remove(feed->id());
}
else if (Rss::FolderPtr folder = qSharedPointerDynamicCast<Rss::Folder>(file)) {
Rss::FeedList feeds = folder->getAllFeeds();
foreach (const Rss::FeedPtr &feed, feeds)
m_feedsItems.remove(feed->id());
}
}
}

bool FeedListWidget::hasFeed(const QString &url) const {
return m_feedsItems.contains(QUrl(url).toString());
bool FeedListWidget::hasFeed(const QString &url) const
{
return m_feedsItems.contains(QUrl(url).toString());
}

QList<QTreeWidgetItem*> FeedListWidget::getAllFeedItems() const {
return m_feedsItems.values();
QList<QTreeWidgetItem *> FeedListWidget::getAllFeedItems() const
{
return m_feedsItems.values();
}

QTreeWidgetItem* FeedListWidget::stickyUnreadItem() const {
return m_unreadStickyItem;
QTreeWidgetItem *FeedListWidget::stickyUnreadItem() const
{
return m_unreadStickyItem;
}

QStringList FeedListWidget::getItemPath(QTreeWidgetItem* item) const {
QStringList path;
if (item) {
if (item->parent())
path << getItemPath(item->parent());
path.append(getRSSItem(item)->id());
}
return path;
QStringList FeedListWidget::getItemPath(QTreeWidgetItem *item) const
{
QStringList path;
if (item) {
if (item->parent())
path << getItemPath(item->parent());
path.append(getRSSItem(item)->id());
}
return path;
}

QList<QTreeWidgetItem*> FeedListWidget::getAllOpenFolders(QTreeWidgetItem *parent) const {
QList<QTreeWidgetItem*> open_folders;
int nbChildren;
if (parent)
nbChildren = parent->childCount();
else
nbChildren = topLevelItemCount();
for (int i=0; i<nbChildren; ++i) {
QTreeWidgetItem *item;
QList<QTreeWidgetItem *> FeedListWidget::getAllOpenFolders(QTreeWidgetItem *parent) const
{
QList<QTreeWidgetItem *> open_folders;
int nbChildren;
if (parent)
item = parent->child(i);
nbChildren = parent->childCount();
else
item = topLevelItem(i);
if (isFolder(item) && item->isExpanded()) {
QList<QTreeWidgetItem*> open_subfolders = getAllOpenFolders(item);
if (!open_subfolders.empty()) {
open_folders << open_subfolders;
} else {
open_folders << item;
}
nbChildren = topLevelItemCount();
for (int i = 0; i<nbChildren; ++i) {
QTreeWidgetItem *item;
if (parent)
item = parent->child(i);
else
item = topLevelItem(i);
if (isFolder(item) && item->isExpanded()) {
QList<QTreeWidgetItem *> open_subfolders = getAllOpenFolders(item);
if (!open_subfolders.empty())
open_folders << open_subfolders;
else
open_folders << item;
}
}
}
return open_folders;
}

QList<QTreeWidgetItem*> FeedListWidget::getAllFeedItems(QTreeWidgetItem* folder) {
QList<QTreeWidgetItem*> feeds;
const int nbChildren = folder->childCount();
for (int i=0; i<nbChildren; ++i) {
QTreeWidgetItem *item = folder->child(i);
if (isFeed(item)) {
feeds << item;
} else {
feeds << getAllFeedItems(item);
return open_folders;
}

QList<QTreeWidgetItem *> FeedListWidget::getAllFeedItems(QTreeWidgetItem *folder)
{
QList<QTreeWidgetItem *> feeds;
const int nbChildren = folder->childCount();
for (int i = 0; i<nbChildren; ++i) {
QTreeWidgetItem *item = folder->child(i);
if (isFeed(item))
feeds << item;
else
feeds << getAllFeedItems(item);
}
}
return feeds;
return feeds;
}

Rss::FilePtr FeedListWidget::getRSSItem(QTreeWidgetItem *item) const {
return m_rssMapping.value(item, Rss::FilePtr());
Rss::FilePtr FeedListWidget::getRSSItem(QTreeWidgetItem *item) const
{
return m_rssMapping.value(item, Rss::FilePtr());
}

bool FeedListWidget::isFeed(QTreeWidgetItem *item) const
{
return (qSharedPointerDynamicCast<Rss::Feed>(m_rssMapping.value(item)) != NULL);
return (qSharedPointerDynamicCast<Rss::Feed>(m_rssMapping.value(item)) != NULL);
}

bool FeedListWidget::isFolder(QTreeWidgetItem *item) const
{
return (qSharedPointerDynamicCast<Rss::Folder>(m_rssMapping.value(item)) != NULL);
return (qSharedPointerDynamicCast<Rss::Folder>(m_rssMapping.value(item)) != NULL);
}

QString FeedListWidget::getItemID(QTreeWidgetItem *item) const {
return m_rssMapping.value(item)->id();
QString FeedListWidget::getItemID(QTreeWidgetItem *item) const
{
return m_rssMapping.value(item)->id();
}

QTreeWidgetItem* FeedListWidget::getTreeItemFromUrl(const QString &url) const {
return m_feedsItems.value(url, 0);
QTreeWidgetItem *FeedListWidget::getTreeItemFromUrl(const QString &url) const
{
return m_feedsItems.value(url, 0);
}

Rss::FeedPtr FeedListWidget::getRSSItemFromUrl(const QString &url) const {
return qSharedPointerDynamicCast<Rss::Feed>(getRSSItem(getTreeItemFromUrl(url)));
Rss::FeedPtr FeedListWidget::getRSSItemFromUrl(const QString &url) const
{
return qSharedPointerDynamicCast<Rss::Feed>(getRSSItem(getTreeItemFromUrl(url)));
}

QTreeWidgetItem* FeedListWidget::currentItem() const {
return m_currentFeed;
QTreeWidgetItem *FeedListWidget::currentItem() const
{
return m_currentFeed;
}

QTreeWidgetItem* FeedListWidget::currentFeed() const {
return m_currentFeed;
QTreeWidgetItem *FeedListWidget::currentFeed() const
{
return m_currentFeed;
}

void FeedListWidget::updateCurrentFeed(QTreeWidgetItem* new_item) {
if (!new_item) return;
if (!m_rssMapping.contains(new_item)) return;
if (isFeed(new_item) || new_item == m_unreadStickyItem)
m_currentFeed = new_item;
void FeedListWidget::updateCurrentFeed(QTreeWidgetItem *new_item)
{
if (!new_item) return;
if (!m_rssMapping.contains(new_item)) return;
if (isFeed(new_item) || ( new_item == m_unreadStickyItem) )
m_currentFeed = new_item;
}

void FeedListWidget::dragMoveEvent(QDragMoveEvent * event) {
QTreeWidget::dragMoveEvent(event);
void FeedListWidget::dragMoveEvent(QDragMoveEvent *event)
{
QTreeWidget::dragMoveEvent(event);

QTreeWidgetItem *item = itemAt(event->pos());
// Prohibit dropping onto global unread counter
if (item == m_unreadStickyItem) {
event->ignore();
return;
}
// Prohibit dragging of global unread counter
if (selectedItems().contains(m_unreadStickyItem)) {
event->ignore();
return;
}
// Prohibit dropping onto feeds
if (item && isFeed(item)) {
event->ignore();
return;
}
QTreeWidgetItem *item = itemAt(event->pos());
// Prohibit dropping onto global unread counter
if (item == m_unreadStickyItem) {
event->ignore();
return;
}
// Prohibit dragging of global unread counter
if (selectedItems().contains(m_unreadStickyItem)) {
event->ignore();
return;
}
// Prohibit dropping onto feeds
if (item && isFeed(item)) {
event->ignore();
return;
}
}

void FeedListWidget::dropEvent(QDropEvent *event) {
qDebug("dropEvent");
QList<QTreeWidgetItem*> folders_altered;
QTreeWidgetItem *dest_folder_item = itemAt(event->pos());
Rss::FolderPtr dest_folder;
if (dest_folder_item) {
dest_folder = qSharedPointerCast<Rss::Folder>(getRSSItem(dest_folder_item));
folders_altered << dest_folder_item;
} else {
dest_folder = m_rssManager->rootFolder();
}
QList<QTreeWidgetItem *> src_items = selectedItems();
// Check if there is not going to overwrite another file
foreach (QTreeWidgetItem *src_item, src_items) {
Rss::FilePtr file = getRSSItem(src_item);
if (dest_folder->hasChild(file->id())) {
QTreeWidget::dropEvent(event);
return;
void FeedListWidget::dropEvent(QDropEvent *event)
{
qDebug("dropEvent");
QList<QTreeWidgetItem *> folders_altered;
QTreeWidgetItem *dest_folder_item = itemAt(event->pos());
Rss::FolderPtr dest_folder;
if (dest_folder_item) {
dest_folder = qSharedPointerCast<Rss::Folder>(getRSSItem(dest_folder_item));
folders_altered << dest_folder_item;
}
else {
dest_folder = m_rssManager->rootFolder();
}
QList<QTreeWidgetItem *> src_items = selectedItems();
// Check if there is not going to overwrite another file
foreach (QTreeWidgetItem *src_item, src_items) {
Rss::FilePtr file = getRSSItem(src_item);
if (dest_folder->hasChild(file->id())) {
QTreeWidget::dropEvent(event);
return;
}
}
// Proceed with the move
foreach (QTreeWidgetItem *src_item, src_items) {
QTreeWidgetItem *parent_folder = src_item->parent();
if (parent_folder && !folders_altered.contains(parent_folder))
folders_altered << parent_folder;
// Actually move the file
Rss::FilePtr file = getRSSItem(src_item);
m_rssManager->moveFile(file, dest_folder);
}
}
// Proceed with the move
foreach (QTreeWidgetItem *src_item, src_items) {
QTreeWidgetItem *parent_folder = src_item->parent();
if (parent_folder && !folders_altered.contains(parent_folder))
folders_altered << parent_folder;
// Actually move the file
Rss::FilePtr file = getRSSItem(src_item);
m_rssManager->moveFile(file, dest_folder);
}
QTreeWidget::dropEvent(event);
if (dest_folder_item)
dest_folder_item->setExpanded(true);
// Emit signal for update
if (!folders_altered.empty())
emit foldersAltered(folders_altered);
QTreeWidget::dropEvent(event);
if (dest_folder_item)
dest_folder_item->setExpanded(true);
// Emit signal for update
if (!folders_altered.empty())
emit foldersAltered(folders_altered);
}
Loading

0 comments on commit 58217f7

Please sign in to comment.