Ejemplo n.º 1
0
    def create_button_view(self, dt):
        import json
        import time

        # Make reference to app root widget
        sm = self._app.root

        # Check that JSON been recieved
        if re.match(r'^\{.*\}$', self._app.sm.json_sensors):
            try:
                j = json.loads(self._app.sm.json_sensors)
            except Exception as e:
                self._app.sm.json_sensors = 'Error in JSON'
            else:
                all_devices_boxlayout = BoxLayout(orientation='vertical', spacing=10)
                all_devices_boxlayout.add_widget(Label(text=''))
                all_devices_boxlayout.add_widget(Label(size_hint_y=0.2, text='[color=ff3333]Devices[/color]', font_size=40, markup=True))
                all_devices_boxlayout.add_widget(Label(text=''))
                all_devices_screen = Screen(id='all_devices_buttons', name='all_devices_buttons')
                all_devices_screen.add_widget(all_devices_boxlayout)
                sm.add_widget(all_devices_screen)

                # Bulding new screens for list of devices and sensors based on json
                # For each device create its own Screen
                for device in j.keys():
                    Logger.info("Creating screen for device %s" % (device,))
                    self._app.log_list.append(get_date("Creating screen for device %s" % (device,)))
                    screen_device = Screen(name=device)

                    # Add button for device on all_devices_boxlayout
                    b = Button(text=device)
                    # This will call the function with 'device' as argument to switch Screen
                    b.bind(on_press=partial(self.change_screen, device))
                    all_devices_boxlayout.add_widget(b)

                    # Create Device Screen with sensors
                    box_device = BoxLayout(orientation='vertical', spacing=10)
                    box_device.add_widget(Label(text=''))
                    box_device.add_widget(Label(size_hint_y=0.2, text='[color=ff3333]' + device + '[/color]', font_size=40, markup=True))
                    box_device.add_widget(Label(text=''))

                    # Create Sensor Screen and button on device screen
                    for sensor in j[device]:
                        sensor_name = sensor.keys()[0]
                        sensor_data = sensor[sensor_name]
                        sensor_values = sensor_data['last_records']

                        sensor_dict = updates_to_plots(sensor_values)
                        sensor_plots = sensor_dict['plots']
                        ymin = sensor_dict['min_y']
                        ymax = sensor_dict['max_y']
                        xmin = sensor_dict['min_x']

                        last_date, last_value = sensor_values[-1]

                        # Determine suffix
                        suffix = ' '
                        if re.match(r'.*temp.*', sensor_name, re.IGNORECASE):
                            suffix = u"\u00b0C"
                        if re.match(r'.*humid.*', sensor_name, re.IGNORECASE):
                            suffix = " %"
                        if re.match(r'.*smoke.*', sensor_name, re.IGNORECASE):
                            suffix = " %"
                        if re.match(r'.*stove.*', sensor_name, re.IGNORECASE):
                            suffix = " %"

                        sensor = device + '_' + sensor_name
                        Logger.info(str(sensor))
                        Logger.info("Last data %s %s" % (last_date, last_value))

                        # Create history view
                        screen_sensor_history = Screen(name=device + "_" + sensor_name + "_history")
                        box_sensor_history = BoxLayout(orientation='vertical', spacing=10)
                        box_sensor_history.add_widget(Label(size_hint_y=0.1, text='[color=B6BAB9]' + sensor_name + ' (' + device + ')[/color]', font_size=30, markup=True))

                        # Create history text
                        text_history = []
                        for d, v in sensor_values:
                            text_history.append(str("%s    %s" % (d, v)))

                        # Create left aligned list
                        adapter = SimpleListAdapter(data=text_history, cls=MyLeftAlignedLabel)
                        list_view = ListView(adapter=adapter)
                        # Fix bug with ListView to refresh if required
                        if(hasattr(list_view, '_reset_spopulate')):
                            Logger.info("Refresh list_view")
                            list_view._reset_spopulate()
                        # Add ListView to Sensor History
                        box_sensor_history.add_widget(list_view)
                        back_button = Button(size_hint_y=0.1, font_size=20, text='Back')
                        back_button.bind(on_press=partial(self.change_screen, device + "_" + sensor_name))
                        box_sensor_history.add_widget(back_button)
                        screen_sensor_history.add_widget(box_sensor_history)
                        sm.add_widget(screen_sensor_history)

                        # Create sensor screen
                        screen_sensor = Screen(name=device + "_" + sensor_name)
                        box_sensor = BoxLayout(orientation='vertical')
                        box_sensor.add_widget(Label(size_hint_y=0.1, text='[color=B6BAB9]' + sensor_name + ' (' + device + ')[/color]', font_size=30, markup=True))
                        # Add sensor value
                        box_sensor.add_widget(Label(text=last_value + suffix, font_size=60))
                        # Add sensor date
                        box_sensor.add_widget(Label(size_hint_y=0.1, markup=True, text='[b]Sensor last updated ' + last_date[:-3] + '[/b]\nPolled ' + get_date(None)[:-3], font_size=15))
                        # Add sensor graph
                        Logger.info("Create plot for %s" % (sensor_name,))
                        Logger.info(str(sensor_plots))
                        plot = MeshLinePlot(mode='line_strip', color=[1, 0, 0, 1])
                        plot.points = sensor_plots
                        sensor_graph = Graph(id='plots_' + sensor_name, precision='%0.0f', x_grid_label=True, y_grid_label=True, xmin=xmin, xmax=0, ymin=ymin, ymax=ymax, xlabel='days ago', ylabel=suffix, x_grid=True, y_grid=False, x_ticks_major=1, y_ticks_major=1)
                        sensor_graph.add_plot(plot)
                        box_sensor.add_widget(sensor_graph)

                        # Add buttonbar for sensor
                        box_buttons = BoxLayout(orientation='horizontal')

                        # Create button for history
                        history_button = Button(size_hint_y=0.2, font_size=20, text='History')
                        history_button.bind(on_press=partial(self.change_screen, device + "_" + sensor_name + "_history"))

                        # Create Back button
                        back_button = Button(size_hint_y=0.2, font_size=20, text='Back')
                        back_button.bind(on_press=partial(self.change_screen, device))

                        # Add buttons to row
                        box_buttons.add_widget(back_button)
                        box_buttons.add_widget(history_button)

                        # Add row to screen
                        box_sensor.add_widget(box_buttons)

                        # Add all of it to screen
                        screen_sensor.add_widget(box_sensor)
                        sm.add_widget(screen_sensor)

                        # Create button on device screen
                        button_sensor = Button(text=sensor_name)
                        button_sensor.bind(on_press=partial(self.change_screen, sensor))
                        box_device.add_widget(button_sensor)

                    # Add Device Screen with all sensor buttons to ScreenManager
                    back_button = Button(font_size=20, text='[b]Back[/b]', markup=True)
                    back_button.bind(on_press=partial(self.change_screen, 'all_devices_buttons'))
                    box_device.add_widget(back_button)
                    screen_device.add_widget(box_device)
                    sm.add_widget(screen_device)

                # Adding Back button to Devices screen
                back_button = Button(font_size=20, text='[b]Back[/b]', markup=True)
                back_button.bind(on_press=partial(self.change_screen, 'initial_screen'))
                all_devices_buttonrow = BoxLayout(orientation='horizontal')
                all_devices_buttonrow.add_widget(back_button)
                slide_all_button = ToggleButton(id='slide_all_button', font_size=20, text='Slideshow All Sensors')
                slide_all_button.bind(on_press=partial(self.control_slideshow_all_sensors, slide_all_button))
                all_devices_buttonrow.add_widget(slide_all_button)
                all_devices_boxlayout.add_widget(all_devices_buttonrow)

                # Unschedule timer
                Clock.unschedule(self.create_button_view)
                # Return to buttons of all devices
                sm.current = 'all_devices_buttons'
                # Schedule updates from server
                Clock.schedule_interval(sm.update_views, int(self._app.sm.settings_dict['refresh_time']))

        # Check if failed pause for error before return
        if re.match(r'.*fail.*', self._app.sm.connect_server_status) or re.match(r'.*error.*', self._app.sm.json_sensors):
            Clock.unschedule(self.create_button_view)
            time.sleep(2)
            self.port = self._app.config.get('network', 'port')
            self.server = self._app.config.get('network', 'ip')
            self._app.sm.connect_server_status = "Connecting to %s:%s" % (self.server, self.port)
            sm.current = 'initial_screen'
Ejemplo n.º 2
0
    def update_views(self, dt):
        ''' Method to be scheduled for updating from server '''
        # Poll new JSON data
        Logger.info(str(self._app.connect_to_server()))

        # Clean records in log_screen if too many lines
        if len(self._app.log_list) > 100:
            self._app.log_list.append(get_date("Cleaning old records in log"))
            while len(self._app.log_list) > 100:
                self._app.log_list.pop(0)

        def return_screen_object(screen_name):
            # Iterate through all screens
            found = None
            for current_screen in self._app.sm.screens:
                if current_screen.name == screen_name:
                   found = current_screen
            return found

        # For each device update Screen
        if re.match(r'^\{.*\}$', self._app.sm.json_sensors):
            try:
                j = json.loads(self._app.sm.json_sensors)
            except Exception as e:
                self._app.sm.json_sensors = 'Error in JSON'
            else:
                for device in j.keys():
                    Logger.info("Updating screen for device %s" % (device,))
                    self._app.log_list.append(get_date("Updating screen for device %s" % (device,)))

                    # Create Device Screen with sensors
                    box_device = BoxLayout(orientation='vertical', spacing=10)
                    box_device.add_widget(Label(text=''))
                    box_device.add_widget(Label(size_hint_y=0.2, text='[color=ff3333]' + device + '[/color]', font_size=40, markup=True))
                    box_device.add_widget(Label(text=''))

                    # Create Sensor Screen and button on device screen
                    for sensor in j[device]:
                        sensor_name = sensor.keys()[0]
                        sensor_data = sensor[sensor_name]
                        sensor_values = sensor_data['last_records']

                        sensor_dict = updates_to_plots(sensor_values)
                        sensor_plots = sensor_dict['plots']
                        ymin = sensor_dict['min_y']
                        ymax = sensor_dict['max_y']
                        xmin = sensor_dict['min_x']

                        last_date, last_value = sensor_values[-1]

                        # Determine suffix
                        suffix = ' '
                        if re.match(r'.*temp.*', sensor_name, re.IGNORECASE):
                            suffix = u"\u00b0C"
                        if re.match(r'.*humid.*', sensor_name, re.IGNORECASE):
                            suffix = " %"
                        if re.match(r'.*smoke.*', sensor_name, re.IGNORECASE):
                            suffix = " %"
                        if re.match(r'.*stove.*', sensor_name, re.IGNORECASE):
                            suffix = " %"

                        sensor = device + '_' + sensor_name
                        Logger.info(str(sensor))
                        Logger.info("Last data %s %s" % (last_date, last_value))

                        # Create new history view
                        box_sensor_history = BoxLayout(orientation='vertical', spacing=10)
                        box_sensor_history.add_widget(Label(size_hint_y=0.1, text='[color=B6BAB9]' + sensor_name + ' (' + device + ')[/color]', font_size=30, markup=True))

                        # Create history text
                        text_history = []
                        for d, v in sensor_values:
                            text_history.append(str("%s    %s" % (d, v)))

                        # Create left aligned list
                        adapter = SimpleListAdapter(data=text_history, cls=MyLeftAlignedLabel)
                        list_view = ListView(adapter=adapter)
                        # Fix bug with ListView to refresh if required
                        if(hasattr(list_view, '_reset_spopulate')):
                            Logger.info("Refresh list_view")
                            list_view._reset_spopulate()
                        # Add ListView to Sensor History
                        box_sensor_history.add_widget(list_view)
                        back_button = Button(size_hint_y=0.1, font_size=20, text='Back')
                        back_button.bind(on_press=partial(self.change_screen, device + "_" + sensor_name))
                        box_sensor_history.add_widget(back_button)

                        screen_sensor_history = return_screen_object(device + "_" + sensor_name + '_history')
                        screen_sensor_history.clear_widgets()
                        screen_sensor_history.add_widget(box_sensor_history)
                        screen_sensor = return_screen_object(device + "_" + sensor_name)

                        box_sensor = BoxLayout(orientation='vertical')
                        box_sensor.add_widget(Label(size_hint_y=0.1, text='[color=B6BAB9]' + sensor_name + ' (' + device + ')[/color]', font_size=30, markup=True))
                        # Add sensor value
                        box_sensor.add_widget(Label(text=last_value + suffix, font_size=60))
                        # Add sensor date
                        box_sensor.add_widget(Label(size_hint_y=0.1, markup=True, text='[b]Sensor last updated ' + last_date[:-3] + '[/b]\nPolled ' + get_date(None)[:-3], font_size=15))
                        # Add sensor graph
                        Logger.info("Create plot for %s" % (sensor_name,))
                        Logger.info(str(sensor_plots))
                        plot = MeshLinePlot(mode='line_strip', color=[1, 0, 0, 1])
                        plot.points = sensor_plots
                        sensor_graph = Graph(id='plots_' + sensor_name, precision='%0.0f', x_grid_label=True, y_grid_label=True, xmin=xmin, xmax=0, ymin=ymin, ymax=ymax, xlabel='days ago', ylabel=suffix, x_grid=True, y_grid=False, x_ticks_major=1, y_ticks_major=1)
                        sensor_graph.add_plot(plot)
                        box_sensor.add_widget(sensor_graph)

                        # Add buttonbar for sensor
                        box_buttons = BoxLayout(orientation='horizontal')

                        # Create button for history
                        history_button = Button(size_hint_y=0.2, font_size=20, text='History')
                        history_button.bind(on_press=partial(self.change_screen, device + "_" + sensor_name + "_history"))

                        # Create Back button
                        back_button = Button(size_hint_y=0.2, font_size=20, text='Back')
                        back_button.bind(on_press=partial(self.change_screen, device))

                        # Add buttons to row
                        box_buttons.add_widget(back_button)
                        box_buttons.add_widget(history_button)

                        # Add row to screen
                        box_sensor.add_widget(box_buttons)

                        # Add all of it to screen
                        screen_sensor.clear_widgets()
                        screen_sensor.add_widget(box_sensor)
Ejemplo n.º 3
0
class QuarantineViewModal(BoxLayout):

    ## Constructon
    # @exception QrException
    # @exception EmptyListException
    def __init__(self, **kwargs):
        self.qrdata = list()
        # FOR NETWORK
        Active.qrList.clear()
        try:
            Active.qrList = Active.qr_task.get_qr_list(Active.client)
        except QrException as qr:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=qr.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = qr.title
            popup.open()
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        for x in range(0, len(Active.qrList)):
            self.qrdata.append({
                'filename': Active.qrList[x].f_name,
                'old_path': Active.qrList[x].o_path
            })

        self.list_adapter = ListAdapter(data=self.qrdata,
                                        args_converter=self.formatter,
                                        selection_mode='single',
                                        allow_empty_selection=False,
                                        cls=QrCompositeListItem)

        super(QuarantineViewModal, self).__init__(**kwargs)
        self.list_view = ListView(adapter=self.list_adapter)
        self.add_widget(self.list_view)
        if len(self.qrdata) is 0:
            detail_view = QrDetailView(qr_name="List is empty",
                                       size_hint=(.6, 1.0))
        else:
            detail_view = QrDetailView(
                qr_name=self.list_adapter.selection[0].text,
                size_hint=(.6, 1.0))

        self.list_adapter.bind(on_selection_change=detail_view.qr_changed)
        self.add_widget(detail_view)
        Clock.schedule_interval(self.callback, 60)
        Clock.schedule_interval(self.callback2, 5)

    ## Callback on clock schedule to update list
    def callback(self, dt):
        self.update_list()

    ## Callback on clock schedule to update list
    def callback2(self, dt):
        global update
        if update != 0:
            update = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)
        if Active.changed['qr'] != 0:
            Active.changed['qr'] = 0
            Clock.schedule_once(lambda dt: self.update_list(), 1)

    ## Updates the Quarantine list
    # @exception QrException
    # @exception EmptyListException
    def update_list(self):
        try:
            Active.qrList = Active.qr_task.get_qr_list(Active.client)
        except QrException as qr:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=qr.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = qr.title
            popup.open()
            return
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        finally:
            self.qrdata.clear()
            for x in range(0, len(Active.qrList)):
                self.qrdata.append({
                    'filename': Active.qrList[x].f_name,
                    'old_path': Active.qrList[x].o_path
                })
            self.list_adapter.data = self.qrdata
            if hasattr(self.list_view, '_reset_spopulate'):
                self.list_view._reset_spopulate()

    ## The args converter
    def formatter(self, rowindex, qr_data):
        return {
            'text':
            qr_data['filename'],
            'size_hint_y':
            None,
            'height':
            50,
            'cls_dicts': [{
                'cls': ListItemButton,
                'kwargs': {
                    'text': qr_data['filename']
                }
            }, {
                'cls': ListItemLabel,
                'kwargs': {
                    'text': "Old Path:"
                }
            }, {
                'cls': ListItemLabel,
                'kwargs': {
                    'text': qr_data['old_path']
                }
            }]
        }
Ejemplo n.º 4
0
class ScannerViewModal(BoxLayout):

    ## Constructor
    # @exception ScanException
    # @exception EmptyListException
    def __init__(self, **kwargs):
        self.scdata = list()
        self.orientation = 'vertical'
        # FOR NETWORK
        Active.scanList.clear()
        try:
            Active.scanList = Active.scan_task.get_scan_list(Active.client)
        except ScanException as se:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=se.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = se.title
            popup.open()
            return
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        for x in range(0, len(Active.scanList)):
            self.scdata.append({'path': Active.scanList[x].path,
                                'options': Active.scanList[x].options})

        self.list_adapter = ListAdapter(data=self.scdata,
                                        args_converter=self.formatter,
                                        cls=ScCompositeListItem,
                                        selection_mode='single',
                                        allow_empty_selection=False)

        super(ScannerViewModal, self).__init__(**kwargs)
        self.list_view = ListView(adapter=self.list_adapter)
        self.add_widget(HeaderBox())
        self.add_widget(self.list_view)
        if len(self.scdata) is 0:
            detail_view = ScDetailView(sc_name="List is empty", size_hint=(.6, 1.0))
        else:
            detail_view = ScDetailView(sc_name=self.list_adapter.selection[0].text, size_hint=(.6, 1.0))

        self.list_adapter.bind(
            on_selection_change=detail_view.sc_changed)
        self.add_widget(detail_view)
        Clock.schedule_interval(self.callback, 60)
        Clock.schedule_interval(self.callback2, 5)

    ## Callback on clock schedule to update list
    def callback(self, dt):
        self.update_list()

    ## Callback on clock schedule to update list
    def callback2(self, dt):
        global update
        if update != 0:
            update = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)
        if Active.changed['sc'] != 0:
            Active.changed['sc'] = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)

    ## Updates the Scanner list
    # @exception ScanException
    # @exception EmptyListException
    def update_list(self):
        try:
            Active.scanList = Active.scan_task.get_scan_list(Active.client)
        except ScanException as se:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=se.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = se.title
            popup.open()
            return
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        self.scdata.clear()
        for x in range(0, len(Active.scanList)):
            self.scdata.append({'path': Active.scanList[x].path,
                              'options': Active.scanList[x].options})
        self.list_adapter.data = self.scdata
        if hasattr(self.list_view, '_reset_spopulate'):
            self.list_view._reset_spopulate()

    ## The args converter
    def formatter(self, rowindex, scdata):
        return {'text': scdata['path'],
                'size_hint_y': None,
                'height': 50,
                'cls_dicts': [{'cls': ListItemButton,
                               'kwargs': {'text': scdata['path'],
                                          'size_hint_x': 0.5}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['BR_S']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['DUP_S']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['DUP_F']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['INTEGRITY']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['CL_TEMP']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['BACKUP_OLD']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['DEL_F_OLD']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['BACKUP']) else False,
                                          'size_hint_x': 0.1}},
                              {'cls': ScCheckBox,
                               'kwargs': {'disabled': True,
                                          'active': True if bool(scdata['options']['DEL_F_SIZE']) else False,
                                          'size_hint_x': 0.1}}]}
Ejemplo n.º 5
0
class QuarantineViewModal(BoxLayout):

    ## Constructon
    # @exception QrException
    # @exception EmptyListException
    def __init__(self, **kwargs):
        self.qrdata = list()
        # FOR NETWORK
        Active.qrList.clear()
        try:
            Active.qrList = Active.qr_task.get_qr_list(Active.client)
        except QrException as qr:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=qr.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = qr.title
            popup.open()
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        for x in range(0, len(Active.qrList)):
            self.qrdata.append({'filename': Active.qrList[x].f_name,
                                'old_path': Active.qrList[x].o_path})

        self.list_adapter = ListAdapter(data=self.qrdata,
                                        args_converter=self.formatter,
                                        selection_mode='single',
                                        allow_empty_selection=False,
                                        cls=QrCompositeListItem)

        super(QuarantineViewModal, self).__init__(**kwargs)
        self.list_view = ListView(adapter=self.list_adapter)
        self.add_widget(self.list_view)
        if len(self.qrdata) is 0:
            detail_view = QrDetailView(qr_name="List is empty", size_hint=(.6, 1.0))
        else:
            detail_view = QrDetailView(qr_name=self.list_adapter.selection[0].text,
                                       size_hint=(.6, 1.0))

        self.list_adapter.bind(
            on_selection_change=detail_view.qr_changed)
        self.add_widget(detail_view)
        Clock.schedule_interval(self.callback, 60)
        Clock.schedule_interval(self.callback2, 5)

    ## Callback on clock schedule to update list
    def callback(self, dt):
        self.update_list()

    ## Callback on clock schedule to update list
    def callback2(self, dt):
        global update
        if update != 0:
            update = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)
        if Active.changed['qr'] != 0:
            Active.changed['qr'] = 0
            Clock.schedule_once(lambda dt: self.update_list(), 1)

    ## Updates the Quarantine list
    # @exception QrException
    # @exception EmptyListException
    def update_list(self):
        try:
            Active.qrList = Active.qr_task.get_qr_list(Active.client)
        except QrException as qr:
            popup = Popup(size_hint=(None, None), size=(400, 150))
            popup.add_widget(Label(text=qr.value))
            popup.bind(on_press=popup.dismiss)
            popup.title = qr.title
            popup.open()
            return
        except EmptyListException as ee:
            global empty
            if empty is 0:
                empty = 1
                popup = Popup(size_hint=(None, None), size=(400, 150))
                popup.add_widget(Label(text=ee.value))
                popup.bind(on_press=popup.dismiss)
                popup.title = ee.title
                popup.open()
        finally:
            self.qrdata.clear()
            for x in range(0, len(Active.qrList)):
                self.qrdata.append({'filename': Active.qrList[x].f_name,
                                  'old_path': Active.qrList[x].o_path})
            self.list_adapter.data = self.qrdata
            if hasattr(self.list_view, '_reset_spopulate'):
                self.list_view._reset_spopulate()

    ## The args converter
    def formatter(self, rowindex, qr_data):
        return {'text': qr_data['filename'],
                'size_hint_y': None,
                'height': 50,
                'cls_dicts': [{'cls': ListItemButton,
                               'kwargs': {'text': qr_data['filename']}},
                              {'cls': ListItemLabel,
                               'kwargs': {'text': "Old Path:"}},
                              {'cls': ListItemLabel,
                               'kwargs': {'text': qr_data['old_path']}}]}
Ejemplo n.º 6
0
class ListViewModal(BoxLayout):
    ## The data containing clients
    data = ListProperty()

    ## Constructor
    def __init__(self, **kwargs):
        self.data = list()
        for x in range(0, len(Active.cl.c_list)):
            self.data.append({
                'hostname': Active.cl.c_list[x].name,
                'ip': Active.cl.c_list[x].ip
            })
        self.list_adapter = ListAdapter(data=self.data,
                                        args_converter=self.formatter,
                                        cls=ClCompositeListItem,
                                        selection_mode='single',
                                        allow_empty_selection=False)
        super(ListViewModal, self).__init__(**kwargs)
        self.list_view = ListView(adapter=self.list_adapter)
        self.add_widget(self.list_view)
        if len(self.data) is 0:
            detail_view = ClientDetailView(cl_name="List is empty", size_hint=(.6, 1.0))
        else:
            detail_view = ClientDetailView(cl_name=self.list_adapter.selection[0].text,
                                       size_hint=(.6, 1.0))

        self.list_adapter.bind(
            on_selection_change=detail_view.cl_changed)
        self.add_widget(detail_view)
        Clock.schedule_interval(self.callback, 5)

    ## Callback on clock schedule to update list
    def callback(self, dt):
        global update
        if update != 0:
            update = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)
        if Active.changed['cl'] != 0:
            Active.changed['cl'] = 0
            Clock.schedule_once(lambda dt: self.update_list(), 0.1)

    ## The args converter
    def formatter(self, rowindex, data):
        return {'text': data['hostname'],
                'size_hint_y': None,
                'height': 40,
                'cls_dicts': [{'cls': ListItemButton,
                               'kwargs': {'text': data['hostname']}},
                              {'cls': ListItemLabel,
                               'kwargs': {'text': data['ip']}}]}

    ## Updates the Client list
    # @exception QrException
    # @exception EmptyListException
    def update_list(self):
        self.data.clear()
        for x in range(0, len(Active.cl.c_list)):
            self.data.append({
                'hostname': Active.cl.c_list[x].name,
                'ip': Active.cl.c_list[x].ip
            })
        self.list_adapter.data = self.data
        if hasattr(self.list_view, '_reset_spopulate'):
            self.list_view._reset_spopulate()