WIP: Cleanup PrintInformation

This commit is contained in:
Lipu Fei 2018-02-22 16:02:41 +01:00
parent 5a6e84a57f
commit 32e1015f25
3 changed files with 27 additions and 57 deletions

View file

@ -1,27 +1,22 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty from typing import Dict
from UM.FlameProfiler import pyqtSlot import math
import os.path
import unicodedata
import json
import re # To create abbreviations for printer names.
from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot
from UM.Application import Application from UM.Application import Application
from UM.Logger import Logger from UM.Logger import Logger
from UM.Qt.Duration import Duration from UM.Qt.Duration import Duration
from UM.Preferences import Preferences from UM.Preferences import Preferences
from UM.Scene.SceneNode import SceneNode from UM.Scene.SceneNode import SceneNode
from UM.Settings.ContainerRegistry import ContainerRegistry
from cura.Scene.CuraSceneNode import CuraSceneNode
from cura.Settings.ExtruderManager import ExtruderManager
from typing import Dict
import math
import os.path
import unicodedata
import json
import re #To create abbreviations for printer names.
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura") catalog = i18nCatalog("cura")
## A class for processing and calculating minimum, current and maximum print time as well as managing the job name ## A class for processing and calculating minimum, current and maximum print time as well as managing the job name
@ -86,9 +81,8 @@ class PrintInformation(QObject):
Preferences.getInstance().preferenceChanged.connect(self._onPreferencesChanged) Preferences.getInstance().preferenceChanged.connect(self._onPreferencesChanged)
self._active_material_container = None self._application.getMachineManager().rootMaterialChanged.connect(self._onActiveMaterialsChanged)
self._application.getMachineManager().activeMaterialChanged.connect(self._onActiveMaterialChanged) self._onActiveMaterialsChanged()
self._onActiveMaterialChanged()
self._material_amounts = [] self._material_amounts = []
@ -203,7 +197,8 @@ class PrintInformation(QObject):
self._current_print_time[build_plate_number].setDuration(total_estimated_time) self._current_print_time[build_plate_number].setDuration(total_estimated_time)
def _calculateInformation(self, build_plate_number): def _calculateInformation(self, build_plate_number):
if Application.getInstance().getGlobalContainerStack() is None: global_stack = Application.getInstance().getGlobalContainerStack()
if global_stack is None:
return return
# Material amount is sent as an amount of mm^3, so calculate length from that # Material amount is sent as an amount of mm^3, so calculate length from that
@ -215,18 +210,13 @@ class PrintInformation(QObject):
material_preference_values = json.loads(Preferences.getInstance().getValue("cura/material_settings")) material_preference_values = json.loads(Preferences.getInstance().getValue("cura/material_settings"))
extruder_stacks = list(ExtruderManager.getInstance().getMachineExtruders(Application.getInstance().getGlobalContainerStack().getId())) extruder_stacks = global_stack.extruders
for index, amount in enumerate(self._material_amounts): for index, amount in enumerate(self._material_amounts):
## Find the right extruder stack. As the list isn't sorted because it's a annoying generator, we do some ## Find the right extruder stack. As the list isn't sorted because it's a annoying generator, we do some
# list comprehension filtering to solve this for us. # list comprehension filtering to solve this for us.
material = None extruder_stack = extruder_stacks[str(index)]
if extruder_stacks: # Multi extrusion machine density = extruder_stack.getMetaDataEntry("properties", {}).get("density", 0)
extruder_stack = [extruder for extruder in extruder_stacks if extruder.getMetaDataEntry("position") == str(index)][0] material = extruder_stack.findContainer({"type": "material"})
density = extruder_stack.getMetaDataEntry("properties", {}).get("density", 0)
material = extruder_stack.findContainer({"type": "material"})
else: # Machine with no extruder stacks
density = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("properties", {}).get("density", 0)
material = Application.getInstance().getGlobalContainerStack().findContainer({"type": "material"})
weight = float(amount) * float(density) / 1000 weight = float(amount) * float(density) / 1000
cost = 0 cost = 0
@ -266,20 +256,6 @@ class PrintInformation(QObject):
for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1): for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1):
self._calculateInformation(build_plate_number) self._calculateInformation(build_plate_number)
def _onActiveMaterialChanged(self):
if self._active_material_container:
try:
self._active_material_container.metaDataChanged.disconnect(self._onMaterialMetaDataChanged)
except TypeError: #pyQtSignal gives a TypeError when disconnecting from something that is already disconnected.
pass
active_material_id = Application.getInstance().getMachineManager().activeMaterialId
active_material_containers = ContainerRegistry.getInstance().findInstanceContainers(id = active_material_id)
if active_material_containers:
self._active_material_container = active_material_containers[0]
self._active_material_container.metaDataChanged.connect(self._onMaterialMetaDataChanged)
def _onActiveBuildPlateChanged(self): def _onActiveBuildPlateChanged(self):
new_active_build_plate = self._multi_build_plate_model.activeBuildPlate new_active_build_plate = self._multi_build_plate_model.activeBuildPlate
if new_active_build_plate != self._active_build_plate: if new_active_build_plate != self._active_build_plate:
@ -293,7 +269,7 @@ class PrintInformation(QObject):
self.materialNamesChanged.emit() self.materialNamesChanged.emit()
self.currentPrintTimeChanged.emit() self.currentPrintTimeChanged.emit()
def _onMaterialMetaDataChanged(self, *args, **kwargs): def _onActiveMaterialsChanged(self, *args, **kwargs):
for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1): for build_plate_number in range(self._multi_build_plate_model.maxBuildPlate + 1):
self._calculateInformation(build_plate_number) self._calculateInformation(build_plate_number)

View file

@ -409,25 +409,16 @@ class MachineManager(QObject):
def stacksHaveErrors(self) -> bool: def stacksHaveErrors(self) -> bool:
return bool(self._stacks_have_errors) return bool(self._stacks_have_errors)
@pyqtProperty(str, notify = activeStackChanged)
def activeUserProfileId(self) -> str:
if self._active_container_stack:
return self._active_container_stack.getTop().getId()
return ""
@pyqtProperty(str, notify = globalContainerChanged) @pyqtProperty(str, notify = globalContainerChanged)
def activeMachineName(self) -> str: def activeMachineName(self) -> str:
if self._global_container_stack: if self._global_container_stack:
return self._global_container_stack.getName() return self._global_container_stack.getName()
return "" return ""
@pyqtProperty(str, notify = globalContainerChanged) @pyqtProperty(str, notify = globalContainerChanged)
def activeMachineId(self) -> str: def activeMachineId(self) -> str:
if self._global_container_stack: if self._global_container_stack:
return self._global_container_stack.getId() return self._global_container_stack.getId()
return "" return ""
@pyqtProperty(QObject, notify = globalContainerChanged) @pyqtProperty(QObject, notify = globalContainerChanged)
@ -438,16 +429,18 @@ class MachineManager(QObject):
def activeStackId(self) -> str: def activeStackId(self) -> str:
if self._active_container_stack: if self._active_container_stack:
return self._active_container_stack.getId() return self._active_container_stack.getId()
return "" return ""
@pyqtProperty(QObject, notify = activeStackChanged)
def activeStack(self) -> Optional["ExtruderStack"]:
return self._active_container_stack
@pyqtProperty(str, notify=activeMaterialChanged) @pyqtProperty(str, notify=activeMaterialChanged)
def activeMaterialId(self) -> str: def activeMaterialId(self) -> str:
if self._active_container_stack: if self._active_container_stack:
material = self._active_container_stack.material material = self._active_container_stack.material
if material: if material:
return material.getId() return material.getId()
return "" return ""
## Gets a dict with the active materials ids set in all extruder stacks and the global stack ## Gets a dict with the active materials ids set in all extruder stacks and the global stack
@ -804,9 +797,9 @@ class MachineManager(QObject):
@pyqtProperty("QVariant", notify = rootMaterialChanged) @pyqtProperty("QVariant", notify = rootMaterialChanged)
def currentRootMaterialId(self): def currentRootMaterialId(self):
# initial filling the current_root_material_id # initial filling the current_root_material_id
self._current_root_material_id = {}
for position in self._global_container_stack.extruders: for position in self._global_container_stack.extruders:
if position not in self._current_root_material_id: self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
return self._current_root_material_id return self._current_root_material_id
@pyqtProperty("QVariant", notify = rootMaterialChanged) @pyqtProperty("QVariant", notify = rootMaterialChanged)

View file

@ -1,13 +1,14 @@
// Copyright (c) 2016 Ultimaker B.V. // Copyright (c) 2016 Ultimaker B.V.
// Cura is released under the terms of the LGPLv3 or higher. // Cura is released under the terms of the LGPLv3 or higher.
import QtQuick 2.1 import QtQuick 2.8
import QtQuick.Controls 1.1 import QtQuick.Controls 1.4
import QtQuick.Window 2.1 import QtQuick.Window 2.1
import UM 1.2 as UM import UM 1.2 as UM
import Cura 1.0 as Cura import Cura 1.0 as Cura
UM.ManagementPage UM.ManagementPage
{ {
id: base; id: base;