• 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
ToolButton.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 Marco Martin <mart@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 import QtQuick 1.1
22 import org.kde.plasma.core 0.1 as PlasmaCore
23 import "private" as Private
24 
28 Item {
29  id: button
30 
31  // Commmon API
35  property bool flat: true
36 
40  property bool checked: defaultAction ? defaultAction.checked : false
41 
45  property bool checkable: defaultAction ? defaultAction.checkable : false
46 
51  property alias pressed: mouse.pressed
52 
57  property alias text: label.text
58 
65  property alias iconSource: icon.source
66 
71  property alias font: label.font
72 
76  signal clicked()
77 
78  // Plasma extensiuons
79  property QtObject defaultAction
80 
81 
82  enabled: defaultAction == undefined || defaultAction.enabled
83 
87  //icon + label + left margin + right margin + spacing between icon and text
88  //here it assumesleft margin = right top = bottom, why?
89  // because the right and bottom margins can be disabled, so they would return 0, but their actual size is still needed for size hints
90  property real minimumWidth: Math.max(theme.smallIconSize, height) + label.paintedWidth + delegate.margins.left + delegate.margins.left + ((icon.valid) ? delegate.margins.left : 0)
91 
95  property real minimumHeight: Math.max(theme.smallIconSize, label.paintedHeight) + delegate.margins.top + delegate.margins.top
96 
97  implicitWidth: {
98  if (label.text.length == 0) {
99  height;
100  } else {
101  Math.max(theme.defaultFont.mSize.width*12, minimumWidth);
102  }
103  }
104 
105  implicitHeight: Math.max(theme.defaultFont.mSize.height*1.6, minimumHeight)
106 
107  // TODO: needs to define if there will be specific graphics for
108  // disabled buttons
109  opacity: enabled ? 1.0 : 0.5
110 
111  Keys.onSpacePressed: internal.userPressed = true
112  Keys.onReturnPressed: internal.userPressed = true
113  Keys.onReleased: {
114  internal.userPressed = false
115  if (event.key == Qt.Key_Space ||
116  event.key == Qt.Key_Return)
117  internal.clickButton()
118  }
119 
120  onActiveFocusChanged: {
121  if (activeFocus) {
122  shadow.state = "focus"
123  } else if (checked) {
124  shadow.state = "hidden"
125  } else {
126  shadow.state = "shadow"
127  }
128  }
129 
130  QtObject {
131  id: internal
132  property bool userPressed: false
133 
134  function clickButton()
135  {
136  if (!button.enabled) {
137  return
138  }
139 
140  if (defaultAction && defaultAction.checkable) {
141  defaultAction.checked = !defaultAction.checked
142  } else if (button.checkable) {
143  button.checked = !button.checked
144  }
145 
146  if (button.KeyNavigation.tab || button.KeyNavigation.backtab) {
147  // Only focus the button if it is set up for keyboard
148  // navigation. This avoid getting a strange focus frame around
149  // buttons which are usually not focusable, such as buttons in
150  // a toolbar.
151  button.forceActiveFocus();
152  }
153  button.clicked()
154 
155  if (defaultAction) {
156  defaultAction.trigger()
157  }
158  }
159  }
160 
161  Loader {
162  id: delegate
163  anchors.fill: parent
164  property QtObject margins: item.margins
165  property string shadowState: "shadow"
166  sourceComponent: {
167  if (label.text.length == 0 && button.width == button.height && (button.parent && button.parent.checkedButton === undefined) && !flat) {
168  return roundButtonComponent
169  } else {
170  return buttonComponent
171  }
172  }
173  }
174 
175  Component {
176  id: buttonComponent
177  Item {
178  parent: delegate
179  anchors.fill: parent
180  property alias margins: surface.margins
181  property alias hasOverState: shadow.hasOverState
182  Private.ButtonShadow {
183  id: shadow
184  anchors.fill: parent
185  visible: !flat && (surface.enabledBorders == "AllBorders" || state == "hover" || state == "focus")
186  state: delegate.shadowState
187  }
188 
189  PlasmaCore.FrameSvgItem {
190  id: surface
191 
192  enabledBorders: {
193  if (flat ||
194  button.parent.width < button.parent.implicitWidth ||
195  button.parent.checkedButton === undefined ||
196  !bordersSvg.hasElement("pressed-hint-compose-over-border")) {
197  if (shadows !== null) {
198  shadows.destroy()
199  }
200  return "AllBorders"
201  }
202 
203  var borders = new Array()
204  if (button.x == 0) {
205  borders.push("LeftBorder")
206  }
207  if (button.y == 0) {
208  borders.push("TopBorder")
209  }
210  if (button.x + button.width >= button.parent.width) {
211  borders.push("RightBorder")
212  }
213  if (button.y + button.height >= button.parent.height) {
214  borders.push("BottomBorder")
215  }
216 
217  if (shadows === null) {
218  shadows = shadowsComponent.createObject(surface)
219  }
220 
221  return borders.join("|")
222  }
223 
224  anchors.fill: parent
225  imagePath: "widgets/button"
226  prefix: (internal.userPressed || checked) ? "pressed" : "normal"
227  //internal: if there is no hover status, don't paint on mouse over in touchscreens
228  opacity: (internal.userPressed || checked || !flat || (shadow.hasOverState && mouse.containsMouse && button.enabled)) ? 1 : 0
229  Behavior on opacity {
230  PropertyAnimation { duration: 250 }
231  }
232 
233  PlasmaCore.Svg {
234  id: bordersSvg
235  imagePath: "widgets/button"
236  }
237 
238  property Item shadows
239  Component {
240  id: shadowsComponent
241  Item {
242  anchors.fill: parent
243 
244  PlasmaCore.SvgItem {
245  svg: bordersSvg
246  width: naturalSize.width
247  elementId: surface.prefix+"-left"
248  visible: button.x > 0
249  anchors {
250  left: parent.left
251  top: parent.top
252  bottom: parent.bottom
253  margins: 1
254  leftMargin: -1
255  }
256  }
257  PlasmaCore.SvgItem {
258  svg: bordersSvg
259  width: naturalSize.width
260  elementId: surface.prefix+"-right"
261  visible: button.x + button.width < button.parent.width
262  anchors {
263  right: parent.right
264  top: parent.top
265  bottom: parent.bottom
266  margins: 1
267  rightMargin: -1
268  }
269  }
270  PlasmaCore.SvgItem {
271  svg: bordersSvg
272  height: naturalSize.height
273  elementId: surface.prefix+"-top"
274  visible: button.y > 0
275  anchors {
276  left: parent.left
277  top: parent.top
278  right: parent.right
279  margins: 1
280  topMargin: -1
281  }
282  }
283  PlasmaCore.SvgItem {
284  svg: bordersSvg
285  width: naturalSize.width
286  elementId: surface.prefix+"-bottom"
287  visible: button.y + button.height < button.parent.height
288  anchors {
289  left: parent.left
290  right: parent.right
291  bottom: parent.bottom
292  margins: 1
293  bottomMargin: -1
294  }
295  }
296  }
297  }
298  }
299  }
300  }
301 
302  Component {
303  id: roundButtonComponent
304  Item {
305  id: roundButtonDelegate
306  parent: delegate
307  anchors.fill: parent
308  property QtObject margins: QtObject {
309  property int left: delegate.width/8
310  property int top: delegate.width/8
311  property int right: delegate.width/8
312  property int bottom: delegate.width/8
313  }
314  property alias hasOverState: roundShadow.hasOverState
315  Private.RoundShadow {
316  id: roundShadow
317  visible: !flat
318  anchors.fill: parent
319  state: delegate.shadowState
320  }
321 
322  PlasmaCore.Svg {
323  id: buttonSvg
324  imagePath: "widgets/actionbutton"
325  }
326 
327  PlasmaCore.SvgItem {
328  id: buttonItem
329  svg: buttonSvg
330  elementId: (internal.userPressed || checked) ? "pressed" : "normal"
331  width: parent.height
332  height: width
333  //internal: if there is no hover status, don't paint on mouse over in touchscreens
334  opacity: (internal.userPressed || checked || !flat || (roundShadow.hasOverState && mouse.containsMouse)) ? 1 : 0
335  Behavior on opacity {
336  PropertyAnimation { duration: 250 }
337  }
338  }
339  }
340  }
341 
342  Row {
343  anchors {
344  fill: parent
345  leftMargin: delegate.margins.left
346  topMargin: delegate.margins.top
347  rightMargin: delegate.margins.right
348  bottomMargin: delegate.margins.bottom
349  }
350 
351  spacing: icon.valid ? delegate.margins.left : 0
352 
353  PlasmaCore.IconItem {
354  id: icon
355  anchors.verticalCenter: parent.verticalCenter
356  width: valid ? Math.min(parent.width, parent.height): 0
357  height: width
358  active: delegate.item.hasOverState && mouse.containsMouse
359  }
360 
361  Text {
362  id: label
363 
364  width: parent.width - icon.width - parent.spacing
365  height: parent.height
366 
367  font.capitalization: theme.defaultFont.capitalization
368  font.family: theme.defaultFont.family
369  font.italic: theme.defaultFont.italic
370  font.letterSpacing: theme.defaultFont.letterSpacing
371  font.pointSize: theme.defaultFont.pointSize
372  font.strikeout: theme.defaultFont.strikeout
373  font.underline: theme.defaultFont.underline
374  font.weight: theme.defaultFont.weight
375  font.wordSpacing: theme.defaultFont.wordSpacing
376 
377  color: mouse.containsMouse ? theme.buttonTextColor : theme.textColor
378  Behavior on color { ColorAnimation { duration: 100 } }
379 
380  horizontalAlignment: icon.valid ? Text.AlignLeft : Text.AlignHCenter
381  verticalAlignment: Text.AlignVCenter
382  }
383  }
384 
385  MouseArea {
386  id: mouse
387 
388  anchors.fill: parent
389  hoverEnabled: delegate.item.hasOverState
390 
391  onPressed: internal.userPressed = true
392  onReleased: internal.userPressed = false
393  onCanceled: {
394  internal.userPressed = false
395  delegate.shadowState = "shadow"
396  }
397  onClicked: internal.clickButton()
398 
399  onEntered: {
400  if (delegate.item.hasOverState && !flat && !internal.userPressed && !checked) {
401  delegate.shadowState = "hover"
402  }
403  button.z += 2
404  }
405  onExited: {
406  if (!flat) {
407  if (button.activeFocus) {
408  delegate.shadowState = "focus"
409  } else if (checked) {
410  delegate.shadowState = "hidden"
411  } else {
412  delegate.shadowState = "shadow"
413  }
414  }
415  button.z -= 2
416  }
417  }
418 }
419 
Item
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