1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "16/10/2020"
from math import pi
from silx.gui import qt
from pyFAI.utils import get_ui_file
from ..utils import units
from ..model.DataModel import DataModel
from ..model.GeometryModel import GeometryModel
from ..model.Fit2dGeometryModel import Fit2dGeometryModel
from ...geometry import Geometry
class GeometryDialog(qt.QDialog):
"""Dialog to display a selected geometry
"""
def __init__(self, parent=None):
super(GeometryDialog, self).__init__(parent)
filename = get_ui_file("geometry-dialog.ui")
qt.loadUi(filename, self)
self.__geometry = GeometryModel()
self.__fit2dGeometry = Fit2dGeometryModel()
self.__detector = None
self.__originalGeometry = None
self.__updatingModel = False
# Connect buttons
self._buttonBox.rejected.connect(self.reject)
self._buttonBox.accepted.connect(self.accept)
# Create shared units
angleUnit = DataModel()
angleUnit.setValue(units.Unit.RADIAN)
lengthUnit = DataModel()
lengthUnit.setValue(units.Unit.METER)
pixelUnit = DataModel()
pixelUnit.setValue(units.Unit.PIXEL)
# Connect pyFAI widgets to units
self._pyfaiDistance.setDisplayedUnitModel(lengthUnit)
self._pyfaiDistance.setModelUnit(units.Unit.METER)
self._pyfaiDistanceUnit.setUnitModel(lengthUnit)
self._pyfaiDistanceUnit.setUnitEditable(True)
self._pyfaiPoni1.setDisplayedUnitModel(lengthUnit)
self._pyfaiPoni1.setModelUnit(units.Unit.METER)
self._pyfaiPoni1Unit.setUnitModel(lengthUnit)
self._pyfaiPoni1Unit.setUnitEditable(True)
self._pyfaiPoni2.setDisplayedUnitModel(lengthUnit)
self._pyfaiPoni2.setModelUnit(units.Unit.METER)
self._pyfaiPoni2Unit.setUnitModel(lengthUnit)
self._pyfaiPoni2Unit.setUnitEditable(True)
self._pyfaiRotation1.setDisplayedUnitModel(angleUnit)
self._pyfaiRotation1.setModelUnit(units.Unit.RADIAN)
self._pyfaiRotation1Unit.setUnitModel(angleUnit)
self._pyfaiRotation1Unit.setUnitEditable(True)
self._pyfaiRotation2.setDisplayedUnitModel(angleUnit)
self._pyfaiRotation2.setModelUnit(units.Unit.RADIAN)
self._pyfaiRotation2Unit.setUnitModel(angleUnit)
self._pyfaiRotation2Unit.setUnitEditable(True)
self._pyfaiRotation3.setDisplayedUnitModel(angleUnit)
self._pyfaiRotation3.setModelUnit(units.Unit.RADIAN)
self._pyfaiRotation3Unit.setUnitModel(angleUnit)
self._pyfaiRotation3Unit.setUnitEditable(True)
# Connect fit2d widgets to units
self._fit2dDistance.setDisplayedUnit(units.Unit.MILLIMETER)
self._fit2dDistance.setModelUnit(units.Unit.MILLIMETER)
self._fit2dDistanceUnit.setUnit(units.Unit.MILLIMETER)
self._fit2dCenterX.setDisplayedUnitModel(pixelUnit)
self._fit2dCenterX.setModelUnit(units.Unit.PIXEL)
self._fit2dCenterXUnit.setUnit(units.Unit.PIXEL)
self._fit2dCenterY.setDisplayedUnitModel(pixelUnit)
self._fit2dCenterY.setModelUnit(units.Unit.PIXEL)
self._fit2dCenterYUnit.setUnit(units.Unit.PIXEL)
self._fit2dTilt.setDisplayedUnit(units.Unit.DEGREE)
self._fit2dTilt.setModelUnit(units.Unit.DEGREE)
self._fit2dTiltUnit.setUnit(units.Unit.DEGREE)
self._fit2dTiltPlan.setDisplayedUnit(units.Unit.DEGREE)
self._fit2dTiltPlan.setModelUnit(units.Unit.DEGREE)
self._fit2dTiltPlanUnit.setUnit(units.Unit.DEGREE)
# Connect fit2d model-widget
self._fit2dDistance.setModel(self.__fit2dGeometry.distance())
self._fit2dCenterX.setModel(self.__fit2dGeometry.centerX())
self._fit2dCenterY.setModel(self.__fit2dGeometry.centerY())
self._fit2dTilt.setModel(self.__fit2dGeometry.tilt())
self._fit2dTiltPlan.setModel(self.__fit2dGeometry.tiltPlan())
self._pyfaiDistance.setModel(self.__geometry.distance())
self._pyfaiPoni1.setModel(self.__geometry.poni1())
self._pyfaiPoni2.setModel(self.__geometry.poni2())
self._pyfaiRotation1.setModel(self.__geometry.rotation1())
self._pyfaiRotation2.setModel(self.__geometry.rotation2())
self._pyfaiRotation3.setModel(self.__geometry.rotation3())
self.__geometry.changed.connect(self.__updateFit2dFromPyfai)
self.__fit2dGeometry.changed.connect(self.__updatePyfaiFromFit2d)
self.__geometry.changed.connect(self.__updateButtons)
# NOTE: All the buttons have to be create here.
# Changing available buttons on the focus event create a segfault
types = (qt.QDialogButtonBox.Ok | qt.QDialogButtonBox.Cancel |
qt.QDialogButtonBox.Reset | qt.QDialogButtonBox.Close)
self._buttonBox.setStandardButtons(types)
resetButton = self._buttonBox.button(qt.QDialogButtonBox.Reset)
resetButton.clicked.connect(self.__resetToOriginalGeometry)
self.__updateButtons()
def accept(self):
self.__originalGeometry = None
return qt.QDialog.accept(self)
def reject(self):
self.__originalGeometry = None
return qt.QDialog.reject(self)
def isReadOnly(self):
"""
Returns True if the dialog is in read only.
In read-only mode, the geometry is displayed, but the user can't edited
it.
By default, this returns false.
:rtype: bool
"""
return self._pyfaiDistance.isReadOnly()
def setReadOnly(self, readOnly):
"""
Enable or disable the read-only mode.
:param bool readOnly: True to enable the read-only mode.
"""
self._pyfaiDistance.setReadOnly(readOnly)
self._pyfaiPoni1.setReadOnly(readOnly)
self._pyfaiPoni2.setReadOnly(readOnly)
self._pyfaiRotation1.setReadOnly(readOnly)
self._pyfaiRotation2.setReadOnly(readOnly)
self._pyfaiRotation3.setReadOnly(readOnly)
self._fit2dDistance.setReadOnly(readOnly)
self._fit2dCenterX.setReadOnly(readOnly)
self._fit2dCenterY.setReadOnly(readOnly)
self._fit2dTilt.setReadOnly(readOnly)
self._fit2dTiltPlan.setReadOnly(readOnly)
def __createPyfaiGeometry(self):
geometry = self.__geometry
if not geometry.isValid(checkWaveLength=False):
raise RuntimeError("The geometry is not valid")
dist = geometry.distance().value()
poni1 = geometry.poni1().value()
poni2 = geometry.poni2().value()
rot1 = geometry.rotation1().value()
rot2 = geometry.rotation2().value()
rot3 = geometry.rotation3().value()
wavelength = geometry.wavelength().value()
result = Geometry(dist=dist,
poni1=poni1,
poni2=poni2,
rot1=rot1,
rot2=rot2,
rot3=rot3,
detector=self.__detector,
wavelength=wavelength)
return result
def __updatePyfaiFromFit2d(self):
if self.__updatingModel:
return
self.__updatingModel = True
geometry = self.__fit2dGeometry
error = None
distance = None
poni1 = None
poni2 = None
rotation1 = None
rotation2 = None
rotation3 = None
if geometry is None:
error = "No geometry to compute pyFAI geometry."
pass
elif self.__detector is None:
error = "No detector defined. It is needed to compute the pyFAI geometry."
elif not geometry.isValid():
error = "The current geometry is not valid to compute the pyFAI one."
else:
pyFAIGeometry = Geometry(detector=self.__detector)
try:
f2d_distance = geometry.distance().value()
f2d_centerX = geometry.centerX().value()
f2d_centerY = geometry.centerY().value()
f2d_tiltPlan = geometry.tiltPlan().value()
f2d_tilt = geometry.tilt().value()
pyFAIGeometry.setFit2D(directDist=f2d_distance,
centerX=f2d_centerX,
centerY=f2d_centerY,
tilt=f2d_tilt,
tiltPlanRotation=f2d_tiltPlan)
except Exception:
error = "This geometry can't be modelized with pyFAI."
else:
distance = pyFAIGeometry.dist
poni1 = pyFAIGeometry.poni1
poni2 = pyFAIGeometry.poni2
rotation1 = pyFAIGeometry.rot1
rotation2 = pyFAIGeometry.rot2
rotation3 = pyFAIGeometry.rot3
self._fit2dError.setVisible(error is not None)
self._fit2dError.setText(error)
self.__geometry.lockSignals()
self.__geometry.distance().setValue(distance)
self.__geometry.poni1().setValue(poni1)
self.__geometry.poni2().setValue(poni2)
self.__geometry.rotation1().setValue(rotation1)
self.__geometry.rotation2().setValue(rotation2)
self.__geometry.rotation3().setValue(rotation3)
self.__geometry.unlockSignals()
self.__updatingModel = False
def __updateFit2dFromPyfai(self):
if self.__updatingModel:
return
self.__updatingModel = True
geometry = self.__geometry
error = None
distance = None
centerX = None
centerY = None
tiltPlan = None
tilt = None
if geometry is None:
error = "No geometry to compute Fit2D geometry."
pass
elif self.__detector is None:
error = "No detector defined. It is needed to compute the Fit2D geometry."
elif not geometry.isValid(checkWaveLength=False):
error = "The current geometry is not valid to compute the Fit2D one."
else:
pyFAIGeometry = self.__createPyfaiGeometry()
try:
result = pyFAIGeometry.getFit2D()
except Exception:
error = "This geometry can't be modelized with Fit2D."
else:
distance = result["directDist"]
centerX = result["centerX"]
centerY = result["centerY"]
tilt = result["tilt"]
tiltPlan = result["tiltPlanRotation"]
self._fit2dError.setVisible(error is not None)
self._fit2dError.setText(error)
self.__fit2dGeometry.lockSignals()
self.__fit2dGeometry.distance().setValue(distance)
self.__fit2dGeometry.centerX().setValue(centerX)
self.__fit2dGeometry.centerY().setValue(centerY)
self.__fit2dGeometry.tilt().setValue(tilt)
self.__fit2dGeometry.tiltPlan().setValue(tiltPlan)
self.__fit2dGeometry.unlockSignals()
self.__updatingModel = False
def __resetToOriginalGeometry(self):
if self.__originalGeometry is None:
return
self.__geometry.setFrom(self.__originalGeometry)
def __updateButtons(self):
"""Update the state of the dialog's buttons"""
haveChanges = self.__geometry != self.__originalGeometry
existing = [qt.QDialogButtonBox.Ok, qt.QDialogButtonBox.Cancel, qt.QDialogButtonBox.Reset, qt.QDialogButtonBox.Close]
if haveChanges:
available = set([qt.QDialogButtonBox.Ok, qt.QDialogButtonBox.Cancel, qt.QDialogButtonBox.Reset])
else:
available = set([qt.QDialogButtonBox.Close])
for buttonType in existing:
button = self._buttonBox.button(buttonType)
isVisible = buttonType in available
button.setVisible(isVisible)
def setDetector(self, detector):
"""Set the used detector.
This information is needed to display the Fit2D geometry.
"""
self.__detector = detector
self.__updateFit2dFromPyfai()
def setGeometryModel(self, geometryModel):
"""Set the geometry to display.
:param ~pyFAI.gui.model.GeometryModel geometryModel: A geometry.
"""
assert(isinstance(geometryModel, GeometryModel))
if self.__geometry is geometryModel:
return
self.__originalGeometry = geometryModel
self.__geometry.changed.disconnect(self.__updateButtons)
self.__geometry.setFrom(geometryModel)
self.__geometry.changed.connect(self.__updateButtons)
self.__updateButtons()
def geometryModel(self):
"""Returns the geometry model
:rtype: ~pyFAI.gui.model.GeometryModel
"""
return self.__geometry
|