Browse Source

Follow project coding style. Issue #2192.

adaptive-webui-19844
Chocobo1 9 years ago
parent
commit
e330a91921
  1. 390
      src/gui/torrentcontentmodel.cpp

390
src/gui/torrentcontentmodel.cpp

@ -39,299 +39,297 @@
#include "torrentcontentmodelfolder.h" #include "torrentcontentmodelfolder.h"
#include "torrentcontentmodelfile.h" #include "torrentcontentmodelfile.h"
namespace { namespace
QIcon get_directory_icon() { {
static QIcon cached = GuiIconProvider::instance()->getIcon("inode-directory"); QIcon get_directory_icon()
return cached; {
} static QIcon cached = GuiIconProvider::instance()->getIcon("inode-directory");
return cached;
}
QIcon get_file_icon() { QIcon get_file_icon()
static QIcon cached = GuiIconProvider::instance()->getIcon("text-plain"); {
return cached; static QIcon cached = GuiIconProvider::instance()->getIcon("text-plain");
} return cached;
}
} }
TorrentContentModel::TorrentContentModel(QObject *parent): TorrentContentModel::TorrentContentModel(QObject *parent)
QAbstractItemModel(parent), : QAbstractItemModel(parent)
m_rootItem(new TorrentContentModelFolder(QList<QVariant>() << tr("Name") << tr("Size") , m_rootItem(new TorrentContentModelFolder(QList<QVariant>() << tr("Name") << tr("Size")
<< tr("Progress") << tr("Download Priority"))) << tr("Progress") << tr("Download Priority")))
{ {
} }
TorrentContentModel::~TorrentContentModel() TorrentContentModel::~TorrentContentModel()
{ {
delete m_rootItem; delete m_rootItem;
} }
void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp) void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp)
{ {
Q_ASSERT(m_filesIndex.size() == fp.size()); Q_ASSERT(m_filesIndex.size() == fp.size());
// XXX: Why is this necessary? // XXX: Why is this necessary?
if (m_filesIndex.size() != fp.size()) return; if (m_filesIndex.size() != fp.size()) return;
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
for (int i = 0; i < fp.size(); ++i) { for (int i = 0; i < fp.size(); ++i)
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(); emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
emit dataChanged(index(0,0), index(rowCount(), columnCount()));
} }
void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio) void TorrentContentModel::updateFilesPriorities(const QVector<int> &fprio)
{ {
Q_ASSERT(m_filesIndex.size() == (int)fprio.size()); Q_ASSERT(m_filesIndex.size() == (int)fprio.size());
// XXX: Why is this necessary? // XXX: Why is this necessary?
if (m_filesIndex.size() != (int)fprio.size()) if (m_filesIndex.size() != (int)fprio.size())
return; return;
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
for (int i = 0; i < fprio.size(); ++i) { for (int i = 0; i < fprio.size(); ++i)
m_filesIndex[i]->setPriority(fprio[i]); m_filesIndex[i]->setPriority(fprio[i]);
} emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
} }
QVector<int> TorrentContentModel::getFilePriorities() const QVector<int> TorrentContentModel::getFilePriorities() const
{ {
QVector<int> prio; QVector<int> prio;
prio.reserve(m_filesIndex.size()); prio.reserve(m_filesIndex.size());
foreach (const TorrentContentModelFile* file, m_filesIndex) { foreach (const TorrentContentModelFile* file, m_filesIndex)
prio.push_back(file->priority()); prio.push_back(file->priority());
} return prio;
return prio;
} }
bool TorrentContentModel::allFiltered() const bool TorrentContentModel::allFiltered() const
{ {
foreach (const TorrentContentModelFile* fileItem, m_filesIndex) { foreach (const TorrentContentModelFile* fileItem, m_filesIndex)
if (fileItem->priority() != prio::IGNORED) if (fileItem->priority() != prio::IGNORED)
return false; return false;
} return true;
return true;
} }
int TorrentContentModel::columnCount(const QModelIndex& parent) const 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 m_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)
{ {
if (!index.isValid()) if (!index.isValid())
return false; return false;
if (index.column() == 0 && role == Qt::CheckStateRole) { if ((index.column() == 0) && (role == Qt::CheckStateRole)) {
TorrentContentModelItem *item = static_cast<TorrentContentModelItem*>(index.internalPointer()); TorrentContentModelItem *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
qDebug("setData(%s, %d", qPrintable(item->name()), value.toInt()); qDebug("setData(%s, %d", qPrintable(item->name()), value.toInt());
if (item->priority() != value.toInt()) { if (item->priority() != value.toInt()) {
if (value.toInt() == Qt::PartiallyChecked) if (value.toInt() == Qt::PartiallyChecked)
item->setPriority(prio::MIXED); item->setPriority(prio::MIXED);
else if (value.toInt() == Qt::Unchecked) else if (value.toInt() == Qt::Unchecked)
item->setPriority(prio::IGNORED); item->setPriority(prio::IGNORED);
else else
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();
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();
}
return true;
} }
return true;
}
if (role == Qt::EditRole) { if (role == Qt::EditRole) {
Q_ASSERT(index.isValid()); Q_ASSERT(index.isValid());
TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer()); TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer());
switch(index.column()) { switch (index.column()) {
case TorrentContentModelItem::COL_NAME: case TorrentContentModelItem::COL_NAME:
item->setName(value.toString()); item->setName(value.toString());
break; break;
case TorrentContentModelItem::COL_PRIO: case TorrentContentModelItem::COL_PRIO:
item->setPriority(value.toInt()); item->setPriority(value.toInt());
break; break;
default: default:
return false; return false;
}
emit dataChanged(index, index);
return true;
} }
emit dataChanged(index, index);
return true;
}
return false; return false;
} }
TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex& index) const TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex& index) const
{ {
return static_cast<const TorrentContentModelItem*>(index.internalPointer())->itemType(); return static_cast<const TorrentContentModelItem*>(index.internalPointer())->itemType();
} }
int TorrentContentModel::getFileIndex(const QModelIndex& index) int TorrentContentModel::getFileIndex(const QModelIndex& index)
{ {
TorrentContentModelFile* item = dynamic_cast<TorrentContentModelFile*>(static_cast<TorrentContentModelItem*>(index.internalPointer())); TorrentContentModelFile* item = dynamic_cast<TorrentContentModelFile*>(static_cast<TorrentContentModelItem*>(index.internalPointer()));
Q_ASSERT(item); Q_ASSERT(item);
return item->fileIndex(); return item->fileIndex();
} }
QVariant TorrentContentModel::data(const QModelIndex& index, int role) const QVariant TorrentContentModel::data(const QModelIndex& index, int role) const
{ {
if (!index.isValid()) if (!index.isValid())
return QVariant(); return QVariant();
TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer()); TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer());
if (index.column() == 0 && role == Qt::DecorationRole) { if ((index.column() == 0) && (role == Qt::DecorationRole)) {
if (item->itemType() == TorrentContentModelItem::FolderType) if (item->itemType() == TorrentContentModelItem::FolderType)
return get_directory_icon(); return get_directory_icon();
else else
return get_file_icon(); return get_file_icon();
} }
if (index.column() == 0 && role == Qt::CheckStateRole) { if ((index.column() == 0) && (role == Qt::CheckStateRole)) {
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::IGNORED) if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::IGNORED)
return Qt::Unchecked; return Qt::Unchecked;
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::MIXED) if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::MIXED)
return Qt::PartiallyChecked; return Qt::PartiallyChecked;
return Qt::Checked; return Qt::Checked;
} }
if (role != Qt::DisplayRole) if (role != Qt::DisplayRole)
return QVariant(); return QVariant();
return item->data(index.column()); return item->data(index.column());
} }
Qt::ItemFlags TorrentContentModel::flags(const QModelIndex& index) const Qt::ItemFlags TorrentContentModel::flags(const QModelIndex& index) const
{ {
if (!index.isValid()) if (!index.isValid())
return 0; return 0;
if (itemType(index) == TorrentContentModelItem::FolderType) if (itemType(index) == TorrentContentModelItem::FolderType)
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsTristate; return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
} }
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 m_rootItem->data(section); return m_rootItem->data(section);
return QVariant(); return QVariant();
} }
QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex& parent) const QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex& parent) const
{ {
if (parent.isValid() && parent.column() != 0) if (parent.isValid() && (parent.column() != 0))
return QModelIndex(); return QModelIndex();
if (column >= TorrentContentModelItem::NB_COL) if (column >= TorrentContentModelItem::NB_COL)
return QModelIndex(); return QModelIndex();
TorrentContentModelFolder* parentItem; TorrentContentModelFolder* parentItem;
if (!parent.isValid()) if (!parent.isValid())
parentItem = m_rootItem; parentItem = m_rootItem;
else else
parentItem = static_cast<TorrentContentModelFolder*>(parent.internalPointer()); parentItem = static_cast<TorrentContentModelFolder*>(parent.internalPointer());
Q_ASSERT(parentItem); Q_ASSERT(parentItem);
if (row >= parentItem->childCount()) if (row >= parentItem->childCount())
return QModelIndex(); return QModelIndex();
TorrentContentModelItem* childItem = parentItem->child(row); TorrentContentModelItem* childItem = parentItem->child(row);
if (childItem) if (childItem)
return createIndex(row, column, childItem); return createIndex(row, column, childItem);
return QModelIndex(); return QModelIndex();
} }
QModelIndex TorrentContentModel::parent(const QModelIndex& index) const QModelIndex TorrentContentModel::parent(const QModelIndex& index) const
{ {
if (!index.isValid()) if (!index.isValid())
return QModelIndex(); return QModelIndex();
TorrentContentModelItem* childItem = static_cast<TorrentContentModelItem*>(index.internalPointer()); TorrentContentModelItem* childItem = static_cast<TorrentContentModelItem*>(index.internalPointer());
if (!childItem) if (!childItem)
return QModelIndex(); return QModelIndex();
TorrentContentModelItem *parentItem = childItem->parent(); TorrentContentModelItem *parentItem = childItem->parent();
if (parentItem == m_rootItem) if (parentItem == m_rootItem)
return QModelIndex(); return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem); return createIndex(parentItem->row(), 0, parentItem);
} }
int TorrentContentModel::rowCount(const QModelIndex& parent) const int TorrentContentModel::rowCount(const QModelIndex& parent) const
{ {
if (parent.column() > 0) if (parent.column() > 0)
return 0; return 0;
TorrentContentModelFolder* parentItem; TorrentContentModelFolder* parentItem;
if (!parent.isValid()) if (!parent.isValid())
parentItem = m_rootItem; parentItem = m_rootItem;
else else
parentItem = dynamic_cast<TorrentContentModelFolder*>(static_cast<TorrentContentModelItem*>(parent.internalPointer())); parentItem = dynamic_cast<TorrentContentModelFolder*>(static_cast<TorrentContentModelItem*>(parent.internalPointer()));
return parentItem ? parentItem->childCount() : 0; return parentItem ? parentItem->childCount() : 0;
} }
void TorrentContentModel::clear() void TorrentContentModel::clear()
{ {
qDebug("clear called"); qDebug("clear called");
beginResetModel(); beginResetModel();
m_filesIndex.clear(); m_filesIndex.clear();
m_rootItem->deleteAllChildren(); m_rootItem->deleteAllChildren();
endResetModel(); endResetModel();
} }
void TorrentContentModel::setupModelData(const BitTorrent::TorrentInfo &info) void TorrentContentModel::setupModelData(const BitTorrent::TorrentInfo &info)
{ {
qDebug("setup model data called"); qDebug("setup model data called");
if (info.filesCount() == 0) if (info.filesCount() == 0)
return; return;
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
// Initialize files_index array // Initialize files_index array
qDebug("Torrent contains %d files", info.filesCount()); qDebug("Torrent contains %d files", info.filesCount());
m_filesIndex.reserve(info.filesCount()); m_filesIndex.reserve(info.filesCount());
TorrentContentModelFolder* current_parent; TorrentContentModelFolder* current_parent;
// Iterate over files // Iterate over files
for (int i = 0; i < info.filesCount(); ++i) { for (int i = 0; i < info.filesCount(); ++i) {
current_parent = m_rootItem; current_parent = m_rootItem;
QString path = Utils::Fs::fromNativePath(info.filePath(i)); QString path = Utils::Fs::fromNativePath(info.filePath(i));
// Iterate of parts of the path to create necessary folders // Iterate of parts of the path to create necessary folders
QStringList pathFolders = path.split("/", QString::SkipEmptyParts); QStringList pathFolders = path.split("/", QString::SkipEmptyParts);
pathFolders.removeLast(); pathFolders.removeLast();
foreach (const QString& pathPart, pathFolders) { foreach (const QString& pathPart, pathFolders) {
if (pathPart == ".unwanted") if (pathPart == ".unwanted")
continue; continue;
TorrentContentModelFolder* new_parent = current_parent->childFolderWithName(pathPart); TorrentContentModelFolder* new_parent = current_parent->childFolderWithName(pathPart);
if (!new_parent) { if (!new_parent) {
new_parent = new TorrentContentModelFolder(pathPart, current_parent); new_parent = new TorrentContentModelFolder(pathPart, current_parent);
current_parent->appendChild(new_parent); current_parent->appendChild(new_parent);
} }
current_parent = new_parent; current_parent = new_parent;
}
// Actually create the file
TorrentContentModelFile* fileItem = new TorrentContentModelFile(info.fileName(i), info.fileSize(i), current_parent, i);
current_parent->appendChild(fileItem);
m_filesIndex.push_back(fileItem);
} }
// Actually create the file emit layoutChanged();
TorrentContentModelFile* fileItem = new TorrentContentModelFile(info.fileName(i), info.fileSize(i), current_parent, i);
current_parent->appendChild(fileItem);
m_filesIndex.push_back(fileItem);
}
emit layoutChanged();
} }
void TorrentContentModel::selectAll() void TorrentContentModel::selectAll()
{ {
for (int i=0; i<m_rootItem->childCount(); ++i) { for (int i = 0; i < m_rootItem->childCount(); ++i) {
TorrentContentModelItem* child = m_rootItem->child(i); TorrentContentModelItem* child = m_rootItem->child(i);
if (child->priority() == prio::IGNORED) if (child->priority() == prio::IGNORED)
child->setPriority(prio::NORMAL); child->setPriority(prio::NORMAL);
} }
emit dataChanged(index(0, 0), index(rowCount(), columnCount())); emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
} }
void TorrentContentModel::selectNone() void TorrentContentModel::selectNone()
{ {
for (int i=0; i<m_rootItem->childCount(); ++i) { for (int i = 0; i < m_rootItem->childCount(); ++i)
m_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()));
} }

Loading…
Cancel
Save