From 2d65371c202ab5f6e280dd1e81b988c70f72d404 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Wed, 16 Jan 2019 22:01:21 +0800 Subject: [PATCH] citra_qt/multiplayer: Update themed icon on theme change --- src/citra_qt/multiplayer/chat_room.cpp | 66 ++++++++++++------------ src/citra_qt/multiplayer/chat_room.h | 2 +- src/citra_qt/multiplayer/client_room.cpp | 4 ++ src/citra_qt/multiplayer/client_room.h | 1 + src/citra_qt/multiplayer/state.cpp | 1 + 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/citra_qt/multiplayer/chat_room.cpp b/src/citra_qt/multiplayer/chat_room.cpp index 440d9ce82..e16b86217 100644 --- a/src/citra_qt/multiplayer/chat_room.cpp +++ b/src/citra_qt/multiplayer/chat_room.cpp @@ -352,6 +352,8 @@ void ChatRoom::UpdateIconDisplay() { item->data(PlayerListItem::AvatarUrlRole).toString().toStdString(); if (icon_cache.count(avatar_url)) { item->setData(icon_cache.at(avatar_url), Qt::DecorationRole); + } else { + item->setData(QIcon::fromTheme("no_avatar").pixmap(48), Qt::DecorationRole); } } } @@ -365,45 +367,41 @@ void ChatRoom::SetPlayerList(const Network::RoomMember::MemberList& member_list) QStandardItem* name_item = new PlayerListItem(member.nickname, member.username, member.avatar_url, member.game_info.name); - if (!icon_cache.count(member.avatar_url)) { - // Emplace a default question mark icon as avatar - icon_cache.emplace(member.avatar_url, QIcon::fromTheme("no_avatar").pixmap(48)); - if (!member.avatar_url.empty()) { #ifdef ENABLE_WEB_SERVICE - // Start a request to get the member's avatar - const QUrl url(QString::fromStdString(member.avatar_url)); - QFuture future = QtConcurrent::run([url] { - WebService::Client client( - QString("%1://%2").arg(url.scheme(), url.host()).toStdString(), "", ""); - auto result = client.GetImage(url.path().toStdString(), true); - if (result.returned_data.empty()) { - LOG_ERROR(WebService, "Failed to get avatar"); - } - return result.returned_data; - }); - auto* future_watcher = new QFutureWatcher(this); - connect(future_watcher, &QFutureWatcher::finished, this, - [this, future_watcher, avatar_url = member.avatar_url] { - const std::string result = future_watcher->result(); - if (result.empty()) - return; - QPixmap pixmap; - if (!pixmap.loadFromData(reinterpret_cast(result.data()), - result.size())) - return; - icon_cache[avatar_url] = pixmap.scaled(48, 48, Qt::IgnoreAspectRatio, - Qt::SmoothTransformation); - // Update all the displayed icons with the new icon_cache - UpdateIconDisplay(); - }); - future_watcher->setFuture(future); -#endif - } + if (!icon_cache.count(member.avatar_url) && !member.avatar_url.empty()) { + // Start a request to get the member's avatar + const QUrl url(QString::fromStdString(member.avatar_url)); + QFuture future = QtConcurrent::run([url] { + WebService::Client client( + QString("%1://%2").arg(url.scheme(), url.host()).toStdString(), "", ""); + auto result = client.GetImage(url.path().toStdString(), true); + if (result.returned_data.empty()) { + LOG_ERROR(WebService, "Failed to get avatar"); + } + return result.returned_data; + }); + auto* future_watcher = new QFutureWatcher(this); + connect(future_watcher, &QFutureWatcher::finished, this, + [this, future_watcher, avatar_url = member.avatar_url] { + const std::string result = future_watcher->result(); + if (result.empty()) + return; + QPixmap pixmap; + if (!pixmap.loadFromData(reinterpret_cast(result.data()), + result.size())) + return; + icon_cache[avatar_url] = + pixmap.scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + // Update all the displayed icons with the new icon_cache + UpdateIconDisplay(); + }); + future_watcher->setFuture(future); } - name_item->setData(icon_cache.at(member.avatar_url), Qt::DecorationRole); +#endif player_list->invisibleRootItem()->appendRow(name_item); } + UpdateIconDisplay(); // TODO(B3N30): Restore row selection } diff --git a/src/citra_qt/multiplayer/chat_room.h b/src/citra_qt/multiplayer/chat_room.h index 70f786dbe..a810377f7 100644 --- a/src/citra_qt/multiplayer/chat_room.h +++ b/src/citra_qt/multiplayer/chat_room.h @@ -37,6 +37,7 @@ public: ~ChatRoom(); void SetModPerms(bool is_mod); + void UpdateIconDisplay(); public slots: void OnRoomUpdate(const Network::RoomInformation& info); @@ -58,7 +59,6 @@ private: void AppendChatMessage(const QString&); bool ValidateMessage(const std::string&); void SendModerationRequest(Network::RoomMessageTypes type, const std::string& nickname); - void UpdateIconDisplay(); bool has_mod_perms = false; QStandardItemModel* player_list; diff --git a/src/citra_qt/multiplayer/client_room.cpp b/src/citra_qt/multiplayer/client_room.cpp index 458f8c864..4b5446508 100644 --- a/src/citra_qt/multiplayer/client_room.cpp +++ b/src/citra_qt/multiplayer/client_room.cpp @@ -109,3 +109,7 @@ void ClientRoomWindow::UpdateView() { // TODO(B3N30): can't get RoomMember*, show error and close window close(); } + +void ClientRoomWindow::UpdateIconDisplay() { + ui->chat->UpdateIconDisplay(); +} diff --git a/src/citra_qt/multiplayer/client_room.h b/src/citra_qt/multiplayer/client_room.h index 584a51642..cbbcff61f 100644 --- a/src/citra_qt/multiplayer/client_room.h +++ b/src/citra_qt/multiplayer/client_room.h @@ -18,6 +18,7 @@ public: ~ClientRoomWindow(); void RetranslateUi(); + void UpdateIconDisplay(); public slots: void OnRoomUpdate(const Network::RoomInformation&); diff --git a/src/citra_qt/multiplayer/state.cpp b/src/citra_qt/multiplayer/state.cpp index 75d74e189..ba717650a 100644 --- a/src/citra_qt/multiplayer/state.cpp +++ b/src/citra_qt/multiplayer/state.cpp @@ -194,6 +194,7 @@ void MultiplayerState::UpdateThemedIcons() { } else { status_icon->setPixmap(QIcon::fromTheme("disconnected").pixmap(16)); } + client_room->UpdateIconDisplay(); } static void BringWidgetToFront(QWidget* widget) {