示例#1
0
    def _find(self, image):
        """ Attempts to return the **ImageVolume** object corresponding to a
            specified *image **ImageInfo** object.
        """
        volume = ImageLibrary.find_volume(image.image_name)
        for vm in self.volumes:
            if volume is vm.volume:
                return vm

        return volume
示例#2
0
    def _find ( self, image ):
        """ Attempts to return the **ImageVolume** object corresponding to a
            specified *image **ImageInfo** object.
        """
        volume = ImageLibrary.find_volume( image.image_name )
        for vm in self.volumes:
            if volume is vm.volume:
                return vm

        return volume
    def _find ( self, image_name ):
        """ Tries the find a current ImageLibraryItem matching the specified
            **image_name** and return it. Returns None if not found.
        """
        image = ImageLibrary.image_info( image_name )
        if image is not None:
            for item in self.items:
                if image is item.image:
                    return item

        return None
    def _find(self, image_name):
        """ Tries the find a current ImageLibraryItem matching the specified
            **image_name** and return it. Returns None if not found.
        """
        image = ImageLibrary.image_info(image_name)
        if image is not None:
            for item in self.items:
                if image is item.image:
                    return item

        return None
    def _image_names_modified ( self, object, name, removed, added ):
        """ Handles image names being added to or removed from the editor.
        """
        items = self.items

        # fixme: Remove this code if the problem in the TableEditor that allows
        # the same item to be in both the 'removed' and 'added' list is fixed:
        for name in [ name for name in removed if name in added ]:
            removed.remove( name )
            added.remove(   name )

        for image_name in added:
            item = self._find( image_name )
            if item is None:
                image = ImageLibrary.image_info( image_name )
                if image is not None:
                    items.append( self.item_class( image = image ) )

        for image_name in removed:
            item = self._find( image_name )
            if item is not None:
                items.remove( item )
    def _image_names_modified(self, object, name, removed, added):
        """ Handles image names being added to or removed from the editor.
        """
        items = self.items

        # fixme: Remove this code if the problem in the TableEditor that allows
        # the same item to be in both the 'removed' and 'added' list is fixed:
        for name in [name for name in removed if name in added]:
            removed.remove(name)
            added.remove(name)

        for image_name in added:
            item = self._find(image_name)
            if item is None:
                image = ImageLibrary.image_info(image_name)
                if image is not None:
                    items.append(self.item_class(image=image))

        for image_name in removed:
            item = self._find(image_name)
            if item is not None:
                items.remove(item)
示例#7
0
class DataSourceWizardView(DataSourceWizard):

    #----------------------------------------------------------------------
    # Private traits
    #----------------------------------------------------------------------

    _top_label = Str('Describe your data')

    _info_text = Str('Array size do not match')

    _array_label = Str('Available arrays')

    _data_type_text = Str("What does your data represents?")

    _lines_text = Str("Connect the points with lines")

    _scalar_data_text = Str("Array giving the value of the scalars")

    _optional_scalar_data_text = Str("Associate scalars with the data points")

    _connectivity_text = Str("Array giving the triangles")

    _vector_data_text = Str("Associate vector components")

    _position_text = Property(depends_on="position_type_")

    _position_text_dict = {
        'explicit': 'Coordinnates of the data points:',
        'orthogonal grid': 'Position of the layers along each axis:',
    }

    def _get__position_text(self):
        return self._position_text_dict.get(self.position_type_, "")

    _shown_help_text = Str

    _data_sources_wrappers = Property(depends_on='data_sources')

    def _get__data_sources_wrappers(self):
        return [
            ArrayColumnWrapper(name=name,
                               shape=repr(self.data_sources[name].shape))
            for name in self._data_sources_names
        ]

    # A traits pointing to the object, to play well with traitsUI
    _self = Instance(DataSourceWizard)

    _suitable_traits_view = Property(depends_on="data_type_")

    def _get__suitable_traits_view(self):
        return "_%s_data_view" % self.data_type_

    ui = Any(False)

    _preview_button = Button(label='Preview structure')

    def __preview_button_fired(self):
        if self.ui:
            self.build_data_source()
            self.preview()

    _ok_button = Button(label='OK')

    def __ok_button_fired(self):
        if self.ui:
            self.ui.dispose()
            self.build_data_source()

    _cancel_button = Button(label='Cancel')

    def __cancel_button_fired(self):
        if self.ui:
            self.ui.dispose()

    _is_ok = Bool

    _is_not_ok = Bool

    def _anytrait_changed(self):
        """ Validates if the OK button is enabled.
        """
        if self.ui:
            self._is_ok = self.check_arrays()
            self._is_not_ok = not self._is_ok

    _preview_window = Instance(PreviewWindow, ())

    _info_image = Instance(ImageResource,
                           ImageLibrary.image_resource('@std:alert16', ))

    #----------------------------------------------------------------------
    # TraitsUI views
    #----------------------------------------------------------------------

    _coordinates_group = \
                        HGroup(
                           Item('position_x', label='x',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('position_y', label='y',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('position_z', label='z',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                       )


    _position_group = \
                    Group(
                       Item('position_type'),
                       Group(
                           Item('_position_text', style='readonly',
                                    resizable=False,
                                    show_label=False),
                           _coordinates_group,
                           visible_when='not position_type_=="image data"',
                       ),
                       Group(
                           Item('grid_shape_source_',
                            label='Grid shape',
                            editor=EnumEditor(
                                name='_grid_shape_source_labels',
                                        invalid='_is_not_ok')),
                           HGroup(
                            spring,
                            Item('grid_shape', style='custom',
                                    editor=ArrayEditor(width=-60),
                                    show_label=False),
                           enabled_when='grid_shape_source==""',
                            ),
                           visible_when='position_type_=="image data"',
                       ),
                       label='Position of the data points',
                       show_border=True,
                       show_labels=False,
                   ),


    _connectivity_group = \
                   Group(
                       HGroup(
                         Item('_connectivity_text', style='readonly',
                                resizable=False),
                         spring,
                         Item('connectivity_triangles',
                                editor=EnumEditor(name='_data_sources_names'),
                                show_label=False,
                                ),
                         show_labels=False,
                       ),
                       label='Connectivity information',
                       show_border=True,
                       show_labels=False,
                       enabled_when='position_type_=="explicit"',
                   ),


    _scalar_data_group = \
                   Group(
                       Item('_scalar_data_text', style='readonly',
                           resizable=False,
                           show_label=False),
                       HGroup(
                           spring,
                           Item('scalar_data',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           show_labels=False,
                           ),
                       label='Scalar value',
                       show_border=True,
                       show_labels=False,
                   )


    _optional_scalar_data_group = \
                   Group(
                       HGroup(
                       'has_scalar_data',
                       Item('_optional_scalar_data_text',
                            resizable=False,
                            style='readonly'),
                       show_labels=False,
                       ),
                       Item('_scalar_data_text', style='readonly',
                            resizable=False,
                            enabled_when='has_scalar_data',
                           show_label=False),
                       HGroup(
                           spring,
                           Item('scalar_data',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok'),
                               enabled_when='has_scalar_data'),
                           show_labels=False,
                           ),
                       label='Scalar data',
                       show_border=True,
                       show_labels=False,
                   ),


    _vector_data_group = \
                   VGroup(
                       HGroup(
                           Item('vector_u', label='u',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('vector_v', label='v',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('vector_w', label='w',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                       ),
                       label='Vector data',
                       show_border=True,
                   ),


    _optional_vector_data_group = \
                   VGroup(
                        HGroup(
                            Item('has_vector_data', show_label=False),
                            Item('_vector_data_text', style='readonly',
                                resizable=False,
                                show_label=False),
                        ),
                       HGroup(
                           Item('vector_u', label='u',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('vector_v', label='v',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           Item('vector_w', label='w',
                               editor=EnumEditor(name='_data_sources_names',
                                        invalid='_is_not_ok')),
                           enabled_when='has_vector_data',
                       ),
                       label='Vector data',
                       show_border=True,
                   ),


    _array_view = \
                View(
                    Item('_array_label', editor=TitleEditor(),
                        show_label=False),
                    Group(
                    Item('_data_sources_wrappers',
                      editor=TabularEditor(
                          adapter = ArrayColumnAdapter(),
                      ),
                    ),
                    show_border=True,
                    show_labels=False
                ))

    _questions_view = View(
        Item('_top_label', editor=TitleEditor(), show_label=False),
        HGroup(
            Item('_data_type_text', style='readonly', resizable=False),
            spring,
            'data_type',
            spring,
            show_border=True,
            show_labels=False,
        ),
        HGroup(
            Item(
                '_self',
                style='custom',
                editor=InstanceEditor(view_name='_suitable_traits_view'),
            ),
            Group(
                # FIXME: Giving up on context sensitive help
                # because of lack of time.
                #Group(
                #    Item('_shown_help_text', editor=HTMLEditor(),
                #        width=300,
                #        label='Help',
                #        ),
                #    show_labels=False,
                #    label='Help',
                #),
                #Group(
                Item('_preview_button', enabled_when='_is_ok'),
                Item('_preview_window',
                     style='custom',
                     label='Preview structure'),
                show_labels=False,
                #label='Preview structure',
                #),
                #layout='tabbed',
                #dock='tab',
            ),
            show_labels=False,
            show_border=True,
        ),
    )

    _point_data_view = \
                View(Group(
                   Group(_coordinates_group,
                        label='Position of the data points',
                        show_border=True,
                   ),
                   HGroup(
                       'lines',
                       Item('_lines_text', style='readonly',
                                        resizable=False),
                       label='Lines',
                       show_labels=False,
                       show_border=True,
                   ),
                   _optional_scalar_data_group,
                   _optional_vector_data_group,
                   # XXX: hack to have more vertical space
                   Label('\n'),
                   Label('\n'),
                   Label('\n'),
                ))


    _surface_data_view = \
                View(Group(
                   _position_group,
                   _connectivity_group,
                   _optional_scalar_data_group,
                   _optional_vector_data_group,
                ))


    _vector_data_view = \
                View(Group(
                   _vector_data_group,
                   _position_group,
                   _optional_scalar_data_group,
                ))


    _volumetric_data_view = \
                View(Group(
                   _scalar_data_group,
                   _position_group,
                   _optional_vector_data_group,
                ))

    _wizard_view = View(
        Group(
            HGroup(
                Item(
                    '_self',
                    style='custom',
                    show_label=False,
                    editor=InstanceEditor(view='_array_view'),
                    width=0.17,
                ),
                '_',
                Item(
                    '_self',
                    style='custom',
                    show_label=False,
                    editor=InstanceEditor(view='_questions_view'),
                ),
            ),
            HGroup(
                Item('_info_image',
                     editor=ImageEditor(),
                     visible_when="_is_not_ok"),
                Item('_info_text',
                     style='readonly',
                     resizable=False,
                     visible_when="_is_not_ok"),
                spring,
                '_cancel_button',
                Item('_ok_button', enabled_when='_is_ok'),
                show_labels=False,
            ),
        ),
        title='Import arrays',
        resizable=True,
    )

    #----------------------------------------------------------------------
    # Public interface
    #----------------------------------------------------------------------

    def __init__(self, **traits):
        DataSourceFactory.__init__(self, **traits)
        self._self = self

    def view_wizard(self):
        """ Pops up the view of the wizard, and keeps the reference it to
            be able to close it.
        """
        # FIXME: Workaround for traits bug in enabled_when
        self.position_type_
        self.data_type_
        self._suitable_traits_view
        self.grid_shape_source
        self._is_ok
        self.ui = self.edit_traits(view='_wizard_view')

    def preview(self):
        """ Display a preview of the data structure in the preview
            window.
        """
        self._preview_window.clear()
        self._preview_window.add_source(self.data_source)
        data = lambda name: self.data_sources[name]
        g = Glyph()
        g.glyph.glyph_source.glyph_source = \
                    g.glyph.glyph_source.glyph_list[0]
        g.glyph.scale_mode = 'data_scaling_off'
        if not (self.has_vector_data or self.data_type_ == 'vector'):
            g.glyph.glyph_source.glyph_source.glyph_type = 'cross'
            g.actor.property.representation = 'points'
            g.actor.property.point_size = 3.
        self._preview_window.add_module(g)
        if not self.data_type_ in ('point', 'vector') or self.lines:
            s = Surface()
            s.actor.property.opacity = 0.3
            self._preview_window.add_module(s)
        if not self.data_type_ == 'point':
            self._preview_window.add_filter(ExtractEdges())
            s = Surface()
            s.actor.property.opacity = 0.2
            self._preview_window.add_module(s)
    def _build_volume ( self, file_name ):
        """ Creates the image volume using the specified file name and the
            current user supplied information and input files.
        """
        # Set the current status:
        self.status = 'Saving to %s...' % file_name

        # Create an image volume to hold the information collected. Note that we
        # set a bogus time stamp to force the ImageLibrary to reload all of the
        # image information from the actual files so that we end up with the
        # correct image sizes:
        volume = ImageVolume(
            category   = self.category,
            keywords   = self.keywords,
            time_stamp = time_stamp_for( 0.0 ),
            info       = [ ImageVolumeInfo(
                               description = self.description,
                               copyright   = self.copyright,
                               license     = self.license ) ]
        )

        # Create an ImageInfo descriptor for each image file that will become
        # part of the volume:
        volume_name   = splitext( basename( file_name ) )[0]
        volume.images = [ ImageInfo(
                              name        = item.image_name,
                              image_name  = '@%s:%s' % ( volume_name,
                                                         item.image_name ),
                              description = self.image_description,
                              category    = self.image_category,
                              keywords    = self.image_keywords )
                          for item in self.build_files ]

        # Pre-compute the images code, because it can require a long time
        # to load all of the images so that we can determine their size, and we
        # don't want that time to interfere with the time stamp of the image
        # volume:
        images_code = volume.images_code

        # Create the new zip file:
        zf = ZipFile( file_name, 'w', ZIP_DEFLATED )

        # Assume that an error will occur:
        error = True
        fh    = None
        try:
            # Copy all of the image files from the file system to the new zip
            # file:
            for item in self.build_files:
                fh   = file( item.file_name, 'rb' )
                data = fh.read()
                fh.close()
                fh = None
                zf.writestr( item.image_name, data )

            # Write the volume manifest source code to the zip file:
            zf.writestr( 'image_volume.py', volume.image_volume_code )

            # Write the image info source code to the zip file:
            zf.writestr( 'image_info.py', images_code )

            # Done creating the new zip file:
            zf.close()
            zf = None

            # Add the new image volume to the image library:
            ImageLibrary.add_volume( file_name )

            # Now invoke the normal volume save so that it can add the correct
            # image info data and license file:
            volume = [ volume for volume in ImageLibrary.volumes
                       if volume.path == file_name ][0]
            volume.path     = file_name
            volume.zip_file = FastZipFile( path = file_name )

            volume.save()

            # Set the final status:
            self.status = '%s has been saved successfully.' % file_name

            # Indicate no errors occurred:
            error = False
        finally:
            if fh is not None:
                fh.close()

            if zf is not None:
                zf.close()

            if error:
                self.status = 'An error occurred trying to save %s.' % file_name
                remove( file_name )