| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/NetPlay/ChunkedProgressDialog.h" |
| 5 | |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | |
| 10 | #include <QDialogButtonBox> |
| 11 | #include <QGroupBox> |
| 12 | #include <QLabel> |
| 13 | #include <QProgressBar> |
| 14 | #include <QPushButton> |
| 15 | #include <QVBoxLayout> |
| 16 | |
| 17 | #include "Common/Contains.h" |
| 18 | #include "Core/NetPlayClient.h" |
| 19 | #include "Core/NetPlayServer.h" |
| 20 | |
| 21 | #include "DolphinQt/Settings.h" |
| 22 | |
| 23 | static QString GetPlayerNameFromPID(int pid) |
| 24 | { |
| 25 | QString player_name = QObject::tr("Invalid Player ID"); |
| 26 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 27 | if (!client) |
| 28 | return player_name; |
| 29 | |
| 30 | for (const auto* player : client->GetPlayers()) |
| 31 | { |
| 32 | if (player->pid == pid) |
| 33 | { |
| 34 | player_name = QString::fromStdString(player->name); |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | return player_name; |
| 39 | } |
| 40 | |
| 41 | ChunkedProgressDialog::ChunkedProgressDialog(QWidget* parent) : QDialog(parent) |
| 42 | { |
| 43 | CreateWidgets(); |
| 44 | ConnectWidgets(); |
| 45 | setWindowTitle(tr("Data Transfer")); |
| 46 | } |
| 47 | |
| 48 | void ChunkedProgressDialog::CreateWidgets() |
| 49 | { |
| 50 | m_main_layout = new QVBoxLayout; |
| 51 | m_progress_box = new QGroupBox; |
| 52 | m_progress_layout = new QVBoxLayout; |
| 53 | m_button_box = new QDialogButtonBox(QDialogButtonBox::NoButton); |
| 54 | |
| 55 | m_progress_box->setLayout(m_progress_layout); |
| 56 | |
| 57 | m_main_layout->addWidget(m_progress_box); |
| 58 | m_main_layout->addWidget(m_button_box); |
| 59 | setLayout(m_main_layout); |
| 60 | } |
| 61 | |
| 62 | void ChunkedProgressDialog::ConnectWidgets() |
| 63 | { |
| 64 | connect(m_button_box, &QDialogButtonBox::rejected, this, &ChunkedProgressDialog::reject); |
| 65 | } |
| 66 | |
| 67 | void ChunkedProgressDialog::show(const QString& title, const u64 data_size, |
| 68 | std::span<const int> players) |
| 69 | { |
| 70 | m_progress_box->setTitle(title); |
| 71 | m_data_size = data_size; |
| 72 | |
| 73 | for (const auto& pair : m_progress_bars) |
| 74 | { |
| 75 | m_progress_layout->removeWidget(pair.second); |
| 76 | pair.second->deleteLater(); |
| 77 | } |
| 78 | |
| 79 | for (const auto& pair : m_status_labels) |
| 80 | { |
| 81 | m_progress_layout->removeWidget(pair.second); |
| 82 | pair.second->deleteLater(); |
| 83 | } |
| 84 | |
| 85 | m_progress_bars.clear(); |
| 86 | m_status_labels.clear(); |
| 87 | |
| 88 | const auto client = Settings::Instance().GetNetPlayClient(); |
| 89 | if (!client) |
| 90 | return; |
| 91 | |
| 92 | if (Settings::Instance().GetNetPlayServer()) |
| 93 | { |
| 94 | m_button_box->setStandardButtons(QDialogButtonBox::Cancel); |
| 95 | QPushButton* cancel_button = m_button_box->button(QDialogButtonBox::Cancel); |
| 96 | cancel_button->setAutoDefault(false); |
| 97 | cancel_button->setDefault(false); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | m_button_box->setStandardButtons(QDialogButtonBox::Close); |
| 102 | QPushButton* close_button = m_button_box->button(QDialogButtonBox::Close); |
| 103 | close_button->setAutoDefault(false); |
| 104 | close_button->setDefault(false); |
| 105 | } |
| 106 | |
| 107 | for (const auto* player : client->GetPlayers()) |
| 108 | { |
| 109 | if (!Common::Contains(players, player->pid)) |
| 110 | continue; |
| 111 | |
| 112 | m_progress_bars[player->pid] = new QProgressBar; |
| 113 | m_status_labels[player->pid] = new QLabel; |
| 114 | |
| 115 | m_progress_layout->addWidget(m_progress_bars[player->pid]); |
| 116 | m_progress_layout->addWidget(m_status_labels[player->pid]); |
| 117 | } |
| 118 | |
| 119 | QDialog::show(); |
| 120 | } |
| 121 | |
| 122 | void ChunkedProgressDialog::SetProgress(const int pid, const u64 progress) |
| 123 | { |
| 124 | QString player_name = GetPlayerNameFromPID(pid); |
| 125 | |
| 126 | if (!m_status_labels.contains(pid)) |
| 127 | return; |
| 128 | |
| 129 | const float acquired = progress / 1024.0f / 1024.0f; |
| 130 | const float total = m_data_size / 1024.0f / 1024.0f; |
| 131 | const int prog = std::lround((static_cast<float>(progress) / m_data_size) * 100.0f); |
| 132 | |
| 133 | m_status_labels[pid]->setText(tr("%1[%2]: %3/%4 MiB") |
| 134 | .arg(player_name, QString::number(pid), |
| 135 | QString::fromStdString(fmt::format("{:.2f}", acquired)), |
| 136 | QString::fromStdString(fmt::format("{:.2f}", total)))); |
| 137 | m_progress_bars[pid]->setValue(prog); |
| 138 | } |
| 139 | |
| 140 | void ChunkedProgressDialog::reject() |
| 141 | { |
| 142 | const auto server = Settings::Instance().GetNetPlayServer(); |
| 143 | |
| 144 | if (server) |
| 145 | server->AbortGameStart(); |
| 146 | |
| 147 | QDialog::reject(); |
| 148 | } |