Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2020 by Savoir-faire Linux |
| 4 | * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | import QtQuick 2.14 |
| 20 | import QtQuick.Controls 2.14 |
| 21 | import net.jami.Models 1.0 |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 22 | import QtGraphicalEffects 1.15 |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /* |
| 26 | * HoverableButton contains the following configurable properties: |
| 27 | * 1. Color changes on different button state |
| 28 | * 2. Radius control (rounded) |
| 29 | * 3. Text content or image content |
| 30 | * 4. Can use OnClicked slot to implement some click logic |
Ming Rui Zhang | 44dba71 | 2020-08-25 14:32:34 -0400 | [diff] [blame] | 31 | * |
| 32 | * Note: if use text property directly, buttonTextColor will not work. |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 33 | */ |
| 34 | Button { |
| 35 | id: hoverableButton |
| 36 | |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 37 | checkable: true |
| 38 | checked: false |
| 39 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 40 | property int fontPointSize: 9 |
| 41 | property int buttonImageHeight: hoverableButtonBackground.height - 10 |
| 42 | property int buttonImageWidth: hoverableButtonBackground.width - 10 |
| 43 | |
Ming Rui Zhang | 44dba71 | 2020-08-25 14:32:34 -0400 | [diff] [blame] | 44 | property string buttonText: "" |
| 45 | property string buttonTextColor: "black" |
| 46 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 47 | property string backgroundColor: JamiTheme.releaseColor |
| 48 | property string onPressColor: JamiTheme.pressColor |
| 49 | property string onReleaseColor: JamiTheme.releaseColor |
| 50 | property string onEnterColor: JamiTheme.hoverColor |
| 51 | property string onExitColor: JamiTheme.releaseColor |
| 52 | |
| 53 | property alias radius: hoverableButtonBackground.radius |
| 54 | property alias source: hoverableButtonImage.source |
Sébastien Blin | c75335f | 2020-08-04 20:54:02 -0400 | [diff] [blame] | 55 | property var checkedImage: "" |
| 56 | property var baseImage: "" |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 57 | property var checkedColor: null |
| 58 | property var baseColor: null |
| 59 | property alias color: hoverableButton.baseColor |
Yang Wang | b97bde4 | 2020-08-07 11:24:18 -0400 | [diff] [blame] | 60 | property string toolTipText: "" |
| 61 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 62 | font.pointSize: fontPointSize |
| 63 | |
| 64 | hoverEnabled: true |
| 65 | |
Ming Rui Zhang | 44dba71 | 2020-08-25 14:32:34 -0400 | [diff] [blame] | 66 | text: "<font color=" + "'" + buttonTextColor + "'>" + buttonText + "</font>" |
| 67 | |
Yang Wang | b97bde4 | 2020-08-07 11:24:18 -0400 | [diff] [blame] | 68 | ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval |
| 69 | ToolTip.visible: hovered && (toolTipText.length > 0) |
| 70 | ToolTip.text: toolTipText |
| 71 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 72 | background: Rectangle { |
| 73 | id: hoverableButtonBackground |
| 74 | |
| 75 | color: backgroundColor |
| 76 | |
| 77 | Image { |
| 78 | id: hoverableButtonImage |
| 79 | |
| 80 | anchors.centerIn: hoverableButtonBackground |
| 81 | |
| 82 | height: buttonImageHeight |
| 83 | width: buttonImageWidth |
| 84 | |
| 85 | fillMode: Image.PreserveAspectFit |
| 86 | mipmap: true |
| 87 | asynchronous: true |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 88 | |
Ming Rui Zhang | 44dba71 | 2020-08-25 14:32:34 -0400 | [diff] [blame] | 89 | source: { |
| 90 | if (checkable && checkedImage) |
| 91 | return hoverableButton.checked ? checkedImage : baseImage |
| 92 | else |
| 93 | return "" |
| 94 | } |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 95 | |
| 96 | layer { |
| 97 | enabled: true |
| 98 | effect: ColorOverlay { |
| 99 | id: overlay |
| 100 | color: hoverableButton.checked && checkedColor? |
| 101 | checkedColor : |
| 102 | (baseColor? baseColor : "transparent") |
| 103 | } |
| 104 | } |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | MouseArea { |
| 108 | anchors.fill: parent |
| 109 | |
Yang Wang | b97bde4 | 2020-08-07 11:24:18 -0400 | [diff] [blame] | 110 | hoverEnabled: hoverableButton.hoverEnabled |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 111 | |
| 112 | onPressed: { |
| 113 | hoverableButtonBackground.color = onPressColor |
| 114 | } |
| 115 | onReleased: { |
| 116 | hoverableButtonBackground.color = onReleaseColor |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 117 | hoverableButton.toggle() |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 118 | hoverableButton.clicked() |
| 119 | } |
| 120 | onEntered: { |
| 121 | hoverableButtonBackground.color = onEnterColor |
| 122 | } |
| 123 | onExited: { |
| 124 | hoverableButtonBackground.color = onExitColor |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |