Unloader/unloader/manager.py
Juraj Oravec f5f059c3eb
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>
2019-06-14 10:37:02 +02:00

162 lines
5.3 KiB
Python

"""
# ============================================================
# Unloader - plugin for Falkon
# Copyright (C) 2019 Juraj Oravec <sgd.orava@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ============================================================
"""
import Falkon
import os
from PySide2 import QtCore
from unloader.settingsDialog import SettingsDialog
class UnloaderManager(QtCore.QObject):
tabs = None
timer = None
config = None
configChanged = QtCore.Signal()
def __init__(self, settingsPath, parent=None):
super().__init__(parent)
self.config = {
"settingsFile": os.path.join(settingsPath, "unloader", "settings.ini"),
"threshold": 600,
"closeThreshold": 0,
"updateInterval": 5
}
self.loadSettings()
self.tabs = {}
self.timer = QtCore.QTimer(self)
self.timer.setTimerType(QtCore.Qt.VeryCoarseTimer)
self.timer.setInterval(self.config["updateInterval"] * 1000)
self.timer.timeout.connect(self.onTimeout)
self.timer.start()
self.configChanged.connect(self.onConfigChanged)
def onMainWindowCreated(self, window):
tabs = window.tabWidget().allTabs()
for tab in tabs:
self.tabs[tab] = 0
tab.restoredChanged.connect(
lambda r: self.onTabRestoredChanged(tab, r)
)
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):
tabs = window.tabWidget().allTabs()
for tab in tabs:
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()
def onTimeout(self):
if not self.tabs:
return
for tab, time in list(self.tabs.items()):
if tab.isCurrentTab():
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
tab.unload()
def refreshTabList(self):
oldTabs = self.tabs.copy()
self.tabs.clear()
for window in Falkon.MainApplication.instance().windows():
winTabs = window.tabWidget().allTabs()
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("threshold", self.config["threshold"]))
self.config["closeThreshold"] = int(
settings.value("closeThreshold", self.config["closeThreshold"])
)
self.config["updateInterval"] = int(
settings.value("defaultZoom", self.config["updateInterval"])
)
settings.endGroup()
def saveSettings(self):
settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat)
settings.beginGroup("Unloader")
settings.setValue("threshold", self.config["threshold"])
settings.setValue("closeThreshold", self.config["closeThreshold"])
settings.setValue("updateInterval", self.config["updateInterval"])
settings.endGroup()
settings.sync()
def showSettings(self, parent=None):
settings = SettingsDialog(self.config, parent)
settings.accepted.connect(self.saveSettings)
settings.accepted.connect(self.configChanged)
settings.exec_()
def onConfigChanged(self):
self.timer.setInterval(self.config["updateInterval"] * 1000)
def onTabRestoredChanged(self, tab, restored):
self.tabs[tab] = 0