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
This commit is contained in:
Ian Paschal 2018-08-24 12:10:32 +02:00
parent 13069a2f99
commit 2304aeaceb
6 changed files with 74 additions and 28 deletions

View file

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

View file

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

View file

@ -13,7 +13,7 @@ import Cura 1.0 as Cura
Rectangle Rectangle
{ {
id: brand_section id: brand_section
property var expanded: true property var expanded: base.collapsed_brands.indexOf(model.name) > -1
property var types_model: model.material_types property var types_model: model.material_types
height: childrenRect.height height: childrenRect.height
width: parent.width width: parent.width
@ -69,7 +69,19 @@ Rectangle
anchors.fill: brand_header anchors.fill: brand_header
onPressed: 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 Column

View file

@ -102,7 +102,7 @@ Item
} }
Rectangle Rectangle
{ {
property var expanded: true property var expanded: base.collapsed_brands.indexOf("Generic") > -1
id: generic_section id: generic_section
height: childrenRect.height height: childrenRect.height
@ -158,7 +158,19 @@ Item
anchors.fill: generic_header anchors.fill: generic_header
onPressed: 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 Column

View file

@ -26,36 +26,44 @@ Item
property string newRootMaterialIdToSwitchTo: "" property string newRootMaterialIdToSwitchTo: ""
property bool toActivateNewMaterial: false property bool toActivateNewMaterial: false
// TODO: Save these to preferences
property var collapsed_brands: []
property var collapsed_types: []
UM.I18nCatalog UM.I18nCatalog
{ {
id: catalog id: catalog
name: "cura" name: "cura"
} }
Cura.MaterialBrandsModel Cura.MaterialBrandsModel { id: materialsModel }
function findModelByRootId( search_root_id )
{ {
id: materialsModel for (var i = 0; i < materialsModel.rowCount(); i++)
}
Cura.GenericMaterialsModel
{ {
id: genericMaterialsModel 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
}
}
}
}
}
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];
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 } onCurrentItemChanged: { MaterialsDetailsPanel.currentItem = currentItem }
Connections Connections

View file

@ -13,7 +13,7 @@ import Cura 1.0 as Cura
Rectangle Rectangle
{ {
id: material_type_section 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 property var colors_model: model.colors
height: childrenRect.height height: childrenRect.height
width: parent.width width: parent.width
@ -76,7 +76,19 @@ Rectangle
anchors.fill: material_type_header anchors.fill: material_type_header
onPressed: 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 Column