Completed
Pull Request — master (#384)
by
unknown
02:54
created

AnalogParametersWidget   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 98
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setData() 0 13 5
C __init__() 0 63 7
A sizeHint() 0 2 1
B data() 0 12 5
1
# -*- coding: utf-8 -*-
2
3
"""
4
This file contains custom item widgets for the pulse editor QTableViews.
5
6
Qudi is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
Qudi is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
18
19
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
20
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
21
"""
22
23
from qtpy import QtCore, QtGui
24
from collections import OrderedDict
25
from qtwidgets.scientific_spinbox import ScienDSpinBox, ScienSpinBox
26
27
28
class DigitalChannelsWidget(QtGui.QWidget):
29
    """
30
    """
31
    stateChanged = QtCore.Signal()
32
33
    def __init__(self, parent=None, digital_channels=None):
34
        super().__init__(parent)
35
36
        if digital_channels is None:
37
            self._digital_channels = list()
38
        else:
39
            self._digital_channels = digital_channels
40
41
        self._dch_checkboxes = OrderedDict()
42
        self._width_hint = 30 * len(self._digital_channels)
43
44
        main_layout = QtGui.QHBoxLayout()
45
        for chnl in self._digital_channels:
46
            # Create QLabel and QCheckBox for each digital channel
47
            label = QtGui.QLabel(chnl.rsplit('ch')[1])
48
            label.setFixedWidth(30)
49
            label.setAlignment(QtCore.Qt.AlignCenter)
50
            widget = QtGui.QCheckBox()
51
            widget.setFixedWidth(19)
52
            widget.setChecked(False)
53
            self._dch_checkboxes[chnl] = {'label': label, 'widget': widget}
54
55
            # Forward editingFinished signal of child widget
56
            widget.stateChanged.connect(self.stateChanged)
57
58
            # Arrange CheckBoxes and Labels in a layout
59
            v_layout = QtGui.QVBoxLayout()
60
            v_layout.addWidget(label)
61
            v_layout.addWidget(widget)
62
            v_layout.setAlignment(label, QtCore.Qt.AlignHCenter)
63
            v_layout.setAlignment(widget, QtCore.Qt.AlignHCenter)
64
            main_layout.addLayout(v_layout)
65
        main_layout.addStretch(1)
66
        main_layout.setSpacing(0)
67
        main_layout.setContentsMargins(0, 0, 0, 0)
68
        self.setLayout(main_layout)
69
70
    def data(self):
71
        digital_states = OrderedDict()
72
        for chnl in self._digital_channels:
73
            digital_states[chnl] = self._dch_checkboxes[chnl]['widget'].isChecked()
74
        return digital_states
75
76
    def setData(self, data):
77
        for chnl in data:
78
            self._dch_checkboxes[chnl]['widget'].setChecked(data[chnl])
79
        self.stateChanged.emit()
80
        return
81
82
    def sizeHint(self):
83
        return QtCore.QSize(self._width_hint, 50)
84
85
86
class AnalogParametersWidget(QtGui.QWidget):
87
    """
88
    """
89
    editingFinished = QtCore.Signal()
90
91
    def __init__(self, parent=None, parameters_dict=None):
92
        super().__init__(parent)
93
        if parameters_dict is None:
94
            self._parameters = OrderedDict()
95
        else:
96
            self._parameters = parameters_dict
97
98
        self._width_hint = 90 * len(self._parameters)
99
        self._ach_widgets = OrderedDict()
100
101
        main_layout = QtGui.QHBoxLayout()
102
        for param in self._parameters:
103
            label = QtGui.QLabel(param)
104
            label.setAlignment(QtCore.Qt.AlignCenter)
105
            if self._parameters[param]['type'] == float:
106
                widget = ScienDSpinBox()
107
                widget.setMinimum(self._parameters[param]['min'])
108
                widget.setMaximum(self._parameters[param]['max'])
109
                widget.setDecimals(6, False)
110
                widget.setValue(self._parameters[param]['init'])
111
                widget.setSuffix(self._parameters[param]['unit'])
112
                # Set size constraints
113
                widget.setFixedWidth(90)
114
                # Forward editingFinished signal of child widget
115
                widget.editingFinished.connect(self.editingFinished)
116
            elif self._parameters[param]['type'] == int:
117
                widget = ScienSpinBox()
118
                widget.setValue(self._parameters[param]['init'])
119
                widget.setMinimum(self._parameters[param]['min'])
120
                widget.setMaximum(self._parameters[param]['max'])
121
                widget.setSuffix(self._parameters[param]['unit'])
122
                # Set size constraints
123
                widget.setFixedWidth(90)
124
                # Forward editingFinished signal of child widget
125
                widget.editingFinished.connect(self.editingFinished)
126
            elif self._parameters[param]['type'] == str:
127
                widget = QtGui.QLineEdit()
128
                widget.setText(self._parameters[param]['init'])
129
                # Set size constraints
130
                widget.setFixedWidth(90)
131
                # Forward editingFinished signal of child widget
132
                widget.editingFinished.connect(self.editingFinished)
133
            elif self._parameters[param]['type'] == bool:
134
                widget = QtGui.QCheckBox()
135
                widget.setChecked(self._parameters[param]['init'])
136
                # Set size constraints
137
                widget.setFixedWidth(90)
138
                # Forward editingFinished signal of child widget
139
                widget.stateChanged.connect(self.editingFinished)
140
141
            self._ach_widgets[param] = {'label': label, 'widget': widget}
142
143
            v_layout = QtGui.QVBoxLayout()
144
            v_layout.addWidget(label)
145
            v_layout.addWidget(widget)
146
            v_layout.setAlignment(label, QtCore.Qt.AlignHCenter)
147
            v_layout.setAlignment(widget, QtCore.Qt.AlignHCenter)
148
            main_layout.addLayout(v_layout)
149
150
        main_layout.addStretch(1)
151
        main_layout.setSpacing(0)
152
        main_layout.setContentsMargins(0, 0, 0, 0)
153
        self.setLayout(main_layout)
154
155
    def setData(self, data):
156
        # Set analog parameter widget values
157
        for param in self._ach_widgets:
158
            widget = self._ach_widgets[param]['widget']
159
            if self._parameters[param]['type'] in [int, float]:
160
                widget.setValue(data[param])
161
            elif self._parameters[param]['type'] == str:
162
                widget.setText(data[param])
163
            elif self._parameters[param]['type'] == bool:
164
                widget.setChecked(data[param])
165
166
        self.editingFinished.emit()
167
        return
168
169
    def data(self):
170
        # Get all analog parameters from widgets
171
        analog_params = OrderedDict()
172
        for param in self._parameters:
173
            widget = self._ach_widgets[param]['widget']
174
            if self._parameters[param]['type'] in [int, float]:
175
                analog_params[param] = widget.value()
176
            elif self._parameters[param]['type'] == str:
177
                analog_params[param] = widget.text()
178
            elif self._parameters[param]['type'] == bool:
179
                analog_params[param] = widget.isChecked()
180
        return analog_params
181
182
    def sizeHint(self):
183
        return QtCore.QSize(self._width_hint, 50)
184
185
    # def selectNumber(self):
186
    #     """
187
    #     """
188
    #     for param in self._parameters:
189
    #         widget = self._ach_widgets[param]['widget']
190
    #         if self._parameters[param]['type'] in [int, float]:
191
    #             widget.selectNumber()  # that is specific for the ScientificSpinBox
192