# CellTracking.py
# By MW, Jul 2013
# GPLv3+
#
# Class managing import/export - graphical part.
#+ likely to be almost empty...
import sys, logging, shutil, os, gtk
sys.path.append("./bin/ImportExport")
import ImportExport
[docs]class ImportExport_gtk :
    def __init__(self) :
        self.importexport = ImportExport.ImportExport()
[docs]    def save_button(self) :
        fn = self.importexport.get_path()
        if fn == None :
            self.saveas_button()
        else :
            if not os.path.isfile(fn) :
                self.importexport.save() # Save directly
            else :
                ext = 1 # Generate filename
                while os.path.isfile(fn+'.backup.'+str(ext)) :
                    ext += 1
                shutil.move(fn, fn+'.backup.'+str(ext)) # Make backup copy
                self.importexport.save()
                #os.remove(fn+'.backup.'+str(ext)) # Delete backup
[docs]    def saveas_button(self) :
        logging.info("Saving movie as...")
        dialog = gtk.FileChooserDialog("Select CellTracking .ct file", action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL,  gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
        resp = dialog.run()
        f = None
        if resp == gtk.RESPONSE_OK :
            f = dialog.get_filename()
            if not f.endswith('.ct') :
                f+='.ct'
            logging.info("CellTracking file selected %s", f)
        dialog.destroy()
        if f != None and not os.path.isfile(f) :
            self.importexport.set_path(f)
            self.importexport.save()
        else :
            logging.error("File %s exists", f)
            dialog = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, "Selected file exists. Please choose another filename.")
            dialog.run()
            dialog.destroy()