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 |
| 22 | |
| 23 | |
| 24 | /* |
| 25 | * HoverableButton containes functionalites: |
| 26 | * 1. Color changes on different button state |
| 27 | * 2. Radius control (rounded) |
| 28 | * 3. Text content or image content |
| 29 | * 4. Can use OnClicked slot to implement some click logic |
| 30 | */ |
| 31 | Button { |
| 32 | id: hoverableButton |
| 33 | |
| 34 | property int fontPointSize: 9 |
| 35 | property int buttonImageHeight: hoverableButtonBackground.height - 10 |
| 36 | property int buttonImageWidth: hoverableButtonBackground.width - 10 |
| 37 | |
| 38 | property string backgroundColor: JamiTheme.releaseColor |
| 39 | property string onPressColor: JamiTheme.pressColor |
| 40 | property string onReleaseColor: backgroundColor |
| 41 | property string onEnterColor: JamiTheme.hoverColor |
| 42 | property string onExitColor: backgroundColor |
| 43 | property string onDisabledBackgroundColor: backgroundColor |
| 44 | property string textColor: "black" |
| 45 | |
| 46 | property alias radius: hoverableButtonBackground.radius |
| 47 | property alias source: hoverableButtonImage.source |
| 48 | |
| 49 | font.pointSize: fontPointSize |
| 50 | font.kerning: true |
| 51 | |
| 52 | hoverEnabled: true |
| 53 | |
| 54 | contentItem: Text { |
| 55 | text: hoverableButton.text |
| 56 | font: hoverableButton.font |
| 57 | opacity: enabled ? 1.0 : 0.3 |
| 58 | color: textColor |
| 59 | horizontalAlignment: Text.AlignHCenter |
| 60 | verticalAlignment: Text.AlignVCenter |
| 61 | elide: Text.ElideRight |
| 62 | } |
| 63 | |
| 64 | background: Rectangle { |
| 65 | id: hoverableButtonBackground |
| 66 | |
| 67 | color: hoverableButton.enabled ? backgroundColor:onDisabledBackgroundColor |
| 68 | |
| 69 | Image { |
| 70 | id: hoverableButtonImage |
| 71 | |
| 72 | anchors.centerIn: hoverableButtonBackground |
| 73 | |
| 74 | height: buttonImageHeight |
| 75 | width: buttonImageWidth |
| 76 | |
| 77 | fillMode: Image.PreserveAspectFit |
| 78 | mipmap: true |
| 79 | asynchronous: true |
| 80 | } |
| 81 | |
| 82 | MouseArea { |
| 83 | enabled: hoverableButton.enabled |
| 84 | anchors.fill: parent |
| 85 | |
| 86 | hoverEnabled: true |
| 87 | |
| 88 | onPressed: { |
| 89 | hoverableButtonBackground.color = onPressColor |
| 90 | } |
| 91 | onReleased: { |
| 92 | hoverableButtonBackground.color = onReleaseColor |
| 93 | hoverableButton.clicked() |
| 94 | } |
| 95 | onEntered: { |
| 96 | hoverableButtonBackground.color = onEnterColor |
| 97 | } |
| 98 | onExited: { |
| 99 | hoverableButtonBackground.color = onExitColor |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |