Skip to content

Commit

Permalink
Factor out use of qmlRegisterType
Browse files Browse the repository at this point in the history
pyQt only allows up to 60 non-qml types to be registered via qmlRegisterType. Since Cura and Uranium use up their fair share of these registrations, best to avoid it altogether.

Fixes #7
  • Loading branch information
fieldOfView committed Aug 7, 2023
1 parent 913030c commit 3599750
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
36 changes: 33 additions & 3 deletions TabbedSettingsPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
# The TabbedSettingsPlugin is released under the terms of the AGPLv3 or higher.

import os.path
from typing import Optional

from PyQt6.QtCore import QObject, pyqtSlot

from cura.CuraApplication import CuraApplication
from UM.Extension import Extension
from UM.Logger import Logger

from . import PerCategoryVisibilityHandler
from . import InstanceContainerVisibilityHandler
from . import ExtendedSettingPreferenceVisibilityHandler


class TabbedSettingsPlugin(Extension):
class TabbedSettingsPlugin(QObject, Extension):
def __init__(self):
super().__init__()

self._qml_patcher = None
self._main_window = None
CuraApplication.getInstance().engineCreatedSignal.connect(self._onEngineCreated)

self._visibility_handlers = {}

def _onEngineCreated(self):
self._main_window = CuraApplication.getInstance().getMainWindow()
if not self._main_window:
Expand All @@ -38,12 +48,32 @@ def _onEngineCreated(self):
)

self._qml_patcher = CuraApplication.getInstance().createQmlComponent(
path, {"withSidebarGUI": has_sidebar_gui}
path, {
"manager": self,
"withSidebarGUI": has_sidebar_gui
}
)
if not self._qml_patcher:
Logger.log(
"w", "Could not create qml components for TabbedSettingsPlugin"
)
return

self._qml_patcher.patch(self._main_window.contentItem())
self._qml_patcher.patch(self._main_window.contentItem())

@pyqtSlot(str, result = QObject)
def getVisibilityHandler(self, handler_type: str) -> Optional["QObject"]:
if handler_type not in self._visibility_handlers:
handler = None
if handler_type == "PerCategory":
handler = PerCategoryVisibilityHandler.PerCategoryVisibilityHandler()
elif handler_type == "InstanceContainer":
handler = InstanceContainerVisibilityHandler.InstanceContainerVisibilityHandler()
elif handler_type == "ExtendedSettingPreference":
handler = ExtendedSettingPreferenceVisibilityHandler.ExtendedSettingPreferenceVisibilityHandler()
if handler:
self._visibility_handlers[handler_type] = handler
else:
return

return self._visibility_handlers[handler_type]
20 changes: 0 additions & 20 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
from UM.Application import Application
from UM.Logger import Logger

from PyQt6.QtQml import qmlRegisterType

from . import TabbedSettingsPlugin
from . import PerCategoryVisibilityHandler
from . import InstanceContainerVisibilityHandler
from . import ExtendedSettingPreferenceVisibilityHandler


def getMetaData():
Expand All @@ -25,21 +20,6 @@ def register(app):
Logger.log("w", "Plugin not loaded because of a version mismatch")
return {}

qmlRegisterType(
PerCategoryVisibilityHandler.PerCategoryVisibilityHandler,
"Cura", 1, 0,
"PerCategoryVisibilityHandler",
)
qmlRegisterType(
InstanceContainerVisibilityHandler.InstanceContainerVisibilityHandler,
"Cura", 1, 0,
"InstanceContainerVisibilityHandler",
)
qmlRegisterType(
ExtendedSettingPreferenceVisibilityHandler.ExtendedSettingPreferenceVisibilityHandler,
"Cura", 1, 0,
"ExtendedSettingPreferenceVisibilityHandler",
)
return {"extension": TabbedSettingsPlugin.TabbedSettingsPlugin()}


Expand Down
12 changes: 7 additions & 5 deletions resources/qml/TabbedSettingsView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Item
property var tooltipItem
property var backgroundItem

property var settingPreferenceVisibilityHandler: Cura.ExtendedSettingPreferenceVisibilityHandler {}
property var perCategoryVisibilityHandler: Cura.PerCategoryVisibilityHandler {}
property var instanceContainerVisibilityHandler: Cura.InstanceContainerVisibilityHandler
property var settingPreferenceVisibilityHandler: manager.getVisibilityHandler("ExtendedSettingPreference")
property var perCategoryVisibilityHandler: manager.getVisibilityHandler("PerCategory")
property var instanceContainerVisibilityHandler:
{
active: false
containerIndex: 0
var handler = manager.getVisibilityHandler("InstanceContainer")
handler.active = false
handler.containerIndex = 0
return handler
}

property string selectedKey: categoryTabs.itemAt(categoryTabs.currentIndex).key
Expand Down

0 comments on commit 3599750

Please sign in to comment.