MaterialMenu.qml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2016 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.2
  4. import QtQuick.Controls 1.1
  5. import UM 1.2 as UM
  6. import Cura 1.0 as Cura
  7. Menu
  8. {
  9. id: menu
  10. title: "Material"
  11. property int extruderIndex: 0
  12. property bool printerConnected: Cura.MachineManager.printerOutputDevices.length != 0
  13. UM.SettingPropertyProvider
  14. {
  15. id: materialDiameterProvider
  16. containerStackId: Cura.MachineManager.activeMachineId
  17. key: "material_diameter"
  18. watchedProperties: [ "value" ]
  19. }
  20. MenuItem
  21. {
  22. id: automaticMaterial
  23. text:
  24. {
  25. if(printerConnected && Cura.MachineManager.printerOutputDevices[0].materialNames.length > extruderIndex)
  26. {
  27. var materialName = Cura.MachineManager.printerOutputDevices[0].materialNames[extruderIndex];
  28. return catalog.i18nc("@title:menuitem %1 is the automatically selected material", "Automatic: %1").arg(materialName);
  29. }
  30. return "";
  31. }
  32. visible: printerConnected && Cura.MachineManager.printerOutputDevices[0].materialNames.length > extruderIndex
  33. onTriggered:
  34. {
  35. var materialId = Cura.MachineManager.printerOutputDevices[0].materialIds[extruderIndex];
  36. var items = materialsModel.items;
  37. for(var i in items)
  38. {
  39. if (items[i]["metadata"]["GUID"] == materialId)
  40. {
  41. Cura.MachineManager.setActiveMaterial(items[i].id);
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. MenuSeparator
  48. {
  49. visible: automaticMaterial.visible
  50. }
  51. Instantiator
  52. {
  53. model: genericMaterialsModel
  54. MenuItem
  55. {
  56. text: model.name
  57. checkable: true
  58. checked: model.id == Cura.MachineManager.allActiveMaterialIds[ExtruderManager.extruderIds[extruderIndex]]
  59. exclusiveGroup: group
  60. onTriggered:
  61. {
  62. // This workaround is done because of the application menus for materials and variants for multiextrusion printers.
  63. // The extruder menu would always act on the correspoding extruder only, instead of acting on the extruder selected in the UI.
  64. var activeExtruderIndex = ExtruderManager.activeExtruderIndex;
  65. ExtruderManager.setActiveExtruderIndex(extruderIndex);
  66. Cura.MachineManager.setActiveMaterial(model.id);
  67. ExtruderManager.setActiveExtruderIndex(activeExtruderIndex);
  68. }
  69. }
  70. onObjectAdded: menu.insertItem(index, object)
  71. onObjectRemoved: menu.removeItem(object)
  72. }
  73. MenuSeparator { }
  74. Instantiator
  75. {
  76. model: brandModel
  77. Menu
  78. {
  79. id: brandMenu
  80. title: brandName
  81. property string brandName: model.name
  82. property var brandMaterials: model.materials
  83. Instantiator
  84. {
  85. model: brandMaterials
  86. Menu
  87. {
  88. id: brandMaterialsMenu
  89. title: materialName
  90. property string materialName: model.name
  91. property var brandMaterialColors: model.colors
  92. Instantiator
  93. {
  94. model: brandMaterialColors
  95. MenuItem
  96. {
  97. text: model.name
  98. checkable: true
  99. checked: model.id == Cura.MachineManager.allActiveMaterialIds[ExtruderManager.extruderIds[extruderIndex]]
  100. exclusiveGroup: group
  101. onTriggered:
  102. {
  103. // This workaround is done because of the application menus for materials and variants for multiextrusion printers.
  104. // The extruder menu would always act on the correspoding extruder only, instead of acting on the extruder selected in the UI.
  105. var activeExtruderIndex = ExtruderManager.activeExtruderIndex;
  106. ExtruderManager.setActiveExtruderIndex(extruderIndex);
  107. Cura.MachineManager.setActiveMaterial(model.id);
  108. ExtruderManager.setActiveExtruderIndex(activeExtruderIndex);
  109. }
  110. }
  111. onObjectAdded: brandMaterialsMenu.insertItem(index, object)
  112. onObjectRemoved: brandMaterialsMenu.removeItem(object)
  113. }
  114. }
  115. onObjectAdded: brandMenu.insertItem(index, object)
  116. onObjectRemoved: brandMenu.removeItem(object)
  117. }
  118. }
  119. onObjectAdded: menu.insertItem(index, object)
  120. onObjectRemoved: menu.removeItem(object)
  121. }
  122. ListModel
  123. {
  124. id: genericMaterialsModel
  125. Component.onCompleted: populateMenuModels()
  126. }
  127. ListModel
  128. {
  129. id: brandModel
  130. }
  131. //: Model used to populate the brandModel
  132. UM.InstanceContainersModel
  133. {
  134. id: materialsModel
  135. filter: materialFilter()
  136. onModelReset: populateMenuModels()
  137. onDataChanged: populateMenuModels()
  138. }
  139. ExclusiveGroup { id: group }
  140. MenuSeparator { }
  141. MenuItem { action: Cura.Actions.manageMaterials }
  142. function materialFilter()
  143. {
  144. var result = { "type": "material", "approximate_diameter": Math.round(materialDiameterProvider.properties.value).toString() };
  145. if(Cura.MachineManager.filterMaterialsByMachine)
  146. {
  147. result.definition = Cura.MachineManager.activeQualityDefinitionId;
  148. if(Cura.MachineManager.hasVariants)
  149. {
  150. result.variant = Cura.MachineManager.activeQualityVariantId;
  151. }
  152. }
  153. else
  154. {
  155. result.definition = "fdmprinter";
  156. result.compatible = true; //NB: Only checks for compatibility in global version of material, but we don't have machine-specific materials anyway.
  157. }
  158. return result;
  159. }
  160. function populateMenuModels()
  161. {
  162. // Create a structure of unique brands and their material-types
  163. genericMaterialsModel.clear()
  164. brandModel.clear();
  165. var items = materialsModel.items;
  166. var materialsByBrand = {};
  167. for (var i in items) {
  168. var brandName = items[i]["metadata"]["brand"];
  169. var materialName = items[i]["metadata"]["material"];
  170. if (brandName == "Generic")
  171. {
  172. // Add to top section
  173. var materialId = items[i].id;
  174. genericMaterialsModel.append({
  175. id:materialId,
  176. name:items[i].name
  177. });
  178. }
  179. else
  180. {
  181. // Add to per-brand, per-material menu
  182. if (!materialsByBrand.hasOwnProperty(brandName))
  183. {
  184. materialsByBrand[brandName] = {};
  185. }
  186. if (!materialsByBrand[brandName].hasOwnProperty(materialName))
  187. {
  188. materialsByBrand[brandName][materialName] = [];
  189. }
  190. materialsByBrand[brandName][materialName].push({
  191. id: items[i].id,
  192. name: items[i].name
  193. });
  194. }
  195. }
  196. for (var brand in materialsByBrand)
  197. {
  198. var materialsByBrandModel = [];
  199. var materials = materialsByBrand[brand];
  200. for (var material in materials)
  201. {
  202. materialsByBrandModel.push({
  203. name: material,
  204. colors: materials[material]
  205. })
  206. }
  207. brandModel.append({
  208. name: brand,
  209. materials: materialsByBrandModel
  210. });
  211. }
  212. }
  213. }