MaterialsPage.qml 12 KB

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