ColorDialog.qml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2022 UltiMaker
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.15
  4. import QtQuick.Controls 2.2
  5. import QtQuick.Window 2.1
  6. import QtQuick.Layouts 1.1
  7. import UM 1.7 as UM
  8. import Cura 1.7 as Cura
  9. /*
  10. * A dialog that provides the option to pick a color. Currently it only asks for a hex code and shows the color
  11. * in a color swath
  12. */
  13. UM.Dialog
  14. {
  15. id: base
  16. property variant catalog: UM.I18nCatalog { name: "cura" }
  17. margin: UM.Theme.getSize("default_margin").width
  18. property alias swatchGridColumns: colorSwatchGrid.columns
  19. // In this case we would like to let the content of the dialog determine the size of the dialog
  20. // however with the current implementation of the dialog this is not possible, so instead we calculate
  21. // the size of the dialog ourselves.
  22. // Ugly workaround for windows having overlapping elements due to incorrect dialog width
  23. minimumWidth: content.width + (Qt.platform.os === "windows" ? 4 * margin : 2 * margin)
  24. minimumHeight: {
  25. const footerHeight = Math.max(okButton.height, cancelButton.height);
  26. return content.height + footerHeight + (Qt.platform.os === "windows" ? 5 * margin : 3 * margin);
  27. }
  28. property alias color: colorInput.text
  29. property var swatchColors: [
  30. "#2161AF", "#57AFB2", "#F7B32D", "#E33D4A", "#C088AD",
  31. "#5D88BE", "#5ABD0E", "#E17239", "#F74E46", "#874AF9",
  32. "#50C2EC", "#8DC15A", "#C3977A", "#CD7776", "#9086BA",
  33. "#FFFFFF", "#D3D3D3", "#9E9E9E", "#5A5A5A", "#000000",
  34. ]
  35. Component.onCompleted: updateSwatches()
  36. onSwatchColorsChanged: updateSwatches()
  37. function updateSwatches()
  38. {
  39. swatchColorsModel.clear();
  40. for (const swatchColor of base.swatchColors)
  41. {
  42. swatchColorsModel.append({ swatchColor });
  43. }
  44. }
  45. Column
  46. {
  47. id: content
  48. width: childrenRect.width
  49. height: childrenRect.height
  50. spacing: UM.Theme.getSize("wide_margin").height
  51. GridLayout {
  52. id: colorSwatchGrid
  53. columns: 5
  54. width: childrenRect.width
  55. height: childrenRect.height
  56. columnSpacing: UM.Theme.getSize("thick_margin").width
  57. rowSpacing: UM.Theme.getSize("thick_margin").height
  58. Repeater
  59. {
  60. model: ListModel
  61. {
  62. id: swatchColorsModel
  63. }
  64. delegate: Rectangle
  65. {
  66. color: swatchColor
  67. implicitWidth: UM.Theme.getSize("medium_button_icon").width
  68. implicitHeight: UM.Theme.getSize("medium_button_icon").height
  69. radius: width / 2
  70. UM.ColorImage
  71. {
  72. anchors.fill: parent
  73. visible: swatchColor == base.color
  74. source: UM.Theme.getIcon("Check", "low")
  75. color: UM.Theme.getColor("checkbox")
  76. }
  77. MouseArea
  78. {
  79. anchors.fill: parent
  80. onClicked: base.color = swatchColor
  81. }
  82. }
  83. }
  84. }
  85. RowLayout
  86. {
  87. width: parent.width
  88. spacing: UM.Theme.getSize("default_margin").width
  89. UM.Label
  90. {
  91. text: catalog.i18nc("@label", "Hex")
  92. }
  93. Cura.TextField
  94. {
  95. id: colorInput
  96. Layout.fillWidth: true
  97. text: "#FFFFFF"
  98. selectByMouse: true
  99. onTextChanged: {
  100. if (!text.startsWith("#"))
  101. {
  102. text = `#${text}`;
  103. }
  104. }
  105. validator: UM.HexColorValidator {}
  106. }
  107. Rectangle
  108. {
  109. color: base.color
  110. Layout.preferredHeight: parent.height
  111. Layout.preferredWidth: height
  112. }
  113. }
  114. }
  115. buttonSpacing: UM.Theme.getSize("thin_margin").width
  116. rightButtons:
  117. [
  118. Cura.TertiaryButton {
  119. id: cancelButton
  120. text: catalog.i18nc("@action:button", "Cancel")
  121. onClicked: base.close()
  122. },
  123. Cura.PrimaryButton {
  124. id: okButton
  125. text: catalog.i18nc("@action:button", "OK")
  126. onClicked: base.accept()
  127. }
  128. ]
  129. }