def __init__(self,controller):
     super(WinFilterChain, self).__init__() 
     self.controller = controller        
     self.filterchain = None
     self.filename = None        
     
     self.controller.add_thread_observer(self.thread_observer)
     
     self.ui = get_ui(self)
     self.loadSources()
     self.ui.sourcesComboBox.currentIndexChanged[str].connect(self.startSource)
     self.ui.filterListWidget.currentItemChanged.connect(self.onSelectedFilterchanged)
     self.ui.sourcesButton.clicked.connect(self.getSourcesFilepath)
     self.winSource = WinSource()
class WinFilterChain(QtCore.QObject):
    
    """Main window
    Allow the user to create, edit and test filter chains
    """
    
    WINDOW_TITLE = "Capra Vision"
    selectedFilterChanged = QtCore.Signal(object)
    
    def __init__(self,controller):
        super(WinFilterChain, self).__init__() 
        self.controller = controller        
        self.filterchain = None
        self.filename = None        
        
        self.controller.add_thread_observer(self.thread_observer)
        
        self.ui = get_ui(self)
        self.loadSources()
        self.ui.sourcesComboBox.currentIndexChanged[str].connect(self.startSource)
        self.ui.filterListWidget.currentItemChanged.connect(self.onSelectedFilterchanged)
        self.ui.sourcesButton.clicked.connect(self.getSourcesFilepath)
        self.winSource = WinSource()

    def open_chain(self):
        filename = QtGui.QFileDialog().getOpenFileName(filter="*.filterchain")[0]
        if filename:
            self.filename = filename
            self.filterchain = filterchain.read(filename)
            self.filterchain.add_filter_observer(self.updateFilterChain)
            self.ui.sourceNameLineEdit.setText(self.filename)
            self.updateFilterChain()

    def new_chain(self):
        self.filename = None
        self.filterchain = filterchain.FilterChain()
        self.filterchain.add_filter_observer(self.updateFilterChain)
        self.updateFilterChain()
        self.ui.sourceNameLineEdit.setText("<new>")

    def save_chain(self):
        if self.filename == None:
            if not self.save_chain_as():
                return
        if self.filterchain is not None and self.filename is not None:
            filterchain.write(self.filename,self.filterchain)
        else:
            QtGui.QMessageBox.warning(self.ui,"filterchain","filterchain is null.")
        
    def save_chain_as(self):
        filename = QtGui.QFileDialog.getSaveFileName(filter="*.filterchain")[0]        
        if len(filename)>0:
            self.filename = filename
            self.ui.sourceNameLineEdit.setText(self.filename)
            self.save_chain()
            return True
        return False     
    
    def add_filter(self,filter):
        if self.filterchain is not None:
            self.filterchain.add_filter(filter)            
        else:
            QtGui.QMessageBox.warning(self.ui,"filterChain","filterchain is null.")
    
    def remove_filter(self):
        if self.filterchain is not None:
            filter = self._getSelectedFilter()
            self.filterchain.remove_filter(filter)
            print filter
        else:
            QtGui.QMessageBox.warning(self.ui,"filterChain","filterchain is null.")
            
           
    def loadSources(self):
        self.ui.sourcesComboBox.clear()
        self.sources = imageproviders.load_sources()
        self.ui.sourcesComboBox.addItem('None')
        for source in self.sources.keys():            
            self.ui.sourcesComboBox.addItem(source) 
             
    def getSourcesFilepath(self):
        self.winSource.show()
        
        #filepath = QtGui.QFileDialog.getExistingDirectory()[0]
        #return filepath        
    
    def startSource(self,sourceText):
        if sourceText == "None":
            return       
        self.controller.change_source(self.sources[sourceText])
        self.winSource.setSource(self.controller.get_source())
            
    def thread_observer(self,image):
        if self.filterchain <> None:
           self.filterchain.execute(image) 
           
    def updateFilterChain(self):
        self.ui.filterListWidget.clear()
        for filter in self.filterchain.filters:
            print filter.__class__
            self.ui.filterListWidget.addItem(filter.__class__.__name__)
            
    def _getSelectedFilter(self):
        filterName = self.ui.filterListWidget.currentRow()
        if filterName == "":
            return None
        return self.filterchain.filters[filterName]
    
    def moveUpSelectedFilter(self):
        filter = self._getSelectedFilter()
        if filter == None:
            return
        self.filterchain.move_filter_up(filter)
    
    def moveDownSelectedFilter(self):
        filter = self._getSelectedFilter()
        if filter == None:
            return
        self.filterchain.move_filter_down(filter)
        
    def deleteSelectedFilter(self):
        filter = self._getSelectedFilter()
        if filter == None:
            return
        self.filterchain.remove_filter(filter)
    
    def onSelectedFilterchanged(self):
        filter = self._getSelectedFilter()
        if filter <> None:
            self.selectedFilterChanged.emit(filter)