diff --git a/cura/Machines/Models/BaseMaterialsModel.py b/cura/Machines/Models/BaseMaterialsModel.py index 7dcdb7fd57..89a50ba39d 100644 --- a/cura/Machines/Models/BaseMaterialsModel.py +++ b/cura/Machines/Models/BaseMaterialsModel.py @@ -45,6 +45,7 @@ class BaseMaterialsModel(ListModel): # Update this model when switching machines, when adding materials or changing their metadata. self._machine_manager.activeStackChanged.connect(self._update) 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 + 2, "id") @@ -115,6 +116,11 @@ class BaseMaterialsModel(ListModel): return 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 # models themselves. def _update(self): diff --git a/cura/Machines/Models/MaterialManagementModel.py b/cura/Machines/Models/MaterialManagementModel.py index a0b61e0b9b..16ef4b81e8 100644 --- a/cura/Machines/Models/MaterialManagementModel.py +++ b/cura/Machines/Models/MaterialManagementModel.py @@ -2,7 +2,7 @@ # Cura is released under the terms of the LGPLv3 or higher. 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 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, # renaming them, etc. 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 # container stacks anywhere? # @@ -178,4 +183,31 @@ class MaterialManagementModel(QObject): } self.duplicateMaterial(preferred_material_node, new_base_id = new_id, new_metadata = new_metadata) - return new_id \ No newline at end of file + 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)) \ No newline at end of file diff --git a/resources/qml/Preferences/Materials/MaterialsSlot.qml b/resources/qml/Preferences/Materials/MaterialsSlot.qml index 0e60bb6558..c6691460cf 100644 --- a/resources/qml/Preferences/Materials/MaterialsSlot.qml +++ b/resources/qml/Preferences/Materials/MaterialsSlot.qml @@ -82,11 +82,12 @@ Rectangle { if (materialSlot.is_favorite) { - CuraApplication.getMaterialManager().removeFavorite(material.root_material_id) - return + CuraApplication.getMaterialManagementModel().removeFavorite(material.root_material_id) + } + else + { + CuraApplication.getMaterialManagementModel().addFavorite(material.root_material_id) } - CuraApplication.getMaterialManager().addFavorite(material.root_material_id) - return } style: ButtonStyle {