Ejemplo n.º 1
0
    def create(self):
        self.click_event_name = self.title

        # Create node paths for the label.
        self.np = self.parent.attachNewNode(self.click_event_name)
        self.text_np = self.np.attachNewNode('text-' + self.title)
        self.card_np = self.np.attachNewNode('card-' + self.title)

        # Create the text object.
        # read the font from configuration.
        textObject = None
        # If we have a hierachy we pad + sign in front of the name. Good idea to place some
        # good image indicator that this label is clickable.

        text_color = (0, 0, 0, 1)  #text color
        if self.hierarchy:
            label_text = '+' + self.title
            if self.aggrigation_num is not None:
                label_text += ' (' + str(self.aggrigation_num) + ')'
                # fix for the size of the clickable card.
                self.axisLabelTextLength = len(label_text)
            textObject = OnscreenText(text=label_text,
                                      scale=1,
                                      fg=text_color,
                                      font=font)
        else:
            textObject = OnscreenText(text=self.title,
                                      scale=1,
                                      fg=text_color,
                                      font=font)

        textObject.reparentTo(self.text_np)
        textObject.setTwoSided(True)

        # If this is a hierarchy label, we make it clickable.
        if self.hierarchy:
            self.picker_message = mouse_handler.makeNPPickableLeft(self.np)

        # Set the position of the label.
        self.np.setPos(self.pos[0], self.pos[1], self.pos[2])

        # if this is z
        if self.axis_num == 2:
            self.np.setR(-90)
            self.np.setX(-1)
Ejemplo n.º 2
0
class Image(ImageNodePath, Hoverable):
    def __init__(self, id, location):
        # Invoke parent constructors.
        ImageNodePath.__init__(self, ';' + str(id) + ';' + location)
        Hoverable.__init__(self)
        # Make the image hovarble.
        mouse_handler.makeNPMouseHoverable(self.np)

        # Get the object for this image.
        self.object = objectCubeService.getObjectByName(location)

        # Member functions for plugin code. Image movements functions can changed for images.
        self.move_image_function = self.default_move_image_function
        self.remove_image_function = self.test_remove_image_function
        self.add_image_function = self.test_add_image_function

        # Member variable for the id, what is the id?
        self.id = id

        # Member variable for the image location.
        self.location = location

        # Member variables for image position.
        self.pos = None
        self.old_pos = None
        self.screen_pos = None
        self.old_screen_pos = None

        # Member variable that contains this image data.
        self.image_data = None
        self.parent_np = None
        self.cluster_index = None

        # Member variable for the image card (contains image texture.)
        self.card = None

        self.random_r = None
        self.visable = False
        # Member variable for data size.
        self.dataSize = IMAGE_SIZE_SMALL

    def setDataSize(self, size):
        # If the size is not on correct range, then we'll throw
        # an exception.
        if size not in range(3):
            raise Exception('Unknown data-size')

        # Setting the data size.
        fileName = self.location.split('/')[-1]
        folder = self.location[0:self.location.rfind('/')]
        self.dataSize = size
        self.image_data = image_service.get_pnmimage(folder, fileName,
                                                     self.dataSize)
        print 'DATA SIZE CHANGED'

    def getTags(self):
        """
        Returns all the tags that the object that this image reprensets contains.
        """
        for tag in self.object.getTags():
            yield tag

    def addTag(self, tag):
        """
        Adds tag to the ObjectCube object for this given image.
        """
        if not self.object.hasTag(tag.id):
            objtag = ObjectTag(tag)
            self.object.addTag(objtag)

    def shake(self):
        if self.cluster_index is not 0:
            self.np.setR(self.random_r)

    def remove_shake(self):
        self.np.setR(0)

    def getClickMessageLeft(self):
        return self.messageLeftClick

    def getClickMessageRight(self):
        return self.messageRightClick

    def get_mouseover_message(self):
        if self.np is not None: return 'zoomin_' + str(self.np)
        else: return None

    def get_mouseleave_message(self):
        if self.np is not None: return 'zoomout_' + str(self.np)
        else: return None

    def default_move_image_function(self, np, location, old_pos, new_pos,
                                    cluster_index):
        new_pos = convert_cluster_pos_to_screen_pos(new_pos)

        # add the stackspace.
        new_pos = (
            new_pos[0], new_pos[1] +
            cluster_index * browser.cube.cubeService.get_cluster_stack_space(),
            new_pos[2])
        LerpPosInterval(self.np, move_speed, new_pos).start()

    def test_remove_image_function(self, np, pos):
        pos_lerp = LerpPosInterval(
            self.np, move_speed,
            ((random.sample([-1, 1], 1)[0]) * 100, 50, 0)).start()
        i = LerpFunc(self.myFunction,
                     fromData=0,
                     toData=5,
                     duration=3,
                     blendType='noBlend',
                     extraArgs=[pos_lerp],
                     name=None)
        i.start()

    #bad function, need to fix.
    def myFunction(self, t, lerp):
        if t >= 5:
            self.np.remove()
            self.visable = False

    def test_add_image_function(self, np, pos):
        new_pos = convert_cluster_pos_to_screen_pos(pos)

        # add the stack space.
        new_pos = (new_pos[0], new_pos[1] + self.cluster_index *
                   browser.cube.cubeService.get_cluster_stack_space(),
                   new_pos[2])

        # this should not be random...
        np.setPos((-60, 0, 0))
        LerpPosInterval(np, move_speed, new_pos).start()

    def get_location(self):
        return self.location

    def get_id(self):
        return self.id

    def getCellKey(self):
        # Create the cell key.
        pos_list = []
        for n in self.pos:
            if n is not None: pos_list.append(str(n))
        key = ':'.join(pos_list)
        return key

    def set_pos(self, pos):
        """
        Set the position of the image, this position is the cube position, not the
        screen position.
        """
        if self.pos is not None:
            self.old_pos = self.pos
        self.pos = pos

    def update_pos(self, pos):
        if self.move_image_function is None:
            self.position()
        else:
            self.move_image_function(self.np, self.location,
                                     self.get_old_pos(), self.get_pos(),
                                     self.get_cluster_index())

    def clusterIndexUpdate(self, animate=False):
        self.position()

    def get_pos(self):
        return self.pos

    def get_old_pos(self):
        return self.old_pos

    def remove(self):
        self.np.hide()
        self.np.setX(-3000)
        self.visable = False

    def get_screen_pos(self):
        return self.screen_pos

    def get_old_screen_pos(self):
        return self.old_screen_pos

    def reload(self):
        #print 'reload image!!!!!!!!!!!!!!'
        self.card.remove()

        # Create a new texture object.
        self.myTexture = Texture()
        self.myTexture.load(self.image_data)

        # Create the card for the image.
        cm = CardMaker('card-' + self.pos.__str__() + '=' + self.location)

        self.card = self.np.attachNewNode(cm.generate())
        self.card.setTwoSided(True)
        self.card.setTexture(self.myTexture)

    def load_data(self):
        """
        Loads the image data from the PNMImage service.
        """
        fileName = self.location.split('/')[-1]
        folder = self.location[0:self.location.rfind('/')]

        # Load the image data.
        if self.image_data is None:
            self.image_data = image_service.get_pnmimage(
                folder, fileName, IMAGE_SIZE_SMALL)
            #print 'got image data from image service', self.image_data

        else:
            raise Exception('IMAGE DATA IS NONE')

        # Create a new texture object.
        self.myTexture = Texture()
        #print 'IMAGE DATA IS:'
        #print self.image_data
        self.myTexture.load(self.image_data)

        # Create the card for the image.
        cm = CardMaker('card-' + self.pos.__str__() + '=' + self.location)

        self.card = self.np.attachNewNode(cm.generate())
        self.card.setTwoSided(True)

        # Make the image np clickable.
        self.messageRightClick = mouse_handler.makeNPPickableRight(self.np)
        self.messageLeftClick = mouse_handler.makeNPPickableLeft(self.np)

        # Set the picture as the texture for the card.
        self.card.setTexture(self.myTexture)

        # Create the tagset card
        cm = CardMaker('card-' + self.pos.__str__() + '=' + self.location +
                       "-tags")

        self.tags_card = self.np.attachNewNode(cm.generate())
        self.tags_card.setTexture(tagTexture)
        self.tags_card.setTransparency(TransparencyAttrib.MAlpha)

        # Create image tags labels to put under the image when drawed.
        obj = objectCubeService.getObjectByName(self.location)

        tags = ''
        for tag in obj.getTags():
            tagset = objectCubeService.get_tagset_by_id(tag.getTag().tagSetId)

            # we only show acces types tags with user (not showing system specific tags.)
            if tagset.accessId == TagSet.ACCESS_TYPE.USER:
                tags += tag.getTag().valueAsString() + ", "

        # Remove the trailing ','.
        tags = tags[0:len(tags) - 2]

        # Create text object. (We must load this elsewhere for chaging image size.)
        self.textObject = OnscreenText(align=TextNode.ALeft,
                                       fg=(1, 1, 1, 1),
                                       text=tags,
                                       scale=(0.03, 0.03),
                                       wordwrap=40,
                                       font=font,
                                       mayChange=True)
        self.t_np = self.np.attachNewNode('t_node')
        self.textObject.reparentTo(self.t_np)
        self.textObject.setTwoSided(True)

        self.t_np.setY(-0.03)
        self.t_np.setX(0.02)
        self.t_np.setZ(-0.03)

        self.scale_image()
        self.hide_tags()

    def show(self):
        if self.image_data is None:
            self.load_data()

        self.np.show()
        self.visable = True

    def isVisible(self):
        return self.visable

    def position(self, animate=False):

        if animate:
            if self.old_pos is not None:
                #print 'WOW!!! WE SHOULD RELOCATED IMAGE!!!'
                if self.move_image_function is None:
                    raise Exception('Move image function is not set.')
                else:
                    self.move_image_function(self.np, self.location,
                                             self.get_old_pos(),
                                             self.get_pos(),
                                             self.cluster_index)
            else:
                if not self.add_image_function is None:
                    self.add_image_function(self.np, self.get_pos())
                else:
                    raise Exception('Add function not defined')

        else:
            if self.screen_pos is not None:
                self.old_screen_pos = self.screen_pos

            screen_pos = convert_cluster_pos_to_screen_pos(self.get_pos())

            if screen_pos[0] is not None:
                self.np.setX(screen_pos[0])

            if screen_pos[1] is not None:
                self.np.setY(screen_pos[1])

            if screen_pos[2] is not None:
                self.np.setZ(screen_pos[2])

            # add the stack space
            self.np.setY(self.np.getY() +
                         (self.cluster_index *
                          browser.cube.cubeService.get_cluster_stack_space()))

            self.screen_pos = self.np.getPos()

    def scale_image(self, scale_factor=None, scale_tags=False):
        xSize = self.image_data.getReadXSize()
        ySize = self.image_data.getReadYSize()

        if scale_factor is None:
            max_image_size = browser.cube.cubeService.get_max_image_size()

        else:
            max_image_size = scale_factor

        max_image_space = browser.cube.cubeService.get_max_image_space()

        # calculate the scale value for the image.
        ratio = float(xSize) / float(ySize)

        if ratio > 1:
            self.width, self.height = (max_image_size, max_image_size / ratio)
        else:
            self.width, self.height = (max_image_size * ratio, max_image_size)

        self.card.setScale(Vec3(self.width, 1, self.height))

        # set the scale of the tagset card with respect to the width of the image card.
        if scale_tags:
            self.tags_card.setScale(self.width, 1, 0.1)
            self.tags_card.setZ(-0.1)
            self.textObject.setWordwrap(self.width * 32)

    def show_tags(self):
        self.tags_card.show()
        self.t_np.show()

    def hide_tags(self):
        self.tags_card.hide()
        self.t_np.hide()