ColorDialog.qml 4.2 KB

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