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 = { self.config = {
"settingsFile": os.path.join(settingsPath, "unloader", "settings.ini"), "settingsFile": os.path.join(settingsPath, "unloader", "settings.ini"),
"threshold": 600, "threshold": 600,
"closeThreshold": 0,
"updateInterval": 5 "updateInterval": 5
} }
self.loadSettings()
self.tabs = {} self.tabs = {}
@ -55,13 +57,16 @@ class UnloaderManager(QtCore.QObject):
for tab in tabs: for tab in tabs:
self.tabs[tab] = 0 self.tabs[tab] = 0
tab.restoredChanged.connect(
lambda r: self.onTabRestoredChanged(tab, r)
)
def onCurrentTabChanged(index): window.tabWidget().currentChanged.connect(
tab = window.tabWidget().webTab(index) lambda index: self.onCurrentTabChanged(window, index)
self.tabs[tab] = 0 )
window.tabWidget().tabInserted.connect(
window.tabWidget().currentChanged.connect(onCurrentTabChanged) lambda index: self.onCurrentTabChanged(window, index, True)
window.tabWidget().tabInserted.connect(onCurrentTabChanged) )
window.tabWidget().tabRemoved.connect(self.onTabRemoved) window.tabWidget().tabRemoved.connect(self.onTabRemoved)
def onMainWindowDeleted(self, window): def onMainWindowDeleted(self, window):
@ -71,6 +76,15 @@ class UnloaderManager(QtCore.QObject):
if tab in self.tabs: if tab in self.tabs:
del self.tabs[tab] 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): def onTabRemoved(self, index):
self.refreshTabList() self.refreshTabList()
@ -78,15 +92,18 @@ class UnloaderManager(QtCore.QObject):
if not self.tabs: if not self.tabs:
return return
for tab, time in self.tabs.items(): for tab, time in list(self.tabs.items()):
if tab.isCurrentTab(): if tab.isCurrentTab():
continue continue
if not tab.isRestored():
self.tabs[tab] = 0
continue
self.tabs[tab] = time + int(self.timer.interval() / 1000) 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.config["threshold"]:
if self.tabs[tab] > self.config["threshold"] and tab.isRestored(): if self.tabs[tab] > self.config["threshold"] and tab.isRestored():
self.tabs[tab] = 0 self.tabs[tab] = 0
@ -102,11 +119,19 @@ class UnloaderManager(QtCore.QObject):
for tab in winTabs: for tab in winTabs:
self.tabs[tab] = oldTabs.get(tab, 0) self.tabs[tab] = oldTabs.get(tab, 0)
if tab not in oldTabs:
tab.restoredChanged.connect(
lambda r: self.onTabRestoredChanged(tab, r)
)
def loadSettings(self): def loadSettings(self):
settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat) settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat)
settings.beginGroup("Unloader") 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( self.config["updateInterval"] = int(
settings.value("defaultZoom", self.config["updateInterval"]) settings.value("defaultZoom", self.config["updateInterval"])
) )
@ -117,6 +142,7 @@ class UnloaderManager(QtCore.QObject):
settings.beginGroup("Unloader") settings.beginGroup("Unloader")
settings.setValue("threshold", self.config["threshold"]) settings.setValue("threshold", self.config["threshold"])
settings.setValue("closeThreshold", self.config["closeThreshold"])
settings.setValue("updateInterval", self.config["updateInterval"]) settings.setValue("updateInterval", self.config["updateInterval"])
settings.endGroup() settings.endGroup()
@ -130,3 +156,6 @@ class UnloaderManager(QtCore.QObject):
def onConfigChanged(self): def onConfigChanged(self):
self.timer.setInterval(self.config["updateInterval"] * 1000) 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> <x>0</x>
<y>0</y> <y>0</y>
<width>412</width> <width>412</width>
<height>118</height> <height>156</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -27,6 +27,9 @@
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QSpinBox" name="updateIntervalSpinBox"> <widget class="QSpinBox" name="updateIntervalSpinBox">
<property name="accelerated">
<bool>true</bool>
</property>
<property name="buttonSymbols"> <property name="buttonSymbols">
<enum>QAbstractSpinBox::PlusMinus</enum> <enum>QAbstractSpinBox::PlusMinus</enum>
</property> </property>
@ -53,6 +56,9 @@
<property name="buttonSymbols"> <property name="buttonSymbols">
<enum>QAbstractSpinBox::PlusMinus</enum> <enum>QAbstractSpinBox::PlusMinus</enum>
</property> </property>
<property name="accelerated">
<bool>true</bool>
</property>
<property name="minimum"> <property name="minimum">
<number>60</number> <number>60</number>
</property> </property>
@ -67,6 +73,32 @@
</property> </property>
</widget> </widget>
</item> </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> </layout>
</item> </item>
<item> <item>

View File

@ -44,6 +44,7 @@ class SettingsDialog(QtWidgets.QDialog):
self.ui.updateIntervalSpinBox.setValue(self.config["updateInterval"]) self.ui.updateIntervalSpinBox.setValue(self.config["updateInterval"])
self.ui.discardInactiveTabAfterSpinBox.setValue(self.config["threshold"]) self.ui.discardInactiveTabAfterSpinBox.setValue(self.config["threshold"])
self.ui.closeTabForSpinBox.setValue(self.config["closeThreshold"])
self.ui.buttonBoxConfirm.accepted.connect(self.accept) self.ui.buttonBoxConfirm.accepted.connect(self.accept)
self.ui.buttonBoxConfirm.rejected.connect(self.reject) self.ui.buttonBoxConfirm.rejected.connect(self.reject)
@ -54,10 +55,14 @@ class SettingsDialog(QtWidgets.QDialog):
self.ui.discardInactiveTabAfterLabel.setText( self.ui.discardInactiveTabAfterLabel.setText(
i18n("Discard inactive tab after (in seconds):") i18n("Discard inactive tab after (in seconds):")
) )
self.ui.closeTabWhenUnloadedForLabel.setText(
i18n("Close tab when unloaded for:")
)
def updateData(self): def updateData(self):
self.config["updateInterval"] = self.ui.updateIntervalSpinBox.value() self.config["updateInterval"] = self.ui.updateIntervalSpinBox.value()
self.config["threshold"] = self.ui.discardInactiveTabAfterSpinBox.value() self.config["threshold"] = self.ui.discardInactiveTabAfterSpinBox.value()
self.config["closeThreshold"] = self.ui.closeTabForSpinBox.value()
def accept(self): def accept(self):
self.updateData() self.updateData()