• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • plasmacomponents
  • qml
Button.qml
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
3 * Copyright (C) 2011 by Mark Gaiser <markg85@gmail.com>
4 * Copyright (C) 2011 by Marco Martin <mart@kde.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
22 
31 import QtQuick 1.1
32 
33 import org.kde.plasma.core 0.1 as PlasmaCore
34 import "private" as Private
35 
36 Item {
37  id: button
38 
39 
40  // Commmon API
50  property bool checked: false
51 
58  property bool checkable: false
59 
65  property alias pressed: mouse.pressed
66 
71  property alias text: label.text
72 
82  property alias iconSource: icon.source
83 
91  property alias font: label.font
92 
93  //icon + label + left margin + right margin + spacing between icon and text
97  property real minimumWidth: icon.width + label.paintedWidth + surfaceNormal.margins.left + surfaceNormal.margins.right + ((icon.valid) ? surfaceNormal.margins.left : 0)
98 
102  property real minimumHeight: Math.max(theme.smallIconSize, label.paintedHeight) + surfaceNormal.margins.top + surfaceNormal.margins.bottom
103 
107  signal clicked()
108 
109  implicitWidth: {
110  if (label.text.length == 0) {
111  height;
112  } else {
113  Math.max(theme.defaultFont.mSize.width*12, minimumWidth);
114  }
115  }
116 
117  implicitHeight: Math.max(theme.defaultFont.mSize.height*1.6, minimumHeight)
118 
119  // TODO: needs to define if there will be specific graphics for
120  // disabled buttons
121  opacity: enabled ? 1.0 : 0.5
122 
123  QtObject {
124  id: internal
125  property bool userPressed: false
126 
127  function belongsToButtonGroup()
128  {
129  return button.parent
130  && button.parent.hasOwnProperty("checkedButton")
131  && button.parent.exclusive
132  }
133 
134  function clickButton()
135  {
136  userPressed = false
137  if (!button.enabled) {
138  return
139  }
140 
141  if ((!belongsToButtonGroup() || !button.checked) && button.checkable) {
142  button.checked = !button.checked
143  }
144 
145  button.forceActiveFocus()
146  button.clicked()
147  }
148  }
149 
150  Keys.onSpacePressed: internal.userPressed = true
151  Keys.onReturnPressed: internal.userPressed = true
152  Keys.onReleased: {
153  internal.userPressed = false
154  if (event.key == Qt.Key_Space ||
155  event.key == Qt.Key_Return)
156  internal.clickButton();
157  }
158 
159  Private.ButtonShadow {
160  id: shadow
161  anchors.fill: parent
162  state: {
163  if (internal.userPressed || checked) {
164  return "hidden"
165  } else if (mouse.containsMouse) {
166  return "hover"
167  } else if (button.activeFocus) {
168  return "focus"
169  } else {
170  return "shadow"
171  }
172  }
173  }
174 
175  // The normal button state
176  PlasmaCore.FrameSvgItem {
177  id: surfaceNormal
178 
179  anchors.fill: parent
180  imagePath: "widgets/button"
181  prefix: "normal"
182  }
183 
184  // The pressed state
185  PlasmaCore.FrameSvgItem {
186  id: surfacePressed
187 
188  anchors.fill: parent
189  imagePath: "widgets/button"
190  prefix: "pressed"
191  opacity: 0
192  }
193 
194  Row {
195  id: buttonContent
196  state: (internal.userPressed || checked) ? "pressed" : "normal"
197  spacing: icon.valid ? surfaceNormal.margins.left : 0
198 
199  states: [
200  State { name: "normal" },
201  State { name: "pressed"
202  PropertyChanges {
203  target: surfaceNormal
204  opacity: 0
205  }
206  PropertyChanges {
207  target: surfacePressed
208  opacity: 1
209  }
210  }
211  ]
212  transitions: [
213  Transition {
214  to: "normal"
215  // Cross fade from pressed to normal
216  ParallelAnimation {
217  NumberAnimation { target: surfaceNormal; property: "opacity"; to: 1; duration: 100 }
218  NumberAnimation { target: surfacePressed; property: "opacity"; to: 0; duration: 100 }
219  }
220  }
221  ]
222 
223  anchors {
224  fill: parent
225  leftMargin: surfaceNormal.margins.left
226  topMargin: surfaceNormal.margins.top
227  rightMargin: surfaceNormal.margins.right
228  bottomMargin: surfaceNormal.margins.bottom
229  }
230 
231  PlasmaCore.IconItem {
232  id: icon
233  anchors.verticalCenter: parent.verticalCenter
234  width: valid? parent.height: 0
235  height: width
236  active: shadow.hasOverState && mouse.containsMouse
237  }
238 
239  Text {
240  id: label
241 
242  width: parent.width - icon.width - parent.spacing
243  height: parent.height
244 
245  font.capitalization: theme.defaultFont.capitalization
246  font.family: theme.defaultFont.family
247  font.italic: theme.defaultFont.italic
248  font.letterSpacing: theme.defaultFont.letterSpacing
249  font.pointSize: theme.defaultFont.pointSize
250  font.strikeout: theme.defaultFont.strikeout
251  font.underline: theme.defaultFont.underline
252  font.weight: theme.defaultFont.weight
253  font.wordSpacing: theme.defaultFont.wordSpacing
254  color: theme.buttonTextColor
255  horizontalAlignment: icon.valid ? Text.AlignLeft : Text.AlignHCenter
256  verticalAlignment: Text.AlignVCenter
257  elide: button.width < button.implicitWidth ? Text.ElideRight : Text.ElideNone
258  }
259  }
260 
261  MouseArea {
262  id: mouse
263 
264  anchors.fill: parent
265  hoverEnabled: true
266  onPressed: internal.userPressed = true
267  onReleased: internal.userPressed = false
268  onCanceled: internal.userPressed = false
269  onClicked: internal.clickButton()
270  }
271 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaComponents

Skip menu "PlasmaComponents"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal