-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathGameDigestDialog.cpp
172 lines (139 loc) · 4.38 KB
/
GameDigestDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/NetPlay/GameDigestDialog.h"
#include <algorithm>
#include <functional>
#include <QDialogButtonBox>
#include <QGroupBox>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
#include <QVBoxLayout>
#include "Core/NetPlayClient.h"
#include "Core/NetPlayServer.h"
#include "DolphinQt/Settings.h"
static QString GetPlayerNameFromPID(int pid)
{
QString player_name = QObject::tr("Invalid Player ID");
const auto client = Settings::Instance().GetNetPlayClient();
if (!client)
return player_name;
for (const auto* player : client->GetPlayers())
{
if (player->pid == pid)
{
player_name = QString::fromStdString(player->name);
break;
}
}
return player_name;
}
GameDigestDialog::GameDigestDialog(QWidget* parent) : QDialog(parent)
{
CreateWidgets();
ConnectWidgets();
setWindowTitle(tr("SHA1 Digest"));
setWindowFlags(Qt::Sheet | Qt::Dialog);
setWindowModality(Qt::WindowModal);
}
void GameDigestDialog::CreateWidgets()
{
m_main_layout = new QVBoxLayout;
m_progress_box = new QGroupBox;
m_progress_layout = new QVBoxLayout;
m_button_box = new QDialogButtonBox(QDialogButtonBox::NoButton);
m_check_label = new QLabel;
m_progress_box->setLayout(m_progress_layout);
m_main_layout->addWidget(m_progress_box);
m_main_layout->addWidget(m_check_label);
m_main_layout->addWidget(m_button_box);
setLayout(m_main_layout);
}
void GameDigestDialog::ConnectWidgets()
{
connect(m_button_box, &QDialogButtonBox::rejected, this, &GameDigestDialog::reject);
}
void GameDigestDialog::show(const QString& title)
{
m_progress_box->setTitle(title);
for (const auto& pair : m_progress_bars)
{
m_progress_layout->removeWidget(pair.second);
pair.second->deleteLater();
}
for (const auto& pair : m_status_labels)
{
m_progress_layout->removeWidget(pair.second);
pair.second->deleteLater();
}
m_progress_bars.clear();
m_status_labels.clear();
m_results.clear();
m_check_label->setText(QString::fromStdString(""));
const auto client = Settings::Instance().GetNetPlayClient();
if (!client)
return;
if (Settings::Instance().GetNetPlayServer())
{
m_button_box->setStandardButtons(QDialogButtonBox::Cancel);
QPushButton* cancel_button = m_button_box->button(QDialogButtonBox::Cancel);
cancel_button->setAutoDefault(false);
cancel_button->setDefault(false);
}
else
{
m_button_box->setStandardButtons(QDialogButtonBox::Close);
QPushButton* close_button = m_button_box->button(QDialogButtonBox::Close);
close_button->setAutoDefault(false);
close_button->setDefault(false);
}
for (const auto* player : client->GetPlayers())
{
m_progress_bars[player->pid] = new QProgressBar;
m_status_labels[player->pid] = new QLabel;
m_progress_layout->addWidget(m_progress_bars[player->pid]);
m_progress_layout->addWidget(m_status_labels[player->pid]);
}
QDialog::show();
}
void GameDigestDialog::SetProgress(int pid, int progress)
{
QString player_name = GetPlayerNameFromPID(pid);
if (!m_status_labels.contains(pid))
return;
m_status_labels[pid]->setText(
tr("%1[%2]: %3 %").arg(player_name, QString::number(pid), QString::number(progress)));
m_progress_bars[pid]->setValue(progress);
}
void GameDigestDialog::SetResult(int pid, const std::string& result)
{
QString player_name = GetPlayerNameFromPID(pid);
if (!m_status_labels.contains(pid))
return;
m_status_labels[pid]->setText(
tr("%1[%2]: %3").arg(player_name, QString::number(pid), QString::fromStdString(result)));
m_results.push_back(result);
const auto client = Settings::Instance().GetNetPlayClient();
if (client && m_results.size() >= client->GetPlayers().size())
{
if (std::ranges::adjacent_find(m_results, std::ranges::not_equal_to{}) == m_results.end())
{
m_check_label->setText(tr("The hashes match!"));
}
else
{
m_check_label->setText(tr("The hashes do not match!"));
}
m_button_box->setStandardButtons(QDialogButtonBox::Close);
QPushButton* close_button = m_button_box->button(QDialogButtonBox::Close);
close_button->setAutoDefault(false);
close_button->setDefault(false);
}
}
void GameDigestDialog::reject()
{
const auto server = Settings::Instance().GetNetPlayServer();
if (server)
server->AbortGameDigest();
QDialog::reject();
}