Use ListModel.setItems() instead of appending one item at a time

setItems sets the items to a (presorted) list of items instead of adding items one by one and then sorting the list. This way if an update to the model causes a signal storm at least it happens only once for an update.

Specifically this reduces switching to a dual extrusion printer by half.

Contributes to CURA-2193
This commit is contained in:
fieldOfView 2016-08-29 13:43:02 +02:00
parent 0e602b8d0b
commit b343649131
3 changed files with 14 additions and 9 deletions

View file

@ -31,7 +31,7 @@ class ContainerSettingsModel(ListModel):
self._update() self._update()
def _update(self): def _update(self):
self.clear() items = []
if len(self._container_ids) == 0: if len(self._container_ids) == 0:
return return
@ -64,14 +64,15 @@ class ContainerSettingsModel(ListModel):
else: else:
values.append("") values.append("")
self.appendItem({ items.append({
"key": key, "key": key,
"values": values, "values": values,
"label": definition.label, "label": definition.label,
"unit": definition.unit, "unit": definition.unit,
"category": category.label "category": category.label
}) })
self.sort(lambda k: (k["category"], k["key"])) items.sort(key = lambda k: (k["category"], k["key"]))
self.setItems(items)
## Set the ids of the containers which have the settings this model should list. ## Set the ids of the containers which have the settings this model should list.
# Also makes sure the model updates when the containers have property changes # Also makes sure the model updates when the containers have property changes

View file

@ -97,9 +97,10 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
changed = False changed = False
if self.rowCount() != 0: if self.rowCount() != 0:
self.clear()
changed = True changed = True
items = []
global_container_stack = UM.Application.getInstance().getGlobalContainerStack() global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
if global_container_stack: if global_container_stack:
if self._add_global: if self._add_global:
@ -111,7 +112,7 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
"color": color, "color": color,
"index": -1 "index": -1
} }
self.appendItem(item) items.append(item)
changed = True changed = True
manager = ExtruderManager.getInstance() manager = ExtruderManager.getInstance()
@ -133,9 +134,10 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
"color": color, "color": color,
"index": position "index": position
} }
self.appendItem(item) items.append(item)
changed = True changed = True
if changed: if changed:
self.sort(lambda item: item["index"]) items.sort(key = lambda i: i["index"])
self.setItems(items)
self.modelChanged.emit() self.modelChanged.emit()

View file

@ -70,7 +70,7 @@ class QualitySettingsModel(UM.Qt.ListModel.ListModel):
if not self._quality: if not self._quality:
return return
self.clear() items = []
settings = collections.OrderedDict() settings = collections.OrderedDict()
definition_container = UM.Application.getInstance().getGlobalContainerStack().getBottom() definition_container = UM.Application.getInstance().getGlobalContainerStack().getBottom()
@ -163,7 +163,7 @@ class QualitySettingsModel(UM.Qt.ListModel.ListModel):
if not profile_value and not user_value: if not profile_value and not user_value:
continue continue
self.appendItem({ items.append({
"key": definition.key, "key": definition.key,
"label": definition.label, "label": definition.label,
"unit": definition.unit, "unit": definition.unit,
@ -171,3 +171,5 @@ class QualitySettingsModel(UM.Qt.ListModel.ListModel):
"user_value": "" if user_value is None else str(user_value), "user_value": "" if user_value is None else str(user_value),
"category": current_category "category": current_category
}) })
self.setItems(items)