1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to manage registered protocol handlers.
8"""
9
10from PyQt5.QtCore import pyqtSlot, Qt
11from PyQt5.QtWidgets import QDialog, QTreeWidgetItem
12
13from .Ui_ProtocolHandlerManagerDialog import Ui_ProtocolHandlerManagerDialog
14
15
16class ProtocolHandlerManagerDialog(QDialog, Ui_ProtocolHandlerManagerDialog):
17    """
18    Class implementing a dialog to manage registered protocol handlers.
19    """
20    def __init__(self, manager, parent=None):
21        """
22        Constructor
23
24        @param manager reference to the protocol handlers manager object
25        @type ProtocolHandlerManager
26        @param parent reference to the parent widget
27        @type QWidget
28        """
29        super().__init__(parent)
30        self.setupUi(self)
31        self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
32
33        self.__manager = manager
34        handlers = self.__manager.protocolHandlers()
35        for scheme in sorted(handlers.keys()):
36            QTreeWidgetItem(self.protocolHandlersList,
37                            [scheme, handlers[scheme].toString()])
38
39        self.on_protocolHandlersList_itemSelectionChanged()
40
41    @pyqtSlot()
42    def on_protocolHandlersList_itemSelectionChanged(self):
43        """
44        Private slot handling a change of the selection.
45        """
46        self.deleteButton.setEnabled(
47            len(self.protocolHandlersList.selectedItems()) == 1)
48
49    @pyqtSlot()
50    def on_deleteButton_clicked(self):
51        """
52        Private slot to delete the selected protocol handler.
53        """
54        itm = self.protocolHandlersList.selectedItems()[0]
55        self.__manager.removeProtocolHandler(itm.text(0))
56
57        self.protocolHandlersList.takeTopLevelItem(
58            self.protocolHandlersList.indexOfTopLevelItem(itm))
59        del itm
60