Browse Source

Add availability column to torrent content model

adaptive-webui-19844
Eugene Shalygin 9 years ago
parent
commit
d5af8722a6
  1. 16
      src/gui/torrentcontentmodel.cpp
  2. 1
      src/gui/torrentcontentmodel.h
  3. 6
      src/gui/torrentcontentmodelfile.cpp
  4. 1
      src/gui/torrentcontentmodelfile.h
  5. 28
      src/gui/torrentcontentmodelfolder.cpp
  6. 1
      src/gui/torrentcontentmodelfolder.h
  7. 11
      src/gui/torrentcontentmodelitem.cpp
  8. 4
      src/gui/torrentcontentmodelitem.h

16
src/gui/torrentcontentmodel.cpp

@ -76,6 +76,7 @@ void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp) @@ -76,6 +76,7 @@ void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp)
m_filesIndex[i]->setProgress(fp[i]);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
}
@ -92,6 +93,20 @@ void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio) @@ -92,6 +93,20 @@ void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio)
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> prio;
@ -134,6 +149,7 @@ bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& valu @@ -134,6 +149,7 @@ bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& valu
item->setPriority(prio::NORMAL);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(this->index(0, 0), this->index(rowCount() - 1, columnCount() - 1));
emit filteredFilesChanged();
}

1
src/gui/torrentcontentmodel.h

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

6
src/gui/torrentcontentmodelfile.cpp

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

1
src/gui/torrentcontentmodelfile.h

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

28
src/gui/torrentcontentmodelfolder.cpp

@ -159,6 +159,34 @@ void TorrentContentModelFolder::recalculateProgress() @@ -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)
{
if (isRootItem())

1
src/gui/torrentcontentmodelfolder.h

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

11
src/gui/torrentcontentmodelitem.cpp

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

4
src/gui/torrentcontentmodelitem.h

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

Loading…
Cancel
Save