MaterialMenu.qml 8.5 KB

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