Skip to content

Commit 9dcdbbc

Browse files
vsklencarPeterPetrik
vsklencar
authored andcommitted
[QgsQuick] Modified Checkbox widget
Replaced checkbox with switch. Registered QVariant::Type to recognize field type. When value was empty/not set, it wasn't clear what is a type of field. It caused wrong behavior while setting default un/checked state.
1 parent 1d258d5 commit 9dcdbbc

File tree

2 files changed

+59
-36
lines changed

2 files changed

+59
-36
lines changed

src/quickgui/plugin/editor/qgsquickcheckbox.qml

+58-36
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* *
1414
***************************************************************************/
1515

16-
import QtQuick 2.0
16+
import QtQuick 2.6
1717
import QtQuick.Controls 2.2
1818
import QgsQuick 0.1 as QgsQuick
1919

@@ -25,60 +25,82 @@ import QgsQuick 0.1 as QgsQuick
2525
Item {
2626
signal valueChanged( var value, bool isNull )
2727

28-
/**
29-
* Handling missing config value for un/checked state for boolean field
30-
*/
3128
property var checkedState: getConfigValue(config['CheckedState'], true)
3229
property var uncheckedState: getConfigValue(config['UncheckedState'], false)
30+
property string booleanEnum: "1" // QMetaType::Bool Enum of Qvariant::Type
3331

3432
id: fieldItem
3533
enabled: !readOnly
3634
height: childrenRect.height
3735
anchors {
3836
right: parent.right
3937
left: parent.left
38+
rightMargin: 10 * QgsQuick.Utils.dp
4039
}
4140

4241
function getConfigValue(configValue, defaultValue) {
43-
if (typeof value === "boolean" && !configValue) {
44-
return defaultValue
45-
} else return configValue
42+
if (!configValue && field.type + "" === fieldItem.booleanEnum) {
43+
return defaultValue
44+
} else return configValue
4645
}
4746

48-
CheckBox {
49-
property var currentValue: value
47+
Rectangle {
48+
id: fieldContainer
5049
height: customStyle.fields.height
51-
id: checkBox
52-
leftPadding: 0
53-
checked: value === fieldItem.checkedState
50+
color: customStyle.fields.backgroundColor
51+
radius: customStyle.fields.cornerRadius
52+
anchors { right: parent.right; left: parent.left }
5453

55-
indicator: Rectangle {
56-
implicitWidth: customStyle.fields.height
57-
implicitHeight: customStyle.fields.height
58-
radius: customStyle.fields.cornerRadius
59-
border.color: checkBox.activeFocus ? customStyle.fields.fontColor : "grey"
60-
border.width: 1
61-
Rectangle {
62-
visible: checkBox.checked
63-
color: customStyle.fields.fontColor
64-
radius: customStyle.fields.cornerRadius
65-
anchors.margins: 4
66-
anchors.fill: parent
67-
}
54+
Text {
55+
text: checkBox.checked ? fieldItem.checkedState : fieldItem.uncheckedState
56+
font.pixelSize: customStyle.fields.fontPixelSize
57+
color: customStyle.fields.fontColor
58+
horizontalAlignment: Text.AlignLeft
59+
verticalAlignment: Text.AlignVCenter
60+
anchors.left: parent.left
61+
anchors.verticalCenter: parent.verticalCenter
62+
leftPadding: 6 * QgsQuick.Utils.dp
63+
}
64+
65+
CheckBox {
66+
property var currentValue: value
67+
height: customStyle.fields.height/2
68+
width: height * 2
69+
anchors.right: parent.right
70+
anchors.rightMargin: fieldItem.anchors.rightMargin
71+
anchors.verticalCenter: parent.verticalCenter
72+
id: checkBox
73+
leftPadding: 0
74+
checked: value === fieldItem.checkedState
75+
76+
indicator: Rectangle {
77+
implicitWidth: parent.width
78+
implicitHeight: parent.height
79+
x: checkBox.leftPadding
80+
y: parent.height / 2 - height / 2
81+
radius: parent.height/2
82+
color: checkBox.checked ? customStyle.fields.fontColor : "#ffffff"
83+
border.color: checkBox.checked ? customStyle.fields.fontColor : customStyle.fields.normalColor
6884

69-
MouseArea {
70-
anchors.fill: parent
71-
onClicked: checkBox.checked = !checkBox.checked
72-
}
85+
Rectangle {
86+
x: checkBox.checked ? parent.width - width : 0
87+
width: parent.height
88+
height: parent.height
89+
radius: parent.height/2
90+
color: "#ffffff"
91+
border.color: checkBox.checked ? customStyle.fields.fontColor : customStyle.fields.normalColor
7392
}
74-
onCheckedChanged: {
75-
valueChanged( checked ? fieldItem.checkedState : fieldItem.uncheckedState, false )
76-
forceActiveFocus()
77-
}
93+
}
94+
95+
onCheckedChanged: {
96+
valueChanged( checked ? fieldItem.checkedState : fieldItem.uncheckedState, false )
97+
forceActiveFocus()
98+
}
7899

79-
// Workaround to get a signal when the value has changed
80-
onCurrentValueChanged: {
81-
checked = currentValue === fieldItem.checkedState
100+
// Workaround to get a signal when the value has changed
101+
onCurrentValueChanged: {
102+
checked = currentValue === fieldItem.checkedState
103+
}
82104
}
83105
}
84106
}

src/quickgui/plugin/qgsquickplugin.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void QgsQuickPlugin::registerTypes( const char *uri )
6969
qRegisterMetaType< QgsUnitTypes::DistanceUnit >( "QgsUnitTypes::DistanceUnit" );
7070
qRegisterMetaType< QgsCoordinateFormatter::FormatFlags >( "QgsCoordinateFormatter::FormatFlags" );
7171
qRegisterMetaType< QgsCoordinateFormatter::Format >( "QgsCoordinateFormatter::Format" );
72+
qRegisterMetaType< QVariant::Type >( "QVariant::Type" );
7273

7374
qmlRegisterUncreatableType< QgsUnitTypes >( uri, 0, 1, "QgsUnitTypes", "Only enums from QgsUnitTypes can be used" );
7475

0 commit comments

Comments
 (0)