blob: 6037d436f8609bb4d1c9e6f08c067b2e21c7ad7d [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)
ababi69f5dfc2020-08-25 15:07:57 +020030 signal needToBackToWelcomePage()
Sébastien Blin1f915762020-08-03 13:27:42 -040031 signal newAccountButtonClicked
32 signal settingBtnClicked
33
34 currentIndex: 0
35
ababi69f5dfc2020-08-25 15:07:57 +020036 function backToWelcomePage() {
37 needToBackToWelcomePage()
Sébastien Blin1f915762020-08-03 13:27:42 -040038 }
39
ababi69f5dfc2020-08-25 15:07:57 +020040 // Refresh every item in accountListModel.
Sébastien Blin1f915762020-08-03 13:27:42 -040041 function updateAccountListModel() {
42 accountListModel.dataChanged(accountListModel.index(0, 0),
43 accountListModel.index(
44 accountListModel.rowCount() - 1, 0))
45 }
46
ababi69f5dfc2020-08-25 15:07:57 +020047 // Reset accountListModel.
Sébastien Blin1f915762020-08-03 13:27:42 -040048 function resetAccountListModel() {
49 accountListModel.reset()
50 }
51
52 Image {
53 id: userImageRoot
54
55 anchors.left: accountComboBox.left
ababidf651a22020-07-30 13:38:57 +020056 anchors.leftMargin: 16
Sébastien Blin1f915762020-08-03 13:27:42 -040057 anchors.verticalCenter: accountComboBox.verticalCenter
58
ababidf651a22020-07-30 13:38:57 +020059 width: 30
60 height: 30
Sébastien Blin1f915762020-08-03 13:27:42 -040061
62 fillMode: Image.PreserveAspectFit
63
ababi69f5dfc2020-08-25 15:07:57 +020064 // Base 64 format
Sébastien Blin1f915762020-08-03 13:27:42 -040065 source: {
66 if (currentIndex !== -1)
67 return "data:image/png;base64," + accountListModel.data(
68 accountListModel.index(
69 accountComboBox.currentIndex, 0), 259)
70 else
71 return source
72 }
73 mipmap: true
74
75 Rectangle {
76 id: presenseRect
77
78 anchors.right: userImageRoot.right
ababidf651a22020-07-30 13:38:57 +020079 anchors.rightMargin: -2
Sébastien Blin1f915762020-08-03 13:27:42 -040080 anchors.bottom: userImageRoot.bottom
ababidf651a22020-07-30 13:38:57 +020081 anchors.bottomMargin: -2
Sébastien Blin1f915762020-08-03 13:27:42 -040082
ababidf651a22020-07-30 13:38:57 +020083 width: 12
84 height: 12
Sébastien Blin1f915762020-08-03 13:27:42 -040085
ababi69f5dfc2020-08-25 15:07:57 +020086 // Visible when account is registered, enum REGISTERED == 5.
Sébastien Blin1f915762020-08-03 13:27:42 -040087 visible: {
88 if (currentIndex !== -1)
89 return accountListModel.data(
90 accountListModel.index(
91 accountComboBox.currentIndex, 0), 261) === 5
92 else
93 return visible
94 }
95
96 Rectangle {
97 id: presenseCycle
98
99 anchors.centerIn: presenseRect
100
101 width: 10
102 height: 10
103
104 radius: 30
105 color: JamiTheme.presenceGreen
106 }
107
108 radius: 30
ababidf651a22020-07-30 13:38:57 +0200109 color: JamiTheme.backgroundColor
Sébastien Blin1f915762020-08-03 13:27:42 -0400110 }
111 }
112
113 Text {
114 id: textUserAliasRoot
115
116 anchors.left: userImageRoot.right
ababidf651a22020-07-30 13:38:57 +0200117 anchors.leftMargin: 16
Sébastien Blin1f915762020-08-03 13:27:42 -0400118 anchors.top: rootItemBackground.top
ababidf651a22020-07-30 13:38:57 +0200119 anchors.topMargin: 16
Sébastien Blin1f915762020-08-03 13:27:42 -0400120
121 text: textMetricsUserAliasRoot.elidedText
122 font.pointSize: JamiTheme.textFontSize
123 }
124
ababidf651a22020-07-30 13:38:57 +0200125 Image {
Ming Rui Zhangc6b33692020-08-25 15:47:20 -0400126 id: arrowDropDown
127
ababidf651a22020-07-30 13:38:57 +0200128 anchors.left: textUserAliasRoot.right
129 anchors.verticalCenter: textUserAliasRoot.verticalCenter
130
131 width: 24
132 height: 24
133
134 fillMode: Image.PreserveAspectFit
135 mipmap: true
136 source: "qrc:/images/icons/round-arrow_drop_down-24px.svg"
137 }
138
Sébastien Blin1f915762020-08-03 13:27:42 -0400139 Text {
140 id: textUsernameRoot
141
142 anchors.left: userImageRoot.right
ababidf651a22020-07-30 13:38:57 +0200143 anchors.leftMargin: 16
Sébastien Blin1f915762020-08-03 13:27:42 -0400144 anchors.top: textUserAliasRoot.bottom
Sébastien Blin1f915762020-08-03 13:27:42 -0400145
146 text: textMetricsUsernameRoot.elidedText
147 font.pointSize: JamiTheme.textFontSize
ababidf651a22020-07-30 13:38:57 +0200148 color: JamiTheme.faddedLastInteractionFontColor
Sébastien Blin1f915762020-08-03 13:27:42 -0400149 }
150
151 TextMetrics {
152 id: textMetricsUserAliasRoot
153
154 font: textUserAliasRoot.font
ababidf651a22020-07-30 13:38:57 +0200155 elide: Text.ElideRight
Ming Rui Zhangc6b33692020-08-25 15:47:20 -0400156 elideWidth: accountComboBox.width - userImageRoot.width - settingsButton.width
157 - arrowDropDown.width - qrCodeGenerateButton.width - 55
Sébastien Blin1f915762020-08-03 13:27:42 -0400158
ababi69f5dfc2020-08-25 15:07:57 +0200159 // Role::Alias
Sébastien Blin1f915762020-08-03 13:27:42 -0400160 text: {
161 if (currentIndex !== -1)
162 return accountListModel.data(accountListModel.index(
163 accountComboBox.currentIndex,
164 0), 257)
165 else
166 return text
167 }
168 }
169
170 TextMetrics {
171 id: textMetricsUsernameRoot
172
173 font: textUsernameRoot.font
ababidf651a22020-07-30 13:38:57 +0200174 elide: Text.ElideRight
Ming Rui Zhangc6b33692020-08-25 15:47:20 -0400175 elideWidth: accountComboBox.width - userImageRoot.width - settingsButton.width
176 - qrCodeGenerateButton.width - 55
Sébastien Blin1f915762020-08-03 13:27:42 -0400177
178
ababi69f5dfc2020-08-25 15:07:57 +0200179
180 // Role::Username
Sébastien Blin1f915762020-08-03 13:27:42 -0400181 text: {
182 if (currentIndex !== -1)
183 return accountListModel.data(accountListModel.index(
184 accountComboBox.currentIndex,
185 0), 258)
186 else
187 return text
188 }
189 }
190
ababidf651a22020-07-30 13:38:57 +0200191 HoverableButton {
192 id: qrCodeGenerateButton
193
194 anchors.right: settingsButton.left
195 anchors.rightMargin: 10
196 anchors.verticalCenter: accountComboBox.verticalCenter
197
198 buttonImageHeight: height - 8
199 buttonImageWidth: width - 8
200 radius: height / 2
201 width: 24
202 height: 24
203
Yang Wangb97bde42020-08-07 11:24:18 -0400204 toolTipText: qsTr("Press to display QR code")
205 hoverEnabled: true
206
ababidf651a22020-07-30 13:38:57 +0200207 source: "qrc:/images/qrcode.png"
208 backgroundColor: "white"
209 onClicked: {
210 qrDialog.open()
211 }
212 }
213
Sébastien Blin1f915762020-08-03 13:27:42 -0400214 HoverableButton {
215 id: settingsButton
216
217 anchors.right: accountComboBox.right
218 anchors.rightMargin: 10
219 anchors.verticalCenter: accountComboBox.verticalCenter
220
221 buttonImageHeight: height - 8
222 buttonImageWidth: width - 8
Sébastien Blin1f915762020-08-03 13:27:42 -0400223 radius: height / 2
224 width: 25
225 height: 25
226
ababia284cae2020-08-10 12:33:34 +0200227 source: !mainViewWindow.inSettingsView ? "qrc:/images/icons/round-settings-24px.svg" :
228 "qrc:/images/icons/round-close-24px.svg"
Yang Wangb97bde42020-08-07 11:24:18 -0400229 toolTipText: !mainViewWindow.inSettingsView ?qsTr("Press to toggle to settings page") : qsTr("Press to toggle to call page")
230 hoverEnabled: true
231
ababidf651a22020-07-30 13:38:57 +0200232 backgroundColor: "white"
Sébastien Blin1f915762020-08-03 13:27:42 -0400233 onClicked: {
234 settingBtnClicked()
235 }
236 }
237
238 background: Rectangle {
239 id: rootItemBackground
240
241 implicitWidth: accountComboBox.width
242 implicitHeight: accountComboBox.height
ababidf651a22020-07-30 13:38:57 +0200243 color: JamiTheme.backgroundColor
Sébastien Blin1f915762020-08-03 13:27:42 -0400244 }
245
246 MouseArea {
247 id: comboBoxRootMouseArea
248
249 anchors.fill: parent
250
251 hoverEnabled: true
252 propagateComposedEvents: true
253
254 onPressed: {
ababidf651a22020-07-30 13:38:57 +0200255 if (isMouseOnButton(mouse, qrCodeGenerateButton)) {
256 qrCodeGenerateButton.backgroundColor = JamiTheme.pressColor
257 qrCodeGenerateButton.clicked()
258 }if (isMouseOnButton(mouse, settingsButton)) {
Sébastien Blin1f915762020-08-03 13:27:42 -0400259 settingsButton.backgroundColor = JamiTheme.pressColor
260 settingsButton.clicked()
ababidf651a22020-07-30 13:38:57 +0200261 } else {
Sébastien Blin1f915762020-08-03 13:27:42 -0400262 rootItemBackground.color = JamiTheme.pressColor
ababidf651a22020-07-30 13:38:57 +0200263 }
Sébastien Blin1f915762020-08-03 13:27:42 -0400264 }
ababidf651a22020-07-30 13:38:57 +0200265
Sébastien Blin1f915762020-08-03 13:27:42 -0400266 onReleased: {
ababidf651a22020-07-30 13:38:57 +0200267 if (isMouseOnButton(mouse, qrCodeGenerateButton)) {
268 qrCodeGenerateButton.backgroundColor = JamiTheme.releaseColor
269 } else if (isMouseOnButton(mouse, settingsButton)) {
Sébastien Blin1f915762020-08-03 13:27:42 -0400270 settingsButton.backgroundColor = JamiTheme.releaseColor
271 } else {
272 rootItemBackground.color = JamiTheme.releaseColor
273 if (comboBoxPopup.opened) {
274 accountComboBox.popup.close()
275 } else {
276 accountComboBox.popup.open()
277 }
278 }
279 }
280 onEntered: {
281 rootItemBackground.color = JamiTheme.hoverColor
282 }
283 onExited: {
ababidf651a22020-07-30 13:38:57 +0200284 rootItemBackground.color = JamiTheme.backgroundColor
Sébastien Blin1f915762020-08-03 13:27:42 -0400285 }
286 onMouseXChanged: {
ababi69f5dfc2020-08-25 15:07:57 +0200287
288 // Manually making button hover.
ababidf651a22020-07-30 13:38:57 +0200289 qrCodeGenerateButton.backgroundColor = (isMouseOnButton(mouse, qrCodeGenerateButton)) ?
290 JamiTheme.hoverColor : "white"
291
292 settingsButton.backgroundColor = (isMouseOnButton(mouse, settingsButton)) ?
293 JamiTheme.hoverColor : "white"
Sébastien Blin1f915762020-08-03 13:27:42 -0400294 }
ababidf651a22020-07-30 13:38:57 +0200295
296 function isMouseOnButton(mouse, button) {
Sébastien Blin1f915762020-08-03 13:27:42 -0400297 var mousePos = mapToItem(comboBoxRootMouseArea, mouse.x, mouse.y)
ababidf651a22020-07-30 13:38:57 +0200298 var qrButtonPos = mapToItem(comboBoxRootMouseArea,
299 button.x,
300 button.y)
301 if ((mousePos.x >= qrButtonPos.x
302 && mousePos.x <= qrButtonPos.x + button.width)
303 && (mousePos.y >= qrButtonPos.y
304 && mousePos.y <= qrButtonPos.y + button.height))
Sébastien Blin1f915762020-08-03 13:27:42 -0400305 return true
306 return false
307 }
308 }
309
310 indicator: null
311
ababi69f5dfc2020-08-25 15:07:57 +0200312 // Overwrite the combo box pop up to add footer (for add accounts).
Sébastien Blin1f915762020-08-03 13:27:42 -0400313 popup: AccountComboBoxPopup {
314 id: comboBoxPopup
315
316 onAccountNeedToChange: {
317 accountComboBox.accountChanged(index)
318 }
319
320 onNewAccountButtonClicked: {
321 accountComboBox.newAccountButtonClicked()
322 }
323 }
324}