blob: 420b581546a33be0ce56a8a210fdae9de6a812cd [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
31 */
32Button {
33 id: hoverableButton
34
Sébastien Blin8940f3c2020-07-23 17:03:11 -040035 checkable: true
36 checked: false
37
Sébastien Blin1f915762020-08-03 13:27:42 -040038 property int fontPointSize: 9
39 property int buttonImageHeight: hoverableButtonBackground.height - 10
40 property int buttonImageWidth: hoverableButtonBackground.width - 10
41
42 property string backgroundColor: JamiTheme.releaseColor
43 property string onPressColor: JamiTheme.pressColor
44 property string onReleaseColor: JamiTheme.releaseColor
45 property string onEnterColor: JamiTheme.hoverColor
46 property string onExitColor: JamiTheme.releaseColor
47
48 property alias radius: hoverableButtonBackground.radius
49 property alias source: hoverableButtonImage.source
Sébastien Blin8940f3c2020-07-23 17:03:11 -040050 property var checkedImage: null
51 property var baseImage: null
52 property var checkedColor: null
53 property var baseColor: null
54 property alias color: hoverableButton.baseColor
Sébastien Blin1f915762020-08-03 13:27:42 -040055
Yang Wangb97bde42020-08-07 11:24:18 -040056 property string toolTipText: ""
57
Sébastien Blin1f915762020-08-03 13:27:42 -040058 font.pointSize: fontPointSize
59
60 hoverEnabled: true
61
Yang Wangb97bde42020-08-07 11:24:18 -040062 ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
63 ToolTip.visible: hovered && (toolTipText.length > 0)
64 ToolTip.text: toolTipText
65
Sébastien Blin1f915762020-08-03 13:27:42 -040066 background: Rectangle {
67 id: hoverableButtonBackground
68
69 color: backgroundColor
70
71 Image {
72 id: hoverableButtonImage
73
74 anchors.centerIn: hoverableButtonBackground
75
76 height: buttonImageHeight
77 width: buttonImageWidth
78
79 fillMode: Image.PreserveAspectFit
80 mipmap: true
81 asynchronous: true
Sébastien Blin8940f3c2020-07-23 17:03:11 -040082
83 source: hoverableButton.checked && checkedImage? checkedImage : baseImage
84
85 layer {
86 enabled: true
87 effect: ColorOverlay {
88 id: overlay
89 color: hoverableButton.checked && checkedColor?
90 checkedColor :
91 (baseColor? baseColor : "transparent")
92 }
93 }
Sébastien Blin1f915762020-08-03 13:27:42 -040094 }
95
96 MouseArea {
97 anchors.fill: parent
98
Yang Wangb97bde42020-08-07 11:24:18 -040099 hoverEnabled: hoverableButton.hoverEnabled
Sébastien Blin1f915762020-08-03 13:27:42 -0400100
101 onPressed: {
102 hoverableButtonBackground.color = onPressColor
103 }
104 onReleased: {
105 hoverableButtonBackground.color = onReleaseColor
Sébastien Blin8940f3c2020-07-23 17:03:11 -0400106 hoverableButton.toggle()
Sébastien Blin1f915762020-08-03 13:27:42 -0400107 hoverableButton.clicked()
108 }
109 onEntered: {
110 hoverableButtonBackground.color = onEnterColor
111 }
112 onExited: {
113 hoverableButtonBackground.color = onExitColor
114 }
115 }
116 }
117}