summaryrefslogtreecommitdiff
path: root/silx/gui/data/TextFormatter.py
blob: f074de5f790516a99853867ed507cae1963c83e7 (plain)
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017 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.
#
# ###########################################################################*/
"""This package provides a class sharred by widget from the
data module to format data as text in the same way."""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "26/04/2017"

import numpy
import numbers
import binascii
from silx.third_party import six
from silx.gui import qt


class TextFormatter(qt.QObject):
    """Formatter to convert data to string.

    The method :meth:`toString` returns a formatted string from an input data
    using parameters set to this object.

    It support most python and numpy data, expecting dictionary. Unsupported
    data are displayed using the string representation of the object (`str`).

    It provides a set of parameters to custom the formatting of integer and
    float values (:meth:`setIntegerFormat`, :meth:`setFloatFormat`).

    It also allows to custom the use of quotes to display text data
    (:meth:`setUseQuoteForText`), and custom unit used to display imaginary
    numbers (:meth:`setImaginaryUnit`).

    The object emit an event `formatChanged` every time a parametter is
    changed.
    """

    formatChanged = qt.Signal()
    """Emitted when properties of the formatter change."""

    def __init__(self, parent=None, formatter=None):
        """
        Constructor

        :param qt.QObject parent: Owner of the object
        :param TextFormatter formatter: Instantiate this object from the
            formatter
        """
        qt.QObject.__init__(self, parent)
        if formatter is not None:
            self.__integerFormat = formatter.integerFormat()
            self.__floatFormat = formatter.floatFormat()
            self.__useQuoteForText = formatter.useQuoteForText()
            self.__imaginaryUnit = formatter.imaginaryUnit()
        else:
            self.__integerFormat = "%d"
            self.__floatFormat = "%g"
            self.__useQuoteForText = True
            self.__imaginaryUnit = u"j"

    def integerFormat(self):
        """Returns the format string controlling how the integer data
        are formated by this object.

        This is the C-style format string used by python when formatting
        strings with the modulus operator.

        :rtype: str
        """
        return self.__integerFormat

    def setIntegerFormat(self, value):
        """Set format string controlling how the integer data are
        formated by this object.

        :param str value: Format string (e.g. "%d", "%i", "%08i").
            This is the C-style format string used by python when formatting
            strings with the modulus operator.
        """
        if self.__integerFormat == value:
            return
        self.__integerFormat = value
        self.formatChanged.emit()

    def floatFormat(self):
        """Returns the format string controlling how the floating-point data
        are formated by this object.

        This is the C-style format string used by python when formatting
        strings with the modulus operator.

        :rtype: str
        """
        return self.__floatFormat

    def setFloatFormat(self, value):
        """Set format string controlling how the floating-point data are
        formated by this object.

        :param str value: Format string (e.g. "%.3f", "%d", "%-10.2f",
            "%10.3e").
            This is the C-style format string used by python when formatting
            strings with the modulus operator.
        """
        if self.__floatFormat == value:
            return
        self.__floatFormat = value
        self.formatChanged.emit()

    def useQuoteForText(self):
        """Returns true if the string data are formatted using double quotes.

        Else, no quotes are used.
        """
        return self.__integerFormat

    def setUseQuoteForText(self, useQuote):
        """Set the use of quotes to delimit string data.

        :param bool useQuote: True to use quotes.
        """
        if self.__useQuoteForText == useQuote:
            return
        self.__useQuoteForText = useQuote
        self.formatChanged.emit()

    def imaginaryUnit(self):
        """Returns the unit display for imaginary numbers.

        :rtype: str
        """
        return self.__imaginaryUnit

    def setImaginaryUnit(self, imaginaryUnit):
        """Set the unit display for imaginary numbers.

        :param str imaginaryUnit: Unit displayed after imaginary numbers
        """
        if self.__imaginaryUnit == imaginaryUnit:
            return
        self.__imaginaryUnit = imaginaryUnit
        self.formatChanged.emit()

    def toString(self, data):
        """Format a data into a string using formatter options

        :param object data: Data to render
        :rtype: str
        """
        if isinstance(data, tuple):
            text = [self.toString(d) for d in data]
            return "(" + " ".join(text) + ")"
        elif isinstance(data, (list, numpy.ndarray)):
            text = [self.toString(d) for d in data]
            return "[" + " ".join(text) + "]"
        elif isinstance(data, numpy.void):
            dtype = data.dtype
            if data.dtype.fields is not None:
                text = [self.toString(data[f]) for f in dtype.fields]
                return "(" + " ".join(text) + ")"
            return "0x" + binascii.hexlify(data).decode("ascii")
        elif isinstance(data, (numpy.string_, numpy.object_, bytes)):
            # This have to be done before checking python string inheritance
            try:
                text = "%s" % data.decode("utf-8")
                if self.__useQuoteForText:
                    text = "\"%s\"" % text.replace("\"", "\\\"")
                return text
            except UnicodeDecodeError:
                pass
            return "0x" + binascii.hexlify(data).decode("ascii")
        elif isinstance(data, six.string_types):
            text = "%s" % data
            if self.__useQuoteForText:
                text = "\"%s\"" % text.replace("\"", "\\\"")
            return text
        elif isinstance(data, (numpy.integer, numbers.Integral)):
            return self.__integerFormat % data
        elif isinstance(data, (numbers.Real, numpy.floating)):
            # It have to be done before complex checking
            return self.__floatFormat % data
        elif isinstance(data, (numpy.complex_, numbers.Complex)):
            text = ""
            if data.real != 0:
                text += self.__floatFormat % data.real
            if data.real != 0 and data.imag != 0:
                if data.imag < 0:
                    template = self.__floatFormat + " - " + self.__floatFormat + self.__imaginaryUnit
                    params = (data.real, -data.imag)
                else:
                    template = self.__floatFormat + " + " + self.__floatFormat + self.__imaginaryUnit
                    params = (data.real, data.imag)
            else:
                if data.imag != 0:
                    template = self.__floatFormat + self.__imaginaryUnit
                    params = (data.imag)
                else:
                    template = self.__floatFormat
                    params = (data.real)
            return template % params
        return str(data)