1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2002 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the variables filter dialog.
8"""
9
10from PyQt5.QtCore import Qt
11from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem
12
13from Debugger.Config import ConfigVarTypeDispStrings
14import Preferences
15
16from .Ui_VariablesFilterDialog import Ui_VariablesFilterDialog
17
18
19class VariablesFilterDialog(QDialog, Ui_VariablesFilterDialog):
20    """
21    Class implementing the variables filter dialog.
22
23    It opens a dialog window for the configuration of the variables type
24    filter to be applied during a debugging session.
25    """
26    def __init__(self, parent=None, name=None, modal=False):
27        """
28        Constructor
29
30        @param parent parent widget of this dialog (QWidget)
31        @param name name of this dialog (string)
32        @param modal flag to indicate a modal dialog (boolean)
33        """
34        super().__init__(parent)
35        if name:
36            self.setObjectName(name)
37        self.setModal(modal)
38        self.setupUi(self)
39
40        self.defaultButton = self.buttonBox.addButton(
41            self.tr("Save Default"), QDialogButtonBox.ButtonRole.ActionRole)
42
43        #populate the list widgets and set the default selection
44        for widget in self.localsList, self.globalsList:
45            for varType, varTypeStr in ConfigVarTypeDispStrings.items():
46                itm = QListWidgetItem(self.tr(varTypeStr), widget)
47                itm.setData(Qt.ItemDataRole.UserRole, varType)
48                itm.setFlags(Qt.ItemFlag.ItemIsEnabled |
49                             Qt.ItemFlag.ItemIsUserCheckable)
50                itm.setCheckState(Qt.CheckState.Unchecked)
51                widget.addItem(itm)
52
53        lDefaultFilter, gDefaultFilter = Preferences.getVarFilters()
54        self.setSelection(lDefaultFilter, gDefaultFilter)
55
56    def getSelection(self):
57        """
58        Public slot to retrieve the current selections.
59
60        @return tuple of lists containing the variable filters. The first list
61            is the locals variables filter, the second the globals variables
62            filter.
63        @rtype tuple of (list of str, list of str)
64        """
65        lList = []
66        for row in range(self.localsList.count()):
67            itm = self.localsList.item(row)
68            if itm.checkState() == Qt.CheckState.Unchecked:
69                lList.append(itm.data(Qt.ItemDataRole.UserRole))
70
71        gList = []
72        for row in range(self.globalsList.count()):
73            itm = self.globalsList.item(row)
74            if itm.checkState() == Qt.CheckState.Unchecked:
75                gList.append(itm.data(Qt.ItemDataRole.UserRole))
76        return (lList, gList)
77
78    def setSelection(self, lList, gList):
79        """
80        Public slot to set the current selection.
81
82        @param lList local variables filter
83        @type list of str
84        @param gList global variables filter
85        @type list of str
86        """
87        for row in range(self.localsList.count()):
88            itm = self.localsList.item(row)
89            if itm.data(Qt.ItemDataRole.UserRole) in lList:
90                itm.setCheckState(Qt.CheckState.Unchecked)
91            else:
92                itm.setCheckState(Qt.CheckState.Checked)
93
94        for row in range(self.globalsList.count()):
95            itm = self.globalsList.item(row)
96            if itm.data(Qt.ItemDataRole.UserRole) in gList:
97                itm.setCheckState(Qt.CheckState.Unchecked)
98            else:
99                itm.setCheckState(Qt.CheckState.Checked)
100
101    def on_buttonBox_clicked(self, button):
102        """
103        Private slot called by a button of the button box clicked.
104
105        @param button button that was clicked (QAbstractButton)
106        """
107        if button == self.defaultButton:
108            Preferences.setVarFilters(self.getSelection())
109