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 | Button { |
| 24 | id: tintedButton |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * TintColor, color for the pixmap when button is hovered. |
| 29 | */ |
| 30 | property string tintColor: "white" |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * NormalPixmapSource - icons in normal state (non-toggled). |
| 35 | * SelectedPixmapSource - icons once button is toggled. |
| 36 | */ |
| 37 | property string normalPixmapSource: "" |
| 38 | property string selectedPixmapSource: "" |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | * IsSelected property is to help set button current state manually. |
| 43 | */ |
| 44 | property bool isSelected: false |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * ButtonEntered signal is to help call overlay change its opacity |
| 49 | */ |
| 50 | signal buttonEntered |
| 51 | |
| 52 | function setChecked(checked) { |
| 53 | isSelected = checked |
| 54 | if (isSelected) { |
| 55 | tintedButtonImage.source = selectedPixmapSource |
| 56 | } else { |
| 57 | tintedButtonImage.source = normalPixmapSource |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | hoverEnabled: true |
| 62 | |
| 63 | background: Rectangle { |
| 64 | id: tintedButtonBackground |
| 65 | |
| 66 | radius: 30 |
| 67 | color: "transparent" |
| 68 | |
| 69 | Image { |
| 70 | id: tintedButtonImage |
| 71 | |
| 72 | anchors.centerIn: tintedButtonBackground |
| 73 | |
| 74 | height: tintedButtonBackground.height - 10 |
| 75 | width: tintedButtonBackground.width - 10 |
| 76 | |
| 77 | source: normalPixmapSource |
| 78 | fillMode: Image.PreserveAspectFit |
| 79 | mipmap: true |
| 80 | asynchronous: true |
| 81 | } |
| 82 | |
| 83 | MouseArea { |
| 84 | anchors.fill: parent |
| 85 | |
| 86 | hoverEnabled: true |
| 87 | |
| 88 | onReleased: { |
| 89 | isSelected = !isSelected |
| 90 | if (isSelected) { |
| 91 | tintedButtonImage.source = "image://tintedPixmap/" |
| 92 | + selectedPixmapSource.replace( |
| 93 | "qrc:/images/icons/", "") + "+" + tintColor |
| 94 | } else { |
| 95 | tintedButtonImage.source = "image://tintedPixmap/" + normalPixmapSource.replace( |
| 96 | "qrc:/images/icons/", "") + "+" + tintColor |
| 97 | } |
| 98 | tintedButton.clicked() |
| 99 | } |
| 100 | onEntered: { |
| 101 | |
| 102 | |
| 103 | /* |
| 104 | * Tinted. |
| 105 | */ |
| 106 | if (isSelected) { |
| 107 | tintedButtonImage.source = "image://tintedPixmap/" |
| 108 | + selectedPixmapSource.replace( |
| 109 | "qrc:/images/icons/", "") + "+" + tintColor |
| 110 | } else { |
| 111 | tintedButtonImage.source = "image://tintedPixmap/" + normalPixmapSource.replace( |
| 112 | "qrc:/images/icons/", "") + "+" + tintColor |
| 113 | } |
| 114 | tintedButton.buttonEntered() |
| 115 | } |
| 116 | onExited: { |
| 117 | if (isSelected) { |
| 118 | tintedButtonImage.source = selectedPixmapSource |
| 119 | } else { |
| 120 | tintedButtonImage.source = normalPixmapSource |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |