| 1 |
/* |
| 2 |
* Copyright 2012 Canonical Ltd. |
| 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/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.2 |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmlabstract AbstractButton |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\brief The AbstractButton class defines the behavior of the button. |
| 25 |
|
| 26 |
This class defines the behavior of the button: it defines the MouseArea |
| 27 |
and the states. |
| 28 |
All components deriving from this class support haptic feedback out of the box. |
| 29 |
*/ |
| 30 |
ActionItem { |
| 31 |
id: button |
| 32 |
|
| 33 |
/*! |
| 34 |
If an action is specified, the button's clicked signal will trigger the action. |
| 35 |
Subclasses of AbstractButton can use other properties of action (for example |
| 36 |
the text and iconName). |
| 37 |
\qmlproperty Action action |
| 38 |
*/ |
| 39 |
|
| 40 |
/*! |
| 41 |
This handler is called when there is a mouse click on the button |
| 42 |
and the button is not disabled. If \b action is defined, |
| 43 |
the action will be triggered. |
| 44 |
*/ |
| 45 |
signal clicked() |
| 46 |
|
| 47 |
/*! |
| 48 |
If a button is clicked, its triggered() signal will automatically be called. |
| 49 |
*/ |
| 50 |
onClicked: button.trigger() |
| 51 |
|
| 52 |
Keys.onEnterPressed: clicked() |
| 53 |
Keys.onReturnPressed: clicked() |
| 54 |
|
| 55 |
/*! |
| 56 |
This handler is called when there is a long press. |
| 57 |
*/ |
| 58 |
signal pressAndHold() |
| 59 |
|
| 60 |
/*! |
| 61 |
True if the user presses a mouse button in the button's mouse area. |
| 62 |
*/ |
| 63 |
property bool pressed: mouseArea.pressed |
| 64 |
|
| 65 |
/*! |
| 66 |
True if the mouse cursor hovers over the button's mouse area. |
| 67 |
*/ |
| 68 |
property bool hovered: __acceptEvents && mouseArea.containsMouse |
| 69 |
|
| 70 |
/*! |
| 71 |
\internal |
| 72 |
Disable or enable signal emition by default. |
| 73 |
Some classes want to emit the signal by themselves (ListItem.Standard) |
| 74 |
*/ |
| 75 |
property bool __acceptEvents: true |
| 76 |
|
| 77 |
/*! |
| 78 |
\internal |
| 79 |
To get the properties of the mouse area in subclasses. |
| 80 |
*/ |
| 81 |
property alias __mouseArea: mouseArea |
| 82 |
|
| 83 |
activeFocusOnPress: true |
| 84 |
|
| 85 |
MouseArea { |
| 86 |
id: mouseArea |
| 87 |
anchors.fill: parent |
| 88 |
// if mouseArea is given a new value, disable defaultMouseArea |
| 89 |
// as it might occlude the newly assigned mouse area. |
| 90 |
hoverEnabled: true |
| 91 |
|
| 92 |
// invoke Haptics singleton earlier than we press the button, |
| 93 |
// so we give some time for the singleton to sync settings with the service |
| 94 |
property bool hapticsEnabled: Haptics.enabled |
| 95 |
|
| 96 |
onClicked: { |
| 97 |
if (button.__acceptEvents) { |
| 98 |
// FIXME (Vivid) call this in the style rather than from AbstractButton |
| 99 |
Haptics.play(); |
| 100 |
button.clicked() |
| 101 |
} |
| 102 |
} |
| 103 |
onPressAndHold: { |
| 104 |
if (button.__acceptEvents) { |
| 105 |
button.pressAndHold() |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |