From ad116edac74587ccca88cb64afa445b8ce87f6d7 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Tue, 21 Oct 2014 12:13:22 +0400 Subject: [PATCH 1/3] Use Qt::GlobalColor to refer to color instead of string Qt uses binary search to convert string to QColor, we don't need that binary search at all. This patch could be considered as optimization, but in reality creating QColor takes only 0.2% of time. So it should be visible at all. This could be considered as cleanup for not calling expensive functions from non-expensive ones. --- src/qtlibtorrent/torrentmodel.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/qtlibtorrent/torrentmodel.cpp b/src/qtlibtorrent/torrentmodel.cpp index fcaee6bd3..91301e0f2 100644 --- a/src/qtlibtorrent/torrentmodel.cpp +++ b/src/qtlibtorrent/torrentmodel.cpp @@ -102,7 +102,7 @@ TorrentModelItem::State TorrentModelItem::state() const { // Pause or Queued if (m_torrent.is_paused(m_lastStatus)) { m_icon = get_paused_icon(); - m_fgColor = QColor("red"); + m_fgColor = QColor(Qt::red); return m_torrent.is_seed(m_lastStatus) ? STATE_PAUSED_UP : STATE_PAUSED_DL; } if (m_torrent.is_queued(m_lastStatus)) { @@ -110,7 +110,7 @@ TorrentModelItem::State TorrentModelItem::state() const { && m_lastStatus.state != torrent_status::checking_resume_data && m_lastStatus.state != torrent_status::checking_files) { m_icon = get_queued_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return m_torrent.is_seed(m_lastStatus) ? STATE_QUEUED_UP : STATE_QUEUED_DL; } } @@ -118,20 +118,20 @@ TorrentModelItem::State TorrentModelItem::state() const { switch(m_lastStatus.state) { case torrent_status::allocating: m_icon = get_stalled_downloading_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return STATE_ALLOCATING; case torrent_status::downloading_metadata: m_icon = get_downloading_icon(); - m_fgColor = QColor("green"); + m_fgColor = QColor(Qt::green); return STATE_DOWNLOADING_META; case torrent_status::downloading: { if (m_lastStatus.download_payload_rate > 0) { m_icon = get_downloading_icon(); - m_fgColor = QColor("green"); + m_fgColor = QColor(Qt::green); return STATE_DOWNLOADING; } else { m_icon = get_stalled_downloading_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return STATE_STALLED_DL; } } @@ -139,33 +139,33 @@ TorrentModelItem::State TorrentModelItem::state() const { case torrent_status::seeding: if (m_lastStatus.upload_payload_rate > 0) { m_icon = get_uploading_icon(); - m_fgColor = QColor("orange"); + m_fgColor = QColor(255, 165, 0); return STATE_SEEDING; } else { m_icon = get_stalled_uploading_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return STATE_STALLED_UP; } case torrent_status::queued_for_checking: m_icon = get_checking_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return STATE_QUEUED_CHECK; case torrent_status::checking_resume_data: m_icon = get_checking_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return STATE_QUEUED_FASTCHECK; case torrent_status::checking_files: m_icon = get_checking_icon(); - m_fgColor = QColor("grey"); + m_fgColor = QColor(Qt::gray); return m_torrent.is_seed(m_lastStatus) ? STATE_CHECKING_UP : STATE_CHECKING_DL; default: m_icon = get_error_icon(); - m_fgColor = QColor("red"); + m_fgColor = QColor(Qt::red); return STATE_INVALID; } } catch(invalid_handle&) { m_icon = get_error_icon(); - m_fgColor = QColor("red"); + m_fgColor = QColor(Qt::red); return STATE_INVALID; } } From 80297697dde7c7e5d318c395f36d59af6043aace Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Sat, 25 Oct 2014 14:12:05 +0400 Subject: [PATCH 2/3] Remove mutable fields from TorrentModelItem The querying of TR_STATUS doesn't affect color and icon now. --- src/qtlibtorrent/torrentmodel.cpp | 122 ++++++++++++++++++------------ src/qtlibtorrent/torrentmodel.h | 4 +- 2 files changed, 75 insertions(+), 51 deletions(-) diff --git a/src/qtlibtorrent/torrentmodel.cpp b/src/qtlibtorrent/torrentmodel.cpp index 91301e0f2..d2bf7054f 100644 --- a/src/qtlibtorrent/torrentmodel.cpp +++ b/src/qtlibtorrent/torrentmodel.cpp @@ -100,76 +100,100 @@ void TorrentModelItem::refreshStatus(libtorrent::torrent_status const& status) { TorrentModelItem::State TorrentModelItem::state() const { try { // Pause or Queued - if (m_torrent.is_paused(m_lastStatus)) { - m_icon = get_paused_icon(); - m_fgColor = QColor(Qt::red); + if (m_torrent.is_paused(m_lastStatus)) return m_torrent.is_seed(m_lastStatus) ? STATE_PAUSED_UP : STATE_PAUSED_DL; - } - if (m_torrent.is_queued(m_lastStatus)) { - if (m_lastStatus.state != torrent_status::queued_for_checking - && m_lastStatus.state != torrent_status::checking_resume_data - && m_lastStatus.state != torrent_status::checking_files) { - m_icon = get_queued_icon(); - m_fgColor = QColor(Qt::gray); - return m_torrent.is_seed(m_lastStatus) ? STATE_QUEUED_UP : STATE_QUEUED_DL; - } - } + + if (m_torrent.is_queued(m_lastStatus) + && m_lastStatus.state != torrent_status::queued_for_checking + && m_lastStatus.state != torrent_status::checking_resume_data + && m_lastStatus.state != torrent_status::checking_files) + return m_torrent.is_seed(m_lastStatus) ? STATE_QUEUED_UP : STATE_QUEUED_DL; + // Other states switch(m_lastStatus.state) { case torrent_status::allocating: - m_icon = get_stalled_downloading_icon(); - m_fgColor = QColor(Qt::gray); return STATE_ALLOCATING; case torrent_status::downloading_metadata: - m_icon = get_downloading_icon(); - m_fgColor = QColor(Qt::green); return STATE_DOWNLOADING_META; - case torrent_status::downloading: { - if (m_lastStatus.download_payload_rate > 0) { - m_icon = get_downloading_icon(); - m_fgColor = QColor(Qt::green); - return STATE_DOWNLOADING; - } else { - m_icon = get_stalled_downloading_icon(); - m_fgColor = QColor(Qt::gray); - return STATE_STALLED_DL; - } - } + case torrent_status::downloading: + return m_lastStatus.download_payload_rate > 0 ? STATE_DOWNLOADING : STATE_STALLED_DL; case torrent_status::finished: case torrent_status::seeding: - if (m_lastStatus.upload_payload_rate > 0) { - m_icon = get_uploading_icon(); - m_fgColor = QColor(255, 165, 0); - return STATE_SEEDING; - } else { - m_icon = get_stalled_uploading_icon(); - m_fgColor = QColor(Qt::gray); - return STATE_STALLED_UP; - } + return m_lastStatus.upload_payload_rate > 0 ? STATE_SEEDING : STATE_STALLED_UP; case torrent_status::queued_for_checking: - m_icon = get_checking_icon(); - m_fgColor = QColor(Qt::gray); return STATE_QUEUED_CHECK; case torrent_status::checking_resume_data: - m_icon = get_checking_icon(); - m_fgColor = QColor(Qt::gray); return STATE_QUEUED_FASTCHECK; case torrent_status::checking_files: - m_icon = get_checking_icon(); - m_fgColor = QColor(Qt::gray); return m_torrent.is_seed(m_lastStatus) ? STATE_CHECKING_UP : STATE_CHECKING_DL; default: - m_icon = get_error_icon(); - m_fgColor = QColor(Qt::red); return STATE_INVALID; } } catch(invalid_handle&) { - m_icon = get_error_icon(); - m_fgColor = QColor(Qt::red); return STATE_INVALID; } } +QIcon TorrentModelItem::getIconByState(State state) { + switch (state) { + case STATE_DOWNLOADING: + case STATE_DOWNLOADING_META: + return get_downloading_icon(); + case STATE_ALLOCATING: + case STATE_STALLED_DL: + return get_stalled_downloading_icon(); + case STATE_STALLED_UP: + return get_stalled_uploading_icon(); + case STATE_SEEDING: + return get_uploading_icon(); + case STATE_PAUSED_DL: + case STATE_PAUSED_UP: + return get_paused_icon(); + case STATE_QUEUED_DL: + case STATE_QUEUED_UP: + return get_queued_icon(); + case STATE_CHECKING_UP: + case STATE_CHECKING_DL: + case STATE_QUEUED_CHECK: + case STATE_QUEUED_FASTCHECK: + return get_checking_icon(); + case STATE_INVALID: + return get_error_icon(); + default: + Q_ASSERT(false); + return get_error_icon(); + } +} + +QColor TorrentModelItem::getColorByState(State state) { + switch (state) { + case STATE_DOWNLOADING: + case STATE_DOWNLOADING_META: + return QColor(Qt::green); + case STATE_ALLOCATING: + case STATE_STALLED_DL: + case STATE_STALLED_UP: + return QColor(Qt::gray); + case STATE_SEEDING: + return QColor(255, 165, 0); + case STATE_PAUSED_DL: + case STATE_PAUSED_UP: + return QColor(Qt::red); + case STATE_QUEUED_DL: + case STATE_QUEUED_UP: + case STATE_CHECKING_UP: + case STATE_CHECKING_DL: + case STATE_QUEUED_CHECK: + case STATE_QUEUED_FASTCHECK: + return QColor(Qt::gray); + case STATE_INVALID: + return QColor(Qt::red); + default: + Q_ASSERT(false); + return QColor(Qt::red); + } +} + bool TorrentModelItem::setData(int column, const QVariant &value, int role) { qDebug() << Q_FUNC_INFO << column << value; @@ -199,10 +223,10 @@ bool TorrentModelItem::setData(int column, const QVariant &value, int role) QVariant TorrentModelItem::data(int column, int role) const { if (role == Qt::DecorationRole && column == TR_NAME) { - return m_icon; + return getIconByState(state()); } if (role == Qt::ForegroundRole) { - return m_fgColor; + return getColorByState(state()); } if (role != Qt::DisplayRole && role != Qt::UserRole) return QVariant(); switch(column) { diff --git a/src/qtlibtorrent/torrentmodel.h b/src/qtlibtorrent/torrentmodel.h index 40e35bb0a..dc61c1cb7 100644 --- a/src/qtlibtorrent/torrentmodel.h +++ b/src/qtlibtorrent/torrentmodel.h @@ -63,6 +63,8 @@ signals: void labelChanged(QString previous, QString current); private: + static QIcon getIconByState(State state); + static QColor getColorByState(State state); State state() const; private: @@ -71,8 +73,6 @@ private: QDateTime m_addedTime; QString m_label; QString m_name; - mutable QIcon m_icon; - mutable QColor m_fgColor; QString m_hash; // Cached for safety reasons }; From 2d98b8f1921ca025db082b32fdfbd2f3c6e1d985 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Sat, 25 Oct 2014 14:13:09 +0400 Subject: [PATCH 3/3] Call TorrentModelItem::state() directly in getTorrentStatusReport There is no point in wraping/unwraping QVariant. --- src/qtlibtorrent/torrentmodel.cpp | 2 +- src/qtlibtorrent/torrentmodel.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qtlibtorrent/torrentmodel.cpp b/src/qtlibtorrent/torrentmodel.cpp index d2bf7054f..09f063abd 100644 --- a/src/qtlibtorrent/torrentmodel.cpp +++ b/src/qtlibtorrent/torrentmodel.cpp @@ -504,7 +504,7 @@ TorrentStatusReport TorrentModel::getTorrentStatusReport() const QList::const_iterator it = m_torrents.constBegin(); QList::const_iterator itend = m_torrents.constEnd(); for ( ; it != itend; ++it) { - switch((*it)->data(TorrentModelItem::TR_STATUS).toInt()) { + switch((*it)->state()) { case TorrentModelItem::STATE_DOWNLOADING: ++report.nb_active; ++report.nb_downloading; diff --git a/src/qtlibtorrent/torrentmodel.h b/src/qtlibtorrent/torrentmodel.h index dc61c1cb7..e77bf4644 100644 --- a/src/qtlibtorrent/torrentmodel.h +++ b/src/qtlibtorrent/torrentmodel.h @@ -58,6 +58,7 @@ public: QVariant data(int column, int role = Qt::DisplayRole) const; bool setData(int column, const QVariant &value, int role = Qt::DisplayRole); inline QString hash() const { return m_hash; } + State state() const; signals: void labelChanged(QString previous, QString current); @@ -65,7 +66,6 @@ signals: private: static QIcon getIconByState(State state); static QColor getColorByState(State state); - State state() const; private: QTorrentHandle m_torrent;