AutoZoomer/autoZoomer/autoZoomer.py

149 lines
5.0 KiB
Python
Raw Normal View History

# ============================================================
# 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
from autoZoomer.i18n import i18n
class AutoZoomer(QtCore.QObject):
menuAction = None
data = None
remove = None
config = None
configChanged = QtCore.Signal()
def __init__(self, settingsPath, parent=None):
super().__init__(parent)
self.config = {
"settingsFile": os.path.join(settingsPath, "autoZoomer", "settings.ini"),
"active": True,
"defaultZoom": True,
"shortcut": ""
}
self.data = {}
self.remove = []
self.loadSettings()
def showSettings(self, parent=None):
settings = SettingsDialog(self.config, self.data, self.remove, parent)
settings.accepted.connect(self.saveSettings)
settings.accepted.connect(self.configChanged)
settings.exec_()
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))
self.config["shortcut"] = str(settings.value("shortcut", ""))
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.setValue("shortcut", self.config["shortcut"])
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"]:
if view.zoomLevel() != self.data[host]["zoom"]:
view.setZoomLevel(self.data[host]["zoom"])
elif self.config["defaultZoom"]:
defaultZoom = Falkon.Settings.staticSettings().defaultZoomLevel
if view.zoomLevel() != defaultZoom:
view.setZoomLevel(defaultZoom)
def createMenuAction(self, parent=None, shortcut=False):
menuAction = Falkon.Action(
QtGui.QIcon(os.path.join(os.path.dirname(__file__), "icon.svg")),
i18n("Save current zoom level"),
parent
)
if self.config["shortcut"] and shortcut:
menuAction.setShortcut(QtGui.QKeySequence(self.config["shortcut"]))
menuAction.triggered.connect(self.onMenuActionTriggered)
return menuAction
def onMenuActionTriggered(self):
view = Falkon.MainApplication.instance().getWindow().weView()
host = view.page().url().host()
zoom = view.zoomLevel()
print(host, zoom)
self.addItem(host=host, zoom=zoom)
def onPopulateExtensionsMenu(self, menu):
menuAction = self.createMenuAction(menu)
menu.addAction(menuAction)
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
}