Plasma

RoundButton.qml
1 /*
2  * Copyright 2018 Marco Martin <[email protected]>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 import QtQuick 2.6
21 import QtQuick.Layouts 1.2
22 import QtQuick.Templates @[email protected] as T
23 import org.kde.plasma.core 2.0 as PlasmaCore
24 import org.kde.kirigami 2.5 as Kirigami
25 import "private" as Private
26 
27 T.RoundButton {
28  id: control
29 
30  implicitWidth: Math.max(units.gridUnit, contentItem.implicitWidth)
31  + leftPadding + rightPadding
32  implicitHeight: Math.max(units.gridUnit, contentItem.implicitHeight)
33  + topPadding + bottomPadding
34 
35  leftPadding: text.length > 0 ? surfaceNormal.margins.left : contentItem.extraSpace
36  topPadding: text.length > 0 ? surfaceNormal.margins.top : contentItem.extraSpace
37  rightPadding: text.length > 0 ? surfaceNormal.margins.right : contentItem.extraSpace
38  bottomPadding: text.length > 0 ? surfaceNormal.margins.bottom : contentItem.extraSpace
39 
40  hoverEnabled: !Kirigami.Settings.tabletMode
41 
42  contentItem: RowLayout {
43  // This is the spacing which will make the icon a square inscribed in the circle with an extra smallspacing of margins
44  readonly property int extraSpace: width/2 - width/2*Math.sqrt(2)/2 + units.smallSpacing
45  PlasmaCore.IconItem {
46  Layout.preferredWidth: units.iconSizes.smallMedium
47  Layout.preferredHeight: units.iconSizes.smallMedium
48  Layout.fillWidth: true
49  Layout.fillHeight: true
50  visible: source.length > 0
51  source: control.icon ? (control.icon.name || control.icon.source) : ""
52  }
53  Label {
54  visible: text.length > 0
55  text: control.text
56  font: control.font
57  opacity: enabled || control.highlighted || control.checked ? 1 : 0.4
58  color: theme.buttonTextColor
59  horizontalAlignment: Text.AlignHCenter
60  verticalAlignment: Text.AlignVCenter
61  elide: Text.ElideRight
62  }
63  }
64 
65  background: Item {
66  opacity: control.enabled ? 1 : 0.6
67 
68  PlasmaCore.Svg {
69  id: buttonSvg
70  imagePath: "widgets/actionbutton"
71  colorGroup: PlasmaCore.Theme.ButtonColorGroup
72  }
73 
74  Private.RoundShadow {
75  id: roundShadow
76  visible: !control.flat || control.activeFocus || control.highlighted
77  anchors.fill: parent
78  state: {
79  if (control.pressed) {
80  return "hidden"
81  } else if (control.hovered) {
82  return "hover"
83  } else if (control.activeFocus || control.highlighted) {
84  return "focus"
85  } else {
86  return "shadow"
87  }
88  }
89  }
90 
91  PlasmaCore.SvgItem {
92  id: buttonItem
93  svg: buttonSvg
94  elementId: (control.pressed || control.checked) ? "pressed" : "normal"
95  anchors.fill: parent
96  //internal: if there is no hover status, don't paint on mouse over in touchscreens
97  opacity: (control.pressed || control.checked || !control.flat || (roundShadow.hasOverState && control.hovered)) ? 1 : 0
98  Behavior on opacity {
99  PropertyAnimation { duration: units.longDuration }
100  }
101  }
102 
103  Private.ButtonShadow {
104  anchors.fill: parent
105  visible: control.text.length > 0 && (!control.flat || control.hovered) && (!control.pressed || !control.checked)
106  state: {
107  if (control.pressed) {
108  return "hidden"
109  } else if (control.hovered) {
110  return "hover"
111  } else if (control.activeFocus) {
112  return "focus"
113  } else {
114  return "shadow"
115  }
116  }
117  }
118  PlasmaCore.FrameSvgItem {
119  id: surfaceNormal
120  anchors.fill: parent
121  imagePath: "widgets/button"
122  prefix: "normal"
123  opacity: control.text.length > 0 && (!control.flat || control.hovered) && (!control.pressed || !control.checked) ? 1 : 0
124  Behavior on opacity {
125  OpacityAnimator {
126  duration: units.longDuration
127  easing.type: Easing.InOutQuad
128  }
129  }
130  }
131  PlasmaCore.FrameSvgItem {
132  anchors.fill: parent
133  imagePath: "widgets/button"
134  prefix: "pressed"
135  visible: control.text.length > 0
136  opacity: control.checked || control.pressed ? 1 : 0
137  Behavior on opacity {
138  OpacityAnimator {
139  duration: units.longDuration
140  easing.type: Easing.InOutQuad
141  }
142  }
143  }
144  }
145 }
This is a label which uses the plasma theme.
Definition: qml/Label.qml:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:39:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.