Browse Source

Clean up TorrentContentModel

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
691b976e11
  1. 68
      src/torrentcontentmodel.cpp
  2. 9
      src/torrentcontentmodel.h

68
src/torrentcontentmodel.cpp

@ -35,26 +35,24 @@
#include <QDir> #include <QDir>
TorrentContentModel::TorrentContentModel(QObject *parent): TorrentContentModel::TorrentContentModel(QObject *parent):
QAbstractItemModel(parent), files_index(0) QAbstractItemModel(parent), m_filesIndex(0),
m_rootItem(new TorrentContentModelItem(QList<QVariant>() << tr("Name") << tr("Size")
<< tr("Progress") << tr("Priority")))
{ {
QList<QVariant> rootData;
rootData << tr("Name") << tr("Size") << tr("Progress") << tr("Priority");
rootItem = new TorrentContentModelItem(rootData);
} }
TorrentContentModel::~TorrentContentModel() TorrentContentModel::~TorrentContentModel()
{ {
if (files_index) qDeleteAll(m_filesIndex);
delete [] files_index; delete m_rootItem;
delete rootItem;
} }
void TorrentContentModel::updateFilesProgress(const std::vector<libtorrent::size_type>& fp) void TorrentContentModel::updateFilesProgress(const std::vector<libtorrent::size_type>& fp)
{ {
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
for (unsigned int i=0; i<fp.size(); ++i) { Q_ASSERT(m_filesIndex.size() == fp.size());
Q_ASSERT(fp[i] >= 0); for (uint i=0; i<fp.size(); ++i) {
files_index[i]->setProgress(fp[i]); m_filesIndex[i]->setProgress(fp[i]);
} }
emit dataChanged(index(0,0), index(rowCount(), columnCount())); emit dataChanged(index(0,0), index(rowCount(), columnCount()));
} }
@ -62,9 +60,8 @@ void TorrentContentModel::updateFilesProgress(const std::vector<libtorrent::size
void TorrentContentModel::updateFilesPriorities(const std::vector<int> &fprio) void TorrentContentModel::updateFilesPriorities(const std::vector<int> &fprio)
{ {
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
for (unsigned int i=0; i<fprio.size(); ++i) { for (uint i=0; i<fprio.size(); ++i) {
//qDebug("Called updateFilesPriorities with %d", fprio[i]); m_filesIndex[i]->setPriority(fprio[i]);
files_index[i]->setPriority(fprio[i]);
} }
emit dataChanged(index(0,0), index(rowCount(), columnCount())); emit dataChanged(index(0,0), index(rowCount(), columnCount()));
} }
@ -72,17 +69,16 @@ void TorrentContentModel::updateFilesPriorities(const std::vector<int> &fprio)
std::vector<int> TorrentContentModel::getFilesPriorities(unsigned int nbFiles) const std::vector<int> TorrentContentModel::getFilesPriorities(unsigned int nbFiles) const
{ {
std::vector<int> prio; std::vector<int> prio;
for (unsigned int i=0; i<nbFiles; ++i) { for (uint i=0; i<nbFiles; ++i) {
//qDebug("Called getFilesPriorities: %d", files_index[i]->getPriority()); prio.push_back(m_filesIndex[i]->getPriority());
prio.push_back(files_index[i]->getPriority());
} }
return prio; return prio;
} }
bool TorrentContentModel::allFiltered() const bool TorrentContentModel::allFiltered() const
{ {
for (int i=0; i<rootItem->childCount(); ++i) { for (int i=0; i<m_rootItem->childCount(); ++i) {
if (rootItem->child(i)->getPriority() != prio::IGNORED) if (m_rootItem->child(i)->getPriority() != prio::IGNORED)
return false; return false;
} }
return true; return true;
@ -93,7 +89,7 @@ int TorrentContentModel::columnCount(const QModelIndex& parent) const
if (parent.isValid()) if (parent.isValid())
return static_cast<TorrentContentModelItem*>(parent.internalPointer())->columnCount(); return static_cast<TorrentContentModelItem*>(parent.internalPointer())->columnCount();
else else
return rootItem->columnCount(); return m_rootItem->columnCount();
} }
bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& value, int role) bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& value, int role)
@ -191,7 +187,7 @@ Qt::ItemFlags TorrentContentModel::flags(const QModelIndex& index) const
QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section); return m_rootItem->data(section);
return QVariant(); return QVariant();
} }
@ -207,7 +203,7 @@ QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex& p
TorrentContentModelItem *parentItem; TorrentContentModelItem *parentItem;
if (!parent.isValid()) if (!parent.isValid())
parentItem = rootItem; parentItem = m_rootItem;
else else
parentItem = static_cast<TorrentContentModelItem*>(parent.internalPointer()); parentItem = static_cast<TorrentContentModelItem*>(parent.internalPointer());
Q_ASSERT(parentItem); Q_ASSERT(parentItem);
@ -228,10 +224,11 @@ QModelIndex TorrentContentModel::parent(const QModelIndex& index) const
return QModelIndex(); return QModelIndex();
TorrentContentModelItem *childItem = static_cast<TorrentContentModelItem*>(index.internalPointer()); TorrentContentModelItem *childItem = static_cast<TorrentContentModelItem*>(index.internalPointer());
if (!childItem) return QModelIndex(); if (!childItem)
return QModelIndex();
TorrentContentModelItem *parentItem = childItem->parent(); TorrentContentModelItem *parentItem = childItem->parent();
if (parentItem == rootItem) if (parentItem == m_rootItem)
return QModelIndex(); return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem); return createIndex(parentItem->row(), 0, parentItem);
@ -245,7 +242,7 @@ int TorrentContentModel::rowCount(const QModelIndex& parent) const
return 0; return 0;
if (!parent.isValid()) if (!parent.isValid())
parentItem = rootItem; parentItem = m_rootItem;
else else
parentItem = static_cast<TorrentContentModelItem*>(parent.internalPointer()); parentItem = static_cast<TorrentContentModelItem*>(parent.internalPointer());
@ -256,11 +253,9 @@ void TorrentContentModel::clear()
{ {
qDebug("clear called"); qDebug("clear called");
beginResetModel(); beginResetModel();
if (files_index) { qDeleteAll(m_filesIndex);
delete [] files_index; m_filesIndex.clear();
files_index = 0; m_rootItem->deleteAllChildren();
}
rootItem->deleteAllChildren();
endResetModel(); endResetModel();
} }
@ -273,9 +268,9 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t)
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
// Initialize files_index array // Initialize files_index array
qDebug("Torrent contains %d files", t.num_files()); qDebug("Torrent contains %d files", t.num_files());
files_index = new TorrentContentModelItem*[t.num_files()]; m_filesIndex.reserve(t.num_files());
TorrentContentModelItem *parent = this->rootItem; TorrentContentModelItem *parent = m_rootItem;
TorrentContentModelItem *root_folder = parent; TorrentContentModelItem *root_folder = parent;
TorrentContentModelItem *current_parent; TorrentContentModelItem *current_parent;
@ -300,16 +295,15 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t)
current_parent = new_parent; current_parent = new_parent;
} }
// Actually create the file // Actually create the file
TorrentContentModelItem *f = new TorrentContentModelItem(t, fentry, current_parent, i); m_filesIndex.push_back(new TorrentContentModelItem(t, fentry, current_parent, i));
files_index[i] = f;
} }
emit layoutChanged(); emit layoutChanged();
} }
void TorrentContentModel::selectAll() void TorrentContentModel::selectAll()
{ {
for (int i=0; i<rootItem->childCount(); ++i) { for (int i=0; i<m_rootItem->childCount(); ++i) {
TorrentContentModelItem *child = rootItem->child(i); TorrentContentModelItem *child = m_rootItem->child(i);
if (child->getPriority() == prio::IGNORED) if (child->getPriority() == prio::IGNORED)
child->setPriority(prio::NORMAL); child->setPriority(prio::NORMAL);
} }
@ -318,8 +312,8 @@ void TorrentContentModel::selectAll()
void TorrentContentModel::selectNone() void TorrentContentModel::selectNone()
{ {
for (int i=0; i<rootItem->childCount(); ++i) { for (int i=0; i<m_rootItem->childCount(); ++i) {
rootItem->child(i)->setPriority(prio::IGNORED); m_rootItem->child(i)->setPriority(prio::IGNORED);
} }
emit dataChanged(index(0,0), index(rowCount(), columnCount())); emit dataChanged(index(0,0), index(rowCount(), columnCount()));
} }

9
src/torrentcontentmodel.h

@ -33,6 +33,7 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QModelIndex> #include <QModelIndex>
#include <QVector>
#include <QVariant> #include <QVariant>
#include <libtorrent/torrent_info.hpp> #include <libtorrent/torrent_info.hpp>
#include "torrentcontentmodelitem.h" #include "torrentcontentmodelitem.h"
@ -40,10 +41,6 @@
class TorrentContentModel: public QAbstractItemModel { class TorrentContentModel: public QAbstractItemModel {
Q_OBJECT Q_OBJECT
private:
TorrentContentModelItem *rootItem;
TorrentContentModelItem **files_index;
public: public:
TorrentContentModel(QObject *parent = 0); TorrentContentModel(QObject *parent = 0);
~TorrentContentModel(); ~TorrentContentModel();
@ -71,6 +68,10 @@ signals:
public slots: public slots:
void selectAll(); void selectAll();
void selectNone(); void selectNone();
private:
TorrentContentModelItem *m_rootItem;
QVector<TorrentContentModelItem *> m_filesIndex;
}; };
#endif // TORRENTCONTENTMODEL_H #endif // TORRENTCONTENTMODEL_H

Loading…
Cancel
Save