blob: 0668487b9e55effe96ab7b86e9dc45f2d2b20747 [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001
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 */
19import QtQuick 2.14
20import QtQuick.Controls 2.14
21import net.jami.Models 1.0
Sébastien Blin8940f3c2020-07-23 17:03:11 -040022import QtGraphicalEffects 1.15
Sébastien Blin1f915762020-08-03 13:27:42 -040023
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 Zhang44dba712020-08-25 14:32:34 -040031 *
32 * Note: if use text property directly, buttonTextColor will not work.
Sébastien Blin1f915762020-08-03 13:27:42 -040033 */
34Button {
35 id: hoverableButton
36
Sébastien Blin8940f3c2020-07-23 17:03:11 -040037 checkable: true
38 checked: false
39
Sébastien Blin1f915762020-08-03 13:27:42 -040040 property int fontPointSize: 9
41 property int buttonImageHeight: hoverableButtonBackground.height - 10
42 property int buttonImageWidth: hoverableButtonBackground.width - 10
43
Ming Rui Zhang44dba712020-08-25 14:32:34 -040044 property string buttonText: ""
45 property string buttonTextColor: "black"
46
Sébastien Blin1f915762020-08-03 13:27:42 -040047 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 Blinc75335f2020-08-04 20:54:02 -040055 property var checkedImage: ""
56 property var baseImage: ""
Sébastien Blin8940f3c2020-07-23 17:03:11 -040057 property var checkedColor: null
58 property var baseColor: null
59 property alias color: hoverableButton.baseColor
Yang Wangb97bde42020-08-07 11:24:18 -040060 property string toolTipText: ""
61
Sébastien Blin1f915762020-08-03 13:27:42 -040062 font.pointSize: fontPointSize
63
64 hoverEnabled: true
65
Ming Rui Zhang44dba712020-08-25 14:32:34 -040066 text: "<font color=" + "'" + buttonTextColor + "'>" + buttonText + "</font>"
67
Yang Wangb97bde42020-08-07 11:24:18 -040068 ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
69 ToolTip.visible: hovered && (toolTipText.length > 0)
70 ToolTip.text: toolTipText
71
Sébastien Blin1f915762020-08-03 13:27:42 -040072 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 Blin8940f3c2020-07-23 17:03:11 -040088
Ming Rui Zhang44dba712020-08-25 14:32:34 -040089 source: {
90 if (checkable && checkedImage)
91 return hoverableButton.checked ? checkedImage : baseImage
92 else
93 return ""
94 }
Sébastien Blin8940f3c2020-07-23 17:03:11 -040095
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 Blin1f915762020-08-03 13:27:42 -0400105 }
106
107 MouseArea {
108 anchors.fill: parent
109
Yang Wangb97bde42020-08-07 11:24:18 -0400110 hoverEnabled: hoverableButton.hoverEnabled
Sébastien Blin1f915762020-08-03 13:27:42 -0400111
112 onPressed: {
113 hoverableButtonBackground.color = onPressColor
114 }
115 onReleased: {
116 hoverableButtonBackground.color = onReleaseColor
Sébastien Blin8940f3c2020-07-23 17:03:11 -0400117 hoverableButton.toggle()
Sébastien Blin1f915762020-08-03 13:27:42 -0400118 hoverableButton.clicked()
119 }
120 onEntered: {
121 hoverableButtonBackground.color = onEnterColor
122 }
123 onExited: {
124 hoverableButtonBackground.color = onExitColor
125 }
126 }
127 }
128}