Skip to content

Commit

Permalink
Follow project coding style. Issue #2192.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeule authored and sledgehammer999 committed May 30, 2017
1 parent 2fa2d36 commit 1000298
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 222 deletions.
51 changes: 26 additions & 25 deletions src/gui/torrentcontentmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,41 @@

class TorrentContentModelFile;

class TorrentContentModel: public QAbstractItemModel {
Q_OBJECT
class TorrentContentModel: public QAbstractItemModel
{
Q_OBJECT

public:
TorrentContentModel(QObject *parent = 0);
~TorrentContentModel();
TorrentContentModel(QObject *parent = 0);
~TorrentContentModel();

void updateFilesProgress(const QVector<qreal> &fp);
void updateFilesPriorities(const QVector<int> &fprio);
QVector<int> getFilePriorities() const;
bool allFiltered() const;
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const;
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
TorrentContentModelItem::ItemType itemType(const QModelIndex& index) const;
int getFileIndex(const QModelIndex& index);
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual Qt::ItemFlags flags(const QModelIndex& index) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
virtual QModelIndex parent(const QModelIndex& index) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
void clear();
void setupModelData(const BitTorrent::TorrentInfo &info);
void updateFilesProgress(const QVector<qreal> &fp);
void updateFilesPriorities(const QVector<int> &fprio);
QVector<int> getFilePriorities() const;
bool allFiltered() const;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
TorrentContentModelItem::ItemType itemType(const QModelIndex& index) const;
int getFileIndex(const QModelIndex& index);
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual Qt::ItemFlags flags(const QModelIndex& index) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
virtual QModelIndex parent(const QModelIndex& index) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
void clear();
void setupModelData(const BitTorrent::TorrentInfo &info);

signals:
void filteredFilesChanged();
void filteredFilesChanged();

public slots:
void selectAll();
void selectNone();
void selectAll();
void selectNone();

private:
TorrentContentModelFolder* m_rootItem;
QVector<TorrentContentModelFile*> m_filesIndex;
TorrentContentModelFolder *m_rootItem;
QVector<TorrentContentModelFile *> m_filesIndex;
};

#endif // TORRENTCONTENTMODEL_H
47 changes: 26 additions & 21 deletions src/gui/torrentcontentmodelfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,48 @@
#include "torrentcontentmodelfolder.h"

TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize,
TorrentContentModelFolder* parent, int file_index)
: TorrentContentModelItem(parent)
, m_fileIndex(file_index)
TorrentContentModelFolder *parent, int fileIndex)
: TorrentContentModelItem(parent)
, m_fileIndex(fileIndex)
{
Q_ASSERT(parent);
Q_ASSERT(parent);

m_name = fileName;
m_name = fileName;

// Do not display incomplete extensions
if (m_name.endsWith(".!qB"))
m_name.chop(4);
// Do not display incomplete extensions
if (m_name.endsWith(".!qB"))
m_name.chop(4);

m_size = fileSize;
m_size = fileSize;
}

int TorrentContentModelFile::fileIndex() const
{
return m_fileIndex;
return m_fileIndex;
}

void TorrentContentModelFile::setPriority(int new_prio, bool update_parent)
void TorrentContentModelFile::setPriority(int newPriority, bool updateParent)
{
Q_ASSERT(new_prio != prio::MIXED);
Q_ASSERT(newPriority != prio::MIXED);

if (m_priority == new_prio)
return;
if (m_priority == newPriority)
return;

m_priority = new_prio;
m_priority = newPriority;

// Update parent
if (update_parent)
m_parentItem->updatePriority();
// Update parent
if (updateParent)
m_parentItem->updatePriority();
}

void TorrentContentModelFile::setProgress(qreal progress)
{
m_progress = progress;
m_remaining = (qulonglong)(m_size * (1.0 - m_progress));
Q_ASSERT(m_progress <= 1.);
m_progress = progress;
m_remaining = static_cast<qulonglong>(m_size * (1.0 - m_progress));
Q_ASSERT(m_progress <= 1.);
}

TorrentContentModelItem::ItemType TorrentContentModelFile::itemType() const
{
return FileType;
}
16 changes: 8 additions & 8 deletions src/gui/torrentcontentmodelfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@

#include "torrentcontentmodelitem.h"

class TorrentContentModelFile : public TorrentContentModelItem
class TorrentContentModelFile: public TorrentContentModelItem
{
public:
TorrentContentModelFile(const QString &fileName, qulonglong fileSize,
TorrentContentModelFolder* parent, int file_index);
TorrentContentModelFile(const QString &fileName, qulonglong fileSize,
TorrentContentModelFolder *parent, int fileIndex);

int fileIndex() const;
void setPriority(int new_prio, bool update_parent = true);
void setProgress(qreal progress);
ItemType itemType() const { return FileType; }
int fileIndex() const;
void setPriority(int newPriority, bool updateParent = true) override;
void setProgress(qreal progress);
ItemType itemType() const override;

private:
int m_fileIndex;
int m_fileIndex;
};

#endif // TORRENTCONTENTMODELFILE_H
165 changes: 84 additions & 81 deletions src/gui/torrentcontentmodelfolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,136 +31,139 @@
#include <QDebug>
#include "torrentcontentmodelfolder.h"

TorrentContentModelFolder::TorrentContentModelFolder(const QString& name, TorrentContentModelFolder* parent)
: TorrentContentModelItem(parent)
TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent)
: TorrentContentModelItem(parent)
{
Q_ASSERT(parent);
m_name = name;
// Do not display incomplete extensions
if (m_name.endsWith(".!qB"))
m_name.chop(4);
Q_ASSERT(parent);
m_name = name;
// Do not display incomplete extensions
if (m_name.endsWith(".!qB"))
m_name.chop(4);
}

TorrentContentModelFolder::TorrentContentModelFolder(const QList<QVariant>& data)
: TorrentContentModelItem(0)
TorrentContentModelFolder::TorrentContentModelFolder(const QList<QVariant> &data)
: TorrentContentModelItem(0)
{
Q_ASSERT(data.size() == NB_COL);
m_itemData = data;
Q_ASSERT(data.size() == NB_COL);
m_itemData = data;
}

TorrentContentModelFolder::~TorrentContentModelFolder()
{
qDeleteAll(m_childItems);
qDeleteAll(m_childItems);
}

TorrentContentModelItem::ItemType TorrentContentModelFolder::itemType() const
{
return FolderType;
}

void TorrentContentModelFolder::deleteAllChildren()
{
Q_ASSERT(isRootItem());
qDeleteAll(m_childItems);
m_childItems.clear();
Q_ASSERT(isRootItem());
qDeleteAll(m_childItems);
m_childItems.clear();
}

const QList<TorrentContentModelItem*>& TorrentContentModelFolder::children() const
const QList<TorrentContentModelItem *> &TorrentContentModelFolder::children() const
{
return m_childItems;
return m_childItems;
}

void TorrentContentModelFolder::appendChild(TorrentContentModelItem* item)
void TorrentContentModelFolder::appendChild(TorrentContentModelItem *item)
{
Q_ASSERT(item);
m_childItems.append(item);
// Update own size
if (item->itemType() == FileType)
increaseSize(item->size());
Q_ASSERT(item);
m_childItems.append(item);
// Update own size
if (item->itemType() == FileType)
increaseSize(item->size());
}

TorrentContentModelItem* TorrentContentModelFolder::child(int row) const
TorrentContentModelItem *TorrentContentModelFolder::child(int row) const
{
return m_childItems.value(row, 0);
return m_childItems.value(row, 0);
}

TorrentContentModelFolder* TorrentContentModelFolder::childFolderWithName(const QString& name) const
TorrentContentModelFolder *TorrentContentModelFolder::childFolderWithName(const QString &name) const
{
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->itemType() == FolderType && child->name() == name)
return static_cast<TorrentContentModelFolder*>(child);
}
return 0;
foreach (TorrentContentModelItem *child, m_childItems)
if ((child->itemType() == FolderType) && (child->name() == name))
return static_cast<TorrentContentModelFolder *>(child);
return 0;
}

int TorrentContentModelFolder::childCount() const
{
return m_childItems.count();
return m_childItems.count();
}

// Only non-root folders use this function
void TorrentContentModelFolder::updatePriority()
{
if (isRootItem())
return;

Q_ASSERT(!m_childItems.isEmpty());

// If all children have the same priority
// then the folder should have the same
// priority
const int prio = m_childItems.first()->priority();
for (int i=1; i<m_childItems.size(); ++i) {
if (m_childItems.at(i)->priority() != prio) {
setPriority(prio::MIXED);
return;
if (isRootItem())
return;

Q_ASSERT(!m_childItems.isEmpty());

// If all children have the same priority
// then the folder should have the same
// priority
const int prio = m_childItems.first()->priority();
for (int i = 1; i < m_childItems.size(); ++i) {
if (m_childItems.at(i)->priority() != prio) {
setPriority(prio::MIXED);
return;
}
}
}
// All child items have the same priority
// Update own if necessary
setPriority(prio);
// All child items have the same priority
// Update own if necessary
setPriority(prio);
}

void TorrentContentModelFolder::setPriority(int new_prio, bool update_parent)
void TorrentContentModelFolder::setPriority(int newPriority, bool updateParent)
{
if (m_priority == new_prio)
return;
if (m_priority == newPriority)
return;

m_priority = new_prio;
m_priority = newPriority;

// Update parent priority
if (update_parent)
m_parentItem->updatePriority();
// Update parent priority
if (updateParent)
m_parentItem->updatePriority();

// Update children
if (m_priority != prio::MIXED) {
foreach (TorrentContentModelItem* child, m_childItems)
child->setPriority(m_priority, false);
}
// Update children
if (m_priority != prio::MIXED)
foreach (TorrentContentModelItem *child, m_childItems)
child->setPriority(m_priority, false);
}

void TorrentContentModelFolder::recalculateProgress()
{
qreal tProgress = 0;
qulonglong tSize = 0;
qulonglong tRemaining = 0;
foreach (TorrentContentModelItem* child, m_childItems) {
if (child->priority() != prio::IGNORED) {
if (child->itemType() == FolderType)
static_cast<TorrentContentModelFolder*>(child)->recalculateProgress();
tProgress += child->progress() * child->size();
tSize += child->size();
tRemaining += child->remaining();
qreal tProgress = 0;
qulonglong tSize = 0;
qulonglong tRemaining = 0;
foreach (TorrentContentModelItem *child, m_childItems) {
if (child->priority() != prio::IGNORED) {
if (child->itemType() == FolderType)
static_cast<TorrentContentModelFolder *>(child)->recalculateProgress();
tProgress += child->progress() * child->size();
tSize += child->size();
tRemaining += child->remaining();
}
}
}

if (!isRootItem() && tSize > 0) {
m_progress = tProgress / tSize;
m_remaining = tRemaining;
Q_ASSERT(m_progress <= 1.);
}
if (!isRootItem() && (tSize > 0)) {
m_progress = tProgress / tSize;
m_remaining = tRemaining;
Q_ASSERT(m_progress <= 1.);
}
}

void TorrentContentModelFolder::increaseSize(qulonglong delta)
{
if (isRootItem())
return;
if (isRootItem())
return;

m_size += delta;
m_parentItem->increaseSize(delta);
m_size += delta;
m_parentItem->increaseSize(delta);
}
Loading

0 comments on commit 1000298

Please sign in to comment.