From c18611d4af82f295b911bd2f27a001b957067491 Mon Sep 17 00:00:00 2001 From: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> Date: Thu, 5 Dec 2019 11:38:29 +0100 Subject: [PATCH] Finished account switch back- and frontend in tray menuwindow Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> --- src/gui/systray.cpp | 2 ++ src/gui/tray/UserLine.qml | 2 +- src/gui/tray/UserModel.cpp | 52 +++++++++++++++++++++++--------------- src/gui/tray/UserModel.h | 15 +++++++++-- src/gui/tray/window.qml | 24 +++++++++--------- 5 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/gui/systray.cpp b/src/gui/systray.cpp index 74bc1347e..6e326c4cd 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -44,7 +44,9 @@ Systray::Systray() // TODO: make singleton, provide ::instance() _trayContext = engine->contextForObject(systray.create()); _accountMenuModel = UserModel::instance(); + systray.engine()->addImageProvider("avatars", new ImageProvider); systray.engine()->rootContext()->setContextProperty("systrayBackend", _accountMenuModel); + // TODO: hack to pass the icon to QML //ctxt->setContextProperty("theme", QLatin1String("colored")); diff --git a/src/gui/tray/UserLine.qml b/src/gui/tray/UserLine.qml index d694aaaf2..fa80d2a2b 100644 --- a/src/gui/tray/UserLine.qml +++ b/src/gui/tray/UserLine.qml @@ -55,7 +55,7 @@ MenuItem { id: accountAvatar Layout.leftMargin: 4 verticalAlignment: Qt.AlignCenter - source: avatar + source: ("image://avatars/" + index) Layout.preferredHeight: (userLineBackground.height -16) Layout.preferredWidth: (userLineBackground.height -16) } diff --git a/src/gui/tray/UserModel.cpp b/src/gui/tray/UserModel.cpp index 4e42f48ef..63513003f 100644 --- a/src/gui/tray/UserModel.cpp +++ b/src/gui/tray/UserModel.cpp @@ -41,30 +41,19 @@ QString User::server() const QString serverUrl = _account->account()->url().toString(); serverUrl.replace(QLatin1String("https://"), QLatin1String("")); serverUrl.replace(QLatin1String("http://"), QLatin1String("")); - if (serverUrl.size() > 24) { - serverUrl.truncate(22); + if (serverUrl.size() > 21) { + serverUrl.truncate(19); serverUrl.append("..."); } return serverUrl; } -// TODO: Lots of memory shifting here -// Probably OK because the avatar is not changing a trillion times per second -// But should consider moving to a generic ImageProvider helper class for img/QML-provision -QString User::avatar() const +QImage User::avatar() const { - QByteArray bArray; - QBuffer buffer(&bArray); - buffer.open(QIODevice::WriteOnly); - AvatarJob::makeCircularAvatar(_account->account()->avatar()).save(&buffer, "PNG"); - - // If AvatarJob doesn't deliver anything, fall back to placeholder image (may be due to missing login session) - if (buffer.size() == 0) { - QIcon(":/client/resources/account.svg").pixmap(QSize(250, 250)).save(&buffer, "PNG"); + QImage img = AvatarJob::makeCircularAvatar(_account->account()->avatar()); + if (img.isNull()) { + img = QImage(":/client/resources/account.svg"); } - - QString img("data:image/png;base64,"); - img.append(QString::fromLatin1(bArray.toBase64().data())); return img; } @@ -125,11 +114,16 @@ Q_INVOKABLE bool UserModel::isCurrentUserConnected() return _users[_currentUserId].isConnected(); } -Q_INVOKABLE QString UserModel::currentUserAvatar() +QImage UserModel::currentUserAvatar() { return _users[_currentUserId].avatar(); } +QImage UserModel::avatarById(const int &id) +{ + return _users[id].avatar(); +} + Q_INVOKABLE QString UserModel::currentUserName() { return _users[_currentUserId].name(); @@ -156,9 +150,9 @@ Q_INVOKABLE void UserModel::switchCurrentUser(const int &id) _users[_currentUserId].setCurrentUser(false); _users[id].setCurrentUser(true); _currentUserId = id; - emit refreshCurrentUserGui(); - emit refreshUserMenu(); emit newUserSelected(); + emit refreshUserMenu(); + emit refreshCurrentUserGui(); } int UserModel::rowCount(const QModelIndex &parent) const @@ -195,4 +189,22 @@ QHash UserModel::roleNames() const roles[IsCurrentUserRole] = "isCurrentUser"; return roles; } + +/*-------------------------------------------------------------------------------------*/ + +ImageProvider::ImageProvider() + : QQuickImageProvider(QQuickImageProvider::Image) +{ +} + +QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) +{ + if (id == "currentUser") { + return UserModel::instance()->currentUserAvatar(); + } else { + int uid = id.toInt(); + return UserModel::instance()->avatarById(uid); + } +} + } \ No newline at end of file diff --git a/src/gui/tray/UserModel.h b/src/gui/tray/UserModel.h index b8a9eb776..ddd958fd4 100644 --- a/src/gui/tray/UserModel.h +++ b/src/gui/tray/UserModel.h @@ -2,7 +2,9 @@ #define USERMODEL_H #include +#include #include +#include #include "accountmanager.h" @@ -22,7 +24,7 @@ public: void logout(); QString name() const; QString server() const; - QString avatar() const; + QImage avatar() const; QString id() const; private: @@ -43,9 +45,11 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QImage currentUserAvatar(); + QImage avatarById(const int &id); + Q_INVOKABLE int numUsers(); Q_INVOKABLE bool isCurrentUserConnected(); - Q_INVOKABLE QString currentUserAvatar(); Q_INVOKABLE QString currentUserName(); Q_INVOKABLE QString currentUserServer(); Q_INVOKABLE void switchCurrentUser(const int &id); @@ -79,5 +83,12 @@ private: void initUserList(); }; +class ImageProvider : public QQuickImageProvider +{ +public: + ImageProvider(); + QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; +}; + } #endif // USERMODEL_H \ No newline at end of file diff --git a/src/gui/tray/window.qml b/src/gui/tray/window.qml index 85cff3b5b..ade716b5d 100644 --- a/src/gui/tray/window.qml +++ b/src/gui/tray/window.qml @@ -25,7 +25,8 @@ Window { Connections { target: systrayBackend onRefreshCurrentUserGui: { - currentAccountAvatar.source = systrayBackend.currentUserAvatar() + currentAccountAvatar.source = "" + currentAccountAvatar.source = "image://avatars/currentUser" currentAccountUser.text = systrayBackend.currentUserName() currentAccountServer.text = systrayBackend.currentUserServer() } @@ -64,6 +65,7 @@ Window { Button { id: currentAccountButton Layout.preferredWidth: 220 + Layout.maximumWidth: 220 Layout.preferredHeight: (trayWindowHeaderBackground.height) display: AbstractButton.IconOnly flat: true @@ -74,6 +76,7 @@ Window { hoverEnabled: true onClicked: { + accountMenuLoginLogout.text = (systrayBackend.isCurrentUserConnected() ? "Logout" : "Login") accountMenu.open() } @@ -98,7 +101,7 @@ Window { MenuSeparator { id: accountMenuSeparator } MenuItem { - text: (systrayBackend.isCurrentUserConnected() ? "Logout" : "Login") + id: accountMenuLoginLogout onClicked: (systrayBackend.isCurrentUserConnected() ? systrayBackend.logout() : systrayBackend.login() ) @@ -177,7 +180,8 @@ Window { id: currentAccountAvatar Layout.leftMargin: 8 verticalAlignment: Qt.AlignCenter - source: systrayBackend.currentUserAvatar() + cache: false + source: "image://avatars/currentUser" Layout.preferredHeight: (trayWindowHeaderBackground.height -16) Layout.preferredWidth: (trayWindowHeaderBackground.height -16) } @@ -202,21 +206,20 @@ Window { } } + Item { + id: trayWindowHeaderSpacer + Layout.fillWidth: true + } + Image { Layout.alignment: Qt.AlignLeft verticalAlignment: Qt.AlignCenter Layout.margins: 12 - //source: "../../theme/white/caret-down.svg" source: "qrc:///client/theme/white/caret-down.svg" } } } - Item { - id: trayWindowHeaderSpacer - Layout.fillWidth: true - } - Button { id: openLocalFolderButton Layout.alignment: Qt.AlignRight @@ -225,7 +228,6 @@ Window { Layout.preferredHeight: (trayWindowHeaderBackground.height) flat: true - //icon.source: "../../theme/white/folder.svg" icon.source: "qrc:///client/theme/white/folder.svg" icon.color: "transparent" @@ -253,7 +255,6 @@ Window { Layout.preferredHeight: (trayWindowHeaderBackground.height) flat: true - //icon.source: "../../theme/white/talk-app.svg" icon.source: "qrc:///client/theme/white/talk-app.svg" icon.color: "transparent" @@ -281,7 +282,6 @@ Window { Layout.preferredHeight: (trayWindowHeaderBackground.height) flat: true - //icon.source: "../../theme/white/more-apps.svg" icon.source: "qrc:///client/theme/white/more-apps.svg" icon.color: "transparent" -- 2.30.2