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 <sgd.orava@gmail.com>
This commit is contained in:
Juraj Oravec 2019-06-14 10:37:02 +02:00
parent 796dc1b544
commit f5f059c3eb
No known key found for this signature in database
GPG Key ID: 63ACB65056BC8D07
3 changed files with 78 additions and 12 deletions

View File

@ -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

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>412</width>
<height>118</height>
<height>156</height>
</rect>
</property>
<property name="windowTitle">
@ -27,6 +27,9 @@
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="updateIntervalSpinBox">
<property name="accelerated">
<bool>true</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::PlusMinus</enum>
</property>
@ -53,6 +56,9 @@
<property name="buttonSymbols">
<enum>QAbstractSpinBox::PlusMinus</enum>
</property>
<property name="accelerated">
<bool>true</bool>
</property>
<property name="minimum">
<number>60</number>
</property>
@ -67,6 +73,32 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="closeTabWhenUnloadedForLabel">
<property name="text">
<string>Close tab when unloaded for:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="closeTabForSpinBox">
<property name="accelerated">
<bool>true</bool>
</property>
<property name="toolTip">
<string>0 - Disabled</string>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::PlusMinus</enum>
</property>
<property name="maximum">
<number>86400</number>
</property>
<property name="singleStep">
<number>10</number>
</property>
</widget>
</item>
</layout>
</item>
<item>

View File

@ -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()