qt_profile_select.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <mutex>
  4. #include <QApplication>
  5. #include <QDialogButtonBox>
  6. #include <QHeaderView>
  7. #include <QLabel>
  8. #include <QLineEdit>
  9. #include <QScrollArea>
  10. #include <QStandardItemModel>
  11. #include <QTreeView>
  12. #include <QVBoxLayout>
  13. #include "common/fs/path_util.h"
  14. #include "common/string_util.h"
  15. #include "core/constants.h"
  16. #include "yuzu/applets/qt_profile_select.h"
  17. #include "yuzu/main.h"
  18. #include "yuzu/util/controller_navigation.h"
  19. namespace {
  20. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  21. return QtProfileSelectionDialog::tr(
  22. "%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
  23. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  24. .arg(username, QString::fromStdString(uuid.FormattedString()));
  25. }
  26. QString GetImagePath(Common::UUID uuid) {
  27. const auto path =
  28. Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  29. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  30. return QString::fromStdString(Common::FS::PathToUTF8String(path));
  31. }
  32. QPixmap GetIcon(Common::UUID uuid) {
  33. QPixmap icon{GetImagePath(uuid)};
  34. if (!icon) {
  35. icon.fill(Qt::black);
  36. icon.loadFromData(Core::Constants::ACCOUNT_BACKUP_JPEG.data(),
  37. static_cast<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  38. }
  39. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  40. }
  41. } // Anonymous namespace
  42. QtProfileSelectionDialog::QtProfileSelectionDialog(Core::HID::HIDCore& hid_core, QWidget* parent)
  43. : QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  44. outer_layout = new QVBoxLayout;
  45. instruction_label = new QLabel(tr("Select a user:"));
  46. scroll_area = new QScrollArea;
  47. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  48. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  49. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  50. outer_layout->addWidget(instruction_label);
  51. outer_layout->addWidget(scroll_area);
  52. outer_layout->addWidget(buttons);
  53. layout = new QVBoxLayout;
  54. tree_view = new QTreeView;
  55. item_model = new QStandardItemModel(tree_view);
  56. tree_view->setModel(item_model);
  57. controller_navigation = new ControllerNavigation(hid_core, this);
  58. tree_view->setAlternatingRowColors(true);
  59. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  60. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  61. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  62. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  63. tree_view->setSortingEnabled(true);
  64. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  65. tree_view->setUniformRowHeights(true);
  66. tree_view->setIconSize({64, 64});
  67. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  68. item_model->insertColumns(0, 1);
  69. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  70. // We must register all custom types with the Qt Automoc system so that we are able to use it
  71. // with signals/slots. In this case, QList falls under the umbrella of custom types.
  72. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  73. layout->setContentsMargins(0, 0, 0, 0);
  74. layout->setSpacing(0);
  75. layout->addWidget(tree_view);
  76. scroll_area->setLayout(layout);
  77. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  78. connect(controller_navigation, &ControllerNavigation::TriggerKeyboardEvent,
  79. [this](Qt::Key key) {
  80. if (!this->isActiveWindow()) {
  81. return;
  82. }
  83. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
  84. QCoreApplication::postEvent(tree_view, event);
  85. SelectUser(tree_view->currentIndex());
  86. });
  87. const auto& profiles = profile_manager->GetAllUsers();
  88. for (const auto& user : profiles) {
  89. Service::Account::ProfileBase profile{};
  90. if (!profile_manager->GetProfileBase(user, profile))
  91. continue;
  92. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  93. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  94. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  95. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  96. }
  97. for (const auto& item : list_items)
  98. item_model->appendRow(item);
  99. setLayout(outer_layout);
  100. setWindowTitle(tr("Profile Selector"));
  101. resize(550, 400);
  102. }
  103. QtProfileSelectionDialog::~QtProfileSelectionDialog() {
  104. controller_navigation->UnloadController();
  105. };
  106. int QtProfileSelectionDialog::exec() {
  107. // Skip profile selection when there's only one.
  108. if (profile_manager->GetUserCount() == 1) {
  109. user_index = 0;
  110. return QDialog::Accepted;
  111. }
  112. return QDialog::exec();
  113. }
  114. void QtProfileSelectionDialog::accept() {
  115. QDialog::accept();
  116. }
  117. void QtProfileSelectionDialog::reject() {
  118. user_index = 0;
  119. QDialog::reject();
  120. }
  121. int QtProfileSelectionDialog::GetIndex() const {
  122. return user_index;
  123. }
  124. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  125. user_index = index.row();
  126. }
  127. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  128. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  129. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  130. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  131. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  132. }
  133. QtProfileSelector::~QtProfileSelector() = default;
  134. void QtProfileSelector::SelectProfile(
  135. std::function<void(std::optional<Common::UUID>)> callback_) const {
  136. callback = std::move(callback_);
  137. emit MainWindowSelectProfile();
  138. }
  139. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::UUID> uuid) {
  140. callback(uuid);
  141. }