From 9779c41071304a348fb7c41040ff6c7c4e90438e Mon Sep 17 00:00:00 2001 From: Aleksei S Date: Tue, 8 May 2018 10:47:59 +0200 Subject: [PATCH] Use MimeTypeDatabase to find loaded file extension and set proper project name CURA-5323 --- cura/PrintInformation.py | 33 ++++++++++++++---------------- plugins/3MFReader/ThreeMFReader.py | 10 +++++++++ plugins/GCodeReader/GCodeReader.py | 10 +++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/cura/PrintInformation.py b/cura/PrintInformation.py index 0de57d00e0..6737ebdfb9 100644 --- a/cura/PrintInformation.py +++ b/cura/PrintInformation.py @@ -16,6 +16,7 @@ from UM.Qt.Duration import Duration from UM.Preferences import Preferences from UM.Scene.SceneNode import SceneNode from UM.i18n import i18nCatalog +from UM.MimeTypeDatabase import MimeTypeDatabase catalog = i18nCatalog("cura") @@ -322,7 +323,7 @@ class PrintInformation(QObject): # when a file is opened using the terminal; the filename comes from _onFileLoaded and still contains its # extension. This cuts the extension off if necessary. - name = os.path.splitext(name)[0] + check_name = os.path.splitext(name)[0] filename_parts = os.path.basename(base_name).split(".") # If it's a gcode, also always update the job name @@ -333,25 +334,21 @@ class PrintInformation(QObject): # if this is a profile file, always update the job name # name is "" when I first had some meshes and afterwards I deleted them so the naming should start again - is_empty = name == "" - if is_gcode or is_project_file or (is_empty or (self._base_name == "" and self._base_name != name)): + is_empty = check_name == "" + if is_gcode or is_project_file or (is_empty or (self._base_name == "" and self._base_name != check_name)): # Only take the file name part, Note : file name might have 'dot' in name as well - if is_project_file: - # This is for .curaproject, loaded as project - self._base_name = ".".join(filename_parts) - elif len(filename_parts) > 1: - if "gcode" in filename_parts: - gcode_index = filename_parts.index('gcode') - self._base_name = ".".join(filename_parts[0:gcode_index]) - elif "curaproject" in filename_parts: - #load a project and import only models - curaproject_index = filename_parts.index('curaproject') - self._base_name = ".".join(filename_parts[0:curaproject_index]) - else: - self._base_name = name - else: - self._base_name = name + data = '' + try: + mime_type = MimeTypeDatabase.getMimeTypeForFile(name) + data = mime_type.stripExtension(name) + except: + Logger.log("w", "Unsupported Mime Type Database file extension") + + if data is not None: + self._base_name = data + else: + self._base_name = '' self._updateJobName() diff --git a/plugins/3MFReader/ThreeMFReader.py b/plugins/3MFReader/ThreeMFReader.py index 6c2fb9a59d..9eec09b202 100755 --- a/plugins/3MFReader/ThreeMFReader.py +++ b/plugins/3MFReader/ThreeMFReader.py @@ -15,6 +15,7 @@ from UM.Math.Vector import Vector from UM.Mesh.MeshBuilder import MeshBuilder from UM.Mesh.MeshReader import MeshReader from UM.Scene.GroupDecorator import GroupDecorator +from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType from cura.Settings.ExtruderManager import ExtruderManager from cura.Scene.CuraSceneNode import CuraSceneNode @@ -25,6 +26,15 @@ from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch MYPY = False + +MimeTypeDatabase.addMimeType( + MimeType( + name = "application/x-cura-project-file", + comment = "Cura Project File", + suffixes = ["curaproject.3mf"] + ) +) + try: if not MYPY: import xml.etree.cElementTree as ET diff --git a/plugins/GCodeReader/GCodeReader.py b/plugins/GCodeReader/GCodeReader.py index 6f872bc6f7..80a6bea98a 100755 --- a/plugins/GCodeReader/GCodeReader.py +++ b/plugins/GCodeReader/GCodeReader.py @@ -5,10 +5,20 @@ from UM.FileHandler.FileReader import FileReader from UM.Mesh.MeshReader import MeshReader from UM.i18n import i18nCatalog from UM.Preferences import Preferences +from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType catalog = i18nCatalog("cura") from . import MarlinFlavorParser, RepRapFlavorParser + +MimeTypeDatabase.addMimeType( + MimeType( + name = "application/x-cura-gcode-file", + comment = "Cura GCode File", + suffixes = ["gcode", "gcode.gz"] + ) +) + # Class for loading and parsing G-code files class GCodeReader(MeshReader):