Use polymorphism to distinguish folder / file items in torrent content model

Step to address issue #24.
This commit is contained in:
Christophe Dumez
2012-08-26 18:10:32 +03:00
parent 30be83d445
commit 0fdacf4d54
13 changed files with 507 additions and 297 deletions

View File

@@ -31,281 +31,113 @@
#include "misc.h"
#include "fs_utils.h"
#include "torrentcontentmodelitem.h"
#include "torrentcontentmodelfolder.h"
#include <QDebug>
TorrentContentModelItem::TorrentContentModelItem(const libtorrent::torrent_info &t,
const libtorrent::file_entry &f,
TorrentContentModelItem *parent,
int file_index):
m_parentItem(parent), m_type(TFILE), m_fileIndex(file_index), m_totalDone(0)
TorrentContentModelItem::TorrentContentModelItem(TorrentContentModelFolder* parent)
: m_parentItem(parent)
, m_progress(0)
, m_priority(prio::NORMAL)
, m_totalDone(0)
{
Q_ASSERT(parent);
#if LIBTORRENT_VERSION_MINOR >= 16
QString name = fsutils::fileName(misc::toQStringU(t.files().file_path(f)));
#else
Q_UNUSED(t);
QString name = misc::toQStringU(f.path.filename());
#endif
// Do not display incomplete extensions
if (name.endsWith(".!qB"))
name.chop(4);
m_itemData << name
<< QVariant((qulonglong)f.size)
<< 0.
<< prio::NORMAL;
/* Update parent */
m_parentItem->appendChild(this);
m_parentItem->updateSize();
}
TorrentContentModelItem::TorrentContentModelItem(QString name, TorrentContentModelItem *parent):
m_parentItem(parent), m_type(FOLDER), m_totalDone(0)
{
// Do not display incomplete extensions
if (name.endsWith(".!qB"))
name.chop(4);
m_itemData << name
<< 0.
<< 0.
<< prio::NORMAL;
/* Update parent */
m_parentItem->appendChild(this);
}
TorrentContentModelItem::TorrentContentModelItem(const QList<QVariant>& data):
m_parentItem(0), m_type(ROOT), m_itemData(data), m_totalDone(0)
{
Q_ASSERT(data.size() == 4);
}
TorrentContentModelItem::~TorrentContentModelItem()
{
qDeleteAll(m_childItems);
}
TorrentContentModelItem::FileType TorrentContentModelItem::getType() const
QString TorrentContentModelItem::name() const
{
return m_type;
}
int TorrentContentModelItem::getFileIndex() const
{
Q_ASSERT(m_type == TFILE);
return m_fileIndex;
}
void TorrentContentModelItem::deleteAllChildren()
{
Q_ASSERT(m_type == ROOT);
qDeleteAll(m_childItems);
m_childItems.clear();
}
const QList<TorrentContentModelItem*>& TorrentContentModelItem::children() const
{
return m_childItems;
}
QString TorrentContentModelItem::getName() const
{
return m_itemData.at(COL_NAME).toString();
Q_ASSERT(!isRootItem());
return m_name;
}
void TorrentContentModelItem::setName(const QString& name)
{
Q_ASSERT(m_type != ROOT);
m_itemData.replace(COL_NAME, name);
Q_ASSERT(!isRootItem());
m_name = name;
}
qulonglong TorrentContentModelItem::getSize() const
qulonglong TorrentContentModelItem::size() const
{
return m_itemData.value(COL_SIZE).toULongLong();
Q_ASSERT(!isRootItem());
return m_size;
}
void TorrentContentModelItem::setSize(qulonglong size)
{
Q_ASSERT (m_type != ROOT);
if (getSize() == size)
Q_ASSERT(!isRootItem());
if (m_size == size)
return;
m_itemData.replace(COL_SIZE, (qulonglong)size);
m_parentItem->updateSize();
}
void TorrentContentModelItem::updateSize()
{
if (m_type == ROOT)
return;
Q_ASSERT(m_type == FOLDER);
qulonglong size = 0;
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->getPriority() != prio::IGNORED)
size += child->getSize();
}
setSize(size);
m_size = size;
m_parentItem->updateSize();
}
void TorrentContentModelItem::setProgress(qulonglong done)
{
Q_ASSERT (m_type != ROOT);
if (getPriority() == 0) return;
Q_ASSERT(!isRootItem());
if (m_priority == prio::IGNORED)
return;
m_totalDone = done;
qulonglong size = getSize();
Q_ASSERT(m_totalDone <= size);
Q_ASSERT(m_totalDone <= m_size);
qreal progress;
if (size > 0)
progress = m_totalDone/(float)size;
if (m_size > 0)
progress = m_totalDone / (double) m_size;
else
progress = 1.;
Q_ASSERT(progress >= 0. && progress <= 1.);
m_itemData.replace(COL_PROGRESS, progress);
m_progress = progress;
m_parentItem->updateProgress();
}
qulonglong TorrentContentModelItem::getTotalDone() const
qulonglong TorrentContentModelItem::totalDone() const
{
Q_ASSERT(!isRootItem());
return m_totalDone;
}
float TorrentContentModelItem::getProgress() const
float TorrentContentModelItem::progress() const
{
Q_ASSERT (m_type != ROOT);
if (getPriority() == 0)
Q_ASSERT(!isRootItem());
if (m_priority == prio::IGNORED)
return -1;
qulonglong size = getSize();
if (size > 0)
return m_totalDone / (float) getSize();
return 1.;
if (m_size > 0)
return m_totalDone / (double) m_size;
return 1;
}
void TorrentContentModelItem::updateProgress()
int TorrentContentModelItem::priority() const
{
if (m_type == ROOT) return;
Q_ASSERT(m_type == FOLDER);
m_totalDone = 0;
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->getPriority() > 0)
m_totalDone += child->getTotalDone();
}
//qDebug("Folder: total_done: %llu/%llu", total_done, getSize());
Q_ASSERT(m_totalDone <= getSize());
setProgress(m_totalDone);
}
int TorrentContentModelItem::getPriority() const
{
return m_itemData.value(COL_PRIO).toInt();
}
void TorrentContentModelItem::setPriority(int new_prio, bool update_parent)
{
Q_ASSERT(new_prio != prio::PARTIAL || m_type == FOLDER); // PARTIAL only applies to folders
const int old_prio = getPriority();
if (old_prio == new_prio) return;
qDebug("setPriority(%s, %d)", qPrintable(getName()), new_prio);
// Reset progress if priority is 0
if (new_prio == 0) {
setProgress(0);
}
m_itemData.replace(COL_PRIO, new_prio);
// Update parent
if (update_parent && m_parentItem) {
qDebug("Updating parent item");
m_parentItem->updateSize();
m_parentItem->updateProgress();
m_parentItem->updatePriority();
}
// Update children
if (new_prio != prio::PARTIAL && !m_childItems.empty()) {
qDebug("Updating children items");
foreach (TorrentContentModelItem* child, m_childItems) {
// Do not update the parent since
// the parent is causing the update
child->setPriority(new_prio, false);
}
}
if (m_type == FOLDER) {
updateSize();
updateProgress();
}
}
// Only non-root folders use this function
void TorrentContentModelItem::updatePriority()
{
if (m_type == ROOT) return;
Q_ASSERT(m_type == FOLDER);
if (m_childItems.isEmpty()) return;
// If all children have the same priority
// then the folder should have the same
// priority
const int prio = m_childItems.first()->getPriority();
for (int i=1; i<m_childItems.size(); ++i) {
if (m_childItems.at(i)->getPriority() != prio) {
setPriority(prio::PARTIAL);
return;
}
}
// All child items have the same priority
// Update own if necessary
if (prio != getPriority())
setPriority(prio);
}
TorrentContentModelItem* TorrentContentModelItem::childWithName(const QString& name) const
{
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->getName() == name)
return child;
}
return 0;
}
bool TorrentContentModelItem::isFolder() const
{
return (m_type==FOLDER);
}
void TorrentContentModelItem::appendChild(TorrentContentModelItem* item)
{
Q_ASSERT(item);
Q_ASSERT(m_type != TFILE);
int i=0;
for (i=0; i<m_childItems.size(); ++i) {
QString newchild_name = item->getName();
if (QString::localeAwareCompare(newchild_name, m_childItems.at(i)->getName()) < 0)
break;
}
m_childItems.insert(i, item);
}
TorrentContentModelItem* TorrentContentModelItem::child(int row) const
{
//Q_ASSERT(row >= 0 && row < childItems.size());
return m_childItems.value(row, 0);
}
int TorrentContentModelItem::childCount() const
{
return m_childItems.count();
Q_ASSERT(!isRootItem());
return m_priority;
}
int TorrentContentModelItem::columnCount() const
{
return m_itemData.count();
return NB_COL;
}
QVariant TorrentContentModelItem::data(int column) const
{
if (column == COL_PROGRESS && m_type != ROOT)
return getProgress();
return m_itemData.value(column);
if (isRootItem())
return m_itemData.value(column);
switch(column) {
case COL_NAME:
return m_name;
case COL_PRIO:
return m_priority;
case COL_PROGRESS:
return progress(); // XXX: m_progress ?
case COL_SIZE:
return m_size;
default:
Q_ASSERT(false);
return QVariant();
}
}
int TorrentContentModelItem::row() const
@@ -315,7 +147,7 @@ int TorrentContentModelItem::row() const
return 0;
}
TorrentContentModelItem* TorrentContentModelItem::parent() const
TorrentContentModelFolder* TorrentContentModelItem::parent() const
{
return m_parentItem;
}