blob: 4e96677d29d3773fccbc58e9f6f8ed04f7cc1608 [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 Blinc75335f2020-08-04 20:54:02 -040050 property var checkedImage: ""
51 property var baseImage: ""
Sébastien Blin8940f3c2020-07-23 17:03:11 -040052 property var checkedColor: null
53 property var baseColor: null
54 property alias color: hoverableButton.baseColor
Yang Wangb97bde42020-08-07 11:24:18 -040055 property string toolTipText: ""
56
Sébastien Blin1f915762020-08-03 13:27:42 -040057 font.pointSize: fontPointSize
58
59 hoverEnabled: true
60
Yang Wangb97bde42020-08-07 11:24:18 -040061 ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
62 ToolTip.visible: hovered && (toolTipText.length > 0)
63 ToolTip.text: toolTipText
64
Sébastien Blin1f915762020-08-03 13:27:42 -040065 background: Rectangle {
66 id: hoverableButtonBackground
67
68 color: backgroundColor
69
70 Image {
71 id: hoverableButtonImage
72
73 anchors.centerIn: hoverableButtonBackground
74
75 height: buttonImageHeight
76 width: buttonImageWidth
77
78 fillMode: Image.PreserveAspectFit
79 mipmap: true
80 asynchronous: true
Sébastien Blin8940f3c2020-07-23 17:03:11 -040081
82 source: hoverableButton.checked && checkedImage? checkedImage : baseImage
83
84 layer {
85 enabled: true
86 effect: ColorOverlay {
87 id: overlay
88 color: hoverableButton.checked && checkedColor?
89 checkedColor :
90 (baseColor? baseColor : "transparent")
91 }
92 }
Sébastien Blin1f915762020-08-03 13:27:42 -040093 }
94
95 MouseArea {
96 anchors.fill: parent
97
Yang Wangb97bde42020-08-07 11:24:18 -040098 hoverEnabled: hoverableButton.hoverEnabled
Sébastien Blin1f915762020-08-03 13:27:42 -040099
100 onPressed: {
101 hoverableButtonBackground.color = onPressColor
102 }
103 onReleased: {
104 hoverableButtonBackground.color = onReleaseColor
Sébastien Blin8940f3c2020-07-23 17:03:11 -0400105 hoverableButton.toggle()
Sébastien Blin1f915762020-08-03 13:27:42 -0400106 hoverableButton.clicked()
107 }
108 onEntered: {
109 hoverableButtonBackground.color = onEnterColor
110 }
111 onExited: {
112 hoverableButtonBackground.color = onExitColor
113 }
114 }
115 }
116}