1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-03 13:35:36 +00:00

refactor: make torrents' mime icons lazy-loaded. (#1433)

* refactor: make torrents' mime icons lazy-loaded.

Another "small wins" patch, this time on CPU: icon loading is expensive,
so calculating them for every torrent all at once on startup isn't the
smartest way to do it.
This commit is contained in:
Charles Kerr 2020-09-08 11:51:33 -05:00 committed by GitHub
parent 138ee3d7a0
commit 542feef77d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 44 deletions

View file

@ -152,14 +152,12 @@ QModelIndexList FileTreeModel::getOrphanIndices(QModelIndexList const& indices)
QVariant FileTreeModel::data(QModelIndex const& index, int role) const QVariant FileTreeModel::data(QModelIndex const& index, int role) const
{ {
QVariant value;
if (index.isValid()) if (index.isValid())
{ {
value = itemFromIndex(index)->data(index.column(), role); return itemFromIndex(index)->data(index.column(), role);
} }
return value; return {};
} }
Qt::ItemFlags FileTreeModel::flags(QModelIndex const& index) const Qt::ItemFlags FileTreeModel::flags(QModelIndex const& index) const
@ -193,30 +191,28 @@ bool FileTreeModel::setData(QModelIndex const& index, QVariant const& newname, i
QVariant FileTreeModel::headerData(int column, Qt::Orientation orientation, int role) const QVariant FileTreeModel::headerData(int column, Qt::Orientation orientation, int role) const
{ {
QVariant data;
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{ {
switch (column) switch (column)
{ {
case COL_NAME: case COL_NAME:
data.setValue(tr("File")); return tr("File");
break; break;
case COL_SIZE: case COL_SIZE:
data.setValue(tr("Size")); return tr("Size");
break; break;
case COL_PROGRESS: case COL_PROGRESS:
data.setValue(tr("Progress")); return tr("Progress");
break; break;
case COL_WANTED: case COL_WANTED:
data.setValue(tr("Download")); return tr("Download");
break; break;
case COL_PRIORITY: case COL_PRIORITY:
data.setValue(tr("Priority")); return tr("Priority");
break; break;
default: default:
@ -224,7 +220,7 @@ QVariant FileTreeModel::headerData(int column, Qt::Orientation orientation, int
} }
} }
return data; return {};
} }
QModelIndex FileTreeModel::index(int row, int column, QModelIndex const& parent) const QModelIndex FileTreeModel::index(int row, int column, QModelIndex const& parent) const

View file

@ -161,27 +161,28 @@ int Torrent::compareETA(Torrent const& that) const
**** ****
***/ ***/
void Torrent::updateMimeIcon() QIcon Torrent::getMimeTypeIcon() const
{ {
auto const& files = files_; if (icon_.isNull())
auto const& icon_cache = IconCache::get(); {
auto const& files = files_;
auto const& icon_cache = IconCache::get();
QIcon icon; if (files.size() > 1)
{
if (files.size() > 1) icon_ = icon_cache.folderIcon();
{ }
icon = icon_cache.folderIcon(); else if (files.size() == 1)
} {
else if (files.size() == 1) icon_ = icon_cache.guessMimeIcon(files.at(0).filename, icon_cache.fileIcon());
{ }
icon = icon_cache.guessMimeIcon(files.at(0).filename, icon_cache.fileIcon()); else
} {
else icon_ = icon_cache.guessMimeIcon(name(), icon_cache.folderIcon());
{ }
icon = icon_cache.guessMimeIcon(name(), icon_cache.folderIcon());
} }
icon_ = icon; return icon_;
} }
/*** /***
@ -283,13 +284,13 @@ Torrent::fields_t Torrent::update(tr_quark const* keys, tr_variant const* const*
{ {
case TR_KEY_name: case TR_KEY_name:
{ {
updateMimeIcon(); icon_ = {};
break; break;
} }
case TR_KEY_files: case TR_KEY_files:
{ {
updateMimeIcon(); icon_ = {};
for (int i = 0; i < files_.size(); ++i) for (int i = 0; i < files_.size(); ++i)
{ {
files_[i].index = i; files_[i].index = i;

View file

@ -540,10 +540,7 @@ public:
return isWaitingToDownload() || isWaitingToSeed(); return isWaitingToDownload() || isWaitingToSeed();
} }
QIcon getMimeTypeIcon() const QIcon getMimeTypeIcon() const;
{
return icon_;
}
enum Field enum Field
{ {
@ -609,8 +606,6 @@ public:
fields_t update(tr_quark const* keys, tr_variant const* const* values, size_t n); fields_t update(tr_quark const* keys, tr_variant const* const* values, size_t n);
private: private:
void updateMimeIcon();
int const id_; int const id_;
bool download_limited_ = {}; bool download_limited_ = {};
@ -666,7 +661,8 @@ private:
QString error_string_; QString error_string_;
QString name_; QString name_;
QIcon icon_; // mutable because it's a lazy lookup
mutable QIcon icon_;
PeerList peers_; PeerList peers_;
FileList files_; FileList files_;

View file

@ -93,8 +93,6 @@ int TorrentModel::rowCount(QModelIndex const& parent) const
QVariant TorrentModel::data(QModelIndex const& index, int role) const QVariant TorrentModel::data(QModelIndex const& index, int role) const
{ {
QVariant var;
Torrent const* t = torrents_.value(index.row(), nullptr); Torrent const* t = torrents_.value(index.row(), nullptr);
if (t != nullptr) if (t != nullptr)
@ -102,15 +100,15 @@ QVariant TorrentModel::data(QModelIndex const& index, int role) const
switch (role) switch (role)
{ {
case Qt::DisplayRole: case Qt::DisplayRole:
var.setValue(t->name()); return t->name();
break; break;
case Qt::DecorationRole: case Qt::DecorationRole:
var.setValue(t->getMimeTypeIcon()); return t->getMimeTypeIcon();
break; break;
case TorrentRole: case TorrentRole:
var = QVariant::fromValue(t); return QVariant::fromValue(t);
break; break;
default: default:
@ -119,7 +117,7 @@ QVariant TorrentModel::data(QModelIndex const& index, int role) const
} }
} }
return var; return {};
} }
/*** /***