mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-04 04:23:58 -06:00
Use the file name to create the profile name during import. Also moved the createUniqueName() method up into CuraContainerRegistry.
Contributes to CURA-1667 Profile import/export
This commit is contained in:
parent
89f64eb870
commit
70b2311c59
2 changed files with 45 additions and 28 deletions
|
@ -1,10 +1,14 @@
|
|||
# Copyright (c) 2016 Ultimaker B.V.
|
||||
# Uranium is released under the terms of the AGPLv3 or higher.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import re
|
||||
from PyQt5.QtWidgets import QMessageBox
|
||||
|
||||
from UM.Settings.ContainerRegistry import ContainerRegistry
|
||||
from UM.Settings.ContainerStack import ContainerStack
|
||||
from UM.Settings.InstanceContainer import InstanceContainer
|
||||
from UM.Application import Application
|
||||
from UM.Logger import Logger
|
||||
from UM.Message import Message
|
||||
|
@ -19,6 +23,39 @@ class CuraContainerRegistry(ContainerRegistry):
|
|||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
## Create a name that is not empty and unique
|
||||
# \param container_type \type{string} Type of the container (machine, quality, ...)
|
||||
# \param current_name \type{} Current name of the container, which may be an acceptable option
|
||||
# \param new_name \type{string} Base name, which may not be unique
|
||||
# \param fallback_name \type{string} Name to use when (stripped) new_name is empty
|
||||
# \return \type{string} Name that is unique for the specified type and name/id
|
||||
def createUniqueName(self, container_type, current_name, new_name, fallback_name):
|
||||
new_name = new_name.strip()
|
||||
num_check = re.compile("(.*?)\s*#\d+$").match(new_name)
|
||||
if num_check:
|
||||
new_name = num_check.group(1)
|
||||
if new_name == "":
|
||||
new_name = fallback_name
|
||||
|
||||
unique_name = new_name
|
||||
i = 1
|
||||
# In case we are renaming, the current name of the container is also a valid end-result
|
||||
while self._containerExists(container_type, unique_name) and unique_name != current_name:
|
||||
i += 1
|
||||
unique_name = "%s #%d" % (new_name, i)
|
||||
|
||||
return unique_name
|
||||
|
||||
## Check if a container with of a certain type and a certain name or id exists
|
||||
# Both the id and the name are checked, because they may not be the same and it is better if they are both unique
|
||||
# \param container_type \type{string} Type of the container (machine, quality, ...)
|
||||
# \param container_name \type{string} Name to check
|
||||
def _containerExists(self, container_type, container_name):
|
||||
container_class = ContainerStack if container_type == "machine" else InstanceContainer
|
||||
|
||||
return self.findContainers(container_class, id = container_name, type = container_type) or \
|
||||
self.findContainers(container_class, name = container_name, type = container_type)
|
||||
|
||||
## Exports an profile to a file
|
||||
#
|
||||
# \param instance_id \type{str} the ID of the profile to export.
|
||||
|
@ -104,6 +141,10 @@ class CuraContainerRegistry(ContainerRegistry):
|
|||
if profile: #Success!
|
||||
profile.setReadOnly(False)
|
||||
|
||||
new_name = self.createUniqueName("quality", "", os.path.splitext(os.path.basename(file_name))[0],
|
||||
catalog.i18nc("@label", "Custom profile"))
|
||||
profile.setName(new_name)
|
||||
|
||||
if self._machineHasOwnQualities():
|
||||
profile.setDefinition(self._activeDefinition())
|
||||
if self._machineHasOwnMaterials():
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
import re
|
||||
# Copyright (c) 2016 Ultimaker B.V.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal
|
||||
from UM.Application import Application
|
||||
|
@ -171,31 +171,7 @@ class MachineManagerModel(QObject):
|
|||
# \param fallback_name \type{string} Name to use when (stripped) new_name is empty
|
||||
# \return \type{string} Name that is unique for the specified type and name/id
|
||||
def _createUniqueName(self, container_type, current_name, new_name, fallback_name):
|
||||
new_name = new_name.strip()
|
||||
num_check = re.compile("(.*?)\s*#\d+$").match(new_name)
|
||||
if num_check:
|
||||
new_name = num_check.group(1)
|
||||
if new_name == "":
|
||||
new_name = fallback_name
|
||||
|
||||
unique_name = new_name
|
||||
i = 1
|
||||
# In case we are renaming, the current name of the container is also a valid end-result
|
||||
while self._containerExists(container_type, unique_name) and unique_name != current_name:
|
||||
i += 1
|
||||
unique_name = "%s #%d" % (new_name, i)
|
||||
|
||||
return unique_name
|
||||
|
||||
## Check if a container with of a certain type and a certain name or id exists
|
||||
# Both the id and the name are checked, because they may not be the same and it is better if they are both unique
|
||||
# \param container_type \type{string} Type of the container (machine, quality, ...)
|
||||
# \param container_name \type{string} Name to check
|
||||
def _containerExists(self, container_type, container_name):
|
||||
container_class = ContainerStack if container_type == "machine" else InstanceContainer
|
||||
|
||||
return UM.Settings.ContainerRegistry.getInstance().findContainers(container_class, id = container_name, type = container_type) or \
|
||||
UM.Settings.ContainerRegistry.getInstance().findContainers(container_class, name = container_name, type = container_type)
|
||||
return UM.Settings.ContainerRegistry.createUniqueName(container_type, current_name, new_name, fallback_name)
|
||||
|
||||
## Convenience function to check if a stack has errors.
|
||||
def _checkStackForErrors(self, stack):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue