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
|
# 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.
#
# ###########################################################################*/
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__license__ = "MIT"
__date__ = "03/01/2019"
import functools
from silx.gui import qt
from ..utils import units
class UnitLabel(qt.QLabel):
"""QLabel displaying an unit.
"""
def __init__(self, parent):
super(UnitLabel, self).__init__(parent)
self.__unit = None
self.__model = None
self.__isUnitEditable = False
def setUnit(self, unit):
"""
Set the displayed unit.
:param pyFAI.gui.units.Unit unit: An unit
"""
if self.__unit is unit:
return
self.__unit = unit
if unit is None:
self.setText("")
self.setToolTip("No unit")
else:
self.setText(unit.symbol)
self.setToolTip(u"%s (%s)" % (unit.fullname, unit.symbol))
def getUnit(self):
"""
:rtype: pyFAI.gui.units.Unit
"""
return self.__unit
def setUnitModel(self, model):
"""
Set the model containing an unit.
:param pyFAI.gui.model.DataUnit.DataUnit: Model containing
the unit.
"""
if self.__model is not None:
self.__model.changed.disconnect(self.__unitChanged)
self.__model = model
if self.__model is not None:
self.__model.changed.connect(self.__unitChanged)
self.__unitChanged()
def __unitChanged(self):
if self.__model is None:
self.setUnit(None)
else:
self.setUnit(self.__model.value())
def getUnitModel(self):
"""
:rtype: pyFAI.gui.model.DataUnit.DataUnit
"""
return self.__model
def __popupUnitSelection(self, pos):
"""Display a popup list to allow to select a new unit"""
if self.__unit is None:
return
unitList = units.Unit.get_units(self.__unit.dimensionality)
if len(unitList) <= 1:
return
menu = qt.QMenu(self)
menu.addSection("Unit for %s" % self.__unit.dimensionality.fullname.lower())
for unit in unitList:
action = qt.QAction(menu)
text = u"%s: %s" % (unit.fullname, unit.symbol)
if unit is self.__unit:
text += " (current)"
action.setText(text)
action.triggered.connect(functools.partial(self.__unitSelected, unit))
menu.addAction(action)
menu.popup(pos)
def __unitSelected(self, unit):
model = self.getUnitModel()
if model is not None:
model.setValue(unit)
else:
self.setUnit(unit)
def mouseReleaseEvent(self, event):
if event.button() == qt.Qt.LeftButton and self.__isUnitEditable:
pos = event.pos()
pos = self.mapToGlobal(pos)
self.__popupUnitSelection(pos)
return
super(UnitLabel, self).mouseReleaseEvent(event)
def setUnitEditable(self, isUnitEditable):
self.__isUnitEditable = isUnitEditable
def isUnitEditable(self):
return self.__isUnitEditable
|