Exemple #1
0
class IPUscribe( wx.Panel ):
    """
    Create the whole IPUscribe panel.
    """

    def __init__(self, parent, prefsIO):
        """
        Create a new instance.
        """
        wx.Panel.__init__(self, parent, -1, style=wx.NO_BORDER)
        sizer = wx.BoxSizer( wx.VERTICAL )

        # members
        self._prefsIO = self._check_prefs(prefsIO)
        self._current_page = 0
        self._sndname = None

        # a set of panels:
        self._txtinfo = wx.StaticText(self,  -1, "", style=wx.ALIGN_LEFT)
        self._trsPanel = IPUscribeData(self, prefsIO)
        self._create_media()
        self._create_nav()

        sizer.Add(self._txtinfo,    proportion=0, flag=wx.ALL|wx.EXPAND, border=8 )
        sizer.Add(self._trsPanel,   proportion=1, flag=wx.ALL|wx.EXPAND, border=2 )
        sizer.Add(self._mediaPanel, proportion=0, flag=wx.ALL|wx.EXPAND, border=2 )
        sizer.Add(self._navPanel,   proportion=0, flag=wx.ALL|wx.EXPAND, border=2)

        # Bind events
        self.Bind(spEVT_FILE_WANDER, self.OnFileWander)
        self.Bind(wx.EVT_SET_FOCUS,  self.OnFocus)

        self.SetSizer(sizer)
        self.SetAutoLayout( True )
        self.Layout()

        self.SetBackgroundColour( self._prefsIO.GetValue('M_BG_COLOUR') )
        self.SetForegroundColour( self._prefsIO.GetValue('M_FG_COLOUR') )
        self.SetFont( self._prefsIO.GetValue('M_FONT') )

    # End __init__
    # ----------------------------------------------------------------------


    def __display_text_in_statusbar(self, text):
        wx.GetTopLevelParent(self).SetStatusText(text,0)

    def __reset_text_in_statusbar(self):
        wx.GetTopLevelParent(self).SetStatusText('')


    #-------------------------------------------------------------------------


    def __create_button(self, bmp):
        """
        Create a button and add it to the sizer.
            - bmp is a picture file name
        """
        btn = platebtn.PlateButton(self._navPanel, -1, bmp=spBitmap(bmp, 24, theme=self._prefsIO.GetValue('M_ICON_THEME')))
        btn.SetInitialSize()
        btn.SetPressColor(wx.LIGHT_GREY)
        btn.Enable( True )
        return btn

    #----------------------------------------------------------------------


    def _create_media(self):
        """
        Create the media panel.
        """
        #self._mediaPanel = SndPlayer(self, orient=wx.HORIZONTAL, refreshtimer=10, prefsIO=self._prefsIO)
        self._mediaPanel = SndPlayer(self, orient=wx.HORIZONTAL, refreshtimer=10, prefsIO=self._prefsIO)

        self._mediaPanel.SetPreferences(self._prefsIO)
        self._mediaPanel.ActivateButtons(False)

    #----------------------------------------------------------------------


    def _create_nav(self):
        """
        Create the page-navigation panel.
        """
        self._navPanel = wx.Panel(self)
        s = wx.BoxSizer( wx.HORIZONTAL )

        # Number of IPUs by page
        self._infoipus = wx.StaticText(self._navPanel, -1, label="IPUs by page: ")
        self._textpage = wx.TextCtrl(self._navPanel, -1, value=str(IPU_BY_PAGE))
        self._spinpage = wx.SpinButton(self._navPanel, style=wx.SP_VERTICAL)
        self._spinpage.SetRange(20, 300)
        self._spinpage.SetValue(IPU_BY_PAGE)

        # Buttons to change the page
        self._buttons = {}
        self._buttons['apply'] = self.__create_button(APPLY_ICON)
        self._buttons['first'] = self.__create_button(PAGE_FIRST_ICON)
        self._buttons['prev']  = self.__create_button(PAGE_PREV_ICON)
        self._buttons['next']  = self.__create_button(PAGE_NEXT_ICON)
        self._buttons['last']  = self.__create_button(PAGE_LAST_ICON)

        # Text to indicate the current page
        self._footer = wx.StaticText(self._navPanel, -1, "")
        self.__set_footer()

        s.Add(self._infoipus, 0, wx.ALL|wx.CENTER)
        s.Add(self._textpage, 0, wx.ALL|wx.CENTER)
        s.Add(self._spinpage, 0, wx.ALL|wx.CENTER)
        s.Add(self._buttons['apply'], proportion=0, flag=wx.ALL, border=2)
        s.AddStretchSpacer()
        s.Add(self._buttons['first'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['prev'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['next'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['last'], proportion=0, flag=wx.ALL, border=2)
        s.AddStretchSpacer()
        s.Add(self._footer, 0, wx.ALL, 6)

        self.Bind(wx.EVT_SPIN, self.OnSpinPage, self._spinpage)
        self._buttons['apply'].Bind(wx.EVT_BUTTON, self.OnChangeIPUbyPage)
        self._buttons['first'].Bind(wx.EVT_BUTTON, self.OnFirstPage)
        self._buttons['prev'].Bind(wx.EVT_BUTTON, self.OnPrevPage)
        self._buttons['next'].Bind(wx.EVT_BUTTON, self.OnNextPage)
        self._buttons['last'].Bind(wx.EVT_BUTTON, self.OnLastPage)

        self._navPanel.SetSizer(s)

    # ----------------------------------------------------------------------

    def __set_footer(self):
        """ Set the label of the footer. """
        pagenb = "---"
        if self._current_page:
            pagenb = str(self._current_page)
        pagetotal = "---"
        if self._trsPanel.GetPageCount():
            pagetotal = str(self._trsPanel.GetPageCount())

        self._footer.SetLabel(" Page %s / %s " % (pagenb, pagetotal))

    # ----------------------------------------------------------------------


    def _check_prefs(self, prefs):
        """
        Check if preferences are set properly. Set new ones if required.
        Return the new version.
        """
        if prefs is None:
            prefs = Preferences( BaseTheme() )
        else:
            try:
                bg = prefs.GetValue( 'M_BG_COLOUR' )
                fg = prefs.GetValue( 'M_FG_COLOUR' )
                font = prefs.GetValue( 'M_FONT' )
                icons = prefs.GetValue( 'M_ICON_THEME' )
            except Exception:
                self._prefsIO.SetTheme( BaseTheme() )
        return prefs

    #-------------------------------------------------------------------------


    # ----------------------------------------------------------------------
    # Callbacks... Page navigation
    # ----------------------------------------------------------------------


    def OnFirstPage(self, evt=None):
        """
        Load the first page, except if the current page is already the first one!
        """
        if self._current_page > 1:
            self._current_page = self._trsPanel.LoadPage(1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)


    def OnLastPage(self, evt=None):
        """
        Load the last page, except if the current page is already the last one!
        """
        if self._current_page < self._trsPanel.GetPageCount():
            self._current_page = self._trsPanel.LoadPage(self._trsPanel.GetPageCount())
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)


    def OnPrevPage(self, evt=None):
        """
        Load the previous page, except if the current page is the first one!.
        """
        if self._current_page > 1:
            self._current_page = self._trsPanel.LoadPage(self._current_page-1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)


    def OnNextPage(self, evt=None):
        """
        Load the next page, except if the current page is the last one.
        """
        if self._current_page < self._trsPanel.GetPageCount():
            self._current_page = self._trsPanel.LoadPage(self._current_page+1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)


    def OnSpinPage(self, evt):
        """
        Update the text about the number of IPUs by page.
        """
        self._textpage.SetValue(str(evt.GetPosition()))


    def OnChangeIPUbyPage(self, evt):
        """
        Change the IPU_BY_PAGE value.
        """

        try:
            v = int(self._textpage.GetValue())
        except Exception:
            v = -1
        if v <= 0:
            self._textpage.SetValue(str(IPU_BY_PAGE))
            self._spinpage.SetValue(IPU_BY_PAGE)
            v = IPU_BY_PAGE
        self._current_page = self._trsPanel.SetState(ipu_by_page=v)
        self.__set_footer()

    # ------------------------------------------------------------------------


    def OnFocus(self, event):
        """
        An IPU received the focus.
        """
        obj = event.GetEventObject()
        if obj != self._trsPanel:
            return
        self._mediaPanel.FileSelected( self._sndname )
        (s,e) = self._trsPanel.GetSelectionStartEnd()
        self._mediaPanel.SetOffsetPeriod( int(s),int(e) )

    # ------------------------------------------------------------------------


    # ----------------------------------------------------------------------
    # Functions...
    # ----------------------------------------------------------------------


    def Save(self):
        """ Save the transcription. """

        self._trsPanel.Save()

    # End Save
    # ----------------------------------------------------------------------


    def SetFont(self, font):
        """ Change font of all texts. """

        wx.Window.SetFont( self,font )
        # Apply on all panels
        self._trsPanel.SetFont( font )
        self._mediaPanel.SetFont( font )
        self._navPanel.SetFont( font )

        self._infoipus.SetFont( font )
        self._textpage.SetFont( font )
        self._footer.SetFont( font )
        self._txtinfo.SetFont( font )

    # End SetFont
    # ----------------------------------------------------------------------


    def SetBackgroundColour(self, color):
        """ Change background of all panels. """

        wx.Window.SetBackgroundColour( self,color )
        # Apply as background on all panels
        self._trsPanel.SetBackgroundColour( color )
        self._mediaPanel.SetBackgroundColour( color )
        self._navPanel.SetBackgroundColour( color )

        self._infoipus.SetBackgroundColour( color )
        self._textpage.SetBackgroundColour( color )
        self._spinpage.SetBackgroundColour( color )
        self._footer.SetBackgroundColour( color )
        self._txtinfo.SetBackgroundColour( color )

    # End SetBackgroundColour
    # ----------------------------------------------------------------------


    def SetForegroundColour(self, color):
        """ Change foreground of all panels. """

        wx.Window.SetForegroundColour( self,color )
        # Apply as foreground on all panels
        self._trsPanel.SetForegroundColour( color )
        self._mediaPanel.SetForegroundColour( color )
        self._navPanel.SetForegroundColour( color )

        self._infoipus.SetForegroundColour( color )
        self._textpage.SetForegroundColour( color )
        self._spinpage.SetForegroundColour( color )
        self._footer.SetForegroundColour( color )
        self._txtinfo.SetForegroundColour( color )

    # End SetForegroundColour
    # ----------------------------------------------------------------------


    # ----------------------------------------------------------------------
    # Callbacks
    # ----------------------------------------------------------------------


    def OnFileWander(self, event):
        """
        A file was selected/unselected.
        """
        f = event.filename
        s = event.status

        if s is True:
            self.FileSelected( f )
        else:
            self.FileDeSelected()

    # End OnFileWander
    # ----------------------------------------------------------------------


    # ----------------------------------------------------------------------
    # Data Management
    # ----------------------------------------------------------------------

    def exists(self, filename):
        for x in os.listdir( os.path.dirname(filename)):
            try:
                if os.path.basename(filename.lower()) == x.lower():
                    return os.path.join(os.path.dirname(filename),x)
            except Exception:
                pass
        return None


    def FileSelected(self, filename):
        """
        Add files.
        """
        got = False
        name = os.path.splitext(filename)[0]
        for ext in annotationdata.io.extensions:
            if ext == '.txt':
                continue
            f = self.exists( name+ext )
            if got is False and f is not None:
                # create the object
                r = self._trsPanel.SetData(filename, f)
                if r is True:
                    self._txtinfo.SetLabel("Transcription file: "+f)
                    self._current_page = self._trsPanel.LoadPage(1)
                    # show the current page and the total amount of pages at the bottom of the window
                    self.__set_footer()
                    self.Layout()
                    got = True
                else:
                    self._trsPanel.UnsetData()

        if got is False:
            dlg = wx.MessageDialog(self,
                    "Error. Missing IPUs: A file with an IPUs segmentation is required.",
                    "IPUScribe error", wx.OK | wx.ICON_EXCLAMATION)
            dlg.ShowModal()
            dlg.Destroy()
            self.FileDeSelected()
            return

        self._sndname = filename

    # End OnFileSelected
    # ------------------------------------------------------------------------


    def FileDeSelected(self):
        """
        Remove the file.
        """
        if self._trsPanel._dirty is True:
            # dlg to ask to save or not
            dial = wx.MessageDialog(None, 'Do you want to save changes on the transcription of\n%s?'%self._sndname, 'Question', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
            userChoice = dial.ShowModal()
            if userChoice == wx.ID_YES:
                self._trsPanel.Save()

        self._trsPanel.UnsetData()
        self._mediaPanel.FileDeSelected()
        self._current_page = 0
        self._txtinfo.SetLabel("")
        self._mediaPanel.onClose(None)

        evt = FileWanderEvent(filename=self._sndname, status=False)
        evt.SetEventObject(self)
        wx.PostEvent( self.GetParent().GetParent().GetParent(), evt )
        self._sndname = None
class IPUscribe( wx.Panel ):
    """
    Create the whole IPUscribe panel.

    """
    def __init__(self, parent, prefsIO):
        """ Create a new instance. """

        wx.Panel.__init__(self, parent, -1, style=wx.NO_BORDER)
        sizer = wx.BoxSizer( wx.VERTICAL )

        # members
        self._prefsIO = self._check_prefs(prefsIO)
        self._current_page = 0
        self._sndname = None

        # a set of panels:
        self._txtinfo = wx.StaticText(self,  -1, "", style=wx.ALIGN_LEFT)
        self._trsPanel = IPUscribeData(self, prefsIO)
        self._create_media()
        self._create_nav()

        sizer.Add(self._txtinfo,    proportion=0, flag=wx.ALL|wx.EXPAND, border=8 )
        sizer.Add(self._trsPanel,   proportion=1, flag=wx.ALL|wx.EXPAND, border=2 )
        sizer.Add(self._mediaPanel, proportion=0, flag=wx.ALL|wx.EXPAND, border=2 )
        sizer.Add(self._navPanel,   proportion=0, flag=wx.ALL|wx.EXPAND, border=2)

        # Bind events
        self.Bind(spEVT_FILE_WANDER, self.OnFileWander)
        self.Bind(wx.EVT_SET_FOCUS,  self.OnFocus)
        self.GetTopLevelParent().Bind(wx.EVT_CHAR_HOOK, self.OnKeyPress)

        self.SetSizer(sizer)
        self.SetAutoLayout( True )
        self.Layout()

        self.SetBackgroundColour( self._prefsIO.GetValue('M_BG_COLOUR') )
        self.SetForegroundColour( self._prefsIO.GetValue('M_FG_COLOUR') )
        self.SetFont( self._prefsIO.GetValue('M_FONT') )

    # ----------------------------------------------------------------------

    def __create_button(self, bmp):
        """
        Create a button and add it to the sizer.
            - bmp is a picture file name
        """
        btn = platebtn.PlateButton(self._navPanel, -1, bmp=spBitmap(bmp, 24, theme=self._prefsIO.GetValue('M_ICON_THEME')))
        btn.SetInitialSize()
        btn.SetPressColor(wx.LIGHT_GREY)
        btn.Enable( True )
        return btn

    #----------------------------------------------------------------------

    def _create_media(self):
        """ Create the media panel. """

        self._mediaPanel = SndPlayer(self, orient=wx.HORIZONTAL, refreshtimer=10, prefsIO=self._prefsIO)
        self._mediaPanel.SetPreferences(self._prefsIO)
        self._mediaPanel.ActivateButtons(False)

    #----------------------------------------------------------------------

    def _create_nav(self):
        """ Create the page-navigation panel. """

        self._navPanel = wx.Panel(self)
        s = wx.BoxSizer( wx.HORIZONTAL )

        # Number of IPUs by page
        self._infoipus = wx.StaticText(self._navPanel, -1, label="IPUs by page: ")
        self._textpage = wx.TextCtrl(self._navPanel, -1, value=str(IPU_BY_PAGE))
        self._spinpage = wx.SpinButton(self._navPanel, style=wx.SP_VERTICAL)
        self._spinpage.SetRange(20, 300)
        self._spinpage.SetValue(IPU_BY_PAGE)

        # Buttons to change the page
        self._buttons = {}
        self._buttons['apply'] = self.__create_button(APPLY_ICON)
        self._buttons['first'] = self.__create_button(PAGE_FIRST_ICON)
        self._buttons['prev']  = self.__create_button(PAGE_PREV_ICON)
        self._buttons['next']  = self.__create_button(PAGE_NEXT_ICON)
        self._buttons['last']  = self.__create_button(PAGE_LAST_ICON)

        # Text to indicate the current page
        self._footer = wx.StaticText(self._navPanel, -1, "")
        self.__set_footer()

        s.Add(self._infoipus, 0, wx.ALL|wx.CENTER)
        s.Add(self._textpage, 0, wx.ALL|wx.CENTER)
        s.Add(self._spinpage, 0, wx.ALL|wx.CENTER)
        s.Add(self._buttons['apply'], proportion=0, flag=wx.ALL, border=2)
        s.AddStretchSpacer()
        s.Add(self._buttons['first'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['prev'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['next'], proportion=0, flag=wx.ALL, border=2)
        s.Add(self._buttons['last'], proportion=0, flag=wx.ALL, border=2)
        s.AddStretchSpacer()
        s.Add(self._footer, 0, wx.ALL, 6)

        self.Bind(wx.EVT_SPIN, self.OnSpinPage, self._spinpage)
        self._buttons['apply'].Bind(wx.EVT_BUTTON, self.OnChangeIPUbyPage)
        self._buttons['first'].Bind(wx.EVT_BUTTON, self.OnFirstPage)
        self._buttons['prev'].Bind(wx.EVT_BUTTON, self.OnPrevPage)
        self._buttons['next'].Bind(wx.EVT_BUTTON, self.OnNextPage)
        self._buttons['last'].Bind(wx.EVT_BUTTON, self.OnLastPage)

        self._navPanel.SetSizer(s)

    # ----------------------------------------------------------------------

    def __set_footer(self):
        """ Set the label of the footer. """

        pagenb = "---"
        if self._current_page:
            pagenb = str(self._current_page)
        pagetotal = "---"
        if self._trsPanel.GetPageCount():
            pagetotal = str(self._trsPanel.GetPageCount())

        self._footer.SetLabel(" Page %s / %s " % (pagenb, pagetotal))

    # ----------------------------------------------------------------------

    def _check_prefs(self, prefs):
        """
        Check if preferences are set properly. Set new ones if required.
        Return the new version.

        """
        if prefs is None:
            prefs = Preferences( BaseTheme() )
        else:
            try:
                a = prefs.GetValue( 'M_BG_COLOUR' )
                a = prefs.GetValue( 'M_FG_COLOUR' )
                a = prefs.GetValue( 'M_FONT' )
                a = prefs.GetValue( 'M_ICON_THEME' )
            except Exception:
                self._prefsIO.SetTheme( BaseTheme() )
                prefs = self._prefsIO

        prefs.SetValue('SND_AUTOREPLAY', 'bool', True)
        prefs.SetValue('SND_INFO', 'bool', True)
        prefs.SetValue('SND_PLAY', 'bool', True)
        prefs.SetValue('SND_PAUSE', 'bool', True)
        prefs.SetValue('SND_STOP', 'bool', True)
        prefs.SetValue('SND_NEXT', 'bool', False)
        prefs.SetValue('SND_REWIND', 'bool', False)

        return prefs


    # ----------------------------------------------------------------------
    # Callbacks... Page navigation
    # ----------------------------------------------------------------------

    def OnFirstPage(self, evt=None):
        """
        Load the first page, except if the current page is already the first one!
        """
        if self._current_page > 1:
            self._current_page = self._trsPanel.LoadPage(1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)

    # ----------------------------------------------------------------------

    def OnLastPage(self, evt=None):
        """
        Load the last page, except if the current page is already the last one!
        """
        if self._current_page < self._trsPanel.GetPageCount():
            self._current_page = self._trsPanel.LoadPage(self._trsPanel.GetPageCount())
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)

    # ----------------------------------------------------------------------

    def OnPrevPage(self, evt=None):
        """
        Load the previous page, except if the current page is the first one!.
        """
        if self._current_page > 1:
            self._current_page = self._trsPanel.LoadPage(self._current_page-1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)

    # ----------------------------------------------------------------------

    def OnNextPage(self, evt=None):
        """
        Load the next page, except if the current page is the last one.
        """
        if self._current_page < self._trsPanel.GetPageCount():
            self._current_page = self._trsPanel.LoadPage(self._current_page+1)
            self.__set_footer()
            self._trsPanel.KillFocus()
            self._mediaPanel.SetOffsetPeriod(0,0)

    # ----------------------------------------------------------------------

    def OnSpinPage(self, evt):
        """
        Update the text about the number of IPUs by page.
        """
        self._textpage.SetValue(str(evt.GetPosition()))

    # ----------------------------------------------------------------------

    def OnChangeIPUbyPage(self, evt):
        """
        Change the IPU_BY_PAGE value.
        """

        try:
            v = int(self._textpage.GetValue())
        except Exception:
            v = -1
        if v <= 0:
            self._textpage.SetValue(str(IPU_BY_PAGE))
            self._spinpage.SetValue(IPU_BY_PAGE)
            v = IPU_BY_PAGE
        self._current_page = self._trsPanel.SetState(ipu_by_page=v)
        self.__set_footer()

    # ------------------------------------------------------------------------

    def OnFocus(self, event):
        """
        An IPU received the focus.
        """
        obj = event.GetEventObject()
        if obj != self._trsPanel:
            return
        self._mediaPanel.FileSelected( self._sndname )
        (s,e) = self._trsPanel.GetSelectionStartEnd()
        self._mediaPanel.SetOffsetPeriod( int(s),int(e) )


    # ----------------------------------------------------------------------
    # Functions...
    # ----------------------------------------------------------------------


    def Save(self):
        """ Save the transcription. """

        self._trsPanel.Save()

    # ----------------------------------------------------------------------

    def SetFont(self, font):
        """ Change font of all texts. """

        wx.Window.SetFont( self,font )
        # Apply on all panels
        self._trsPanel.SetFont( font )
        self._mediaPanel.SetFont( font )
        self._navPanel.SetFont( font )

        self._infoipus.SetFont( font )
        self._textpage.SetFont( font )
        self._footer.SetFont( font )
        self._txtinfo.SetFont( font )

    # ----------------------------------------------------------------------

    def SetBackgroundColour(self, color):
        """ Change background of all panels. """

        wx.Window.SetBackgroundColour( self,color )
        # Apply as background on all panels
        self._trsPanel.SetBackgroundColour( color )
        self._mediaPanel.SetBackgroundColour( color )
        self._navPanel.SetBackgroundColour( color )

        self._infoipus.SetBackgroundColour( color )
        self._textpage.SetBackgroundColour( color )
        self._spinpage.SetBackgroundColour( color )
        self._footer.SetBackgroundColour( color )
        self._txtinfo.SetBackgroundColour( color )

    # ----------------------------------------------------------------------

    def SetForegroundColour(self, color):
        """ Change foreground of all panels. """

        wx.Window.SetForegroundColour( self,color )
        # Apply as foreground on all panels
        self._trsPanel.SetForegroundColour( color )
        self._mediaPanel.SetForegroundColour( color )
        self._navPanel.SetForegroundColour( color )

        self._infoipus.SetForegroundColour( color )
        self._textpage.SetForegroundColour( color )
        self._spinpage.SetForegroundColour( color )
        self._footer.SetForegroundColour( color )
        self._txtinfo.SetForegroundColour( color )


    # ----------------------------------------------------------------------
    # Callbacks
    # ----------------------------------------------------------------------

    def OnFileWander(self, event):
        """ A file was selected/unselected.  """

        f = event.filename
        s = event.status
        if s is True:
            self.FileSelected( f )
        else:
            self.FileDeSelected()


    # ----------------------------------------------------------------------
    # Data Management
    # ----------------------------------------------------------------------

    def exists(self, filename):
        for x in os.listdir( os.path.dirname(filename)):
            try:
                if os.path.basename(filename.lower()) == x.lower():
                    return os.path.join(os.path.dirname(filename),x)
            except Exception:
                pass
        return None

    # ----------------------------------------------------------------------

    def FileSelected(self, filename):
        """ Add files. """

        got = False
        name = os.path.splitext(filename)[0]
        for ext in annotationdata.io.extensions:
            if ext in [ '.pitchtier', '.hz', '.txt']:
                continue
            f = self.exists( name+ext )
            if got is False and f is not None:
                # create the object
                r = self._trsPanel.SetData(filename, f)
                if r is True:
                    self._txtinfo.SetLabel("Transcription file: "+f)
                    self._current_page = self._trsPanel.LoadPage(1)
                    # show the current page and the total amount of pages at the bottom of the window
                    self.__set_footer()
                    self.Layout()
                    got = True
                else:
                    self._trsPanel.UnsetData()

        self._sndname = filename

        if got is False:
            ShowInformation(self, self._prefsIO, "Missing IPUs: A file with an IPUs segmentation is required.", wx.ICON_ERROR)
            self.FileDeSelected()
            return

    # ------------------------------------------------------------------------

    def FileDeSelected(self):
        """ Remove the file. """

        if self._trsPanel._dirty is True:
            # dlg to ask to save or not
            userChoice = ShowYesNoQuestion( None, self._prefsIO, "Do you want to save changes on the transcription of audio file %s?"%self._sndname)
            if userChoice == wx.ID_YES:
                self._trsPanel.Save()

        try:
            self._trsPanel.UnsetData()
            self._current_page = 0
            self._txtinfo.SetLabel("")
            self._mediaPanel.FileDeSelected()
            self._mediaPanel.onClose(None)
        except Exception as e:
            logging.debug('ERROR for file %s: %s'%(self._sndname,str(e)))

        evt = FileWanderEvent(filename=self._sndname, status=False)
        evt.SetEventObject(self)
        wx.PostEvent( self.GetParent().GetParent().GetParent(), evt )
        self._sndname = None

    # ------------------------------------------------------------------------

    def OnKeyPress(self, event):
        """ Respond to a keypress event. """

        keycode = event.GetKeyCode()

        # Media player
        #     TAB -> PLay
        #     F7 -> Pause
        #     ESC -> Stop
        if keycode == wx.WXK_TAB:
            self._mediaPanel.onPlay( event )

        elif keycode == wx.WXK_F6:
            self._mediaPanel.onRewind( event )

        elif keycode == wx.WXK_F7:
            self._mediaPanel.onPause( event )

        elif keycode == wx.WXK_F8:
            self._mediaPanel.onNext( event )

        elif keycode == wx.WXK_ESCAPE:
            self._mediaPanel.onStop( event )

        else:
            event.Skip()