def handle_download_action(self): file_name = fs.path.basename(self.attachment_path) file_name, file_ext = fs.path.splitext(file_name) # fs.file_picker cannot take filenames that contain dots default_filename = file_name.replace('.', '_') + file_ext if self.message_type == MessageType.DOCUMENT: file_path = get_save_filename(self, "Save Document", default_filename, f"Document (*{file_ext})") if file_path is not None: file_content = requests.get(url_join(self.chat_window.mscolab_server_url, self.attachment_path)).content with open(file_path, "wb") as f: f.write(file_content) else: file_path = get_save_filename(self, "Save Image", default_filename, f"Image (*{file_ext})") if file_path is not None: self.message_image.save(file_path)
def save_flight_track_as(self): """Slot for the 'Save Active Flight Track As' menu entry. """ default_filename = os.path.join( self.last_save_directory, self.active_flight_track.name + ".ftml") filename = get_save_filename(self, "Save Flight Track", default_filename, "Flight Track (*.ftml)", pickertag="filepicker_default") logging.debug("filename : '%s'", filename) if filename: self.last_save_directory = fs.path.dirname(filename) if filename.endswith('.ftml'): try: self.active_flight_track.save_to_ftml(filename) except (OSError, IOError) as ex: QtWidgets.QMessageBox.critical( self, self.tr("Problem while saving flight track to FTML:"), self.tr(f"ERROR: {type(ex)} {ex}")) for idx in range(self.listFlightTracks.count()): if self.listFlightTracks.item( idx).flighttrack_model == self.active_flight_track: self.listFlightTracks.item(idx).setText( self.active_flight_track.name) else: QtWidgets.QMessageBox.warning( self, "Save flight track", f"File extension is not '.ftml'!\n{filename:}")
def file_saveas(self): default_filename = fs.path.join(MSS_CONFIG_PATH, "mss_settings" + ".json") self.path = get_save_filename(self, "Save file", default_filename, "Text documents (*.json)") if not self.path: # If dialog is cancelled, will return '' return self._save_to_path()
def export_config(self): invalid, dummy = self.validate_data() if invalid: show_popup( self, "Invalid values detected", "Please correct the invalid values (keys colored in red) to be able to save." ) self.statusbar.showMessage( "Please correct the values and try exporting") return False if self.json_model.serialize() == default_options: msg = """Since the current configuration matches the default configuration, \ only an empty json file would be exported.\nDo you still want to continue?""" ret = QtWidgets.QMessageBox.warning( self, self.tr("Mission Support System"), self.tr(msg), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if ret == QtWidgets.QMessageBox.No: return path = get_save_filename(self, "Export configuration", "mss_settings", "JSON files (*.json)") if path: self._save_to_path(path)
def file_saveas(self): if self.check_json(): default_filename = constants.CACHED_CONFIG_FILE path = get_save_filename(self, "Save file", default_filename, "Text documents (*.json)") # If dialog is cancelled, will return '' if path: self._save_to_path(path)
def handle_export(self): # Setting default filename path for filedialogue default_filename = self.active_project_name + ".ftml" file_path = get_save_filename(self, "Save Flight track", default_filename, "Flight track (*.ftml)") if file_path is None: return xml_doc = self.waypoints_model.get_xml_doc() dir_path, file_name = fs.path.split(file_path) with open_fs(dir_path).open(file_name, 'w') as file: xml_doc.writexml(file, indent=" ", addindent=" ", newl="\n", encoding="utf-8")
def merge_file(self): checked_files = [] # list of indices of checked files counter = 0 for count in range(self.listWidget.count()): if hasattr(self.listWidget.item(count), "checkState") and ( self.listWidget.item(count).checkState() == QtCore.Qt.Checked): checked_files.append(count) counter = counter + 1 if counter == 0: self.labelStatusBar.setText("Status: No KML File Found or Selected. Add or Select Files to Merge.") return default_filename = fs.path.join(self.directory_location, "merged_file" + ".kml") filename = get_save_filename(self, "Merge KML Files", default_filename, "KML Files (*.kml)") if filename: _dir_name, file_name = fs.path.split(filename) if filename.endswith('.kml'): try: element = [] count = 0 # used to count elements in order; see usage below for index in checked_files: # index is the indices of checked files _dirname, _name = os.path.split(self.listWidget.item(index).text()) _fs = fs.open_fs(_dirname) with _fs.open(_name, 'r') as kmlf: tree = et.parse(kmlf) # parse kml file root = tree.getroot() # get the root of the file self.remove_ns(root) # removes <kml> and </kml> element.append(copy.deepcopy(root[0])) if index == checked_files[0]: # first checked file becomes the top kml file i.e. the base super_root = et.Element("Folder") super_root.insert(0, element[0]) # adds <Folder> at the top of stripped KML File count = count + 1 continue # since the super root cannot be its own sub root sub_root = et.Element("Folder") sub_root.insert(0, element[count]) # count used since its consecutive, not index element[0].append(sub_root) count = count + 1 logging.debug(et.tostring(super_root, encoding='utf-8').decode('UTF-8')) newkml = et.Element("kml") # create new <kml> element newkml.attrib['xmlns'] = 'http://earth.google.com/kml/2.0' # add xmlns attribute newkml.insert(0, super_root) logging.debug(et.tostring(newkml, encoding='utf-8').decode('UTF-8')) _dirname, _name = os.path.split(filename) _fs = fs.open_fs(_dirname) with _fs.open(_name, 'w') as output: # write file output.write(et.tostring(newkml, encoding='utf-8').decode('UTF-8')) self.labelStatusBar.setText("Status: Merged File " + file_name + " stored at " + _dirname) except (OSError, IOError) as ex: QtWidgets.QMessageBox.critical( self, self.tr("Problem while merging KML Files:"), self.tr(f"ERROR: {type(ex)} {ex}")) else: QtWidgets.QMessageBox.warning(self, "Merge KML Files", f"File extension is not '.kml'!\n{filename:}")
def save_function_wrapper(self): default_filename = os.path.join(self.last_save_directory, self.active_flight_track.name) + "." + extension filename = get_save_filename( self, "Export Flight Track", default_filename, name + " (*." + extension + ")", pickertype=pickertype) if filename is not None: try: function(filename, self.active_flight_track.name, self.active_flight_track.waypoints) # wildcard exception to be resilient against error introduced by user code except Exception as ex: logging.error("file io plugin error: %s %s", type(ex), ex) QtWidgets.QMessageBox.critical( self, self.tr("file io plugin error"), self.tr("ERROR: {} {}".format(type(ex), ex)))