File: QuantityLabel.py

package info (click to toggle)
pyfai 0.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,460 kB
  • sloc: python: 49,743; lisp: 7,059; sh: 225; ansic: 165; makefile: 119
file content (209 lines) | stat: -rw-r--r-- 7,173 bytes parent folder | download | duplicates (4)
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
# 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 numpy
import numbers
import logging
import functools

from silx.gui import qt

from ..utils import units


_logger = logging.getLogger(__name__)


class QuantityLabel(qt.QLabel):

    def __init__(self, parent=None):
        qt.QLabel.__init__(self, parent)
        self.__prefix = ""
        self.__formatter = "{value}"
        self.__floatFormatter = None
        self.__value = None
        self.__internalUnit = None
        self.__displayedUnit = None
        self.__isUnitEditable = False
        self.__displayedUnitModel = None
        self.__preferedSize = None
        self.__elasticSize = False

    def setInternalUnit(self, unit):
        self.__internalUnit = unit
        self.__updateText()

    def setDisplayedUnitModel(self, model):
        """
        Set the model containing an unit.

        :param pyFAI.gui.model.DataUnit.DataUnit: Model containing
            the unit.
        """
        if self.__displayedUnitModel is not None:
            self.__displayedUnitModel.changed.disconnect(self.__displayedUnitChanged)
        self.__displayedUnitModel = model
        if self.__displayedUnitModel is not None:
            self.__displayedUnitModel.changed.connect(self.__displayedUnitChanged)
        self.__displayedUnitChanged()

    def getDisplayedUnitModel(self):
        return self.__displayedUnitModel

    def __displayedUnitChanged(self):
        model = self.__displayedUnitModel
        if model is None:
            self.setDisplayedUnit(None)
        else:
            self.setDisplayedUnit(model.value())

    def usedUnit(self):
        """Returns the unit used to display the quantity"""
        if self.__displayedUnit is not None:
            return self.__displayedUnit
        else:
            return self.__internalUnit

    def setDisplayedUnit(self, unit):
        self.__displayedUnit = unit
        self.__updateText()

    def setPrefix(self, prefix):
        self.__prefix = prefix
        self.__updateText()

    def setFormatter(self, formatter):
        self.__formatter = formatter
        self.__updateText()

    def setFloatFormatter(self, formatter):
        """Set a specific formatter for float.

        If this formatter is None (default value) or the value is not a
        floatting point, the default formatter is used.
        """
        self.__floatFormatter = formatter
        self.__updateText()

    def setElasticSize(self, useElasticSize):
        if self.__elasticSize == useElasticSize:
            return
        self.__elasticSize = useElasticSize
        if self.__elasticSize is None:
            self.__preferedSize = None
        self.updateGeometry()

    def sizeHint(self):
        if self.__preferedSize is not None:
            return self.__preferedSize
        return qt.QLabel.sizeHint(self)

    def setValue(self, value):
        self.__value = value
        self.__updateText()

    def __updateText(self):
        if self.__value is None:
            text = "na"
        else:
            value = self.__value
            if numpy.isscalar(value) and numpy.isnan(value):
                text = "nan"
            else:
                currentUnit = self.usedUnit()
                if currentUnit is not None:
                    symbol = currentUnit.symbol
                else:
                    symbol = None

                try:
                    value = units.convert(value, self.__internalUnit, currentUnit)
                    if isinstance(value, numbers.Real) and self.__floatFormatter is not None:
                        formatter = self.__floatFormatter
                    else:
                        formatter = self.__formatter
                    text = formatter.format(value=value)
                    if symbol is not None:
                        text = text + " " + symbol
                except Exception as e:
                    _logger.error("Error while formating value: %s", e.args[0])
                    _logger.debug("Backtrace", exc_info=True)
                    text = "error"

        text = self.__prefix + text
        self.setText(text)
        if self.__elasticSize:
            size = self.size()
            if self.__preferedSize is None or self.__preferedSize.width() < size.width():
                self.__preferedSize = size

    def __popupUnitSelection(self, pos):
        """Display a popup list to allow to select a new unit"""
        currentUnit = self.usedUnit()
        if currentUnit is None:
            return

        unitList = units.Unit.get_units(currentUnit.dimensionality)
        if len(unitList) <= 1:
            return

        menu = qt.QMenu(self)
        menu.addSection("Unit for %s" % currentUnit.dimensionality.fullname.lower())

        for unit in unitList:
            action = qt.QAction(menu)
            text = "%s: %s" % (unit.fullname, unit.symbol)
            if unit is currentUnit:
                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.getDisplayedUnitModel()
        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(QuantityLabel, self).mouseReleaseEvent(event)

    def setUnitEditable(self, isUnitEditable):
        self.__isUnitEditable = isUnitEditable

    def isUnitEditable(self):
        return self.__isUnitEditable