<commit_before># Copyright (c) 2015 <NAME>.
# Uranium is released under the terms of the AGPLv3 or higher.

from PyQt5.QtCore import Qt, QCoreApplication, pyqtSlot, pyqtProperty, pyqtSignal

from UM.Qt.ListModel import ListModel
from UM.Resources import Resources
from UM.Application import Application

from . import SettingsFromCategoryModel

class SettingCategoriesModel(ListModel):
    IdRole = Qt.UserRole + 1
    NameRole = Qt.UserRole + 2
    IconRole = Qt.UserRole + 3
    VisibleRole = Qt.UserRole + 4
    SettingsRole = Qt.UserRole + 5
    HiddenValuesCountRole = Qt.UserRole + 6 # See SettingsCategory::getHiddenValuesCount()

    def __init__(self, parent = None):
        super().__init__(parent)
        self._machine_instance = None
        Application.getInstance().getMachineManager().activeMachineInstanceChanged.connect(self._onActiveMachineChanged)
        self._onActiveMachineChanged()

        Application.getInstance().getMachineManager().activeProfileChanged.connect(self._onActiveProfileChanged)

        self.addRoleName(self.IdRole, "id")
        self.addRoleName(self.NameRole, "name")
        self.addRoleName(self.IconRole, "icon")
        self.addRoleName(self.VisibleRole, "visible")
        self.addRoleName(self.SettingsRole, "settings")
        self.addRoleName(self.HiddenValuesCountRole, "hiddenValuesCount") # Probably need a better name for this

        self._resetting = False

    @pyqtSlot(str)
    def filter(self, text):
        for item in self.items:
            item["settings"].filter(text)

    resettingChanged = pyqtSignal()
    @pyqtProperty(bool, notify = resettingChanged)
    def resetting(self):
        return self._resetting;

    @pyqtSlot()
    def reload(self):
        self._beginReset()
        self._items = []
        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                self._items.append({
                    "id": category.getKey(),
                    "name": category.getLabel(),
                    "icon": category.getIcon(),
                    "visible": category.isVisible(),
                    "settings": SettingsFromCategoryModel.SettingsFromCategoryModel(category),
                    "hiddenValuesCount": category.getHiddenValuesCount()
                })
        self._endReset()

    def _onActiveMachineChanged(self):
        self._beginReset()

        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                category.visibleChanged.disconnect(self._onCategoryVisibleChanged)

        self._machine_instance = Application.getInstance().getMachineManager().getActiveMachineInstance()

        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                self.appendItem({
                    "id": category.getKey(),
                    "name": category.getLabel(),
                    "icon": category.getIcon(),
                    "visible": category.isVisible(),
                    "settings": SettingsFromCategoryModel.SettingsFromCategoryModel(category),
                    "hiddenValuesCount": category.getHiddenValuesCount()
                })
                category.visibleChanged.connect(self._onCategoryVisibleChanged)

        self._endReset()

    def _onCategoryVisibleChanged(self, category):
        index = self.find("id", category.getKey())
        self.setProperty(index, "visible", category.isVisible())
        self.setProperty(index, "hiddenValuesCount", category.getHiddenValuesCount())

    def _onActiveProfileChanged(self):
        if not self._machine_instance:
            return
<commit_msg>Make sure to clear the list of categories before changing it<commit_after># Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from PyQt5.QtCore import Qt, QCoreApplication, pyqtSlot, pyqtProperty, pyqtSignal

from UM.Qt.ListModel import ListModel
from UM.Resources import Resources
from UM.Application import Application

from . import SettingsFromCategoryModel

class SettingCategoriesModel(ListModel):
    IdRole = Qt.UserRole + 1
    NameRole = Qt.UserRole + 2
    IconRole = Qt.UserRole + 3
    VisibleRole = Qt.UserRole + 4
    SettingsRole = Qt.UserRole + 5
    HiddenValuesCountRole = Qt.UserRole + 6 # See SettingsCategory::getHiddenValuesCount()

    def __init__(self, parent = None):
        super().__init__(parent)
        self._machine_instance = None
        Application.getInstance().getMachineManager().activeMachineInstanceChanged.connect(self._onActiveMachineChanged)
        self._onActiveMachineChanged()

        Application.getInstance().getMachineManager().activeProfileChanged.connect(self._onActiveProfileChanged)

        self.addRoleName(self.IdRole, "id")
        self.addRoleName(self.NameRole, "name")
        self.addRoleName(self.IconRole, "icon")
        self.addRoleName(self.VisibleRole, "visible")
        self.addRoleName(self.SettingsRole, "settings")
        self.addRoleName(self.HiddenValuesCountRole, "hiddenValuesCount") # Probably need a better name for this

        self._resetting = False

    @pyqtSlot(str)
    def filter(self, text):
        for item in self.items:
            item["settings"].filter(text)

    resettingChanged = pyqtSignal()
    @pyqtProperty(bool, notify = resettingChanged)
    def resetting(self):
        return self._resetting;

    @pyqtSlot()
    def reload(self):
        self._beginReset()
        self._items = []
        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                self._items.append({
                    "id": category.getKey(),
                    "name": category.getLabel(),
                    "icon": category.getIcon(),
                    "visible": category.isVisible(),
                    "settings": SettingsFromCategoryModel.SettingsFromCategoryModel(category),
                    "hiddenValuesCount": category.getHiddenValuesCount()
                })
        self._endReset()

    def _onActiveMachineChanged(self):
        self._beginReset()

        self._items = []

        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                category.visibleChanged.disconnect(self._onCategoryVisibleChanged)

        self._machine_instance = Application.getInstance().getMachineManager().getActiveMachineInstance()

        if self._machine_instance:
            for category in self._machine_instance.getMachineDefinition().getAllCategories():
                self.appendItem({
                    "id": category.getKey(),
                    "name": category.getLabel(),
                    "icon": category.getIcon(),
                    "visible": category.isVisible(),
                    "settings": SettingsFromCategoryModel.SettingsFromCategoryModel(category),
                    "hiddenValuesCount": category.getHiddenValuesCount()
                })
                category.visibleChanged.connect(self._onCategoryVisibleChanged)

        self._endReset()

    def _onCategoryVisibleChanged(self, category):
        index = self.find("id", category.getKey())
        self.setProperty(index, "visible", category.isVisible())
        self.setProperty(index, "hiddenValuesCount", category.getHiddenValuesCount())

    def _onActiveProfileChanged(self):
        if not self._machine_instance:
            return

{'fraction_non_alphanumeric': 0.09688900606380174, 'fraction_numerical': 0.0031637226469812814, 'mean_word_length': 2.733759842519685, 'pattern_counts': {'":': 24, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3386266', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1857, 'n_words': 384}