Ejemplo n.º 1
0
class DirectViewWindow(QtGui.QMainWindow):
    '''
    View Window that can be connected to a simulation
    '''
    def __init__(self, views, *args, **kwargs):
        QtGui.QMainWindow.__init__(self, *args, **kwargs)
        
        ''' 1. members '''
        self.builder = GBuilder()       

        ''' 2. initialize the gui factories '''
        self.view_plugins = views
        
        ''' 3. actions '''
        self.init_actions()
        self.create_widgets()
    
    @try_ex          
    def create_widgets(self):
        
        ''' 1. Main window '''
        self.setWindowTitle('TUMCreate - Automotive Simulator - Direct View')
        self.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/tumcreatelogo2.png')))
        self.builder.set_props(self, ctr_lout=True, min_sz_x=1200, min_sz_y=700)

        ''' 2. Two Group Boxes '''              
        self.groupb_info = self.builder.groupbox(self, 'Information', max_height=200, max_width=2000) 
        self.set_info_group()
        
        self.groupb_viewer = self.builder.groupbox(self, 'View')
        self.set_viewer_group()
        
        ''' 3. Init Menubar '''
        self.init_menubar();            
                
        ''' 4. Toolbar'''
        self.toolbar_gen_sets = self.builder.toolbar(self, GEnums.T_TOP, actions=self.gen_set_actions)
        
        ''' 5. Create Layout'''
        self.init_layout()
    
    @try_ex
    def init_actions(self):
        
        ''' 1. Create General Settings'''   

        self.exit_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/exit.png')), '&Exit', self)        
        self.exit_action.setShortcut('Ctrl+Q')
        self.exit_action.setStatusTip('Exit application')
        self.exit_action.triggered.connect(QtGui.qApp.quit)
        
        self.help_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/help.png')), '&Help', self)        
        self.help_action.setShortcut('F3')
        self.help_action.setStatusTip('Open Help')
        self.help_action.triggered.connect(QtGui.qApp.quit)
               
        self.clear_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/clear.png')), '&Clear all parameters', self)        
        self.clear_action.setShortcut('Ctrl+Shift+N')
        self.clear_action.setStatusTip('Open Help')
        self.clear_action.triggered.connect(QtGui.qApp.quit) 
        
        self.load_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/load.png')), '&Clear all parameters', self)        
        self.load_action.setShortcut('Ctrl+Shift+L')
        self.load_action.setStatusTip('Load file')
        self.load_action.triggered.connect(self._load_views_hit) 
        
        ''' 2. define all actions that will appear in the general Toolbar '''
        self.gen_set_actions = [self.clear_action, self.help_action, self.load_action]
    
    @try_ex
    def init_layout(self):
        ''' Sets the layout for the three main groupboxes '''
        
        main_layout = self.centralWidget().layout()
        
        h_layout = QtGui.QHBoxLayout()
        
        h_layout.addWidget(self.groupb_info)        
        
        main_layout.addLayout(h_layout)
        
        main_layout.addWidget(self.groupb_viewer)
         
    @try_ex
    def init_menubar(self):
        
        ''' 1. Create Menubar'''
        menubar = self.menuBar()
        
        ''' 2. add Menus and actions'''        
        # File
        file_menu = menubar.addMenu('&File')
        file_menu.addAction(self.exit_action)
                
        # Help        
        help_menu = menubar.addMenu('&Help')
        help_menu.addAction(self.help_action)
        
    def link_axis(self):   
        link_plot = False     
        for view in self.view_plugins:
            # check if there are link axis
            p = view.link_axis()
            if p != None:
                if link_plot:
                    p.setXLink(link_plot)
                else:
                    link_plot = p
        
    @try_ex
    def set_cb_changed_event_set(self):
        self.settings_stack.setCurrentIndex(self.settings_cbox.currentIndex())

    @try_ex
    def set_info_group(self):

        ''' 1. Logo'''
        self.space = QtGui.QLabel()
        self.info_label = QtGui.QLabel("\nTitle:          \t%s \nAuthor:          \t%s\nCompany:\t%s\nDepartement:\t%s\nVersion:          \t%s" % (__title__, __author__, __enterprise__, __departement__ , __version__))
        self.space.setFixedWidth(10)
        
        self.info_logo = self.builder.image(self.groupb_info, os.path.dirname(__file__)[:-4] + r'/icons/tumcreatelogo.png', 2.4)
        self.info_logo.setMaximumWidth(270)
        
        ''' 2. Description Text '''
        self.desc_txt = self.builder.label(self.groupb_info, "<b>Description:    </b>" + '\n\nThis application simulates an automotive environment. It offers the possibility to perform timing analyses of communication flows between a certain number of ECUs.')
                   
        ''' 3. Groupbox Layout'''
        v_layout = QtGui.QVBoxLayout(self.groupb_info)  
                      
        h_layout_one = QtGui.QHBoxLayout()
        h_layout_one.addWidget(self.space)
        h_layout_one.addWidget(self.info_label)        
        h_layout_one.addWidget(self.info_logo)

        v_layout.addLayout(h_layout_one)
        v_layout.addWidget(self.desc_txt)
    
    @try_ex
    def set_cb_changed_event_view(self, e):
        # check which items are selected and show all of them
        try:
            # clear all
            try:
                for ky in self.wid_to_idx:
                    self.wid_to_idx[ky].setParent(None)                
            except:
                ECULogger().log_traceback()
            
            # get checked items
            checked_idx = []
            for cnt in range(self.viewer_cbox.count()):
                item = self.viewer_cbox.model().item(cnt, 0)
                if item.checkState():                
                    checked_idx.append(cnt)
            
            row_nr = int(self.arrange_rows.text())
            col_nr = int(self.arrange_cols.text())
            
            if row_nr * col_nr < len(checked_idx):
                row_nr = ceil(float(len(checked_idx)) / col_nr) 
                self.arrange_rows.setText(str(row_nr))
            # arrange
            cnt = 0
            for r in range(row_nr):            
                hlay = QHBoxLayout()
                for c in range(col_nr):
                    try:
                        wid = self.wid_to_idx[checked_idx[cnt]]; cnt += 1
                        hlay.addWidget(wid)
                    except:
                        pass
                try:
                    self.main_v_layout.addLayout(hlay)
                except:
                    pass
        except:
            pass

    @try_ex
    def set_viewer_group(self):
        
        ''' 1. Load Items from viewer factory '''
        self.wid_to_idx = {}
        self.viewer_cbox = self.builder.checkable_combobox(self.groupb_viewer, [], self.set_cb_changed_event_view)
        self.save_but = self.builder.pushbutton(self.groupb_viewer, "Save", self._save_hit)
        self.save_but.setFixedWidth(100)
        
        # field to enter arrangement
        [lay_r, self.arrange_rows] = self.builder.label_text(self.groupb_viewer, "rows:", 25, 40, self.set_cb_changed_event_view)
        self.arrange_rows.setText('2')
        [lay_c, self.arrange_cols] = self.builder.label_text(self.groupb_viewer, "columns:", 40, 40, self.set_cb_changed_event_view)
        self.arrange_cols.setText('2')
        
        cnt = 0
        for view in self.view_plugins:            
            if view != None:
                self.viewer_cbox.addItem(view.get_combobox_name())
                item = self.viewer_cbox.model().item(cnt, 0)
                item.setCheckState(QtCore.Qt.Checked)
                self.wid_to_idx[cnt] = view.get_widget(self)
                cnt += 1

        ''' Layout '''
        self.main_v_layout = QVBoxLayout()
        self.viewer_cbox.setFixedHeight(25)       
        
        hl = QHBoxLayout()
        hl.addWidget(self.viewer_cbox)   
        hl.addLayout(lay_r)
        hl.addLayout(lay_c)     
        hl.addWidget(self.save_but)
        self.main_v_layout.addLayout(hl)           
              
        self.groupb_viewer.setLayout(self.main_v_layout)       
    
        self.set_cb_changed_event_view(None)
    
    def _load_views_hit(self, filename=False):
        
        if not filename:
            filename = QtGui.QFileDialog.getOpenFileName(self, 'Load', '', 'Simulation (*.tum)')
        with open(filename, 'rb') as f:
            save_vals = pickle.load(f)
        
        for view in self.view_plugins:  
            if view != None:
                try:
                    save_val = save_vals[view.get_combobox_name()]
                except:
                    logging.warn("Warning: On GUI load no saved values found for %s" % view.__class__.__name__)
#                     ECULogger().log_traceback()
                    continue
                view.load(save_val)

    def _save_hit(self):
        
        save_vals = {}
        
        for view in self.view_plugins:  
            if view != None:
                try:
                    print("Call save on %s" % view)
                    save_val = view.save()
                    print("OK!")
                except:
                    ECULogger().log_traceback()
                    continue
                save_vals[view.get_combobox_name()] = save_val
        
        filename = QtGui.QFileDialog.getSaveFileName(self, "Save file", "", ".tum")
        with open(filename, 'wb') as f:          
            pickle.dump(save_vals, f, pickle.HIGHEST_PROTOCOL)
Ejemplo n.º 2
0
class MainWindow(QtGui.QMainWindow):
    '''
    Main Window of the application
    '''
    def __init__(self, *args, **kwargs):
        QtGui.QMainWindow.__init__(self, *args, **kwargs)
        
        ''' 1. members '''
        self.builder = GBuilder()
        self.monitor = Monitor()
        self.setWindowTitle('TUMCreate - Automotive Simulator')
        self.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/tumcreatelogo2.png')))
        
        ''' 2. initialize the gui factories '''
        ViewerPluginFactory().load_classes()
        SettingsPluginFactory().load_classes()
        
        ''' 3. actions '''
        self.init_actions()
        self.create_widgets()
              
    def create_widgets(self):
        
        ''' 1. Main window '''
        self.builder.set_props(self, ctr_lout=True, min_sz_x=1200, min_sz_y=700)
        
        ''' 2. Three Group Boxes '''
        self.groupb_settings = self.builder.groupbox(self, 'Settings', max_height=200)
        self.set_settings_group()
              
        self.groupb_info = self.builder.groupbox(self, 'Information', max_height=200, max_width=700) 
        self.set_info_group()
        
        self.groupb_viewer = self.builder.groupbox(self, 'View')
        self.set_viewer_group() 
        
        ''' 3. Init Menubar '''
        self.init_menubar();            
                
        ''' 4. Toolbar'''
        self.toolbar_gen_sets = self.builder.toolbar(self, GEnums.T_TOP, actions=self.gen_set_actions)

        ''' 5. Create Layout'''
        self.init_layout()
    
    def create_aut_env(self):  # Spaeter on Button Click z.B.
        ''' this method creates the simulation'''               
        
        api_log_path = os.path.join(os.path.dirname(__file__), "../logs/api.log")
        api.show_logging(logging.INFO, api_log_path, True)
        my_env = api.create_environment(2500)
        
        ecu_spec = SimpleECUSpec([], 200, 200)
        ecu_group_1 = api.set_ecus(my_env, 10, 'SecureECU', ecu_spec)
        
        ecu_spec = SimpleECUSpec(['SEC 1'], 200, 200)
        ecu_spec.set_ecu_setting('t_ecu_auth_trigger_process', 100)
        ecu_spec.set_ecu_setting('t_ecu_auth_trigger_intervall', 1000)
        
        sec_mod_group = api.set_ecus(my_env, 1, 'SecLwAuthSecurityModule', ecu_spec)
        
        bus_spec = SimpleBusSpec(['CAN_0'])
        bus_group = api.set_busses(my_env, 1, 'StdCANBus', bus_spec)
        api.connect_bus_by_obj(my_env, 'CAN_0', ecu_group_1 + sec_mod_group)
        
        api.register_ecu_groups_to_secmod(my_env, sec_mod_group[0].ecu_id, [ecu_group_1])
        
        certeros = api.create_cert_manager()
        for ecu in APICore()._ecu_list_from_groups([[ecu_group_1]]):  # UNINTENDED HACK
            api.generate_valid_ecu_cert_cfg(certeros, ecu.ecu_id, CAEnum.CA_L313, 'SEC 1', 0, float('inf'))
        api.generate_valid_sec_mod_cert_cfg(certeros, 'SEC 1', CAEnum.CA_L313, 0, float('inf'))
        api.apply_certification(my_env, certeros)
        
        
        stream_1 = MessageStream(my_env.get_env(), 'SecureECU_0', ['SecureECU_1', 'SecureECU_4', 'SecureECU_5'], 13, float('inf'), 0, float('inf'))
        stream_2 = MessageStream(my_env.get_env(), 'SecureECU_1', ['SecureECU_3', 'SecureECU_2', 'SecureECU_5'], 12, float('inf'), 0, float('inf'))
        stream_3 = MessageStream(my_env.get_env(), 'SecureECU_0', ['SecureECU_4', 'SecureECU_1', 'SecureECU_5'], 222, float('inf'), 0, float('inf'))
        stream_4 = MessageStream(my_env.get_env(), 'SecureECU_3', ['SecureECU_0', 'SecureECU_1', 'SecureECU_5'], 11, float('inf'), 0, float('inf'))
        stream_5 = MessageStream(my_env.get_env(), 'SecureECU_4', ['SecureECU_2', 'SecureECU_1', 'SecureECU_3'], 500, float('inf'), 0, float('inf'))
        api.add_allowed_stream(my_env, 'SEC 1', stream_1)
        api.add_allowed_stream(my_env, 'SEC 1', stream_2)
        api.add_allowed_stream(my_env, 'SEC 1', stream_3)
        api.add_allowed_stream(my_env, 'SEC 1', stream_4)
        api.add_allowed_stream(my_env, 'SEC 1', stream_5)
        
        t_set = TimingFunctionSet()
        ecu_func_set = StdSecurLwSecModTimingFunctions(main_library_tag='CyaSSL')
        t_set.set_mapping_from_function_set('SEC 1', ecu_func_set)
        api.apply_timing_functions_set(my_env, 'SEC 1', t_set)
          
        t_set2 = TimingFunctionSet() 
        ecu_func_set = StdSecurECUTimingFunctions(main_library_tag='CyaSSL')
        
        for ecu in APICore()._ecu_list_from_groups([[ecu_group_1]]):  # UNINTENDED HACK
            t_set2.set_mapping_from_function_set(ecu.ecu_id, ecu_func_set) 
            api.apply_timing_functions_set(my_env, ecu.ecu_id, t_set2)

        api.connect_monitor(my_env, self.monitor, 50)
        api.build_simulation(my_env)
        
        ''' start this in a new thread'''
        sim = SimulationThread(self, my_env)
        sim.start()
          
    def init_actions(self):
        
        ''' 1. Create General Settings'''   
        self.exit_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/exit.png')), '&Exit', self)    
        self.exit_action.setShortcut('Ctrl+Q')
        self.exit_action.setStatusTip('Exit application')
        self.exit_action.triggered.connect(QtGui.qApp.quit)
        
        self.help_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/help.png')), '&Help', self)        
        self.help_action.setShortcut('F3')
        self.help_action.setStatusTip('Open Help')
        self.help_action.triggered.connect(QtGui.qApp.quit)
               
        self.clear_action = QtGui.QAction(QtGui.QIcon(os.path.join(os.path.dirname(__file__), r'../icons/clear.png')), '&Clear all parameters', self)        
        self.clear_action.setShortcut('Ctrl+Shift+N')
        self.clear_action.setStatusTip('Open Help')
        self.clear_action.triggered.connect(QtGui.qApp.quit) 
        
        ''' 2. define all actions that will appear in the general Toolbar '''
        self.gen_set_actions = [self.clear_action, self.help_action]

    def init_layout(self):
        ''' Sets the layout for the three main groupboxes '''
        
        main_layout = self.centralWidget().layout()
        
        h_layout = QtGui.QHBoxLayout()
        
        h_layout.addWidget(self.groupb_settings)
        h_layout.addWidget(self.groupb_info)        
        
        main_layout.addLayout(h_layout)
        main_layout.addWidget(self.groupb_viewer)
         
    def init_menubar(self):
        
        ''' 1. Create Menubar'''
        menubar = self.menuBar()
        
        ''' 2. add Menus and actions'''        
        # File
        file_menu = menubar.addMenu('&File')
        file_menu.addAction(self.clear_action)
        file_menu.addAction(self.exit_action)
                
        # Help        
        help_menu = menubar.addMenu('&Help')
        help_menu.addAction(self.help_action)
    
    def set_settings_group(self):
        
        ''' 1. Load Items from settings factory '''
        set_fact = SettingsPluginFactory()
        self.settings_stack = QtGui.QStackedWidget()
        
        self.settings_cbox = self.builder.combobox(self.groupb_settings, [], self.set_cb_changed_event_set)

        for setting_plugin in set_fact.createable_objects():
            setting = set_fact.make(setting_plugin)
            
            if setting != None:                                                       
                self.settings_stack.addWidget(setting.get_widget(self))
                self.settings_cbox.addItem(setting.get_combobox_name())                
                
        ''' layout '''
        v_layout = QVBoxLayout()
                
        self.settings_cbox.setFixedHeight(20)        
        v_layout.addWidget(self.settings_cbox) 
        v_layout.addWidget(self.settings_stack) 
    
        self.groupb_settings.setLayout(v_layout)
    
    def set_cb_changed_event_set(self):
        self.settings_stack.setCurrentIndex(self.settings_cbox.currentIndex())

    def set_info_group(self):

        ''' 1. Logo'''
        self.space = QtGui.QLabel()
        self.info_label = QtGui.QLabel("\nTitle:          \t%s \nAuthor:          \t%s\nCompany:\t%s\nDepartement:\t%s\nVersion:          \t%s" % (__title__, __author__, __enterprise__, __departement__ , __version__))
        self.space.setFixedWidth(10)
        
        self.info_logo = self.builder.image(self.groupb_info, os.path.join(os.path.dirname(__file__), r'../icons/tumcreatelogo.png', 2.4))
        self.info_logo.setMaximumWidth(270)
        
        ''' 2. Description Text '''
        self.desc_txt = self.builder.label(self.groupb_info, "<b>Description:    </b>" + '\n\nThis application simulates an automotive environment. It offers the possibility to perform timing analyses of communication flows between a certain number of ECUs.')
                   
        ''' 3. Groupbox Layout'''
        v_layout = QtGui.QVBoxLayout(self.groupb_info)  
                      
        h_layout_one = QtGui.QHBoxLayout()
        h_layout_one.addWidget(self.space)
        h_layout_one.addWidget(self.info_label)        
        h_layout_one.addWidget(self.info_logo)

        v_layout.addLayout(h_layout_one)
        v_layout.addWidget(self.desc_txt)

    def set_cb_changed_event_view(self):
        self.viewer_stack.setCurrentIndex(self.viewer_cbox.currentIndex())

    def set_viewer_group(self):
        
        ''' 1. Load Items from viewer factory '''
        view_fact = ViewerPluginFactory()
        self.viewer_stack = QtGui.QStackedWidget()        
        self.viewer_cbox = self.builder.combobox(self.groupb_viewer, [], self.set_cb_changed_event_view)
        self.set_start_button = self.builder.pushbutton(self.groupb_viewer, 'Start Simulation', self.create_aut_env)
        self.set_start_button.setFixedWidth(200)
        self.set_start_button.setFixedHeight(25)

        for viewer_plugin in view_fact.createable_objects():
            view = view_fact.make(viewer_plugin)
            if view != None:
                view.set_monitor(self.monitor)                 
                self.viewer_stack.addWidget(view.get_widget(self))
                self.viewer_cbox.addItem(view.get_combobox_name())                

        ''' layout '''
        v_layout = QVBoxLayout()                
        self.viewer_cbox.setFixedHeight(25)       
        v_layout.addWidget(self.viewer_stack)  
        
        hl = QHBoxLayout()
        hl.addWidget(self.viewer_cbox)
        hl.addWidget(self.set_start_button)
        v_layout.addLayout(hl)           
              
        self.groupb_viewer.setLayout(v_layout)