ColorDialog.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2022 Ultimaker B.V.
  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.5 as UM
  8. import Cura 1.1 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: content.height + footer.height + (Qt.platform.os == "windows" ? 5 * margin : 3 * margin)
  25. property alias color: colorInput.text
  26. property var swatchColors: [
  27. "#2161AF", "#57AFB2", "#F7B32D", "#E33D4A", "#C088AD",
  28. "#5D88BE", "#5ABD0E", "#E17239", "#F74E46", "#874AF9",
  29. "#50C2EC", "#8DC15A", "#C3977A", "#CD7776", "#9086BA",
  30. "#FFFFFF", "#D3D3D3", "#9E9E9E", "#5A5A5A", "#000000",
  31. ]
  32. Component.onCompleted: updateSwatches()
  33. onSwatchColorsChanged: updateSwatches()
  34. function updateSwatches()
  35. {
  36. swatchColorsModel.clear();
  37. for (const swatchColor of base.swatchColors)
  38. {
  39. swatchColorsModel.append({ swatchColor });
  40. }
  41. }
  42. Column
  43. {
  44. id: content
  45. width: childrenRect.width
  46. height: childrenRect.height
  47. spacing: UM.Theme.getSize("wide_margin").height
  48. GridLayout {
  49. id: colorSwatchGrid
  50. columns: 5
  51. width: childrenRect.width
  52. height: childrenRect.height
  53. columnSpacing: UM.Theme.getSize("thick_margin").width
  54. rowSpacing: UM.Theme.getSize("thick_margin").height
  55. Repeater
  56. {
  57. model: ListModel
  58. {
  59. id: swatchColorsModel
  60. }
  61. delegate: Rectangle
  62. {
  63. color: swatchColor
  64. implicitWidth: UM.Theme.getSize("medium_button_icon").width
  65. implicitHeight: UM.Theme.getSize("medium_button_icon").height
  66. radius: width / 2
  67. UM.ColorImage
  68. {
  69. anchors.fill: parent
  70. visible: swatchColor == base.color
  71. source: UM.Theme.getIcon("Check", "low")
  72. color: UM.Theme.getColor("checkbox")
  73. }
  74. MouseArea
  75. {
  76. anchors.fill: parent
  77. onClicked: base.color = swatchColor
  78. }
  79. }
  80. }
  81. }
  82. RowLayout
  83. {
  84. width: parent.width
  85. spacing: UM.Theme.getSize("default_margin").width
  86. UM.Label
  87. {
  88. text: catalog.i18nc("@label", "Hex")
  89. }
  90. Cura.TextField
  91. {
  92. id: colorInput
  93. Layout.fillWidth: true
  94. text: "#FFFFFF"
  95. selectByMouse: true
  96. onTextChanged: {
  97. if (!text.startsWith("#"))
  98. {
  99. text = `#${text}`;
  100. }
  101. }
  102. validator: RegularExpressionValidator { regularExpression: /^#([a-fA-F0-9]{0,6})$/ }
  103. }
  104. Rectangle
  105. {
  106. color: base.color
  107. Layout.preferredHeight: parent.height
  108. Layout.preferredWidth: height
  109. }
  110. }
  111. }
  112. buttonSpacing: UM.Theme.getSize("thin_margin").width
  113. rightButtons:
  114. [
  115. Cura.TertiaryButton {
  116. text: catalog.i18nc("@action:button", "Cancel")
  117. onClicked: base.close()
  118. },
  119. Cura.PrimaryButton {
  120. text: catalog.i18nc("@action:button", "OK")
  121. onClicked: base.accept()
  122. }
  123. ]
  124. }