File: array_view_editor.py

package info (click to toggle)
python-traitsui 4.4.0-1.3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,680 kB
  • ctags: 6,394
  • sloc: python: 32,786; makefile: 16; sh: 5
file content (183 lines) | stat: -rw-r--r-- 6,122 bytes parent folder | download | duplicates (3)
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
#-------------------------------------------------------------------------------
#
#  Copyright (c) 2007, Enthought, Inc.
#  All rights reserved.
#
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#
#  Author: David C. Morrill
#  Date:   08/29/2007

#-------------------------------------------------------------------------------

""" Defines an ArrayViewEditor for displaying 1-d or 2-d arrays of values.
"""

#-- Imports --------------------------------------------------------------------

from __future__ import absolute_import

from traits.api import Instance, Property, List, Str, Bool, Font

from ..api import View, Item, TabularEditor, BasicEditorFactory

from ..tabular_adapter import TabularAdapter

from ..toolkit import toolkit_object

from ..ui_editor import UIEditor

#-- Tabular Adapter Definition -------------------------------------------------

class ArrayViewAdapter ( TabularAdapter ):

    # Is the array 1D or 2D?
    is_2d = Bool( True )

    # Should array rows and columns be transposed:
    transpose  = Bool( False )

    alignment  = 'right'
    index_text = Property

    def _get_index_text ( self ):
        return str( self.row )

    def _get_content ( self ):
        if self.is_2d:
            return self.item[ self.column_id ]

        return self.item

    def get_item ( self, object, trait, row ):
        """ Returns the value of the *object.trait[row]* item.
        """
        if self.is_2d:
            if self.transpose:
                return getattr( object, trait )[:,row]

            return super( ArrayViewAdapter, self ).get_item( object, trait,
                                                             row )

        return getattr( object, trait )[ row ]

    def len ( self, object, trait ):
        """ Returns the number of items in the specified *object.trait* list.
        """
        if self.transpose:
            return getattr( object, trait ).shape[1]

        return super( ArrayViewAdapter, self ).len( object, trait )

# Define the actual abstract Traits UI array view editor (each backend should
# implement its own editor that inherits from this class.
class _ArrayViewEditor ( UIEditor ):

    # Indicate that the editor is scrollable/resizable:
    scrollable = True

    # Should column titles be displayed:
    show_titles = Bool( False )

    # The tabular adapter being used for the editor view:
    adapter = Instance( ArrayViewAdapter )

    #-- Private Methods --------------------------------------------------------

    def _array_view ( self ):
        """ Return the view used by the editor.
        """
        return View(
            Item( 'object.object.' + self.name,
                  id         = 'tabular_editor',
                  show_label = False,
                  editor     = TabularEditor( show_titles = self.show_titles,
                                              editable    = False,
                                              adapter     = self.adapter )
        ),
        id        = 'array_view_editor',
        resizable = True
    )

    def init_ui ( self, parent ):
        """ Creates the Traits UI for displaying the array.
        """
        # Make sure that the value is an array of the correct shape:
        shape = self.value.shape
        len_shape = len( shape )
        if (len_shape == 0) or (len_shape > 2):
            raise ValueError( "ArrayViewEditor can only display 1D or 2D "
                              "arrays" )

        factory          = self.factory
        cols             = 1
        titles           = factory.titles
        n                = len( titles )
        self.show_titles = (n > 0)
        is_2d            = (len_shape == 2)
        if is_2d:
            index = 1
            if factory.transpose:
                index = 0
            cols = shape[ index ]
            if self.show_titles:
                if n > cols:
                    titles = titles[:cols]
                elif n < cols:
                    if (cols % n) == 0:
                        titles, old_titles, i = [], titles, 0
                        while len( titles ) < cols:
                            titles.extend( '%s%d' % ( title, i )
                                           for title in old_titles )
                            i += 1
                    else:
                        titles.extend( [ '' ] * (cols - n) )
            else:
                titles = [ 'Data %d' % i for i in range( cols ) ]

        columns = [ ( title, i ) for i, title in enumerate( titles ) ]

        if factory.show_index:
            columns.insert( 0, ( 'Index', 'index' ) )

        self.adapter = ArrayViewAdapter( is_2d     = is_2d,
                                         columns   = columns,
                                         transpose = factory.transpose,
                                         format    = factory.format,
                                         font      = factory.font )

        return self.edit_traits( view   = '_array_view',
                                 parent = parent,
                                 kind   = 'subpanel' )

# Define the ArrayViewEditor class used by client code:
class ArrayViewEditor ( BasicEditorFactory ):

    # The editor implementation class:
    klass = Property

    # Should an index column be displayed:
    show_index = Bool( True )

    # List of (optional) column titles:
    titles = List( Str )

    # Should the array be logically transposed:
    transpose = Bool( False )

    # The format used to display each array element:
    format = Str( '%s' )

    # The font to use for displaying each array element:
    font = Font( 'Courier 10' )

    def _get_klass( self ):
        """ The class used to construct editor objects.
        """
        return toolkit_object('array_view_editor:_ArrayViewEditor')