~zsombi/ubuntu-ui-toolkit/statesaver-signals

54.3.12 by Zsombor Egri
CSS-like style parsing. 2 sample styles.
1
/*
564.2.2 by Kaleo
Button: major clean up.
2
 * Copyright 2013 Canonical Ltd.
54.3.12 by Zsombor Egri
CSS-like style parsing. 2 sample styles.
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation; version 3.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU Lesser General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU Lesser General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
564.2.2 by Kaleo
Button: major clean up.
15
 *
16
 * Author: Florian Boucault <florian.boucault@canonical.com>
54.3.12 by Zsombor Egri
CSS-like style parsing. 2 sample styles.
17
 */
18
54.3.23 by Zsombor Egri
Rework on style rules because of Qt5
19
import QtQuick 2.0
190.2.1 by Zsombor Egri
Delegates moved under qmltheme
20
import Ubuntu.Components 0.1
54.3.12 by Zsombor Egri
CSS-like style parsing. 2 sample styles.
21
22
Item {
564.2.2 by Kaleo
Button: major clean up.
23
    id: buttonStyle
24
564.2.12 by Kaleo
Merged simple_theming
25
    property Button button: styledItem
564.2.2 by Kaleo
Button: major clean up.
26
    property real minimumWidth: units.gu(10)
27
    property real horizontalPadding: units.gu(1)
930.1.1 by Florian Boucault
More compact visual style for a few widgets:
28
    property color defaultColor: UbuntuColors.orange
29
    property Gradient defaultGradient
564.2.2 by Kaleo
Button: major clean up.
30
31
    width: button.width
32
    height: button.height
33
    implicitWidth: Math.max(minimumWidth, foreground.implicitWidth + 2*horizontalPadding)
930.1.1 by Florian Boucault
More compact visual style for a few widgets:
34
    implicitHeight: units.gu(4)
564.2.2 by Kaleo
Button: major clean up.
35
901.1.3 by Florian Boucault
Added Right-to-Left languages support to most widgets and to the UI Toolkit Gallery.
36
    LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
37
    LayoutMirroring.childrenInherit: true
38
564.2.17 by Kaleo
Added 'gradient' property to Button.
39
    /* The proxy is necessary because Gradient.stops and GradientStop.color are
40
       non-NOTIFYable properties. They cannot be written to so it is fine but
41
       the proxy avoids the warnings.
42
    */
43
    QtObject {
44
        id: gradientProxy
45
        property color topColor
46
        property color bottomColor
47
48
        function updateGradient() {
49
            if (button.gradient) {
50
                topColor = button.gradient.stops[0].color;
51
                bottomColor = button.gradient.stops[1].color;
52
            }
53
        }
54
55
        Component.onCompleted: {
56
            updateGradient();
57
            button.gradientChanged.connect(updateGradient);
58
        }
59
    }
60
61
    // Use the gradient if it is defined and the color has not been set manually
62
    // or the gradient has been set manually
63
    property bool isGradient: button.gradient && (button.color == defaultColor ||
64
                              button.gradient != defaultGradient)
65
564.2.2 by Kaleo
Button: major clean up.
66
    UbuntuShape {
67
        id: background
68
        anchors.fill: parent
69
        borderSource: "radius_idle.sci"
70
        visible: color.a != 0.0
582.1.1 by Loïc Molinari
Added workaround for QML buggy support for color props in ternary ops.
71
72
        // Color properties in a JS ternary operator don't work as expected in
73
        // QML because it overwrites alpha values with 1. A workaround is to use
74
        // Qt.rgba(). For more information, see
582.1.2 by Loïc Molinari
Added link to bug report and func for color hack.
75
        // https://bugs.launchpad.net/ubuntu-ui-toolkit/+bug/1197802 and
76
        // https://bugreports.qt-project.org/browse/QTBUG-32238.
77
        function colorHack(color) { return Qt.rgba(color.r, color.g, color.b, color.a); }
587.1.1 by Kaleo
Latest fix in ButtonStyle had 3 typos. Fixing them.
78
        color: isGradient ? colorHack(gradientProxy.topColor) : colorHack(button.color)
79
        gradientColor: isGradient ? colorHack(gradientProxy.bottomColor) : colorHack(button.color)
564.2.2 by Kaleo
Button: major clean up.
80
    }
81
82
    UbuntuShape {
83
        id: backgroundPressed
84
        anchors.fill: parent
564.2.7 by Kaleo
ButtonDelegate: slightly better code.
85
        color: background.color
564.2.17 by Kaleo
Added 'gradient' property to Button.
86
        gradientColor: background.gradientColor
564.2.2 by Kaleo
Button: major clean up.
87
        borderSource: "radius_pressed.sci"
88
        opacity: button.pressed ? 1.0 : 0.0
89
        Behavior on opacity {
90
            NumberAnimation {
91
                duration: UbuntuAnimation.SnapDuration
92
                easing.type: Easing.Linear
93
            }
94
        }
95
        visible: background.visible
96
    }
97
98
    ButtonForeground {
99
        id: foreground
100
        width: parent.width - 2*horizontalPadding
101
        anchors.centerIn: parent
102
        text: button.text
103
        /* Pick either a clear or dark text color depending on the luminance of the
104
           background color to maintain good contrast (works in most cases)
105
        */
106
        textColor: ColorUtils.luminance(button.color) <= 0.85 ? "#F3F3E7" : "#888888"
107
        iconSource: button.iconSource
108
        iconPosition: button.iconPosition
109
        iconSize: units.gu(3)
110
        spacing: horizontalPadding
111
        transformOrigin: Item.Top
112
        scale: button.pressed ? 0.98 : 1.0
113
        Behavior on scale {
114
            NumberAnimation {
115
                duration: UbuntuAnimation.SnapDuration
116
                easing.type: Easing.Linear
117
            }
118
        }
564.2.13 by Kaleo
Fixed failed merge.
119
    }
54.3.12 by Zsombor Egri
CSS-like style parsing. 2 sample styles.
120
}