Issues (265)

qtwidgets/checkbox.py (1 issue)

Labels
Severity
1
"""
2
QCheckBox with a callback function for acceptance or denial of state change.
3
4
Qudi is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
Qudi is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
16
17
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
18
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
19
"""
20
21
from qtpy.QtWidgets import QCheckBox
22
23
class CheckBox(QCheckBox):
24
    """
25
    This QCheckBox provides a callback function which is called before
26
    the state is changed and which can be used to prevent state changes on
27
    certain conditions.
28
29
    The callback function has the following signature:
30
    bool callback(new_state: bool)
31
    where new_state is a boolean with the new state if accepted and the return
32
    value is True for acceptance and False for denial.
33
34
    Usage Example:
35
    checkbox = CheckBox()
36
    def on_accept_state_change(new_state):
37
        if (not new_state):
38
            result = QMessageBox.question(
39
                    self,
40
                    'Are you sure you want to disable this?',
41
                    QMessageBox.StandardButtons(
42
                        QMessageBox.Yes | QMessageBox.No)
43
                    )
44
            return result == QMessageBox.Yes
45
    checkbox.accept_state_change_callback = on_accept_state_change
46
    """
47
    def __init__(self, *args, **kwargs):
48
        """
49
        Constructor. See QCheckBox for details.
50
        """
51
        super().__init__(*args, **kwargs)
52
        self._callback = None
53
54
    @property
55
    def accept_state_change_callback(self):
56
        """
57
        Returns state changing callback function.
58
59
        The callback function has the following signature:
60
        bool callback(new_state: bool)
61
        where new_state is a boolean with the new state if accepted and the
62
        return value is True for acceptance and False for denial.
63
        """
64
        return self._callback
65
66
    @accept_state_change_callback.setter
67
    def accept_state_change_callback(self, value):
68
        """
69
        Sets state changing callback function.
70
71
        The callback function has the following signature:
72
        bool callback(new_state: bool)
73
        where new_state is a boolean with the new state if accepted and the
74
        return value is True for acceptance and False for denial.
75
        """
76
        self._callback = value
77
78
    def nextCheckState(self):
79
        """
80
        Protected functions that calls the callback.
81
        """
82
        if (self._callback is not None):
83
            if (self._callback(not self.isChecked())):
0 ignored issues
show
The Instance of CheckBox does not seem to have a member named isChecked.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
84
                super().nextCheckState()
85
        else:
86
            super().nextCheckState()
87