profile_select.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <mutex>
  5. #include <QDialogButtonBox>
  6. #include <QHeaderView>
  7. #include <QLabel>
  8. #include <QLineEdit>
  9. #include <QScrollArea>
  10. #include <QStandardItemModel>
  11. #include <QVBoxLayout>
  12. #include "common/file_util.h"
  13. #include "common/string_util.h"
  14. #include "core/constants.h"
  15. #include "core/hle/lock.h"
  16. #include "yuzu/applets/profile_select.h"
  17. #include "yuzu/main.h"
  18. namespace {
  19. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  20. return QtProfileSelectionDialog::tr(
  21. "%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
  22. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  23. .arg(username, QString::fromStdString(uuid.FormatSwitch()));
  24. }
  25. QString GetImagePath(Common::UUID uuid) {
  26. const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  27. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  28. return QString::fromStdString(path);
  29. }
  30. QPixmap GetIcon(Common::UUID uuid) {
  31. QPixmap icon{GetImagePath(uuid)};
  32. if (!icon) {
  33. icon.fill(Qt::black);
  34. icon.loadFromData(Core::Constants::ACCOUNT_BACKUP_JPEG.data(),
  35. static_cast<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  36. }
  37. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  38. }
  39. } // Anonymous namespace
  40. QtProfileSelectionDialog::QtProfileSelectionDialog(QWidget* parent)
  41. : QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  42. outer_layout = new QVBoxLayout;
  43. instruction_label = new QLabel(tr("Select a user:"));
  44. scroll_area = new QScrollArea;
  45. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  46. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  47. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  48. outer_layout->addWidget(instruction_label);
  49. outer_layout->addWidget(scroll_area);
  50. outer_layout->addWidget(buttons);
  51. layout = new QVBoxLayout;
  52. tree_view = new QTreeView;
  53. item_model = new QStandardItemModel(tree_view);
  54. tree_view->setModel(item_model);
  55. tree_view->setAlternatingRowColors(true);
  56. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  57. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  58. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  59. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  60. tree_view->setSortingEnabled(true);
  61. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  62. tree_view->setUniformRowHeights(true);
  63. tree_view->setIconSize({64, 64});
  64. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  65. item_model->insertColumns(0, 1);
  66. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  67. // We must register all custom types with the Qt Automoc system so that we are able to use it
  68. // with signals/slots. In this case, QList falls under the umbrella of custom types.
  69. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  70. layout->setContentsMargins(0, 0, 0, 0);
  71. layout->setSpacing(0);
  72. layout->addWidget(tree_view);
  73. scroll_area->setLayout(layout);
  74. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  75. const auto& profiles = profile_manager->GetAllUsers();
  76. for (const auto& user : profiles) {
  77. Service::Account::ProfileBase profile;
  78. if (!profile_manager->GetProfileBase(user, profile))
  79. continue;
  80. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  81. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  82. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  83. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  84. }
  85. for (const auto& item : list_items)
  86. item_model->appendRow(item);
  87. setLayout(outer_layout);
  88. setWindowTitle(tr("Profile Selector"));
  89. resize(550, 400);
  90. }
  91. QtProfileSelectionDialog::~QtProfileSelectionDialog() = default;
  92. void QtProfileSelectionDialog::accept() {
  93. QDialog::accept();
  94. }
  95. void QtProfileSelectionDialog::reject() {
  96. user_index = 0;
  97. QDialog::reject();
  98. }
  99. int QtProfileSelectionDialog::GetIndex() const {
  100. return user_index;
  101. }
  102. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  103. user_index = index.row();
  104. }
  105. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  106. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  107. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  108. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  109. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  110. }
  111. QtProfileSelector::~QtProfileSelector() = default;
  112. void QtProfileSelector::SelectProfile(
  113. std::function<void(std::optional<Common::UUID>)> callback) const {
  114. this->callback = std::move(callback);
  115. emit MainWindowSelectProfile();
  116. }
  117. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::UUID> uuid) {
  118. // Acquire the HLE mutex
  119. std::lock_guard lock{HLE::g_hle_lock};
  120. callback(uuid);
  121. }