Browse Source

Expanded states preserved when updating models

Did this so that models can be updated more often fixing bugs where when a material is set as "not favorite" in the favorites section, its updated accordingly in its "normal" section.

It's also the ground work for saving material section expansion to preferences.

Contributes to CURA-5378
Ian Paschal 6 years ago
parent
commit
2304aeaceb

+ 3 - 0
cura/Machines/Models/BaseMaterialsModel.py

@@ -35,6 +35,9 @@ class BaseMaterialsModel(ListModel):
         # Update this model when list of materials changes
         self._material_manager.materialsUpdated.connect(self._update)
 
+        # Update this model when list of favorites changes
+        self._material_manager.favoritesUpdated.connect(self._update)
+
         self.addRoleName(Qt.UserRole + 1, "root_material_id")
         self.addRoleName(Qt.UserRole + 2, "id")
         self.addRoleName(Qt.UserRole + 3, "GUID")

+ 0 - 1
cura/Machines/Models/FavoriteMaterialsModel.py

@@ -8,7 +8,6 @@ class FavoriteMaterialsModel(BaseMaterialsModel):
 
     def __init__(self, parent = None):
         super().__init__(parent)
-        self._material_manager.favoritesUpdated.connect(self._update) # Update when favorites are changed
         self._update()
 
     def _update(self):

+ 14 - 2
resources/qml/Preferences/Materials/MaterialsBrandSection.qml

@@ -13,7 +13,7 @@ import Cura 1.0 as Cura
 Rectangle
 {
     id: brand_section
-    property var expanded: true
+    property var expanded: base.collapsed_brands.indexOf(model.name) > -1
     property var types_model: model.material_types
     height: childrenRect.height
     width: parent.width
@@ -69,7 +69,19 @@ Rectangle
         anchors.fill: brand_header
         onPressed:
         {
-            brand_section.expanded = !brand_section.expanded
+            const i = base.collapsed_brands.indexOf(model.name)
+            if (i > -1)
+            {
+                // Remove it
+                base.collapsed_brands.splice(i, 1)
+                brand_section.expanded = false
+            }
+            else
+            {
+                // Add it
+                base.collapsed_brands.push(model.name)
+                brand_section.expanded = true
+            }
         }
     }
     Column

+ 14 - 2
resources/qml/Preferences/Materials/MaterialsList.qml

@@ -102,7 +102,7 @@ Item
         }
         Rectangle
         {
-            property var expanded: true
+            property var expanded: base.collapsed_brands.indexOf("Generic") > -1
 
             id: generic_section
             height: childrenRect.height
@@ -158,7 +158,19 @@ Item
                 anchors.fill: generic_header
                 onPressed:
                 {
-                    generic_section.expanded = !generic_section.expanded
+                    const i = base.collapsed_brands.indexOf("Generic")
+                    if (i > -1)
+                    {
+                        // Remove it
+                        base.collapsed_brands.splice(i, 1)
+                        generic_section.expanded = false
+                    }
+                    else
+                    {
+                        // Add it
+                        base.collapsed_brands.push("Generic")
+                        generic_section.expanded = true
+                    }
                 }
             }
             Column

+ 29 - 21
resources/qml/Preferences/Materials/MaterialsPage.qml

@@ -26,36 +26,44 @@ Item
     property string newRootMaterialIdToSwitchTo: ""
     property bool toActivateNewMaterial: false
 
+    // TODO: Save these to preferences
+    property var collapsed_brands: []
+    property var collapsed_types: []
+
     UM.I18nCatalog
     {
         id: catalog
         name: "cura"
     }
-    Cura.MaterialBrandsModel
+    Cura.MaterialBrandsModel { id: materialsModel }
+
+    function findModelByRootId( search_root_id )
     {
-        id: materialsModel
+        for (var i = 0; i < materialsModel.rowCount(); i++)
+        {
+            var types_model = materialsModel.getItem(i).material_types;
+            for (var j = 0; j < types_model.rowCount(); j++)
+            {
+                var colors_model = types_model.getItem(j).colors;
+                for (var k = 0; k < colors_model.rowCount(); k++)
+                {
+                    var material = colors_model.getItem(k);
+                    if (material.root_material_id == search_root_id)
+                    {
+                        return material
+                    }
+                }
+            }
+        }
     }
-    Cura.GenericMaterialsModel
+    Component.onCompleted:
     {
-        id: genericMaterialsModel
+        // Select the activated material when this page shows up
+        const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
+        const active_root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
+        console.log("goign to search for", active_root_material_id)
+        base.currentItem = findModelByRootId(active_root_material_id)
     }
-    // Component.onCompleted:
-    // {
-    //     // Select the activated material when this page shows up
-    //     const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
-    //     const active_root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
-    //     var itemIndex = -1;
-    //     for (var i = 0; i < materialsModel.rowCount(); ++i)
-    //     {
-    //         var item = materialsModel.getItem(i);
-    //         if (item.root_material_id == active_root_material_id)
-    //         {
-    //             itemIndex = i;
-    //             break;
-    //         }
-    //     }
-    //     materialListView.currentIndex = itemIndex;
-    // }
 
     onCurrentItemChanged: { MaterialsDetailsPanel.currentItem = currentItem }
     Connections

+ 14 - 2
resources/qml/Preferences/Materials/MaterialsTypeSection.qml

@@ -13,7 +13,7 @@ import Cura 1.0 as Cura
 Rectangle
 {
     id: material_type_section
-    property var expanded: true
+    property var expanded: base.collapsed_types.indexOf(model.brand + "_" + model.name) > -1
     property var colors_model: model.colors
     height: childrenRect.height
     width: parent.width
@@ -76,7 +76,19 @@ Rectangle
         anchors.fill: material_type_header
         onPressed:
         {
-            material_type_section.expanded = !material_type_section.expanded
+            const i = base.collapsed_types.indexOf(model.brand + "_" + model.name)
+            if (i > -1)
+            {
+                // Remove it
+                base.collapsed_types.splice(i, 1)
+                material_type_section.expanded = false
+            }
+            else
+            {
+                // Add it
+                base.collapsed_types.push(model.brand + "_" + model.name)
+                material_type_section.expanded = true
+            }
         }
     }
     Column