Add availability column to torrent content model

This commit is contained in:
Eugene Shalygin 2016-03-22 16:45:54 +01:00
parent 114c9a8421
commit d5af8722a6
8 changed files with 68 additions and 0 deletions

View file

@ -76,6 +76,7 @@ void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp)
m_filesIndex[i]->setProgress(fp[i]); m_filesIndex[i]->setProgress(fp[i]);
// Update folders progress in the tree // Update folders progress in the tree
m_rootItem->recalculateProgress(); m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(index(0, 0), index(rowCount(), columnCount())); emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
} }
@ -92,6 +93,20 @@ void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio)
emit dataChanged(index(0, 0), index(rowCount(), columnCount())); emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
} }
void TorrentContentModel::updateFilesAvailability(const QVector<qreal> &fa)
{
Q_ASSERT(m_filesIndex.size() == fa.size());
// XXX: Why is this necessary?
if (m_filesIndex.size() != fa.size()) return;
emit layoutAboutToBeChanged();
for (int i = 0; i < fa.size(); ++i)
m_filesIndex[i]->setAvailability(fa[i]);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
}
QVector<int> TorrentContentModel::getFilePriorities() const QVector<int> TorrentContentModel::getFilePriorities() const
{ {
QVector<int> prio; QVector<int> prio;
@ -134,6 +149,7 @@ bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& valu
item->setPriority(prio::NORMAL); item->setPriority(prio::NORMAL);
// Update folders progress in the tree // Update folders progress in the tree
m_rootItem->recalculateProgress(); m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(this->index(0, 0), this->index(rowCount() - 1, columnCount() - 1)); emit dataChanged(this->index(0, 0), this->index(rowCount() - 1, columnCount() - 1));
emit filteredFilesChanged(); emit filteredFilesChanged();
} }

View file

@ -51,6 +51,7 @@ public:
void updateFilesProgress(const QVector<qreal> &fp); void updateFilesProgress(const QVector<qreal> &fp);
void updateFilesPriorities(const QVector<int> &fprio); void updateFilesPriorities(const QVector<int> &fprio);
void updateFilesAvailability(const QVector<qreal> &fa);
QVector<int> getFilePriorities() const; QVector<int> getFilePriorities() const;
bool allFiltered() const; bool allFiltered() const;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;

View file

@ -73,6 +73,12 @@ void TorrentContentModelFile::setProgress(qreal progress)
Q_ASSERT(m_progress <= 1.); Q_ASSERT(m_progress <= 1.);
} }
void TorrentContentModelFile::setAvailability(qreal availability)
{
m_availability = availability;
Q_ASSERT(m_availability <= 1.);
}
TorrentContentModelItem::ItemType TorrentContentModelFile::itemType() const TorrentContentModelItem::ItemType TorrentContentModelFile::itemType() const
{ {
return FileType; return FileType;

View file

@ -42,6 +42,7 @@ public:
int fileIndex() const; int fileIndex() const;
void setPriority(int newPriority, bool updateParent = true) override; void setPriority(int newPriority, bool updateParent = true) override;
void setProgress(qreal progress); void setProgress(qreal progress);
void setAvailability(qreal availability);
ItemType itemType() const override; ItemType itemType() const override;
private: private:

View file

@ -159,6 +159,34 @@ void TorrentContentModelFolder::recalculateProgress()
} }
} }
void TorrentContentModelFolder::recalculateAvailability()
{
qreal tAvailability = 0;
qulonglong tSize = 0;
bool foundAnyData = false;
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->priority() == prio::IGNORED)
continue;
if (child->itemType() == FolderType)
static_cast<TorrentContentModelFolder*>(child)->recalculateAvailability();
const qreal childAvailability = child->availability();
if (childAvailability >= 0) { // -1 means "no data"
tAvailability += childAvailability * child->size();
foundAnyData = true;
}
tSize += child->size();
}
if (!isRootItem() && (tSize > 0) && foundAnyData) {
m_availability = tAvailability / tSize;
Q_ASSERT(m_availability <= 1.);
}
else {
m_availability = -1.;
}
}
void TorrentContentModelFolder::increaseSize(qulonglong delta) void TorrentContentModelFolder::increaseSize(qulonglong delta)
{ {
if (isRootItem()) if (isRootItem())

View file

@ -48,6 +48,7 @@ public:
void increaseSize(qulonglong delta); void increaseSize(qulonglong delta);
void recalculateProgress(); void recalculateProgress();
void recalculateAvailability();
void updatePriority(); void updatePriority();
void setPriority(int newPriority, bool updateParent = true) override; void setPriority(int newPriority, bool updateParent = true) override;

View file

@ -40,6 +40,7 @@ TorrentContentModelItem::TorrentContentModelItem(TorrentContentModelFolder *pare
, m_remaining(0) , m_remaining(0)
, m_priority(prio::NORMAL) , m_priority(prio::NORMAL)
, m_progress(0) , m_progress(0)
, m_availability(-1.)
{ {
} }
@ -84,6 +85,13 @@ qulonglong TorrentContentModelItem::remaining() const
return m_remaining; return m_remaining;
} }
qreal TorrentContentModelItem::availability() const
{
Q_ASSERT(!isRootItem());
return m_size > 0 ? m_availability : 0.;
}
int TorrentContentModelItem::priority() const int TorrentContentModelItem::priority() const
{ {
Q_ASSERT(!isRootItem()); Q_ASSERT(!isRootItem());
@ -111,6 +119,8 @@ QVariant TorrentContentModelItem::data(int column) const
return m_size; return m_size;
case COL_REMAINING: case COL_REMAINING:
return remaining(); return remaining();
case COL_AVAILABILITY:
return availability();
default: default:
Q_ASSERT(false); Q_ASSERT(false);
return QVariant(); return QVariant();
@ -128,3 +138,4 @@ TorrentContentModelFolder *TorrentContentModelItem::parent() const
{ {
return m_parentItem; return m_parentItem;
} }

View file

@ -58,6 +58,7 @@ public:
COL_PROGRESS, COL_PROGRESS,
COL_PRIO, COL_PRIO,
COL_REMAINING, COL_REMAINING,
COL_AVAILABILITY,
NB_COL NB_COL
}; };
@ -81,6 +82,8 @@ public:
qreal progress() const; qreal progress() const;
qulonglong remaining() const; qulonglong remaining() const;
qreal availability() const;
int priority() const; int priority() const;
virtual void setPriority(int newPriority, bool updateParent = true) = 0; virtual void setPriority(int newPriority, bool updateParent = true) = 0;
@ -98,6 +101,7 @@ protected:
qulonglong m_remaining; qulonglong m_remaining;
int m_priority; int m_priority;
qreal m_progress; qreal m_progress;
qreal m_availability;
}; };
#endif // TORRENTCONTENTMODELITEM_H #endif // TORRENTCONTENTMODELITEM_H