KDeclarative

ColorButton.qml
1 /*
2  SPDX-FileCopyrightText: 2015 David Edmundson <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 import QtQuick 2.2
8 import QtQuick.Controls 1.2 as QtControls
9 import QtQuick.Dialogs 1.0 as QtDialogs
10 
28 QtControls.Button {
29  id: colorPicker
30 
34  property alias color: colorDialog.color
35 
39  property alias dialogTitle: colorDialog.title
40 
45  property alias currentColor: colorDialog.currentColor
46 
50  property alias showAlphaChannel: colorDialog.showAlphaChannel
51 
57  signal accepted(color color)
58 
59  readonly property real _buttonMarigns: 4 // same as QStyles. Remove if we can get this provided by the QQC theme
60 
61  implicitWidth: 40 + _buttonMarigns*2 //to perfectly clone kcolorbutton from kwidgetaddons
62 
63 
64  //create a checkerboard background for alpha to be adjusted
65  Canvas {
66  anchors.fill: colorBlock
67  visible: colorDialog.color.a < 1
68 
69  onPaint: {
70  var ctx = getContext('2d');
71 
72  ctx.fillStyle = "white";
73  ctx.fillRect(0,0, ctx.width, ctx.height)
74 
75  ctx.fillStyle = "black";
76  //in blocks of 16x16 draw two black squares of 8x8 in top left and bottom right
77  for (var j=0;j<width;j+=16) {
78  for (var i=0;i<height;i+=16) {
79  //top left, bottom right
80  ctx.fillRect(j,i,8,8);
81  ctx.fillRect(j+8,i+8,8,8);
82  }
83  }
84  }
85 
86  }
87 
88  Rectangle {
89  id: colorBlock
90 
91  anchors.centerIn: parent
92  height: parent.height - _buttonMarigns*2
93  width: parent.width - _buttonMarigns*2
94 
95 
96  color: enabled ? colorDialog.color : disabledPalette.button
97 
98  SystemPalette {
99  id: disabledPalette
100  colorGroup: SystemPalette.Disabled
101  }
102  }
103 
104  QtDialogs.ColorDialog {
105  id: colorDialog
106  onAccepted: colorPicker.accepted(color)
107  }
108 
109  onClicked: {
110  colorDialog.open()
111  }
112 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.