Use loader for machine actions in dialog

CURA-6495
This commit is contained in:
Lipu Fei 2019-05-06 13:05:28 +02:00
parent c9e7f3a245
commit 53d595f698
5 changed files with 27 additions and 23 deletions

View file

@ -2,8 +2,9 @@
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import os import os
from typing import Optional
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal from PyQt5.QtCore import QObject, QUrl, pyqtSlot, pyqtProperty, pyqtSignal
from UM.Logger import Logger from UM.Logger import Logger
from UM.PluginObject import PluginObject from UM.PluginObject import PluginObject
@ -72,18 +73,26 @@ class MachineAction(QObject, PluginObject):
return self._finished return self._finished
## Protected helper to create a view object based on provided QML. ## Protected helper to create a view object based on provided QML.
def _createViewFromQML(self) -> None: def _createViewFromQML(self) -> Optional["QObject"]:
plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId()) plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
if plugin_path is None: if plugin_path is None:
Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId()) Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
return return None
path = os.path.join(plugin_path, self._qml_url) path = os.path.join(plugin_path, self._qml_url)
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
self._view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self}) view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
return view
@pyqtProperty(QObject, constant = True) @pyqtProperty(QUrl, constant = True)
def displayItem(self): def qmlPath(self) -> "QUrl":
if not self._view: plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
self._createViewFromQML() if plugin_path is None:
return self._view Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
return QUrl("")
path = os.path.join(plugin_path, self._qml_url)
return QUrl.fromLocalFile(path)
@pyqtSlot(result = QObject)
def getDisplayItem(self) -> Optional["QObject"]:
return self._createViewFromQML()

View file

@ -100,7 +100,7 @@ class FirstStartMachineActionsModel(ListModel):
item_list = [] item_list = []
for item in first_start_actions: for item in first_start_actions:
item_list.append({"title": item.label, item_list.append({"title": item.label,
"content": item.displayItem, "content": item.getDisplayItem(),
"action": item, "action": item,
}) })
item.reset() item.reset()

View file

@ -26,7 +26,7 @@ Item
property int columnWidth: ((parent.width - 2 * UM.Theme.getSize("default_margin").width) / 2) | 0 property int columnWidth: ((parent.width - 2 * UM.Theme.getSize("default_margin").width) / 2) | 0
property int columnSpacing: 3 * screenScaleFactor property int columnSpacing: 3 * screenScaleFactor
property int propertyStoreIndex: manager.storeContainerIndex // definition_changes property int propertyStoreIndex: manager ? manager.storeContainerIndex : 1 // definition_changes
property string extruderStackId: "" property string extruderStackId: ""
property int extruderPosition: 0 property int extruderPosition: 0

View file

@ -26,7 +26,7 @@ Item
property int columnWidth: ((parent.width - 2 * UM.Theme.getSize("default_margin").width) / 2) | 0 property int columnWidth: ((parent.width - 2 * UM.Theme.getSize("default_margin").width) / 2) | 0
property int columnSpacing: 3 * screenScaleFactor property int columnSpacing: 3 * screenScaleFactor
property int propertyStoreIndex: manager.storeContainerIndex // definition_changes property int propertyStoreIndex: manager ? manager.storeContainerIndex : 1 // definition_changes
property string machineStackId: Cura.MachineManager.activeMachineId property string machineStackId: Cura.MachineManager.activeMachineId

View file

@ -100,10 +100,11 @@ UM.ManagementPage
text: machineActionRepeater.model[index].label text: machineActionRepeater.model[index].label
onClicked: onClicked:
{ {
actionDialog.content = machineActionRepeater.model[index].displayItem; var currentItem = machineActionRepeater.model[index]
machineActionRepeater.model[index].displayItem.reset(); actionDialog.loader.manager = currentItem
actionDialog.title = machineActionRepeater.model[index].label; actionDialog.loader.source = currentItem.qmlPath
actionDialog.show(); actionDialog.title = currentItem.label
actionDialog.show()
} }
} }
} }
@ -113,13 +114,7 @@ UM.ManagementPage
UM.Dialog UM.Dialog
{ {
id: actionDialog id: actionDialog
property var content
onContentChanged:
{
contents = content;
content.onCompleted.connect(hide)
content.dialog = actionDialog
}
rightButtons: Button rightButtons: Button
{ {
text: catalog.i18nc("@action:button", "Close") text: catalog.i18nc("@action:button", "Close")