From f5f059c3eb7021197b27f4c614cf0a215a7bba69 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Fri, 14 Jun 2019 10:37:02 +0200 Subject: [PATCH] Mess of fixis and new features - Fix loading settings - Use lamdas in code - Hook into tab restoreChanged event - Add auto close tab option - Make spinboxes in settings accelerated closes GH-2 Signed-off-by: Juraj Oravec --- unloader/manager.py | 51 ++++++++++++++++++++++++++++++-------- unloader/settings.ui | 34 ++++++++++++++++++++++++- unloader/settingsDialog.py | 5 ++++ 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/unloader/manager.py b/unloader/manager.py index 13d4ae1..f1e2671 100644 --- a/unloader/manager.py +++ b/unloader/manager.py @@ -37,8 +37,10 @@ class UnloaderManager(QtCore.QObject): self.config = { "settingsFile": os.path.join(settingsPath, "unloader", "settings.ini"), "threshold": 600, + "closeThreshold": 0, "updateInterval": 5 } + self.loadSettings() self.tabs = {} @@ -55,13 +57,16 @@ class UnloaderManager(QtCore.QObject): for tab in tabs: self.tabs[tab] = 0 + tab.restoredChanged.connect( + lambda r: self.onTabRestoredChanged(tab, r) + ) - def onCurrentTabChanged(index): - tab = window.tabWidget().webTab(index) - self.tabs[tab] = 0 - - window.tabWidget().currentChanged.connect(onCurrentTabChanged) - window.tabWidget().tabInserted.connect(onCurrentTabChanged) + window.tabWidget().currentChanged.connect( + lambda index: self.onCurrentTabChanged(window, index) + ) + window.tabWidget().tabInserted.connect( + lambda index: self.onCurrentTabChanged(window, index, True) + ) window.tabWidget().tabRemoved.connect(self.onTabRemoved) def onMainWindowDeleted(self, window): @@ -71,6 +76,15 @@ class UnloaderManager(QtCore.QObject): if tab in self.tabs: del self.tabs[tab] + def onCurrentTabChanged(self, window, index, new=False): + tab = window.tabWidget().webTab(index) + self.tabs[tab] = 0 + + if new: + tab.restoredChanged.connect( + lambda r: self.onTabRestoredChanged(tab, r) + ) + def onTabRemoved(self, index): self.refreshTabList() @@ -78,15 +92,18 @@ class UnloaderManager(QtCore.QObject): if not self.tabs: return - for tab, time in self.tabs.items(): + for tab, time in list(self.tabs.items()): if tab.isCurrentTab(): continue - if not tab.isRestored(): - self.tabs[tab] = 0 - continue self.tabs[tab] = time + int(self.timer.interval() / 1000) + if not tab.isRestored() and self.config["closeThreshold"]: + if self.tabs[tab] > self.config["closeThreshold"]: + del self.tabs[tab] + tab.closeTab() + continue + if self.config["threshold"]: if self.tabs[tab] > self.config["threshold"] and tab.isRestored(): self.tabs[tab] = 0 @@ -102,11 +119,19 @@ class UnloaderManager(QtCore.QObject): for tab in winTabs: self.tabs[tab] = oldTabs.get(tab, 0) + if tab not in oldTabs: + tab.restoredChanged.connect( + lambda r: self.onTabRestoredChanged(tab, r) + ) + def loadSettings(self): settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat) settings.beginGroup("Unloader") - self.config["threshold"] = int(settings.value("active", self.config["threshold"])) + self.config["threshold"] = int(settings.value("threshold", self.config["threshold"])) + self.config["closeThreshold"] = int( + settings.value("closeThreshold", self.config["closeThreshold"]) + ) self.config["updateInterval"] = int( settings.value("defaultZoom", self.config["updateInterval"]) ) @@ -117,6 +142,7 @@ class UnloaderManager(QtCore.QObject): settings.beginGroup("Unloader") settings.setValue("threshold", self.config["threshold"]) + settings.setValue("closeThreshold", self.config["closeThreshold"]) settings.setValue("updateInterval", self.config["updateInterval"]) settings.endGroup() @@ -130,3 +156,6 @@ class UnloaderManager(QtCore.QObject): def onConfigChanged(self): self.timer.setInterval(self.config["updateInterval"] * 1000) + + def onTabRestoredChanged(self, tab, restored): + self.tabs[tab] = 0 diff --git a/unloader/settings.ui b/unloader/settings.ui index 4e577da..f0730d3 100644 --- a/unloader/settings.ui +++ b/unloader/settings.ui @@ -7,7 +7,7 @@ 0 0 412 - 118 + 156 @@ -27,6 +27,9 @@ + + true + QAbstractSpinBox::PlusMinus @@ -53,6 +56,9 @@ QAbstractSpinBox::PlusMinus + + true + 60 @@ -67,6 +73,32 @@ + + + + Close tab when unloaded for: + + + + + + + true + + + 0 - Disabled + + + QAbstractSpinBox::PlusMinus + + + 86400 + + + 10 + + + diff --git a/unloader/settingsDialog.py b/unloader/settingsDialog.py index 144e18a..4dda068 100644 --- a/unloader/settingsDialog.py +++ b/unloader/settingsDialog.py @@ -44,6 +44,7 @@ class SettingsDialog(QtWidgets.QDialog): self.ui.updateIntervalSpinBox.setValue(self.config["updateInterval"]) self.ui.discardInactiveTabAfterSpinBox.setValue(self.config["threshold"]) + self.ui.closeTabForSpinBox.setValue(self.config["closeThreshold"]) self.ui.buttonBoxConfirm.accepted.connect(self.accept) self.ui.buttonBoxConfirm.rejected.connect(self.reject) @@ -54,10 +55,14 @@ class SettingsDialog(QtWidgets.QDialog): self.ui.discardInactiveTabAfterLabel.setText( i18n("Discard inactive tab after (in seconds):") ) + self.ui.closeTabWhenUnloadedForLabel.setText( + i18n("Close tab when unloaded for:") + ) def updateData(self): self.config["updateInterval"] = self.ui.updateIntervalSpinBox.value() self.config["threshold"] = self.ui.discardInactiveTabAfterSpinBox.value() + self.config["closeThreshold"] = self.ui.closeTabForSpinBox.value() def accept(self): self.updateData()