summaryrefslogtreecommitdiff
path: root/silx/gui/data/HexaTableView.py
blob: 1617f0a711b5a376c8a6b5dc6e66bfa947c4e57a (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
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-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.
#
# ###########################################################################*/
"""
This module defines model and widget to display raw data using an
hexadecimal viewer.
"""
from __future__ import division

import collections

import numpy
import six

from silx.gui import qt
import silx.io.utils
from silx.gui.widgets.TableWidget import CopySelectedCellsAction

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "23/05/2018"


class _VoidConnector(object):
    """Byte connector to a numpy.void data.

    It uses a cache of 32 x 1KB and a direct read access API from HDF5.
    """

    def __init__(self, data):
        self.__cache = collections.OrderedDict()
        self.__len = data.itemsize
        self.__data = data

    def __getBuffer(self, bufferId):
        if bufferId not in self.__cache:
            pos = bufferId << 10
            data = self.__data
            if hasattr(data, "tobytes"):
                data = data.tobytes()[pos:pos + 1024]
            else:
                # Old fashion
                data = data.data[pos:pos + 1024]

            self.__cache[bufferId] = data
            if len(self.__cache) > 32:
                self.__cache.popitem()
        else:
            data = self.__cache[bufferId]
        return data

    def __getitem__(self, pos):
        """Returns the value of the byte at the given position.

        :param uint pos: Position of the byte
        :rtype: int
        """
        bufferId = pos >> 10
        bufferPos = pos & 0b1111111111
        data = self.__getBuffer(bufferId)
        value = data[bufferPos]
        if six.PY2:
            return ord(value)
        else:
            return value

    def __len__(self):
        """
        Returns the number of available bytes.

        :rtype: uint
        """
        return self.__len


class HexaTableModel(qt.QAbstractTableModel):
    """This data model provides access to a numpy void data.

    Bytes are displayed one by one as a hexadecimal viewer.

    The 16th first columns display bytes as hexadecimal, the last column
    displays the same data as ASCII.

    :param qt.QObject parent: Parent object
    :param data: A numpy array or a h5py dataset
    """
    def __init__(self, parent=None, data=None):
        qt.QAbstractTableModel.__init__(self, parent)

        self.__data = None
        self.__connector = None
        self.setArrayData(data)

        if hasattr(qt.QFontDatabase, "systemFont"):
            self.__font = qt.QFontDatabase.systemFont(qt.QFontDatabase.FixedFont)
        else:
            self.__font = qt.QFont("Monospace")
            self.__font.setStyleHint(qt.QFont.TypeWriter)
        self.__palette = qt.QPalette()

    def rowCount(self, parent_idx=None):
        """Returns number of rows to be displayed in table"""
        if self.__connector is None:
            return 0
        return ((len(self.__connector) - 1) >> 4) + 1

    def columnCount(self, parent_idx=None):
        """Returns number of columns to be displayed in table"""
        return 0x10 + 1

    def data(self, index, role=qt.Qt.DisplayRole):
        """QAbstractTableModel method to access data values
        in the format ready to be displayed"""
        if not index.isValid():
            return None

        if self.__connector is None:
            return None

        row = index.row()
        column = index.column()

        if role == qt.Qt.DisplayRole:
            if column == 0x10:
                start = (row << 4)
                text = ""
                for i in range(0x10):
                    pos = start + i
                    if pos >= len(self.__connector):
                        break
                    value = self.__connector[pos]
                    if value > 0x20 and value < 0x7F:
                        text += chr(value)
                    else:
                        text += "."
                return text
            else:
                pos = (row << 4) + column
                if pos < len(self.__connector):
                    value = self.__connector[pos]
                    return "%02X" % value
                else:
                    return ""
        elif role == qt.Qt.FontRole:
            return self.__font

        elif role == qt.Qt.BackgroundColorRole:
            pos = (row << 4) + column
            if column != 0x10 and pos >= len(self.__connector):
                return self.__palette.color(qt.QPalette.Disabled, qt.QPalette.Background)
            else:
                return None

        return None

    def headerData(self, section, orientation, role=qt.Qt.DisplayRole):
        """Returns the 0-based row or column index, for display in the
        horizontal and vertical headers"""
        if section == -1:
            # PyQt4 send -1 when there is columns but no rows
            return None

        if role == qt.Qt.DisplayRole:
            if orientation == qt.Qt.Vertical:
                return "%02X" % (section << 4)
            if orientation == qt.Qt.Horizontal:
                if section == 0x10:
                    return "ASCII"
                else:
                    return "%02X" % section
        elif role == qt.Qt.FontRole:
            return self.__font
        elif role == qt.Qt.TextAlignmentRole:
            if orientation == qt.Qt.Vertical:
                return qt.Qt.AlignRight
            if orientation == qt.Qt.Horizontal:
                if section == 0x10:
                    return qt.Qt.AlignLeft
                else:
                    return qt.Qt.AlignCenter
        return None

    def flags(self, index):
        """QAbstractTableModel method to inform the view whether data
        is editable or not.
        """
        row = index.row()
        column = index.column()
        pos = (row << 4) + column
        if column != 0x10 and pos >= len(self.__connector):
            return qt.Qt.NoItemFlags
        return qt.QAbstractTableModel.flags(self, index)

    def setArrayData(self, data):
        """Set the data array.

        :param data: A numpy object or a dataset.
        """
        if qt.qVersion() > "4.6":
            self.beginResetModel()

        self.__connector = None
        self.__data = data
        if self.__data is not None:
            if silx.io.utils.is_dataset(self.__data):
                data = data[()]
            elif isinstance(self.__data, numpy.ndarray):
                data = data[()]
            self.__connector = _VoidConnector(data)

        if qt.qVersion() > "4.6":
            self.endResetModel()
        else:
            self.reset()

    def arrayData(self):
        """Returns the internal data.

        :rtype: numpy.ndarray of h5py.Dataset
        """
        return self.__data


class HexaTableView(qt.QTableView):
    """TableView using HexaTableModel as default model.

    It customs the column size to provide a better layout.
    """
    def __init__(self, parent=None):
        """
        Constructor

        :param qt.QWidget parent: parent QWidget
        """
        qt.QTableView.__init__(self, parent)

        model = HexaTableModel(self)
        self.setModel(model)
        self._copyAction = CopySelectedCellsAction(self)
        self.addAction(self._copyAction)

    def copy(self):
        self._copyAction.trigger()

    def setArrayData(self, data):
        """Set the data array.

        :param data: A numpy object or a dataset.
        """
        self.model().setArrayData(data)
        self.__fixHeader()

    def __fixHeader(self):
        """Update the view according to the state of the auto-resize"""
        header = self.horizontalHeader()
        if qt.qVersion() < "5.0":
            setResizeMode = header.setResizeMode
        else:
            setResizeMode = header.setSectionResizeMode

        header.setDefaultSectionSize(30)
        header.setStretchLastSection(True)
        for i in range(0x10):
            setResizeMode(i, qt.QHeaderView.Fixed)
        setResizeMode(0x10, qt.QHeaderView.Stretch)