AutoZoomer/autoZoomer/autoZoomer.py
Juraj Oravec ae01a4de54
Add zoom to default zoom level on unknown hosts
Signed-off-by: Juraj Oravec <sgd.orava@gmail.com>
2019-05-15 21:30:22 +02:00

113 lines
3.7 KiB
Python

# ============================================================
# Auto Zoomer - 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, QtGui, QtWidgets
from autoZoomer.settingsDialog import SettingsDialog
from autoZoomer.str2bool import str2bool
class AutoZoomer(QtCore.QObject):
data = None
remove = None
config = None
def __init__(self, settingsPath, parent=None):
super().__init__(parent)
self.config = {
"settingsFile": os.path.join(settingsPath, "autoZoomer", "settings.ini"),
"active": True,
"defaultZoom": True
}
self.data = {}
self.remove = []
self.loadSettings()
def showSettings(self, parent=None):
settings = SettingsDialog(self.config, self.data, self.remove, parent)
settings.exec_()
self.saveSettings()
def loadSettings(self):
settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat)
settings.beginGroup("AutoZoomer")
self.config["active"] = str2bool(settings.value("active", True))
self.config["defaultZoom"] = str2bool(settings.value("defaultZoom", True))
settings.endGroup()
for group in settings.childGroups():
if group == "AutoZoomer":
continue
settings.beginGroup(group)
self.data[group] = {
"active": str2bool(settings.value("active", False)),
"zoom": int(settings.value("zoom", 6))
}
settings.endGroup()
def saveSettings(self):
settings = QtCore.QSettings(self.config["settingsFile"], QtCore.QSettings.IniFormat)
settings.beginGroup("AutoZoomer")
settings.setValue("active", self.config["active"])
settings.setValue("defaultZoom", self.config["defaultZoom"])
settings.endGroup()
for host in self.remove:
settings.remove(host)
self.remove.clear()
for key, value in self.data.items():
settings.beginGroup(key)
settings.setValue("active", value["active"])
settings.setValue("zoom", value["zoom"])
settings.endGroup()
settings.sync()
def onLoadFinished(self, page):
if not self.config["active"]:
return
view = page.view()
host = page.url().host()
if host in self.data.keys():
if self.data[host]["active"]:
view.setZoomLevel(self.data[host]["zoom"])
elif self.config["defaultZoom"]:
view.setZoomLevel(Falkon.Settings.staticSettings().defaultZoomLevel)
def addItem(self, host, zoom=6, active=True):
if not host:
return
if host in self.data.keys():
self.data[host]["zoom"] = zoom
self.data[host]["active"] = active
else:
self.data[host] = {
"active": active,
"zoom": zoom
}