Changeset View
Changeset View
Standalone View
Standalone View
containments/desktop/package/contents/ui/ActionButton.qml
Show All 12 Lines | |||||
13 | * GNU Library General Public License for more details | 13 | * GNU Library General Public License for more details | ||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU Library General Public | 15 | * You should have received a copy of the GNU Library General Public | ||
16 | * License along with this program; if not, write to the | 16 | * License along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | import QtQuick 2.0 | 21 | import QtQuick 2.12 | ||
22 | import QtQuick.Layouts 1.4 | ||||
23 | | ||||
22 | import org.kde.plasma.core 2.0 as PlasmaCore | 24 | import org.kde.plasma.core 2.0 as PlasmaCore | ||
23 | import org.kde.plasma.components 2.0 as PlasmaComponents | 25 | import org.kde.plasma.components 3.0 as PlasmaComponents | ||
24 | 26 | | |||
25 | PlasmaCore.ToolTipArea { | 27 | PlasmaComponents.ToolButton { | ||
26 | id: button | 28 | id: button | ||
29 | Layout.fillWidth: true | ||||
27 | 30 | | |||
28 | location: PlasmaCore.Types.LeftEdge | 31 | property QtObject qAction | ||
29 | mainText: action !== undefined ? action.text : "" | | |||
30 | mainItem: toolTipDelegate | | |||
31 | 32 | | |||
32 | //API | | |||
33 | property PlasmaCore.Svg svg | 33 | property PlasmaCore.Svg svg | ||
34 | property alias elementId: icon.elementId | 34 | property alias elementId: icon.elementId | ||
35 | property QtObject action | | |||
36 | property bool backgroundVisible: false | | |||
37 | property int iconSize: 32 | 35 | property int iconSize: 32 | ||
38 | property int pressedOffset: 1 | 36 | property alias toolTip: toolTip.text | ||
39 | property bool checked: false | | |||
40 | property bool toggle: false | | |||
41 | property string text | | |||
42 | signal clicked | | |||
43 | 37 | | |||
44 | implicitWidth: buttonRow.implicitWidth | | |||
45 | implicitHeight: buttonRow.implicitHeight | | |||
46 | 38 | | |||
davidedmundson: I try and follow a rule to have a rule that all:
Layout.*
Anchors.*
width/x/y/height… | |||||
47 | opacity: action==undefined||action.enabled?1:0.6 | 39 | implicitWidth: Math.min(buttonColumn.implicitWidth, units.gridUnit * 10) + leftPadding + rightPadding | ||
48 | 40 | | |||
49 | Behavior on opacity { | 41 | onClicked: { | ||
50 | NumberAnimation { | 42 | if (qAction) { | ||
51 | duration: units.longDuration | 43 | qAction.trigger() | ||
52 | easing.type: Easing.InOutQuad | | |||
53 | } | 44 | } | ||
45 | appletContainer.editMode = false; | ||||
54 | } | 46 | } | ||
55 | 47 | | |||
56 | onCheckedChanged: { | | |||
57 | if (checked) { | | |||
58 | buttonItem.elementId = "pressed" | | |||
59 | shadowItem.opacity = 0 | | |||
60 | } else { | | |||
61 | buttonItem.elementId = "normal" | | |||
62 | shadowItem.opacity = 1 | | |||
63 | } | | |||
64 | } | | |||
65 | 48 | | |||
66 | PlasmaCore.Svg { | 49 | PlasmaComponents.ToolTip { | ||
67 | id: buttonSvg | 50 | id: toolTip | ||
68 | imagePath: "widgets/actionbutton" | 51 | text: button.qAction ? button.qAction.text : "" | ||
69 | } | 52 | delay: 0 | ||
70 | 53 | visible: button.hovered | |||
71 | PlasmaCore.SvgItem { | 54 | x: button.width | ||
72 | id: shadowItem | 55 | y: button.height/2 - height/2 | ||
73 | svg: buttonSvg | | |||
74 | elementId: "shadow" | | |||
75 | width: iconSize+13//button.backgroundVisible?iconSize+8:iconSize | | |||
76 | height: width | | |||
77 | visible: button.backgroundVisible | | |||
78 | } | | |||
79 | | ||||
80 | Row { | | |||
81 | id: buttonRow | | |||
82 | | ||||
83 | Item { | | |||
84 | width: buttonItem.visible?buttonItem.width:iconSize | | |||
85 | height: buttonItem.visible?buttonItem.height:iconSize | | |||
86 | | ||||
87 | PlasmaCore.SvgItem { | | |||
88 | id: buttonItem | | |||
89 | svg: buttonSvg | | |||
90 | elementId: "normal" | | |||
91 | width: shadowItem.width | | |||
92 | height: shadowItem.height | | |||
93 | visible: backgroundVisible | | |||
94 | } | 56 | } | ||
57 | contentItem: ColumnLayout { | ||||
58 | id: buttonColumn | ||||
95 | 59 | | |||
96 | PlasmaCore.SvgItem { | 60 | PlasmaCore.SvgItem { | ||
97 | id: icon | 61 | id: icon | ||
62 | Layout.preferredWidth: units.iconSizes.small | ||||
63 | Layout.preferredHeight: Layout.preferredWidth | ||||
64 | Layout.alignment: Qt.AlignHCenter | ||||
98 | width: iconSize | 65 | width: iconSize | ||
99 | height: iconSize | 66 | height: iconSize | ||
100 | svg: button.svg | 67 | svg: button.svg | ||
101 | anchors.centerIn: parent | | |||
102 | } | | |||
103 | } | 68 | } | ||
104 | 69 | | |||
105 | Text { | 70 | PlasmaComponents.Label { | ||
106 | id: actionText | 71 | id: actionText | ||
72 | Layout.fillWidth: true | ||||
73 | horizontalAlignment: Text.AlignHCenter | ||||
107 | text: button.text | 74 | text: button.text | ||
108 | style: Text.Outline | 75 | wrapMode: Text.WordWrap | ||
76 | elide: Text.ElideRight | ||||
77 | // The handle uses always the main global theme | ||||
109 | color: theme.textColor | 78 | color: theme.textColor | ||
110 | styleColor: Qt.rgba(1,1,1,0.4) | 79 | visible: text.length > 0 | ||
111 | anchors.verticalCenter: parent.verticalCenter | | |||
112 | } | | |||
113 | } | | |||
114 | | ||||
115 | MouseArea { | | |||
116 | anchors.fill: parent | | |||
117 | anchors.leftMargin: -10 | | |||
118 | anchors.topMargin: -10 | | |||
119 | anchors.rightMargin: -10 | | |||
120 | anchors.bottomMargin: -10 | | |||
121 | preventStealing: true | | |||
122 | onPressed: { | | |||
123 | buttonItem.elementId = "pressed" | | |||
124 | shadowItem.opacity = 0; | | |||
125 | button.x = button.x + button.pressedOffset; | | |||
126 | button.y = button.y + button.pressedOffset; | | |||
127 | } | | |||
128 | onReleased: { | | |||
129 | if (button.checked || !button.toggle) { | | |||
130 | buttonItem.elementId = "normal" | | |||
131 | shadowItem.opacity = 1 | | |||
132 | button.checked = false | | |||
133 | } else { | | |||
134 | button.checked = true | | |||
135 | } | | |||
136 | button.x = button.x - button.pressedOffset; | | |||
137 | button.y = button.y - button.pressedOffset; | | |||
138 | } | | |||
139 | onClicked: { | | |||
140 | if (action) { | | |||
141 | action.trigger() | | |||
142 | } else { | | |||
143 | button.clicked() | | |||
144 | } | | |||
145 | appletContainer.editMode = false; | | |||
146 | } | 80 | } | ||
147 | } | 81 | } | ||
148 | } | 82 | } |
I try and follow a rule to have a rule that all:
Layout.*
Anchors.*
width/x/y/height properties
Should be in the code of whoever creates the component, not in the component itself. (i.e ConfigOverlay) it makes things more portable