Fix favorite materials without material manager

We just track it via the preference value itself rather than duplicating that in any other data structure. It's simple enough.

Contributes to issue CURA-6776.
This commit is contained in:
Ghostkeeper 2019-09-20 09:34:40 +02:00
parent 12043df367
commit 4a68e7ec95
No known key found for this signature in database
GPG key ID: 86BEF881AE2CF276
3 changed files with 45 additions and 6 deletions

View file

@ -45,6 +45,7 @@ class BaseMaterialsModel(ListModel):
# Update this model when switching machines, when adding materials or changing their metadata. # Update this model when switching machines, when adding materials or changing their metadata.
self._machine_manager.activeStackChanged.connect(self._update) self._machine_manager.activeStackChanged.connect(self._update)
ContainerTree.getInstance().materialsChanged.connect(self._materialsListChanged) ContainerTree.getInstance().materialsChanged.connect(self._materialsListChanged)
self._application.getMaterialManagementModel().favoritesChanged.connect(self._update)
self.addRoleName(Qt.UserRole + 1, "root_material_id") self.addRoleName(Qt.UserRole + 1, "root_material_id")
self.addRoleName(Qt.UserRole + 2, "id") self.addRoleName(Qt.UserRole + 2, "id")
@ -115,6 +116,11 @@ class BaseMaterialsModel(ListModel):
return return
self._update() self._update()
## Triggered when the list of favorite materials is changed.
def _favoritesChanged(self, material_base_file: str) -> None:
if material_base_file in self._available_materials:
self._update()
## This is an abstract method that needs to be implemented by the specific ## This is an abstract method that needs to be implemented by the specific
# models themselves. # models themselves.
def _update(self): def _update(self):

View file

@ -2,7 +2,7 @@
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import copy # To duplicate materials. import copy # To duplicate materials.
from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page. from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page.
from typing import Any, Dict, Optional, TYPE_CHECKING from typing import Any, Dict, Optional, TYPE_CHECKING
import uuid # To generate new GUIDs for new materials. import uuid # To generate new GUIDs for new materials.
@ -23,6 +23,11 @@ catalog = i18nCatalog("cura")
# This class handles the actions in that page, such as creating new materials, # This class handles the actions in that page, such as creating new materials,
# renaming them, etc. # renaming them, etc.
class MaterialManagementModel(QObject): class MaterialManagementModel(QObject):
## Triggered when a favorite is added or removed.
# \param The base file of the material is provided as parameter when this
# emits.
favoritesChanged = pyqtSignal(str)
## Can a certain material be deleted, or is it still in use in one of the ## Can a certain material be deleted, or is it still in use in one of the
# container stacks anywhere? # container stacks anywhere?
# #
@ -178,4 +183,31 @@ class MaterialManagementModel(QObject):
} }
self.duplicateMaterial(preferred_material_node, new_base_id = new_id, new_metadata = new_metadata) self.duplicateMaterial(preferred_material_node, new_base_id = new_id, new_metadata = new_metadata)
return new_id return new_id
## Adds a certain material to the favorite materials.
# \param material_base_file The base file of the material to add.
@pyqtSlot(str)
def addFavorite(self, material_base_file: str) -> None:
application = cura.CuraApplication.CuraApplication.getInstance()
favorites = application.getPreferences().getValue("cura/favorite_materials").split(";")
if material_base_file not in favorites:
favorites.append(material_base_file)
application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites))
application.saveSettings()
self.favoritesChanged.emit(material_base_file)
## Removes a certain material from the favorite materials.
#
# If the material was not in the favorite materials, nothing happens.
@pyqtSlot(str)
def removeFavorite(self, material_base_file: str) -> None:
application = cura.CuraApplication.CuraApplication.getInstance()
favorites = application.getPreferences().getValue("cura/favorite_materials").split(";")
try:
favorites.remove(material_base_file)
application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites))
application.saveSettings()
self.favoritesChanged.emit(material_base_file)
except ValueError: # Material was not in the favorites list.
Logger.log("w", "Material {material_base_file} was already not a favorite material.".format(material_base_file = material_base_file))

View file

@ -82,11 +82,12 @@ Rectangle
{ {
if (materialSlot.is_favorite) if (materialSlot.is_favorite)
{ {
CuraApplication.getMaterialManager().removeFavorite(material.root_material_id) CuraApplication.getMaterialManagementModel().removeFavorite(material.root_material_id)
return }
else
{
CuraApplication.getMaterialManagementModel().addFavorite(material.root_material_id)
} }
CuraApplication.getMaterialManager().addFavorite(material.root_material_id)
return
} }
style: ButtonStyle style: ButtonStyle
{ {