1/* This file is part of the KDE project 2 * Copyright (C) 2012 Arjen Hiemstra <ahiemstra@heimr.nl> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, 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 General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 */ 18 19import QtQuick 2.0 20import org.calligra 1.0 21 22Item { 23 id: base; 24 25 signal clicked(); 26 27 property alias image: icon.source; 28 property int imageMargin: 8; 29 property color color: Settings.theme.color("components/button/base"); 30 property alias border: fill.border; 31 property alias radius: fill.radius; 32 property alias text: label.text; 33 property color textColor: Settings.theme.color("components/button/text"); 34 property alias textSize: label.font.pixelSize; 35 property alias bold: label.font.bold; 36 property alias font: label.font; 37 property bool shadow: false; 38 property bool enabled: true; // XXX: visualize disabledness 39 property alias asynchronous: icon.asynchronous; 40 41 property bool highlight: false; 42 property color highlightColor: Settings.theme.color("components/button/highlight"); 43 property double highlightOpacity: 1; 44 45 property bool checkable: false; 46 property bool checked: false; 47 property int checkedMargin: 2; 48 property color checkedColor: Settings.theme.color("components/button/checked"); 49 property double checkedOpacity: 1; 50 51 property bool hasFocus: false; 52 53 property string tooltip: ""; 54 55 width: Constants.GridWidth; 56 height: Constants.GridHeight; 57 58 Rectangle { 59 id: fill; 60 anchors.fill: parent; 61 anchors.margins: 0; 62 color: "transparent"; 63 visible: true 64 65 Rectangle { 66 anchors.fill: parent; 67 radius: parent.radius; 68 opacity: base.highlightOpacity; 69 color: base.highlight && mouse.pressed && base.enabled ? base.highlightColor : base.color; 70 } 71 72 Rectangle { 73 anchors { 74 left: parent.left; 75 right: parent.right; 76 bottom: parent.bottom; 77 margins: fill.radius / 2; 78 } 79 height: fill.radius / 2; 80 radius: fill.radius / 4; 81 color: base.textColor; 82 visible: base.hasFocus; 83 opacity: 0.3 84 } 85 86 Rectangle { 87 id: checkedVisualiser; 88 opacity: base.checked ? base.checkedOpacity : 0; 89 Behavior on opacity { NumberAnimation { duration: Constants.AnimationDuration; } } 90 anchors.fill: parent; 91 anchors.margins: base.checkedMargin; 92 color: base.checkedColor; 93 radius: fill.radius;//base.height === base.width ? base.height / 2 - 1 : base.radius; 94 } 95 96 Image { 97 id: icon; 98 anchors.left: (label.text == "") ? undefined : parent.left; 99 anchors.horizontalCenter: (label.text == "") ? parent.horizontalCenter : undefined; 100 anchors.top: parent.top; 101 anchors.bottom: parent.bottom; 102 anchors.margins: base.imageMargin; 103 width: height; 104 fillMode: Image.PreserveAspectFit; 105 smooth: true; 106 opacity: base.enabled ? 1 : 0.7; 107 Behavior on opacity { NumberAnimation { duration: Constants.AnimationDuration; } } 108 109 sourceSize.width: width > height ? height : width; 110 sourceSize.height: width > height ? height : width; 111 } 112 113 Label { 114 id: label; 115 anchors.verticalCenter: parent.verticalCenter; 116 height: font.pixelSize; 117 width: parent.width; 118 horizontalAlignment: Text.AlignHCenter; 119 elide: Text.ElideRight; 120 opacity: base.enabled ? 1 : 0.7; 121 color: base.textColor; 122 visible: (icon.source == "") 123 } 124 Label { 125 id: otherLabel; 126 anchors.left: icon.right; 127 anchors.leftMargin: Constants.DefaultMargin; 128 anchors.verticalCenter: parent.verticalCenter; 129 text: label.text; 130 font: label.font; 131 height: font.pixelSize; 132 width: parent.width - icon.width; 133 elide: label.elide; 134 opacity: base.enabled ? 1 : 0.7; 135 color: base.textColor; 136 visible: (icon.source != "") 137 } 138// Rectangle { 139// id: enabledVisualiser; 140// opacity: base.enabled ? 0 : 0.7; 141// anchors.fill: parent; 142// color: "black"; 143// } 144 } 145 146 SimpleTouchArea { 147 anchors.fill: parent; 148 onTouched: { 149 if (base.enabled) { 150 base.clicked(); 151 if ( base.checkable ) { 152 base.checked = !base.checked; 153 } 154 } 155 } 156 } 157 MouseArea { 158 id: mouse; 159 anchors.fill: parent; 160 hoverEnabled: true; 161 acceptedButtons: Qt.LeftButton | Qt.RightButton; 162 163 onClicked: { 164 if(mouse.button == Qt.LeftButton && base.enabled) { 165 base.clicked(); 166 if ( base.checkable ) { 167 base.checked = !base.checked; 168 } 169 } else if(mouse.button == Qt.RightButton && base.tooltip != "") { 170 tooltip.show(base.width / 2, 0); 171 } 172 } 173 onEntered: { 174 hoverDelayTimer.start(); 175 } 176 onPositionChanged: { 177 if(hoverDelayTimer.running) { 178 hoverDelayTimer.restart(); 179 } 180 } 181 onExited: { 182 hoverDelayTimer.stop(); 183 tooltip.hide(); 184 } 185 } 186 187 Timer { 188 id: hoverDelayTimer; 189 interval: 1000; 190 onTriggered: { if(base.tooltip != "") tooltip.show(base.width / 2, 0) } 191 } 192 193 Tooltip { 194 id: tooltip; 195 text: base.tooltip; 196 } 197 198 states: State { 199 name: "pressed"; 200 when: (mouse.pressed || base.checked) && enabled; 201 202 PropertyChanges { 203 target: mouse 204 anchors.topMargin: 0 205 } 206 } 207 208 transitions: Transition { 209 ParallelAnimation { 210 NumberAnimation { properties: "size"; duration: 50; } 211 ColorAnimation { duration: 50; } 212 } 213 } 214} 215