Debian Codegraph @

pymeasure/display/widgets/fileinput_widget.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import logging
26
27 from ..Qt import QtCore, QtWidgets
28 from .filename_widget import FilenameLineEdit
29 from .directory_widget import DirectoryLineEdit
30
31 log = logging.getLogger(__name__)
32 log.addHandler(logging.NullHandler())
33
34
35 class FileInputWidget(QtWidgets.QWidget):
36 """
37 Widget for controlling where the data of an experiment will be stored.
38
39 The widget consists of a field for the filename
40 (:class:`~pymeasure.display.widgets.filename_widget.FilenameLineEdit`), a field for the
41 directory (:class:`~pymeasure.display.widgets.directory_widget.DirectoryLineEdit`), and a
42 checkbox to control whether the measurement is stored.
43 """
44 _extensions = ["csv", "txt"]
45 _filename_fixed = False
46
47 def __init__(self, parent=None):
48 super().__init__(parent)
49
50 self._setup_ui()
51 self._layout()
52
53 def _setup_ui(self):
54 self.writefile_toggle = QtWidgets.QCheckBox('Save data', self)
55 self.writefile_toggle.setLayoutDirection(QtCore.Qt.RightToLeft)
56 self.writefile_toggle.setChecked(True)
57 self.writefile_toggle.stateChanged.connect(self.set_input_fields_enabled)
58 self.writefile_toggle.setToolTip(
59 "Control whether the measurement is saved to a file with the filename that is\n"
60 "specified in the field below (checked) or not (unchecked; the data is stored\n"
61 "in a temporary file)."
62 )
63
64 self.filename_input = FilenameLineEdit(self.parent().procedure_class, parent=self)
65
66 self.directory_input = DirectoryLineEdit(parent=self)
67
68 def _layout(self):
69 vbox = QtWidgets.QVBoxLayout()
70 vbox.setContentsMargins(0, 0, 0, 0)
71
72 filename_label = QtWidgets.QLabel(self)
73 filename_label.setText('Filename')
74 filename_label.setToolTip(self.filename_input.toolTip())
75
76 filename_box = QtWidgets.QHBoxLayout()
77 filename_box.addWidget(filename_label)
78 filename_box.addWidget(self.writefile_toggle)
79
80 vbox.addLayout(filename_box)
81 vbox.addWidget(self.filename_input)
82
83 directory_label = QtWidgets.QLabel(self)
84 directory_label.setText('Directory')
85
86 vbox.addWidget(directory_label)
87 vbox.addWidget(self.directory_input)
88
89 self.setLayout(vbox)
90
91 @property
92 def directory(self):
93 """String controlling the directory where the file will be stored."""
94 return self.directory_input.text()
95
96 @directory.setter
97 def directory(self, value):
98 self.directory_input.setText(str(value))
99
100 @property
101 def filename(self):
102 """String controlling the filename that is shown in the filename input field."""
103 return self.filename_input.text()
104
105 @filename.setter
106 def filename(self, value):
107 self.filename_input.setText(str(value))
108
109 @property
110 def filename_base(self):
111 """String containing the base of the filename with which the file will be stored.
112 Can only be read.
113 """
114 filename_split = self.filename.rsplit('.', 1)
115
116 if len(filename_split) > 1 and filename_split[1] in self.extensions:
117 return filename_split[0]
118 else:
119 return self.filename
120
121 @property
122 def filename_extension(self):
123 """String containing the file extension with which the file will be stored.
124
125 Can only be read.
126 """
127 filename_split = self.filename.rsplit('.', 1)
128
129 if len(filename_split) > 1 and filename_split[1] in self.extensions:
130 return filename_split[1]
131 else:
132 return self.extensions[0]
133
134 @property
135 def extensions(self):
136 """List of extensions that are recognized by the widget.
137
138 The first value of this list will be used as default value in case no extension is provided
139 in the filename input field.
140 """
141 return self._extensions
142
143 @extensions.setter
144 def extensions(self, value):
145 self._extensions = [ext.lstrip('.') for ext in value]
146 self.filename_input.set_tool_tip()
147
148 @property
149 def store_measurement(self):
150 """Boolean controlling whether the measurement will be stored."""
151 return self.writefile_toggle.isChecked()
152
153 @store_measurement.setter
154 def store_measurement(self, value):
155 self.writefile_toggle.setChecked(bool(value))
156
157 @property
158 def filename_fixed(self):
159 """Boolean controlling whether the filename input field is frozen.
160 If `True`, the filename field will be visible but disabled (i.e., grayed out)."""
161 return self._filename_fixed
162
163 @filename_fixed.setter
164 def filename_fixed(self, value):
165 self._filename_fixed = value
166
167 # Reassess if the input fields should be enabled or disabled
168 self.set_input_fields_enabled(self.store_measurement)
169
170 def set_input_fields_enabled(self, state):
171 self.filename_input.setEnabled(bool(state) and not self.filename_fixed)
172 self.directory_input.setEnabled(bool(state))