Commit edec39d7 authored by Aleksi Suomalainen's avatar Aleksi Suomalainen Committed by GitHub

Merge pull request #67 from neochapay/checkobx_up

[CheckBox] Add checkBox indeterminate property and clean code
parents bfa881bc b04b5d67
...@@ -37,13 +37,32 @@ import QtQuick.Controls.Styles.Nemo 1.0 ...@@ -37,13 +37,32 @@ import QtQuick.Controls.Styles.Nemo 1.0
Page { Page {
id: root id: root
headerTools: HeaderToolsLayout { showBackButton: true; title: "Switch" } headerTools: HeaderToolsLayout { showBackButton: true; title: qsTr("Switch") }
Column { Column {
spacing: 40 spacing: 40
anchors.centerIn: parent anchors.centerIn: parent
CheckBox { CheckBox {
text: "Test 1: " id: defaultCheckbox
text: qsTr("Default checkbox")
}
CheckBox {
id: indeterminateCheckbox
text: qsTr("Indeterminate checkbox")
onCheckedChanged: {
indeterminateCheckbox.indeterminate = true
indeterminateTimer.start()
}
}
}
Timer{
id: indeterminateTimer
interval: 4000
onTriggered: {
indeterminateCheckbox.indeterminate = false
} }
} }
} }
...@@ -4,5 +4,8 @@ import QtQuick.Controls.Styles.Nemo 1.0 ...@@ -4,5 +4,8 @@ import QtQuick.Controls.Styles.Nemo 1.0
CheckBox { CheckBox {
property int fontSize: Theme.fontSizeSmall property int fontSize: Theme.fontSizeSmall
property bool indeterminate: false
style: CheckBoxStyle{} style: CheckBoxStyle{}
} }
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
****************************************************************************************/ ****************************************************************************************/
import QtQuick 2.6 import QtQuick 2.6
import QtGraphicalEffects 1.0
import QtQuick.Controls.Styles 1.0 import QtQuick.Controls.Styles 1.0
import QtQuick.Controls.Nemo 1.0 import QtQuick.Controls.Nemo 1.0
...@@ -41,83 +42,122 @@ CheckBoxStyle { ...@@ -41,83 +42,122 @@ CheckBoxStyle {
implicitHeight: Theme.itemHeightExtraSmall implicitHeight: Theme.itemHeightExtraSmall
Rectangle { Rectangle {
id: back2 id: bgArea
implicitWidth: Theme.itemWidthExtraSmall implicitWidth: Theme.itemWidthExtraSmall
implicitHeight: Theme.itemHeightExtraSmall - Theme.itemSpacingExtraSmall implicitHeight: Theme.itemHeightExtraSmall - Theme.itemSpacingExtraSmall
color: Theme.fillDarkColor color: control.checked ? Theme.accentColor : Theme.fillDarkColor
anchors.centerIn: parent anchors.centerIn: parent
Rectangle {
id: back1
implicitWidth: Theme.itemWidthExtraSmall
implicitHeight: Theme.itemHeightExtraSmall - Theme.itemSpacingExtraSmall
color: Theme.accentColor
anchors.centerIn: parent
}
} }
Image { Rectangle {
id: ball id: ball
width: Theme.itemHeightSmall width: Theme.itemHeightExtraSmall
height: Theme.itemHeightExtraSmall height: Theme.itemHeightExtraSmall
fillMode: Image.PreserveAspectFit radius: width/2
sourceSize.width: width
sourceSize.height: height
source: "images/switch-ball.png"
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
clip: true
LinearGradient {
anchors.fill: parent
start: Qt.point(0, 0)
end: Qt.point(0, Theme.itemHeightExtraSmall)
source: ball
gradient: Gradient {
GradientStop { position: 0.0; color: "#ffffff" }
GradientStop { position: 1.0; color: "#dcdcdc" }
}
}
x: control.checked ? background.width - ball.width : 0
} }
Connections { Connections {
target: control target: control
onCheckedChanged: { onCheckedChanged: {
if(!indeterminate) {
if (control.checked) { if (control.checked) {
anim1.restart() checkAnimation.restart()
} else {
unCheckAnimation.restart()
}
}
}
onIndeterminateChanged: {
indeterminateAnimation.stop()
if(indeterminate) {
indeterminateAnimation.start()
} else { } else {
anim2.restart() indeterminateAnimation.stop()
bgArea.color = control.checked ? Theme.accentColor : Theme.fillDarkColor
} }
} }
} }
Component.onCompleted: { Component.onCompleted: {
back1.opacity = control.checked ? 1 : 0 if(control.indeterminate){
ball.x = control.checked ? background.width - ball.width : 0 indeterminateAnimation.start()
}
} }
SequentialAnimation { ParallelAnimation {
id: anim1 id: checkAnimation
running: false running: false
NumberAnimation { NumberAnimation {
target: ball target: ball
property: "x" property: "x"
to: background.width - ball.width to: background.width - ball.width
duration: 120 duration: 400
} }
NumberAnimation { PropertyAnimation {
target: back1 target: bgArea
property: "opacity" property: "color"
to: 1 to: Theme.accentColor
duration: 120 duration: 400
} }
} }
SequentialAnimation { ParallelAnimation {
id: anim2 id: unCheckAnimation
running: false running: false
NumberAnimation { NumberAnimation {
target: ball target: ball
property: "x" property: "x"
to: 0 to: 0
duration: 120 duration: 400
} }
NumberAnimation { PropertyAnimation {
target: back1 target: bgArea
property: "opacity" property: "color"
to: 0 to: Theme.fillDarkColor
duration: 120 duration: 400
} }
} }
SequentialAnimation{
id: indeterminateAnimation
running: false
loops: Animation.Infinite
PropertyAnimation {
target: bgArea
property: "color"
to: control.checked ? Theme.fillDarkColor : Theme.accentColor
duration: 500
}
PropertyAnimation {
target: bgArea
property: "color"
to: control.checked ? Theme.accentColor : Theme.fillDarkColor
duration: 500
} }
}
}
label: Label { label: Label {
text: control.text text: control.text
font.pixelSize:control.fontSize font.pixelSize:control.fontSize
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment