def on_import_spectra(self, _widget, *_args): """Load one or more spectra from a file chosen by the user.""" dialog = Gtk.FileChooserDialog( "Import data...", self.win, Gtk.FileChooserAction.OPEN, ("_Cancel", Gtk.ResponseType.CANCEL, "_Open", Gtk.ResponseType.OK), ) dialog.set_current_folder(__config__.get("io", "data_dir")) dialog.set_select_multiple(True) dialog.add_filter( SimpleFileFilter("all files", ["*.xym", "*.txt", "*.xy"])) dialog.add_filter(SimpleFileFilter(".xym", ["*.xym"])) dialog.add_filter(SimpleFileFilter(".xy", ["*.xy"])) dialog.add_filter(SimpleFileFilter(".txt", ["*.txt"])) specdicts = [] response = dialog.run() if response == Gtk.ResponseType.OK: __config__.set("io", "data_dir", dialog.get_current_folder()) for fname in dialog.get_filenames(): ext = fname.split(".")[-1] if ext in ["xym", "txt", "xy"]: specdicts.extend(fileio.parse_spectrum_file(fname)) logger.info("parsed and added file {}".format(fname)) else: logger.warning("file {} not recognized".format(fname)) for specdict in specdicts: self.dh.add_spectrum(**specdict) else: logger.debug("nothing selected to import") dialog.destroy()
def on_export_as_txt(self, _action, *_args): """Export currently viewed stuff as txt.""" spectrumIDs = self.view.get_active_spectra() if len(spectrumIDs) != 1: logger.warning("txt export only supports single spectra") return False dialog = Gtk.FileChooserDialog( "Export as txt...", self.win, Gtk.FileChooserAction.SAVE, ("_Cancel", Gtk.ResponseType.CANCEL, "_Save", Gtk.ResponseType.OK), ) dialog.set_current_folder(__config__.get("io", "export_dir")) pfile = __config__.get("io", "project_file") if pfile != "None": bname = "{}_data.txt".format(os.path.basename(pfile).split(".")[0]) else: bname = "Untitled.txt" dialog.set_current_name(bname) dialog.set_do_overwrite_confirmation(True) dialog.add_filter(SimpleFileFilter(".txt", ["*.txt"])) response = dialog.run() if response == Gtk.ResponseType.OK: fname = dialog.get_filename() fileio.export_txt(self.dh, spectrumIDs[0], fname) __config__.set("io", "export_dir", fname) dialog.destroy() return True logger.debug("abort file export") dialog.destroy() return False
def on_new(self, _widget, *_args): """Clear data and start a new project after asking if the user is sure.""" really_do_it = self.ask_for_save() if really_do_it: self.view.activate_spectra([]) self.dh.clear_spectra() self.win.set_title(u"{}* — {}".format("Untitled", __appname__)) __config__.set("io", "project_file", "None") logger.info("emptied datahandler, new project")
def do_activate(self): """Creates MainWindow.""" self.win = self.builder.get_object("main_window") self.win.set_application(self) self.win.set_helpers(self.builder, self.view) self.win.set_title(__appname__) self.win.show_all() def alter_dh(altered=True): """ Stars the filename in the window title when datahandler gets altered. """ title = self.win.get_title() if altered and u"—" in title and "*" not in title: fname = title.split(u"—")[0].strip() self.win.set_title(u"{}* — {}".format(fname, __appname__)) elif not altered and "*" in title: self.win.set_title(title.replace("*", "")) self.dh.connect("altered", alter_dh) fname = __config__.get("io", "project_file") if fname != "None": try: self.open_project(fname) except FileNotFoundError: self.dh.emit_init_ready() logger.warning("File '{}' not found".format(fname)) __config__.set("io", "project_file", "None") else: logger.info("loaded file {}".format(fname)) else: self.dh.emit_init_ready() handlers = { "on_main_window_delete_event": self.on_quit, "on_smoothing_scale_adjustment_value_changed": self.on_smoothen, "on_calibration_spinbutton_adjustment_value_changed": self.on_calibrate, # "on_normalization_switch_activate": self.on_normalize, "on_normalization_combo_changed": self.on_normalize, "on_region_background_type_combo_changed": self.on_change_bgtype, "on_peak_entry_activate": self.on_peak_entry_activate, "on_peak_name_entry_changed": self.on_peak_name_entry_changed, } handlers.update(self.win.handlers) self.builder.connect_signals(handlers)
def on_export_as_image(self, action, *_args): """Export current view as image.""" spectrumIDs = self.view.get_active_spectra() if len(spectrumIDs) < 1: logger.warning("image export on empty canvas not possible") return False cviface = XPLExportCanvasInterface(self.builder, self.dh) cviface.plot() cviface.configure_figure() cvdialog = self.builder.get_object("export_canvas_dialog") response = cvdialog.run() if response != Gtk.ResponseType.OK: cvdialog.hide() logger.debug("abort file export") return False fcdialog = Gtk.FileChooserDialog( "Export as image...", self.win, Gtk.FileChooserAction.SAVE, ("_Cancel", Gtk.ResponseType.CANCEL, "_Save", Gtk.ResponseType.OK), ) fcdialog.set_current_folder(__config__.get("io", "export_dir")) pfile = __config__.get("io", "project_file") if pfile != "None": bname = "{}_img.png".format(os.path.basename(pfile).split(".")[0]) else: bname = "Untitled.png" fcdialog.set_current_name(bname) fcdialog.set_do_overwrite_confirmation(True) fcdialog.add_filter( SimpleFileFilter( "images", ("*.png", "*.svg", "*.svgz", "*.jpeg", "*.jpg", "*.tif", "*.tiff", "*.ps", "*.eps", "*.pdf", "*.pgf"))) response = fcdialog.run() if response != Gtk.ResponseType.OK: fcdialog.destroy() logger.debug("abort file export") return self.on_export_as_image(action) fname = fcdialog.get_filename() cviface.save(fname) __config__.set("io", "export_dir", fname) fcdialog.destroy() return True
def on_open_project(self, _widget, *_args): """Let the user choose a project file to open and open it through self.open_project.""" really_do_it = self.ask_for_save() if not really_do_it: return dialog = Gtk.FileChooserDialog( "Open...", self.win, Gtk.FileChooserAction.OPEN, ("_Cancel", Gtk.ResponseType.CANCEL, "_Open", Gtk.ResponseType.OK), ) dialog.set_current_folder(__config__.get("io", "project_dir")) dialog.add_filter(SimpleFileFilter(".xpl", ["*.xpl"])) response = dialog.run() if response == Gtk.ResponseType.OK: fname = dialog.get_filename() __config__.set("io", "project_file", fname) __config__.set("io", "project_dir", dialog.get_current_folder()) self.open_project(fname) else: logger.debug("abort project file opening") dialog.destroy()
def on_save_as(self, widget, *_args): """Saves project to a new file chosen by the user.""" dialog = Gtk.FileChooserDialog( "Save as...", self.win, Gtk.FileChooserAction.SAVE, ("_Cancel", Gtk.ResponseType.CANCEL, "_Save", Gtk.ResponseType.OK), ) dialog.set_current_folder(__config__.get("io", "project_dir")) dialog.set_do_overwrite_confirmation(True) dialog.add_filter(SimpleFileFilter(".xpl", ["*.xpl"])) dialog.set_current_name("untitled.xpl") response = dialog.run() if response == Gtk.ResponseType.OK: fname = dialog.get_filename() __config__.set("io", "project_file", fname) __config__.set("io", "project_dir", dialog.get_current_folder()) self.on_save(widget) dialog.destroy() return True logger.debug("abort project file saving") dialog.destroy() return False
def on_quit(self, *_args): """Quit program, write to config file.""" really_do_it = self.ask_for_save() if not really_do_it: return True xsize, ysize = self.win.get_size() xpos, ypos = self.win.get_position() __config__.set("window", "xsize", str(xsize)) __config__.set("window", "ysize", str(ysize)) __config__.set("window", "xpos", str(xpos)) __config__.set("window", "ypos", str(ypos)) with open(str(CFG_PATH), "w") as cfg_file: logger.info("writing config file...") __config__.write(cfg_file) logger.info("quitting...") self.quit() return False