blob: 23225f783d13ce982e2d4c25ce33bdf5af5efc32 [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 QtQuick.Layouts 1.14
22import net.jami.Models 1.0
23
24import "../../commoncomponents"
25
26ComboBox {
27 id: accountComboBox
28
29 signal accountChanged(int index)
30 signal needToBackToWelcomePage(int index)
31 signal needToUpdateSmartList(string accountId)
32 signal newAccountButtonClicked
33 signal settingBtnClicked
34
35 currentIndex: 0
36
37 function backToWelcomePage(index) {
38 accountComboBox.needToBackToWelcomePage(index)
39 }
40
41 function updateSmartList(accountId) {
42 accountComboBox.needToUpdateSmartList(accountId)
43 }
44
45
46 /*
47 * Refresh every item in accountListModel.
48 */
49 function updateAccountListModel() {
50 accountListModel.dataChanged(accountListModel.index(0, 0),
51 accountListModel.index(
52 accountListModel.rowCount() - 1, 0))
53 }
54
55
56 /*
57 * Reset accountListModel.
58 */
59 function resetAccountListModel() {
60 accountListModel.reset()
61 }
62
63 Image {
64 id: userImageRoot
65
66 anchors.left: accountComboBox.left
67 anchors.leftMargin: 5
68 anchors.verticalCenter: accountComboBox.verticalCenter
69
70 width: accountComboBox.height - 10
71 height: accountComboBox.height - 10
72
73 fillMode: Image.PreserveAspectFit
74
75
76 /*
77 * Base 64 format
78 */
79 source: {
80 if (currentIndex !== -1)
81 return "data:image/png;base64," + accountListModel.data(
82 accountListModel.index(
83 accountComboBox.currentIndex, 0), 259)
84 else
85 return source
86 }
87 mipmap: true
88
89 Rectangle {
90 id: presenseRect
91
92 anchors.right: userImageRoot.right
93 anchors.rightMargin: 1
94 anchors.bottom: userImageRoot.bottom
95 anchors.bottomMargin: 2
96
97 width: 14
98 height: 14
99
100
101 /*
102 * Visible when account is registered, enum REGISTERED == 5.
103 */
104 visible: {
105 if (currentIndex !== -1)
106 return accountListModel.data(
107 accountListModel.index(
108 accountComboBox.currentIndex, 0), 261) === 5
109 else
110 return visible
111 }
112
113 Rectangle {
114 id: presenseCycle
115
116 anchors.centerIn: presenseRect
117
118 width: 10
119 height: 10
120
121 radius: 30
122 color: JamiTheme.presenceGreen
123 }
124
125 radius: 30
126 color: "white"
127 }
128 }
129
130 Text {
131 id: textUserAliasRoot
132
133 anchors.left: userImageRoot.right
134 anchors.leftMargin: 10
135 anchors.top: rootItemBackground.top
136 anchors.topMargin: 5
137
138 text: textMetricsUserAliasRoot.elidedText
139 font.pointSize: JamiTheme.textFontSize
140 }
141
142 Text {
143 id: textUsernameRoot
144
145 anchors.left: userImageRoot.right
146 anchors.leftMargin: 10
147 anchors.top: textUserAliasRoot.bottom
148 anchors.topMargin: 5
149
150 text: textMetricsUsernameRoot.elidedText
151 font.pointSize: JamiTheme.textFontSize
152 color: JamiTheme.faddedFontColor
153 }
154
155 TextMetrics {
156 id: textMetricsUserAliasRoot
157
158 font: textUserAliasRoot.font
159 elide: Text.ElideMiddle
160 elideWidth: accountComboBox.width - userImageRoot.width - settingsButton.width - 25
161
162
163 /*
164 * Role::Alias
165 */
166 text: {
167 if (currentIndex !== -1)
168 return accountListModel.data(accountListModel.index(
169 accountComboBox.currentIndex,
170 0), 257)
171 else
172 return text
173 }
174 }
175
176 TextMetrics {
177 id: textMetricsUsernameRoot
178
179 font: textUsernameRoot.font
180 elide: Text.ElideMiddle
181 elideWidth: accountComboBox.width - userImageRoot.width - settingsButton.width - 25
182
183
184 /*
185 * Role::Username
186 */
187 text: {
188 if (currentIndex !== -1)
189 return accountListModel.data(accountListModel.index(
190 accountComboBox.currentIndex,
191 0), 258)
192 else
193 return text
194 }
195 }
196
197 HoverableButton {
198 id: settingsButton
199
200 anchors.right: accountComboBox.right
201 anchors.rightMargin: 10
202 anchors.verticalCenter: accountComboBox.verticalCenter
203
204 buttonImageHeight: height - 8
205 buttonImageWidth: width - 8
206 source: "qrc:/images/icons/round-settings-24px.svg"
207 radius: height / 2
208 width: 25
209 height: 25
210
211 onClicked: {
212 settingBtnClicked()
213 }
214 }
215
216 background: Rectangle {
217 id: rootItemBackground
218
219 implicitWidth: accountComboBox.width
220 implicitHeight: accountComboBox.height
221
222 border.width: 0
223 }
224
225 MouseArea {
226 id: comboBoxRootMouseArea
227
228 anchors.fill: parent
229
230 hoverEnabled: true
231 propagateComposedEvents: true
232
233 onPressed: {
234 if (isMouseOnSettingsButton(mouse)) {
235 settingsButton.backgroundColor = JamiTheme.pressColor
236 settingsButton.clicked()
237 } else
238 rootItemBackground.color = JamiTheme.pressColor
239 }
240 onReleased: {
241 if (isMouseOnSettingsButton(mouse)) {
242 settingsButton.backgroundColor = JamiTheme.releaseColor
243 } else {
244 rootItemBackground.color = JamiTheme.releaseColor
245 if (comboBoxPopup.opened) {
246 accountComboBox.popup.close()
247 } else {
248 accountComboBox.popup.open()
249 }
250 }
251 }
252 onEntered: {
253 rootItemBackground.color = JamiTheme.hoverColor
254 }
255 onExited: {
256 rootItemBackground.color = "white"
257 }
258 onMouseXChanged: {
259
260
261 /*
262 * Manually making settings button hover.
263 */
264 if (isMouseOnSettingsButton(mouse)) {
265 settingsButton.backgroundColor = JamiTheme.hoverColor
266 rootItemBackground.color = "white"
267 } else {
268 settingsButton.backgroundColor = "white"
269 rootItemBackground.color = JamiTheme.hoverColor
270 }
271 }
272 function isMouseOnSettingsButton(mouse) {
273 var mousePos = mapToItem(comboBoxRootMouseArea, mouse.x, mouse.y)
274 var settingsButtonPos = mapToItem(comboBoxRootMouseArea,
275 settingsButton.x,
276 settingsButton.y)
277 if ((mousePos.x >= settingsButtonPos.x
278 && mousePos.x <= settingsButtonPos.x + settingsButton.width)
279 && (mousePos.y >= settingsButtonPos.y
280 && mousePos.y <= settingsButtonPos.y + settingsButton.height))
281 return true
282 return false
283 }
284 }
285
286 indicator: null
287
288
289 /*
290 * Overwrite the combo box pop up to add footer (for add accounts).
291 */
292 popup: AccountComboBoxPopup {
293 id: comboBoxPopup
294
295 onAccountNeedToChange: {
296 accountComboBox.accountChanged(index)
297 }
298
299 onNewAccountButtonClicked: {
300 accountComboBox.newAccountButtonClicked()
301 }
302 }
303}