From 691b976e115f10a01ac9e4ab61edac543b385df1 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 25 Feb 2012 20:05:11 +0200 Subject: [PATCH] Clean up TorrentContentModel --- src/torrentcontentmodel.cpp | 68 +++++++++++++++++-------------------- src/torrentcontentmodel.h | 9 ++--- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/torrentcontentmodel.cpp b/src/torrentcontentmodel.cpp index 7224fab8d..eb86ab5ec 100644 --- a/src/torrentcontentmodel.cpp +++ b/src/torrentcontentmodel.cpp @@ -35,26 +35,24 @@ #include TorrentContentModel::TorrentContentModel(QObject *parent): - QAbstractItemModel(parent), files_index(0) + QAbstractItemModel(parent), m_filesIndex(0), + m_rootItem(new TorrentContentModelItem(QList() << tr("Name") << tr("Size") + << tr("Progress") << tr("Priority"))) { - QList rootData; - rootData << tr("Name") << tr("Size") << tr("Progress") << tr("Priority"); - rootItem = new TorrentContentModelItem(rootData); } TorrentContentModel::~TorrentContentModel() { - if (files_index) - delete [] files_index; - delete rootItem; + qDeleteAll(m_filesIndex); + delete m_rootItem; } void TorrentContentModel::updateFilesProgress(const std::vector& fp) { emit layoutAboutToBeChanged(); - for (unsigned int i=0; i= 0); - files_index[i]->setProgress(fp[i]); + Q_ASSERT(m_filesIndex.size() == fp.size()); + for (uint i=0; isetProgress(fp[i]); } emit dataChanged(index(0,0), index(rowCount(), columnCount())); } @@ -62,9 +60,8 @@ void TorrentContentModel::updateFilesProgress(const std::vector &fprio) { emit layoutAboutToBeChanged(); - for (unsigned int i=0; isetPriority(fprio[i]); + for (uint i=0; isetPriority(fprio[i]); } emit dataChanged(index(0,0), index(rowCount(), columnCount())); } @@ -72,17 +69,16 @@ void TorrentContentModel::updateFilesPriorities(const std::vector &fprio) std::vector TorrentContentModel::getFilesPriorities(unsigned int nbFiles) const { std::vector prio; - for (unsigned int i=0; igetPriority()); - prio.push_back(files_index[i]->getPriority()); + for (uint i=0; igetPriority()); } return prio; } bool TorrentContentModel::allFiltered() const { - for (int i=0; ichildCount(); ++i) { - if (rootItem->child(i)->getPriority() != prio::IGNORED) + for (int i=0; ichildCount(); ++i) { + if (m_rootItem->child(i)->getPriority() != prio::IGNORED) return false; } return true; @@ -93,7 +89,7 @@ int TorrentContentModel::columnCount(const QModelIndex& parent) const if (parent.isValid()) return static_cast(parent.internalPointer())->columnCount(); else - return rootItem->columnCount(); + return m_rootItem->columnCount(); } 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 { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) - return rootItem->data(section); + return m_rootItem->data(section); return QVariant(); } @@ -207,7 +203,7 @@ QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex& p TorrentContentModelItem *parentItem; if (!parent.isValid()) - parentItem = rootItem; + parentItem = m_rootItem; else parentItem = static_cast(parent.internalPointer()); Q_ASSERT(parentItem); @@ -228,10 +224,11 @@ QModelIndex TorrentContentModel::parent(const QModelIndex& index) const return QModelIndex(); TorrentContentModelItem *childItem = static_cast(index.internalPointer()); - if (!childItem) return QModelIndex(); + if (!childItem) + return QModelIndex(); TorrentContentModelItem *parentItem = childItem->parent(); - if (parentItem == rootItem) + if (parentItem == m_rootItem) return QModelIndex(); return createIndex(parentItem->row(), 0, parentItem); @@ -245,7 +242,7 @@ int TorrentContentModel::rowCount(const QModelIndex& parent) const return 0; if (!parent.isValid()) - parentItem = rootItem; + parentItem = m_rootItem; else parentItem = static_cast(parent.internalPointer()); @@ -256,11 +253,9 @@ void TorrentContentModel::clear() { qDebug("clear called"); beginResetModel(); - if (files_index) { - delete [] files_index; - files_index = 0; - } - rootItem->deleteAllChildren(); + qDeleteAll(m_filesIndex); + m_filesIndex.clear(); + m_rootItem->deleteAllChildren(); endResetModel(); } @@ -273,9 +268,9 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t) emit layoutAboutToBeChanged(); // Initialize files_index array 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 *current_parent; @@ -300,16 +295,15 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t) current_parent = new_parent; } // Actually create the file - TorrentContentModelItem *f = new TorrentContentModelItem(t, fentry, current_parent, i); - files_index[i] = f; + m_filesIndex.push_back(new TorrentContentModelItem(t, fentry, current_parent, i)); } emit layoutChanged(); } void TorrentContentModel::selectAll() { - for (int i=0; ichildCount(); ++i) { - TorrentContentModelItem *child = rootItem->child(i); + for (int i=0; ichildCount(); ++i) { + TorrentContentModelItem *child = m_rootItem->child(i); if (child->getPriority() == prio::IGNORED) child->setPriority(prio::NORMAL); } @@ -318,8 +312,8 @@ void TorrentContentModel::selectAll() void TorrentContentModel::selectNone() { - for (int i=0; ichildCount(); ++i) { - rootItem->child(i)->setPriority(prio::IGNORED); + for (int i=0; ichildCount(); ++i) { + m_rootItem->child(i)->setPriority(prio::IGNORED); } emit dataChanged(index(0,0), index(rowCount(), columnCount())); } diff --git a/src/torrentcontentmodel.h b/src/torrentcontentmodel.h index 425e58d0b..9f3988dcf 100644 --- a/src/torrentcontentmodel.h +++ b/src/torrentcontentmodel.h @@ -33,6 +33,7 @@ #include #include +#include #include #include #include "torrentcontentmodelitem.h" @@ -40,10 +41,6 @@ class TorrentContentModel: public QAbstractItemModel { Q_OBJECT -private: - TorrentContentModelItem *rootItem; - TorrentContentModelItem **files_index; - public: TorrentContentModel(QObject *parent = 0); ~TorrentContentModel(); @@ -71,6 +68,10 @@ signals: public slots: void selectAll(); void selectNone(); + +private: + TorrentContentModelItem *m_rootItem; + QVector m_filesIndex; }; #endif // TORRENTCONTENTMODEL_H