From 0339e6ee350688ae3df335663c2b5ca5c574a2bf Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 19 Nov 2018 12:00:50 +0800 Subject: [PATCH] Clean up code --- src/base/bittorrent/torrenthandle.cpp | 18 ++++++++++-------- src/gui/torrentcontentmodelfolder.cpp | 15 ++++++++------- src/gui/torrentcontentmodelitem.cpp | 6 ++---- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 6c4567b35..e8761c66a 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -973,12 +973,13 @@ qulonglong TorrentHandle::eta() const QVector TorrentHandle::filesProgress() const { std::vector fp; - QVector result; m_nativeHandle.file_progress(fp, libt::torrent_handle::piece_granularity); - int count = static_cast(fp.size()); + const int count = static_cast(fp.size()); + QVector result; + result.reserve(count); for (int i = 0; i < count; ++i) { - qlonglong size = fileSize(i); + const qlonglong size = fileSize(i); if ((size <= 0) || (fp[i] == size)) result << 1; else @@ -2100,7 +2101,7 @@ void TorrentHandle::prioritizeFiles(const QVector &priorities) QVector TorrentHandle::availableFileFractions() const { - const auto filesCount = this->filesCount(); + const int filesCount = this->filesCount(); if (filesCount < 0) return {}; const QVector piecesAvailability = pieceAvailability(); @@ -2109,12 +2110,13 @@ QVector TorrentHandle::availableFileFractions() const QVector res; res.reserve(filesCount); - TorrentInfo info = this->info(); - for (int file = 0; file < filesCount; ++file) { - TorrentInfo::PieceRange filePieces = info.filePieces(file); + const TorrentInfo info = this->info(); + for (int i = 0; i < filesCount; ++i) { + const TorrentInfo::PieceRange filePieces = info.filePieces(i); + int availablePieces = 0; for (int piece = filePieces.first(); piece <= filePieces.last(); ++piece) { - availablePieces += piecesAvailability[piece] > 0 ? 1 : 0; + availablePieces += (piecesAvailability[piece] > 0) ? 1 : 0; } res.push_back(static_cast(availablePieces) / filePieces.size()); } diff --git a/src/gui/torrentcontentmodelfolder.cpp b/src/gui/torrentcontentmodelfolder.cpp index 727de5162..cf72971d0 100644 --- a/src/gui/torrentcontentmodelfolder.cpp +++ b/src/gui/torrentcontentmodelfolder.cpp @@ -142,13 +142,14 @@ void TorrentContentModelFolder::recalculateProgress() qulonglong tSize = 0; qulonglong tRemaining = 0; foreach (TorrentContentModelItem *child, m_childItems) { - if (child->priority() != prio::IGNORED) { - if (child->itemType() == FolderType) - static_cast(child)->recalculateProgress(); - tProgress += child->progress() * child->size(); - tSize += child->size(); - tRemaining += child->remaining(); - } + if (child->priority() == prio::IGNORED) + continue; + + if (child->itemType() == FolderType) + static_cast(child)->recalculateProgress(); + tProgress += child->progress() * child->size(); + tSize += child->size(); + tRemaining += child->remaining(); } if (!isRootItem() && (tSize > 0)) { diff --git a/src/gui/torrentcontentmodelitem.cpp b/src/gui/torrentcontentmodelitem.cpp index bc330f702..01f0e39f5 100644 --- a/src/gui/torrentcontentmodelitem.cpp +++ b/src/gui/torrentcontentmodelitem.cpp @@ -74,9 +74,7 @@ qreal TorrentContentModelItem::progress() const { Q_ASSERT(!isRootItem()); - if (m_size > 0) return m_progress; - - return 1; + return (m_size > 0) ? m_progress : 1; } qulonglong TorrentContentModelItem::remaining() const @@ -89,7 +87,7 @@ qreal TorrentContentModelItem::availability() const { Q_ASSERT(!isRootItem()); - return m_size > 0 ? m_availability : 0.; + return (m_size > 0) ? m_availability : 0; } int TorrentContentModelItem::priority() const