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)
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();
} }

1
src/gui/torrentcontentmodel.h

@ -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;

6
src/gui/torrentcontentmodelfile.cpp

@ -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;

1
src/gui/torrentcontentmodelfile.h

@ -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:

28
src/gui/torrentcontentmodelfolder.cpp

@ -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())

1
src/gui/torrentcontentmodelfolder.h

@ -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;

11
src/gui/torrentcontentmodelitem.cpp

@ -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;
} }

4
src/gui/torrentcontentmodelitem.h

@ -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

Loading…
Cancel
Save