From 70c62f6294a684778845d77630856d1f3bfde81e Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 21 Nov 2009 14:16:54 +0000 Subject: [PATCH] - Refresh torrent properties and transfer list only when they are visible in order to save CPU :) --- src/GUI.cpp | 22 +++++++++++++++++----- src/GUI.h | 3 +++ src/propertieswidget.cpp | 9 ++++++--- src/propertieswidget.h | 6 ++++-- src/transferlistwidget.cpp | 10 +++++++--- src/transferlistwidget.h | 4 +++- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index aeb2183c9..6d1613be5 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -60,7 +60,6 @@ #include "statusbar.h" using namespace libtorrent; -enum TabIndex{TAB_TRANSFER, TAB_SEARCH, TAB_RSS}; /***************************************************** * * @@ -110,14 +109,15 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), dis qDebug("create tabWidget"); tabs = new QTabWidget(); + connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int))); vSplitter = new QSplitter(Qt::Horizontal); vSplitter->setChildrenCollapsible(false); hSplitter = new QSplitter(Qt::Vertical); hSplitter->setChildrenCollapsible(false); // Transfer List tab - transferList = new TransferListWidget(hSplitter, BTSession); - properties = new PropertiesWidget(hSplitter, transferList, BTSession); + transferList = new TransferListWidget(hSplitter, this, BTSession); + properties = new PropertiesWidget(hSplitter, this, transferList, BTSession); transferListFilters = new TransferListFiltersWidget(vSplitter, transferList); hSplitter->addWidget(transferList); hSplitter->addWidget(properties); @@ -246,6 +246,14 @@ void GUI::on_actionBugReport_triggered() const { QDesktopServices::openUrl(QUrl(QString::fromUtf8("http://bugs.qbittorrent.org"))); } +void GUI::tab_changed(int new_tab) { + if(new_tab == TAB_TRANSFER) { + qDebug("Changed tab to transfer list, refreshing the list"); + transferList->refreshList(); + properties->loadDynamicData(); + } +} + void GUI::writeSettings() { QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); settings.beginGroup(QString::fromUtf8("MainWindow")); @@ -410,7 +418,7 @@ void GUI::previewFile(QString filePath) { } int GUI::getCurrentTabIndex() const{ - if(isMinimized() || isHidden()) + if(isMinimized() || !isVisible()) return -1; return tabs->currentIndex(); } @@ -463,7 +471,11 @@ void GUI::on_actionAbout_triggered() { void GUI::showEvent(QShowEvent *e) { qDebug("** Show Event **"); - //updateLists(true); + if(getCurrentTabIndex() == TAB_TRANSFER) { + qDebug("-> Refreshing transfer list"); + transferList->refreshList(); + properties->loadDynamicData(); + } e->accept(); } diff --git a/src/GUI.h b/src/GUI.h index e1f9414e6..de03d7912 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -37,6 +37,8 @@ #include "ui_mainwindow.h" #include "qtorrenthandle.h" +enum TabIndex{TAB_TRANSFER, TAB_SEARCH, TAB_RSS}; + class Bittorrent; class QTimer; class downloadFromURL; @@ -112,6 +114,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{ void fullDiskError(QTorrentHandle& h, QString msg) const; void handleDownloadFromUrlFailure(QString, QString) const; void createSystrayDelayed(); + void tab_changed(int); // Keyboard shortcuts void createKeyboardShortcuts(); void displayTransferTab() const; diff --git a/src/propertieswidget.cpp b/src/propertieswidget.cpp index 394a2fde2..3e558c421 100644 --- a/src/propertieswidget.cpp +++ b/src/propertieswidget.cpp @@ -48,6 +48,7 @@ #include "torrentfilesmodel.h" #include "peerlistwidget.h" #include "trackerlist.h" +#include "GUI.h" #ifdef Q_WS_MAC #define DEFAULT_BUTTON_CSS "" @@ -57,7 +58,8 @@ #define SELECTED_BUTTON_CSS "QPushButton {border: 1px solid rgb(85, 81, 91);border-radius: 3px;padding: 2px;background-color: rgb(255, 208, 105); margin-left: 3px; margin-right: 3px;}" #endif -PropertiesWidget::PropertiesWidget(QWidget *parent, TransferListWidget *transferList, Bittorrent* BTSession): QWidget(parent), transferList(transferList), BTSession(BTSession) { +PropertiesWidget::PropertiesWidget(QWidget *parent, GUI* main_window, TransferListWidget *transferList, Bittorrent* BTSession): + QWidget(parent), transferList(transferList), main_window(main_window), BTSession(BTSession) { setupUi(this); state = VISIBLE; @@ -283,8 +285,9 @@ void PropertiesWidget::reloadPreferences() { } void PropertiesWidget::loadDynamicData() { - if(!h.is_valid()) return; - if(state != VISIBLE) return; + // Refresh only if the torrent handle is valid and if visible + if(!h.is_valid() || main_window->getCurrentTabIndex() != TAB_TRANSFER || state != VISIBLE) return; + qDebug("Refreshing torrent properties"); try { // Transfer infos if(stackedProperties->currentIndex() == MAIN_TAB) { diff --git a/src/propertieswidget.h b/src/propertieswidget.h index 580848c2e..068d39435 100644 --- a/src/propertieswidget.h +++ b/src/propertieswidget.h @@ -47,6 +47,7 @@ class QAction; class torrent_file; class PeerListWidget; class TrackerList; +class GUI; enum Tab {MAIN_TAB, TRACKERS_TAB, PEERS_TAB, URLSEEDS_TAB, FILES_TAB}; enum SlideState {REDUCED, VISIBLE}; @@ -56,6 +57,7 @@ class PropertiesWidget : public QWidget, private Ui::PropertiesWidget { private: TransferListWidget *transferList; + GUI *main_window; QTorrentHandle h; QTimer *refreshTimer; RealProgressBar *progressBar; @@ -78,7 +80,6 @@ protected: protected slots: void loadTorrentInfos(QTorrentHandle &h); - void loadDynamicData(); void loadUrlSeeds(); void on_main_infos_button_clicked(); void on_trackers_button_clicked(); @@ -96,6 +97,7 @@ protected slots: void filteredFilesChanged(); public slots: + void loadDynamicData(); void reduce(); void slide(); void clear(); @@ -104,7 +106,7 @@ public slots: void reloadPreferences(); public: - PropertiesWidget(QWidget *parent, TransferListWidget *transferList, Bittorrent* BTSession); + PropertiesWidget(QWidget *parent, GUI* main_window, TransferListWidget *transferList, Bittorrent* BTSession); ~PropertiesWidget(); const QTorrentHandle& getCurrentTorrent() const; Bittorrent* getBTSession() const; diff --git a/src/transferlistwidget.cpp b/src/transferlistwidget.cpp index 02365b641..2e11b935a 100644 --- a/src/transferlistwidget.cpp +++ b/src/transferlistwidget.cpp @@ -35,6 +35,7 @@ #include "previewselect.h" #include "speedlimitdlg.h" #include "options_imp.h" +#include "GUI.h" #include "deletionconfirmationdlg.h" #include #include @@ -48,10 +49,10 @@ #include #include -TransferListWidget::TransferListWidget(QWidget *parent, Bittorrent *_BTSession): QTreeView(parent) { - QSettings settings("qBittorrent", "qBittorrent"); - BTSession = _BTSession; +TransferListWidget::TransferListWidget(QWidget *parent, GUI *main_window, Bittorrent *_BTSession): + QTreeView(parent), BTSession(_BTSession), main_window(main_window) { + QSettings settings("qBittorrent", "qBittorrent"); // Create and apply delegate listDelegate = new TransferListDelegate(this); setItemDelegate(listDelegate); @@ -386,6 +387,9 @@ void TransferListWidget::setRefreshInterval(int t) { } void TransferListWidget::refreshList() { + // Refresh only if displayed + if(main_window->getCurrentTabIndex() != TAB_TRANSFER) return; + qDebug("Refreshing transfer list"); unsigned int nb_downloading = 0, nb_seeding=0, nb_active=0, nb_inactive = 0; for(int i=0; irowCount(); ++i) { int s = updateTorrent(i); diff --git a/src/transferlistwidget.h b/src/transferlistwidget.h index e688dbcc9..69547f0c9 100644 --- a/src/transferlistwidget.h +++ b/src/transferlistwidget.h @@ -39,6 +39,7 @@ class QSortFilterProxyModel; class Bittorrent; class QTimer; class TransferListDelegate; +class GUI; enum TorrentFilter {FILTER_ALL, FILTER_DOWNLOADING, FILTER_COMPLETED, FILTER_ACTIVE, FILTER_INACTIVE}; @@ -51,9 +52,10 @@ private: QSortFilterProxyModel *proxyModel; Bittorrent* BTSession; QTimer *refreshTimer; + GUI *main_window; public: - TransferListWidget(QWidget *parent, Bittorrent* BTSession); + TransferListWidget(QWidget *parent, GUI *main_window, Bittorrent* BTSession); ~TransferListWidget(); protected: