MaterialsPage.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright (c) 2018 Ultimaker B.V.
  2. // Uranium is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 1.4
  5. import QtQuick.Layouts 1.3
  6. import QtQuick.Dialogs 1.2
  7. import UM 1.2 as UM
  8. import Cura 1.0 as Cura
  9. Item
  10. {
  11. id: base
  12. property QtObject materialManager: CuraApplication.getMaterialManager()
  13. // Keep PreferencesDialog happy
  14. property var resetEnabled: false
  15. property var currentItem: null
  16. property var isCurrentItemActivated:
  17. {
  18. const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
  19. const root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
  20. return base.currentItem.root_material_id == root_material_id;
  21. }
  22. property string newRootMaterialIdToSwitchTo: ""
  23. property bool toActivateNewMaterial: false
  24. // TODO: Save these to preferences
  25. property var collapsed_brands: []
  26. property var collapsed_types: []
  27. UM.I18nCatalog
  28. {
  29. id: catalog
  30. name: "cura"
  31. }
  32. Cura.MaterialBrandsModel { id: materialsModel }
  33. function findModelByRootId( search_root_id )
  34. {
  35. for (var i = 0; i < materialsModel.rowCount(); i++)
  36. {
  37. var types_model = materialsModel.getItem(i).material_types;
  38. for (var j = 0; j < types_model.rowCount(); j++)
  39. {
  40. var colors_model = types_model.getItem(j).colors;
  41. for (var k = 0; k < colors_model.rowCount(); k++)
  42. {
  43. var material = colors_model.getItem(k);
  44. if (material.root_material_id == search_root_id)
  45. {
  46. return material
  47. }
  48. }
  49. }
  50. }
  51. }
  52. Component.onCompleted:
  53. {
  54. // Select the activated material when this page shows up
  55. const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
  56. const active_root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
  57. console.log("goign to search for", active_root_material_id)
  58. base.currentItem = findModelByRootId(active_root_material_id)
  59. }
  60. onCurrentItemChanged: { MaterialsDetailsPanel.currentItem = currentItem }
  61. Connections
  62. {
  63. target: materialsModel
  64. onItemsChanged:
  65. {
  66. var currentItemId = base.currentItem == null ? "" : base.currentItem.root_material_id;
  67. var position = Cura.ExtruderManager.activeExtruderIndex;
  68. // try to pick the currently selected item; it may have been moved
  69. if (base.newRootMaterialIdToSwitchTo == "")
  70. {
  71. base.newRootMaterialIdToSwitchTo = currentItemId;
  72. }
  73. for (var idx = 0; idx < materialsModel.rowCount(); ++idx)
  74. {
  75. var item = materialsModel.getItem(idx);
  76. if (item.root_material_id == base.newRootMaterialIdToSwitchTo)
  77. {
  78. // Switch to the newly created profile if needed
  79. materialListView.currentIndex = idx;
  80. materialListView.activateDetailsWithIndex(materialListView.currentIndex);
  81. if (base.toActivateNewMaterial)
  82. {
  83. Cura.MachineManager.setMaterial(position, item.container_node);
  84. }
  85. base.newRootMaterialIdToSwitchTo = "";
  86. base.toActivateNewMaterial = false;
  87. return
  88. }
  89. }
  90. materialListView.currentIndex = 0;
  91. materialListView.activateDetailsWithIndex(materialListView.currentIndex);
  92. if (base.toActivateNewMaterial)
  93. {
  94. Cura.MachineManager.setMaterial(position, materialsModel.getItem(0).container_node);
  95. }
  96. base.newRootMaterialIdToSwitchTo = "";
  97. base.toActivateNewMaterial = false;
  98. }
  99. }
  100. // Main layout
  101. Label
  102. {
  103. id: titleLabel
  104. anchors
  105. {
  106. top: parent.top
  107. left: parent.left
  108. right: parent.right
  109. margins: 5 * screenScaleFactor
  110. }
  111. font.pointSize: 18
  112. text: catalog.i18nc("@title:tab", "Materials")
  113. }
  114. // Button Row
  115. Row
  116. {
  117. id: buttonRow
  118. anchors
  119. {
  120. left: parent.left
  121. right: parent.right
  122. top: titleLabel.bottom
  123. }
  124. height: childrenRect.height
  125. // Activate button
  126. Button
  127. {
  128. text: catalog.i18nc("@action:button", "Activate")
  129. iconName: "list-activate"
  130. enabled: !isCurrentItemActivated
  131. onClicked:
  132. {
  133. forceActiveFocus()
  134. const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
  135. Cura.MachineManager.setMaterial(extruder_position, base.currentItem.container_node);
  136. }
  137. }
  138. // Create button
  139. Button
  140. {
  141. text: catalog.i18nc("@action:button", "Create")
  142. iconName: "list-add"
  143. onClicked:
  144. {
  145. forceActiveFocus();
  146. base.newRootMaterialIdToSwitchTo = base.materialManager.createMaterial();
  147. base.toActivateNewMaterial = true;
  148. }
  149. }
  150. // Duplicate button
  151. Button
  152. {
  153. text: catalog.i18nc("@action:button", "Duplicate");
  154. iconName: "list-add"
  155. enabled: base.hasCurrentItem
  156. onClicked:
  157. {
  158. forceActiveFocus();
  159. base.newRootMaterialIdToSwitchTo = base.materialManager.duplicateMaterial(base.currentItem.container_node);
  160. base.toActivateNewMaterial = true;
  161. }
  162. }
  163. // Remove button
  164. Button
  165. {
  166. text: catalog.i18nc("@action:button", "Remove")
  167. iconName: "list-remove"
  168. enabled: base.hasCurrentItem && !base.currentItem.is_read_only && !base.isCurrentItemActivated
  169. onClicked:
  170. {
  171. forceActiveFocus();
  172. confirmRemoveMaterialDialog.open();
  173. }
  174. }
  175. // Import button
  176. Button
  177. {
  178. text: catalog.i18nc("@action:button", "Import")
  179. iconName: "document-import"
  180. onClicked:
  181. {
  182. forceActiveFocus();
  183. importMaterialDialog.open();
  184. }
  185. visible: true
  186. }
  187. // Export button
  188. Button
  189. {
  190. text: catalog.i18nc("@action:button", "Export")
  191. iconName: "document-export"
  192. onClicked:
  193. {
  194. forceActiveFocus();
  195. exportMaterialDialog.open();
  196. }
  197. enabled: currentItem != null
  198. }
  199. }
  200. Item {
  201. id: contentsItem
  202. anchors
  203. {
  204. top: titleLabel.bottom
  205. left: parent.left
  206. right: parent.right
  207. bottom: parent.bottom
  208. margins: 5 * screenScaleFactor
  209. bottomMargin: 0
  210. }
  211. clip: true
  212. }
  213. Item
  214. {
  215. anchors
  216. {
  217. top: buttonRow.bottom
  218. topMargin: UM.Theme.getSize("default_margin").height
  219. left: parent.left
  220. right: parent.right
  221. bottom: parent.bottom
  222. }
  223. SystemPalette { id: palette }
  224. Label
  225. {
  226. id: captionLabel
  227. anchors
  228. {
  229. top: parent.top
  230. left: parent.left
  231. }
  232. visible: text != ""
  233. text:
  234. {
  235. var caption = catalog.i18nc("@action:label", "Printer") + ": " + Cura.MachineManager.activeMachineName;
  236. if (Cura.MachineManager.hasVariants)
  237. {
  238. caption += ", " + Cura.MachineManager.activeDefinitionVariantsName + ": " + Cura.MachineManager.activeVariantName;
  239. }
  240. return caption;
  241. }
  242. width: materialScrollView.width
  243. elide: Text.ElideRight
  244. }
  245. ScrollView
  246. {
  247. id: materialScrollView
  248. anchors
  249. {
  250. top: captionLabel.visible ? captionLabel.bottom : parent.top
  251. topMargin: captionLabel.visible ? UM.Theme.getSize("default_margin").height : 0
  252. bottom: parent.bottom
  253. left: parent.left
  254. }
  255. Rectangle
  256. {
  257. parent: viewport
  258. anchors.fill: parent
  259. color: palette.light
  260. }
  261. width: true ? (parent.width * 0.4) | 0 : parent.width
  262. frameVisible: true
  263. verticalScrollBarPolicy: Qt.ScrollBarAlwaysOn
  264. MaterialsList {}
  265. }
  266. MaterialsDetailsPanel
  267. {
  268. anchors
  269. {
  270. left: materialScrollView.right
  271. leftMargin: UM.Theme.getSize("default_margin").width
  272. top: parent.top
  273. bottom: parent.bottom
  274. right: parent.right
  275. }
  276. }
  277. }
  278. // Dialogs
  279. MessageDialog
  280. {
  281. id: confirmRemoveMaterialDialog
  282. icon: StandardIcon.Question;
  283. title: catalog.i18nc("@title:window", "Confirm Remove")
  284. text: catalog.i18nc("@label (%1 is object name)", "Are you sure you wish to remove %1? This cannot be undone!").arg(base.currentItem.name)
  285. standardButtons: StandardButton.Yes | StandardButton.No
  286. modality: Qt.ApplicationModal
  287. onYes:
  288. {
  289. base.materialManager.removeMaterial(base.currentItem.container_node);
  290. }
  291. }
  292. FileDialog
  293. {
  294. id: importMaterialDialog
  295. title: catalog.i18nc("@title:window", "Import Material")
  296. selectExisting: true
  297. nameFilters: Cura.ContainerManager.getContainerNameFilters("material")
  298. folder: CuraApplication.getDefaultPath("dialog_material_path")
  299. onAccepted:
  300. {
  301. var result = Cura.ContainerManager.importMaterialContainer(fileUrl);
  302. messageDialog.title = catalog.i18nc("@title:window", "Import Material");
  303. messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!", "Could not import material <filename>%1</filename>: <message>%2</message>").arg(fileUrl).arg(result.message);
  304. if (result.status == "success")
  305. {
  306. messageDialog.icon = StandardIcon.Information;
  307. messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tag <filename>!", "Successfully imported material <filename>%1</filename>").arg(fileUrl);
  308. }
  309. else if (result.status == "duplicate")
  310. {
  311. messageDialog.icon = StandardIcon.Warning;
  312. }
  313. else
  314. {
  315. messageDialog.icon = StandardIcon.Critical;
  316. }
  317. messageDialog.open();
  318. CuraApplication.setDefaultPath("dialog_material_path", folder);
  319. }
  320. }
  321. FileDialog
  322. {
  323. id: exportMaterialDialog
  324. title: catalog.i18nc("@title:window", "Export Material")
  325. selectExisting: false
  326. nameFilters: Cura.ContainerManager.getContainerNameFilters("material")
  327. folder: CuraApplication.getDefaultPath("dialog_material_path")
  328. onAccepted:
  329. {
  330. var result = Cura.ContainerManager.exportContainer(base.currentItem.root_material_id, selectedNameFilter, fileUrl);
  331. messageDialog.title = catalog.i18nc("@title:window", "Export Material");
  332. if (result.status == "error")
  333. {
  334. messageDialog.icon = StandardIcon.Critical;
  335. messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tags <filename> and <message>!", "Failed to export material to <filename>%1</filename>: <message>%2</message>").arg(fileUrl).arg(result.message);
  336. messageDialog.open();
  337. }
  338. else if (result.status == "success")
  339. {
  340. messageDialog.icon = StandardIcon.Information;
  341. messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tag <filename>!", "Successfully exported material to <filename>%1</filename>").arg(result.path);
  342. messageDialog.open();
  343. }
  344. CuraApplication.setDefaultPath("dialog_material_path", folder);
  345. }
  346. }
  347. MessageDialog
  348. {
  349. id: messageDialog
  350. }
  351. }