From 30be83d445aec900973e828e2fdb57f3c1223ab9 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 26 Aug 2012 15:00:37 +0300 Subject: [PATCH] Optimizations to TorrentContentModel First step to address issue #24. --- src/properties/propertieswidget.cpp | 11 +++----- src/torrentcontentmodel.cpp | 41 +++++++++++++++-------------- src/torrentcontentmodel.h | 4 +-- src/torrentcontentmodelitem.cpp | 12 ++++----- src/torrentcontentmodelitem.h | 6 ++--- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index 5eda693c9..7c311cb92 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -234,13 +234,12 @@ void PropertiesWidget::updateTorrentInfos(const QTorrentHandle& _h) { } } -void PropertiesWidget::loadTorrentInfos(const QTorrentHandle &_h) { +void PropertiesWidget::loadTorrentInfos(const QTorrentHandle& _h) +{ clear(); h = _h; - if (!h.is_valid()) { - clear(); + if (!h.is_valid()) return; - } try { // Save path @@ -261,9 +260,7 @@ void PropertiesWidget::loadTorrentInfos(const QTorrentHandle &_h) { PropListModel->model()->setupModelData(h.get_torrent_info()); filesList->setExpanded(PropListModel->index(0, 0), true); } - } catch(invalid_handle& e) { - - } + } catch(const invalid_handle& e) { } // Load dynamic data loadDynamicData(); } diff --git a/src/torrentcontentmodel.cpp b/src/torrentcontentmodel.cpp index 5654edceb..65768c3da 100644 --- a/src/torrentcontentmodel.cpp +++ b/src/torrentcontentmodel.cpp @@ -57,7 +57,7 @@ void TorrentContentModel::updateFilesProgress(const std::vector &fprio) +void TorrentContentModel::updateFilesPriorities(const std::vector& fprio) { emit layoutAboutToBeChanged(); Q_ASSERT(m_filesIndex.size() == (int)fprio.size()); @@ -71,8 +71,9 @@ void TorrentContentModel::updateFilesPriorities(const std::vector &fprio) std::vector TorrentContentModel::getFilesPriorities() const { std::vector prio; - for (int i=0; igetPriority()); + prio.reserve(m_filesIndex.size()); + foreach (const TorrentContentModelItem* file, m_filesIndex) { + prio.push_back(file->getPriority()); } return prio; } @@ -147,7 +148,7 @@ TorrentContentModelItem::FileType TorrentContentModel::getType(const QModelIndex int TorrentContentModel::getFileIndex(const QModelIndex& index) { - TorrentContentModelItem *item = static_cast(index.internalPointer()); + TorrentContentModelItem* item = static_cast(index.internalPointer()); return item->getFileIndex(); } @@ -238,7 +239,7 @@ QModelIndex TorrentContentModel::parent(const QModelIndex& index) const int TorrentContentModel::rowCount(const QModelIndex& parent) const { - TorrentContentModelItem *parentItem; + TorrentContentModelItem* parentItem; if (parent.column() > 0) return 0; @@ -260,7 +261,7 @@ void TorrentContentModel::clear() endResetModel(); } -void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t) +void TorrentContentModel::setupModelData(const libtorrent::torrent_info& t) { qDebug("setup model data called"); if (t.num_files() == 0) @@ -271,28 +272,28 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t) qDebug("Torrent contains %d files", t.num_files()); m_filesIndex.reserve(t.num_files()); - TorrentContentModelItem *parent = m_rootItem; - TorrentContentModelItem *root_folder = parent; - TorrentContentModelItem *current_parent; + TorrentContentModelItem* parent = m_rootItem; + TorrentContentModelItem* root_folder = parent; + TorrentContentModelItem* current_parent; // Iterate over files for (int i=0; i= 16 - QString path = QDir::cleanPath(misc::toQStringU(fentry.path)).replace("\\", "/"); + QString path = misc::toQStringU(fentry.path); #else - QString path = QDir::cleanPath(misc::toQStringU(fentry.path.string())).replace("\\", "/"); + QString path = misc::toQStringU(fentry.path.string()); #endif // Iterate of parts of the path to create necessary folders - QStringList pathFolders = path.split("/"); - pathFolders.removeAll(".unwanted"); - pathFolders.takeLast(); - foreach (const QString &pathPart, pathFolders) { - TorrentContentModelItem *new_parent = current_parent->childWithName(pathPart); - if (!new_parent) { + QStringList pathFolders = path.split(QRegExp("[/\\\\]"), QString::SkipEmptyParts); + pathFolders.removeLast(); + foreach (const QString& pathPart, pathFolders) { + if (pathPart == ".unwanted") + continue; + TorrentContentModelItem* new_parent = current_parent->childWithName(pathPart); + if (!new_parent) new_parent = new TorrentContentModelItem(pathPart, current_parent); - } current_parent = new_parent; } // Actually create the file @@ -304,7 +305,7 @@ void TorrentContentModel::setupModelData(const libtorrent::torrent_info &t) void TorrentContentModel::selectAll() { for (int i=0; ichildCount(); ++i) { - TorrentContentModelItem *child = m_rootItem->child(i); + TorrentContentModelItem* child = m_rootItem->child(i); if (child->getPriority() == prio::IGNORED) child->setPriority(prio::NORMAL); } diff --git a/src/torrentcontentmodel.h b/src/torrentcontentmodel.h index 6651ab758..72773abec 100644 --- a/src/torrentcontentmodel.h +++ b/src/torrentcontentmodel.h @@ -70,8 +70,8 @@ public slots: void selectNone(); private: - TorrentContentModelItem *m_rootItem; - QVector m_filesIndex; + TorrentContentModelItem* m_rootItem; + QVector m_filesIndex; }; #endif // TORRENTCONTENTMODEL_H diff --git a/src/torrentcontentmodelitem.cpp b/src/torrentcontentmodelitem.cpp index 934320aa1..05a0de560 100644 --- a/src/torrentcontentmodelitem.cpp +++ b/src/torrentcontentmodelitem.cpp @@ -252,15 +252,15 @@ void TorrentContentModelItem::updatePriority() return; } } - // All child items have the same priorrity - // Update mine if necessary + // 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) { + foreach (TorrentContentModelItem* child, m_childItems) { if (child->getName() == name) return child; } @@ -272,7 +272,7 @@ bool TorrentContentModelItem::isFolder() const return (m_type==FOLDER); } -void TorrentContentModelItem::appendChild(TorrentContentModelItem *item) +void TorrentContentModelItem::appendChild(TorrentContentModelItem* item) { Q_ASSERT(item); Q_ASSERT(m_type != TFILE); @@ -285,7 +285,7 @@ void TorrentContentModelItem::appendChild(TorrentContentModelItem *item) m_childItems.insert(i, item); } -TorrentContentModelItem* TorrentContentModelItem::child(int row) +TorrentContentModelItem* TorrentContentModelItem::child(int row) const { //Q_ASSERT(row >= 0 && row < childItems.size()); return m_childItems.value(row, 0); @@ -315,7 +315,7 @@ int TorrentContentModelItem::row() const return 0; } -TorrentContentModelItem* TorrentContentModelItem::parent() +TorrentContentModelItem* TorrentContentModelItem::parent() const { return m_parentItem; } diff --git a/src/torrentcontentmodelitem.h b/src/torrentcontentmodelitem.h index fdb490d84..c0a57da8f 100644 --- a/src/torrentcontentmodelitem.h +++ b/src/torrentcontentmodelitem.h @@ -80,18 +80,18 @@ public: bool isFolder() const; void appendChild(TorrentContentModelItem *item); - TorrentContentModelItem *child(int row); + TorrentContentModelItem* child(int row) const; int childCount() const; int columnCount() const; QVariant data(int column) const; int row() const; - TorrentContentModelItem *parent(); + TorrentContentModelItem* parent() const; void deleteAllChildren(); const QList& children() const; private: - TorrentContentModelItem *m_parentItem; + TorrentContentModelItem* m_parentItem; FileType m_type; QList m_childItems; QList m_itemData;