ColorDialog.qml 4.3 KB

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