# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Tool import Tool
from UM.Scene.Selection import Selection
from UM.Application import Application
from UM.Qt.ListModel import ListModel

from . import PerObjectSettingsModel

class PerObjectSettingsTool(Tool):
    def __init__(self):
        super().__init__()
        self._model = None

        self.setExposedProperties("Model", "SelectedIndex")

    def event(self, event):
        return False

    def getModel(self):
        if not self._model:
            self._model = PerObjectSettingsModel.PerObjectSettingsModel()

        #For some reason, casting this model to itself causes the model to properly be cast to a QVariant, even though it ultimately inherits from QVariant.
        #Yeah, we have no idea either...
        return PerObjectSettingsModel.PerObjectSettingsModel(self._model)

    def getSelectedIndex(self):
        selected_object_id = id(Selection.getSelectedObject(0))
        index = self.getModel().find("id", selected_object_id)
        return index
# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Tool import Tool
from UM.Scene.Selection import Selection
from UM.Application import Application

from . import PerObjectSettingsModel

class PerObjectSettingsTool(Tool):
    def __init__(self):
        super().__init__()

        self.setExposedProperties("Model", "SelectedIndex")

    def event(self, event):
        return False

    def getModel(self):
        return PerObjectSettingsModel.PerObjectSettingsModel()

    def getSelectedIndex(self):
        selected_object_id = id(Selection.getSelectedObject(0))
        index = self.getModel().find("id", selected_object_id)
        return index
--- 
+++ 
@@ -4,12 +4,14 @@
 from UM.Tool import Tool
 from UM.Scene.Selection import Selection
 from UM.Application import Application
+from UM.Qt.ListModel import ListModel
 
 from . import PerObjectSettingsModel
 
 class PerObjectSettingsTool(Tool):
     def __init__(self):
         super().__init__()
+        self._model = None
 
         self.setExposedProperties("Model", "SelectedIndex")
 
@@ -17,7 +19,12 @@
         return False
 
     def getModel(self):
-        return PerObjectSettingsModel.PerObjectSettingsModel()
+        if not self._model:
+            self._model = PerObjectSettingsModel.PerObjectSettingsModel()
+
+        #For some reason, casting this model to itself causes the model to properly be cast to a QVariant, even though it ultimately inherits from QVariant.
+        #Yeah, we have no idea either...
+        return PerObjectSettingsModel.PerObjectSettingsModel(self._model)
 
     def getSelectedIndex(self):
         selected_object_id = id(Selection.getSelectedObject(0))
Fix problem with casting to QVariant

This is a magical fix that Nallath and I found for a problem that shouldn't exist in the first place and sometimes doesn't exist at all and in the same time is a superposition of existing and not existing and it's all very complicated and an extremely weird hack. Casting this object to itself properly makes it castable to QVariant.

Contributes to issue CURA-458.
