Esempio n. 1
0
class ButtonsWidget(GridLayout):
    def __init__(self):
        super(ButtonsWidget, self).__init__()
        self.rows = 2
        self.path_input = TextInput(text=G_start_project)
        self.open_button = Button(text='Open')
        self.load_data_button = Button(text='Load')
        self.save_data_button = Button(text='Save')
        self.clear_data_button = Button(text='Clear')
        self.quit_button = Button(text='Quit')
        self.ground_button = Button(text='Ground(0)')
        self.walls_button = Button(text='Walls(0)')
        self.sky_button = Button(text='Sky(0)')
        self.motion_path_button = Button(text='Motion(0)')
        self.calculate_scene_button = Button(text='Calculate Scene')
        self.render_scene_button = Button(text='Render Scene')
        self.widgets = MyDict(length=3)
        self.buttons = (self.path_input, self.load_data_button, self.ground_button,
                        self.sky_button, self.motion_path_button, self.quit_button, self.open_button,
                        self.save_data_button, self.walls_button,
                        self.clear_data_button, self.calculate_scene_button, self.render_scene_button)
        self.reactions = (self.on_load_button_pressed, self.on_ground_button_pressed, self.on_sky_button_pressed,
                          self.on_motion_button_pressed, exit, self.on_open_button_pressed, self.on_save_button_pressed,
                          self.on_walls_button_pressed, self.on_clear_button_pressed, self.on_calculate_button_pressed,
                          self.on_render_button_pressed)
        self.on_buttons_count = MyDict.from_dictionary({
            'Ground': 0,
            'Walls': 0,
            'Sky': 0,
            'Motion': 0,
        }, length=6)
        for i, w in enumerate(self.buttons):
            self.add_widget(w)
            self.widgets['TextInput' if i == 0 else w.text] = w
            if i > 0:
                w.on_press = self.reactions[i - 1]

        self.in_process_action_type = None
        self.current_points = []
        self.current_touches = []

        self.prev_draw_point = None
        self.colors = MyDict.from_dictionary({'Ground': (1, 0, 0),
                                              'Walls': (1, 1, 0),
                                              'Sky': (0, 0, 1),
                                              'Motion': (0, 1, 0)})
        print 'colors:', self.colors

        self.cp = None

    def add_point(self, point):
        if self.in_process_action_type is None:
            return
        self.current_points.append(point)

    def add_touch(self, touch):
        if self.in_process_action_type is None:
            return
        self.current_touches.append(touch)

    def add_object(self):
        if self.in_process_action_type is not None and self.current_points and self.current_points.__len__() > 2:
            ImageObjects.add(ImageObject(points=self.current_points,
                                         kivy_touches=self.current_touches,
                                         type=self.in_process_action_type))
            print 'Object added: %s %s' % (self.in_process_action_type, self.current_points.__str__())

            self.add_button_count(self.in_process_action_type)
            self.upgrade_buttons_count()
        self.in_process_action_type = None
        self.current_points = []
        self.current_touches = []
        self.prev_draw_point = None

    def draw_point(self, touch):
        if not self.in_process_action_type:
            self.prev_draw_point = None
            return
        if self.prev_draw_point:
            image_widget.draw_line_from_touches(self.prev_draw_point, touch, self.colors[self.in_process_action_type])
        self.prev_draw_point = touch

    def upgrade_buttons_count(self):
        for key in self.on_buttons_count:
            self.widgets[key].text = '%s(%d)' % (key, self.on_buttons_count[key])

    def add_button_count(self, key):
        self.on_buttons_count[key] += 1

    def on_open_button_pressed(self):
        try:
            chdir('../%s' % self.widgets['TextInput'].text)
            global Project
            Project = self.widgets['TextInput'].text
            image_widget.source = '../%s/%s' % (Project, G_image_path)
            image_widget.size = get_image_size(G_image_path)
            ImageObjects.copy(ImageProperties(G_image_path))
            print image_widget.size
            self.on_clear_button_pressed()
        except WindowsError:
            pass

    def on_load_button_pressed(self):
        global ImageObjects
        self.add_object()
        ImageObjects = ImageProperties.load()
        image_widget.draw_all_lines()

    def on_save_button_pressed(self):
        self.add_object()
        ImageObjects.save()

    def on_clear_button_pressed(self):
        global ImageObjects, image_widget
        self.in_process_action_type = None
        for key in self.on_buttons_count:
            self.on_buttons_count[key] = 0
            self.widgets[key].text = '%s(0)' % key
        image_widget.clear()

    def on_motion_button_pressed(self):
        if self.in_process_action_type:
            self.add_object()
        else:
            self.in_process_action_type = 'Motion'

    def on_ground_button_pressed(self):
        if self.in_process_action_type:
            self.add_object()
        else:
            self.in_process_action_type = 'Ground'

    def on_walls_button_pressed(self):
        if self.in_process_action_type:
            self.add_object()
        else:
            self.in_process_action_type = 'Walls'

    def on_sky_button_pressed(self):
        if self.in_process_action_type:
            self.add_object()
        else:
            self.in_process_action_type = 'Sky'

    def on_calculate_button_pressed(self):
        self.add_object()
        self.cp = CameraProperties(project=Project, image_path=G_image_path, ip=ImageObjects)
        self.cp.calculate()

    def on_render_button_pressed(self):
        self.add_object()
        if self.cp is not None and self.cp.calculated:
            self.cp.render()