# ============================================================ # Auto Zoomer - plugin for Falkon # Copyright (C) 2019 Juraj Oravec # # 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 . # ============================================================ import os from PySide2 import QtCore, QtWidgets, QtUiTools from autoZoomer.listItem import ListItem from autoZoomer.i18n import i18n class SettingsDialog(QtWidgets.QDialog): ui = None data = None remove = None prepareRemove = None config = None def __init__(self, config, data, remove, parent=None): super().__init__(parent) self.config = config self.data = data self.remove = remove self.prepareRemove = [] file = QtCore.QFile(os.path.join(os.path.dirname(__file__), "settings.ui")) file.open(QtCore.QFile.ReadOnly) self.ui = QtUiTools.QUiLoader().load(file, self) file.close() layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.ui) self.setLayout(layout) self.ui.checkBoxEnableAutoZoomer.setChecked(self.config["active"]) for key, value in self.data.items(): widget = ListItem() widget.setHost(key) widget.setActive(value["active"]) widget.setZoom(value["zoom"]) item = QtWidgets.QListWidgetItem(self.ui.listWidget) item.setSizeHint(widget.sizeHint() * 1.2) self.ui.listWidget.setItemWidget(item, widget) self.ui.pushButtonRemove.clicked.connect(self.handleItemRemove) self.ui.pushButtonAdd.clicked.connect(self.handleItemAdd) self.ui.buttonBoxConfirm.accepted.connect(self.accept) self.ui.buttonBoxConfirm.rejected.connect(self.reject) def addListItem(self, host, zoom=6, active=True): if not host: return if host in self.data.keys(): for i in range(0, self.ui.listWidget.count()): item = self.ui.listWidget.item(i) widget = self.ui.listWidget.itemWidget(item) if widget.host() == host: widget.setActive(active) widget.setZoom(zoom) break else: widget = ListItem() widget.setHost(host) widget.setActive(active) widget.setZoom(zoom) item = QtWidgets.QListWidgetItem(self.ui.listWidget) item.setSizeHint(widget.sizeHint() * 1.2) self.ui.listWidget.setItemWidget(item, widget) def updateData(self): self.config["active"] = self.ui.checkBoxEnableAutoZoomer.isChecked() self.data.clear() for i in range(0, self.ui.listWidget.count()): item = self.ui.listWidget.item(i) widget = self.ui.listWidget.itemWidget(item) self.data[widget.host()] = { "active": widget.active(), "zoom": widget.zoom() } self.remove.clear() for host in self.prepareRemove: self.remove.append(host) self.prepareRemove.clear() def handleItemAdd(self): host = self.ui.lineEditNew.text() zoom = self.ui.spinBoxNew.value() if not host: return self.ui.lineEditNew.clear() self.addListItem(host, zoom=zoom) def handleItemRemove(self): listWidget = self.ui.listWidget itemList = listWidget.selectedItems() if not itemList: return self.prepareRemove.append(listWidget.itemWidget(itemList[-1]).host()) listWidget.takeItem(listWidget.row(itemList[-1])) def accept(self): self.updateData() super().accept() self.close()