Debian Codegraph @

src/fabio/qt/dialogs.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Project: Fast Azimuthal integration
5 # https://github.com/silx-kit/pyFAI
6 #
7 # Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
8 #
9 # Authors: Gael Goret <[email protected]>
10 #
11 # Permission is hereby granted, free of charge, to any person obtaining a copy
12 # of this software and associated documentation files (the "Software"), to deal
13 # in the Software without restriction, including without limitation the rights
14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the Software is
16 # furnished to do so, subject to the following conditions.
17 # .
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
20 # .
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 # FROM THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 """
28 Portable diffraction images viewer/converter
29 * Written in Python, it combines the functionalities of the I/O library fabIO
30 with a user-friendly Qt GUI.
31 * Image converter is also a light viewer based on the visualization tool
32 provided by the module matplotlib.
33 """
34
35 # ----------------------------------------------------------------------
36 # Imports (QtPy, FabIO, NumPy, Matplotlib, …)
37 # ----------------------------------------------------------------------
38 import numpy
39 from qtpy import QtWidgets as qt
40 from qtpy.QtWidgets import (
41 QMessageBox,
42 QLabel,
43 QDialog,
44 QDialogButtonBox,
45 QGroupBox,
46 )
47
48 numpy.seterr(divide="ignore")
49 output_format = [
50 "*.bin",
51 "*.cbf",
52 "*.edf",
53 "*.h5",
54 "*.img",
55 "*.mar2300",
56 "*.mar3450",
57 "*.marccd",
58 "*.tiff",
59 "*.sfrm",
60 ]
61
62
63 # ──────────────────────────────────────────────────────────────────────
64 # Dialog windows – now fully English
65 # ──────────────────────────────────────────────────────────────────────
66 class CounterFormatOptionDialog(QDialog):
67 """Dialog that lets the user set the file‑counter format used when saving
68 a series of files."""
69
70 def __init__(self, counter_format, parent=None):
71 super().__init__(parent)
72 self.setWindowTitle("Counter format")
73 self.resize(350, 100)
74 # ----- layout ----------------------------------------------------
75 button_box = QDialogButtonBox(self)
76 button_box.setGeometry(qt.QRect(0, 60, 341, 32))
77 button_box.setOrientation(qt.Qt.Horizontal)
78 button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
79 label = QLabel("File counter format:", self)
80 label.setGeometry(qt.QRect(38, 23, 181, 16))
81 self.lineEdit = qt.QLineEdit(self)
82 self.lineEdit.setGeometry(qt.QRect(175, 18, 113, 25))
83 self.lineEdit.setText(counter_format)
84 # ----- signals ---------------------------------------------------
85 button_box.accepted.connect(self.accept)
86 button_box.rejected.connect(self.reject)
87
88 def exec_(self):
89 """Return the new format string if the user clicks *Ok*,
90 otherwise return the original value."""
91 if super().exec_() == QDialog.Accepted:
92 new_text = str(self.lineEdit.text()).strip()
93 if new_text:
94 return new_text
95 QMessageBox.warning(
96 self,
97 "Warning",
98 "The counter format cannot be empty – the previous value will be kept.",
99 )
100 return self.lineEdit.text()
101
102
103 class DownSamplingDialog(QDialog):
104 """Dialog that gathers the parameters required for down‑sampling
105 (summation) of a stack of images."""
106
107 def __init__(self, parent=None):
108 super().__init__(parent)
109 self.setWindowTitle("Down‑sampling")
110 self.resize(410, 250)
111 # ----- layout ----------------------------------------------------
112 button_box = QDialogButtonBox(self)
113 button_box.setGeometry(qt.QRect(45, 200, 341, 32))
114 button_box.setOrientation(qt.Qt.Horizontal)
115 button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
116 # Number of files to sum
117 label_n = QLabel("Number of files to sum:", self)
118 label_n.setGeometry(qt.QRect(38, 63, 181, 16))
119 self.lineEdit_n = qt.QLineEdit(self)
120 self.lineEdit_n.setGeometry(qt.QRect(220, 58, 113, 25))
121 # Starting phi angle
122 label_phi0 = QLabel("Starting φ angle (degrees):", self)
123 label_phi0.setGeometry(qt.QRect(90, 100, 191, 20))
124 self.lineEdit_phi0 = qt.QLineEdit(self)
125 self.lineEdit_phi0.setGeometry(qt.QRect(300, 95, 113, 25))
126 # Phi step
127 label_step = QLabel("φ step (degrees):", self)
128 label_step.setGeometry(qt.QRect(151, 133, 101, 16))
129 self.lineEdit_step = qt.QLineEdit(self)
130 self.lineEdit_step.setGeometry(qt.QRect(260, 130, 113, 25))
131 # ----- signals ---------------------------------------------------
132 button_box.accepted.connect(self.accept)
133 button_box.rejected.connect(self.reject)
134
135 def exec_(self):
136 """Return a tuple *(thick, start_angle, step_angle)* if the user
137 confirms, otherwise *(None, None, None)*."""
138 if super().exec_() == QDialog.Accepted:
139 if (
140 self.lineEdit_n.text()
141 and self.lineEdit_phi0.text()
142 and self.lineEdit_step.text()
143 ):
144 try:
145 thick = int(self.lineEdit_n.text())
146 start_angle = float(self.lineEdit_phi0.text())
147 step_angle = float(self.lineEdit_step.text())
148 except ValueError:
149 QMessageBox.warning(
150 self,
151 "Invalid input",
152 "Please enter numeric values for all fields.",
153 )
154 return None, None, None
155 return thick, start_angle, step_angle
156 QMessageBox.warning(
157 self,
158 "Missing data",
159 "All fields are mandatory – please fill them before confirming.",
160 )
161 return None, None, None
162
163
164 class BinDialog(QDialog):
165 """Dialog that asks the user for the parameters needed to read a raw
166 binary data block (dimensions, offset, byte‑code, endian)."""
167
168 def __init__(self, parent=None):
169 super().__init__(parent)
170 self.setWindowTitle("Binary data block opening")
171 self.resize(410, 270)
172 # ----- internal state --------------------------------------------
173 self.dim1 = None
174 self.dim2 = None
175 self.offset = None
176 self.bytecode = None
177 self.endian = None
178 # ----- layout ----------------------------------------------------
179 button_box = QDialogButtonBox(self)
180 button_box.setGeometry(qt.QRect(50, 230, 341, 32))
181 button_box.setOrientation(qt.Qt.Horizontal)
182 button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
183 group_box = QGroupBox("Binary data block required information:", self)
184 group_box.setGeometry(qt.QRect(10, 10, 370, 191))
185 # Dimension 1
186 label_dim1 = QLabel("Dimension 1:", group_box)
187 label_dim1.setGeometry(qt.QRect(67, 48, 91, 16))
188 self.lineEdit_dim1 = qt.QLineEdit(group_box)
189 self.lineEdit_dim1.setGeometry(qt.QRect(185, 40, 91, 25))
190 # Dimension 2
191 label_dim2 = QLabel("Dimension 2:", group_box)
192 label_dim2.setGeometry(qt.QRect(66, 76, 91, 16))
193 self.lineEdit_dim2 = qt.QLineEdit(group_box)
194 self.lineEdit_dim2.setGeometry(qt.QRect(185, 70, 91, 25))
195 # Offset
196 label_offset = QLabel("Offset:", group_box)
197 label_offset.setGeometry(qt.QRect(105, 106, 51, 16))
198 self.lineEdit_offset = qt.QLineEdit(group_box)
199 self.lineEdit_offset.setGeometry(qt.QRect(184, 100, 91, 25))
200 self.lineEdit_offset.setText("0")
201 # Byte code
202 label_bc = QLabel("Byte code:", group_box)
203 label_bc.setGeometry(qt.QRect(70, 130, 91, 16))
204 self.comboBox_bc = qt.QComboBox(group_box)
205 self.comboBox_bc.setGeometry(qt.QRect(173, 123, 91, 25))
206 for bc in [
207 "int8",
208 "int16",
209 "int32",
210 "int64",
211 "uint8",
212 "uint16",
213 "uint32",
214 "uint64",
215 "float32",
216 "float64",
217 ]:
218 self.comboBox_bc.addItem(bc)
219 self.comboBox_bc.setCurrentIndex(2) # default to int32
220 # Endian
221 label_endian = QLabel("Endian:", group_box)
222 label_endian.setGeometry(qt.QRect(98, 170, 61, 16))
223 self.comboBox_endian = qt.QComboBox(group_box)
224 self.comboBox_endian.setGeometry(qt.QRect(182, 166, 91, 25))
225 self.comboBox_endian.addItem("Little‑endian")
226 self.comboBox_endian.addItem("Big‑endian")
227 # ----- signals ---------------------------------------------------
228 button_box.accepted.connect(self._accept)
229 button_box.rejected.connect(self.reject)
230
231 # ------------------------------------------------------------------
232 def _accept(self):
233 """Validate the entries and store them before closing the dialog."""
234 if (
235 self.lineEdit_dim1.text()
236 and self.lineEdit_dim2.text()
237 and self.lineEdit_offset.text()
238 ):
239 try:
240 self.dim1 = int(self.lineEdit_dim1.text())
241 self.dim2 = int(self.lineEdit_dim2.text())
242 self.offset = int(self.lineEdit_offset.text())
243 except ValueError:
244 QMessageBox.warning(
245 self,
246 "Invalid input",
247 "Dimensions and offset must be integer numbers.",
248 )
249 return
250 else:
251 QMessageBox.warning(
252 self,
253 "Missing data",
254 "All fields are mandatory – please fill them before confirming.",
255 )
256 return
257 self.bytecode = str(self.comboBox_bc.currentText())
258 # QtPy returns the displayed text, which we map back to the original
259 # “Short” / “Long” terminology used later in the code.
260 self.endian = (
261 "Short" if self.comboBox_endian.currentText() == "Little‑endian" else "Long"
262 )
263 self.accept()
264
265 # ------------------------------------------------------------------
266 def exec_(self):
267 """Return the 5‑tuple *(dim1, dim2, offset, bytecode, endian)*
268 if the user clicks **Ok**, otherwise a tuple of ``None``."""
269 if super().exec_() == QDialog.Accepted:
270 return (
271 self.dim1,
272 self.dim2,
273 self.offset,
274 self.bytecode,
275 self.endian,
276 )
277 return None, None, None, None, None