qt_profile_select.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <QApplication>
  6. #include <QDialogButtonBox>
  7. #include <QHeaderView>
  8. #include <QLabel>
  9. #include <QLineEdit>
  10. #include <QScrollArea>
  11. #include <QStandardItemModel>
  12. #include <QVBoxLayout>
  13. #include "common/fs/path_util.h"
  14. #include "common/string_util.h"
  15. #include "core/constants.h"
  16. #include "core/hle/lock.h"
  17. #include "yuzu/applets/qt_profile_select.h"
  18. #include "yuzu/main.h"
  19. #include "yuzu/util/controller_navigation.h"
  20. namespace {
  21. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  22. return QtProfileSelectionDialog::tr(
  23. "%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
  24. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  25. .arg(username, QString::fromStdString(uuid.FormatSwitch()));
  26. }
  27. QString GetImagePath(Common::UUID uuid) {
  28. const auto path =
  29. Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  30. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormatSwitch());
  31. return QString::fromStdString(Common::FS::PathToUTF8String(path));
  32. }
  33. QPixmap GetIcon(Common::UUID uuid) {
  34. QPixmap icon{GetImagePath(uuid)};
  35. if (!icon) {
  36. icon.fill(Qt::black);
  37. icon.loadFromData(Core::Constants::ACCOUNT_BACKUP_JPEG.data(),
  38. static_cast<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  39. }
  40. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  41. }
  42. } // Anonymous namespace
  43. QtProfileSelectionDialog::QtProfileSelectionDialog(Core::HID::HIDCore& hid_core, QWidget* parent)
  44. : QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  45. outer_layout = new QVBoxLayout;
  46. instruction_label = new QLabel(tr("Select a user:"));
  47. scroll_area = new QScrollArea;
  48. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  49. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  50. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  51. outer_layout->addWidget(instruction_label);
  52. outer_layout->addWidget(scroll_area);
  53. outer_layout->addWidget(buttons);
  54. layout = new QVBoxLayout;
  55. tree_view = new QTreeView;
  56. item_model = new QStandardItemModel(tree_view);
  57. tree_view->setModel(item_model);
  58. controller_navigation = new ControllerNavigation(hid_core, this);
  59. tree_view->setAlternatingRowColors(true);
  60. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  61. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  62. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  63. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  64. tree_view->setSortingEnabled(true);
  65. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  66. tree_view->setUniformRowHeights(true);
  67. tree_view->setIconSize({64, 64});
  68. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  69. item_model->insertColumns(0, 1);
  70. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  71. // We must register all custom types with the Qt Automoc system so that we are able to use it
  72. // with signals/slots. In this case, QList falls under the umbrella of custom types.
  73. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  74. layout->setContentsMargins(0, 0, 0, 0);
  75. layout->setSpacing(0);
  76. layout->addWidget(tree_view);
  77. scroll_area->setLayout(layout);
  78. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  79. connect(controller_navigation, &ControllerNavigation::TriggerKeyboardEvent,
  80. [this](Qt::Key key) {
  81. if (!this->isActiveWindow()) {
  82. return;
  83. }
  84. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
  85. QCoreApplication::postEvent(tree_view, event);
  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. // Acquire the HLE mutex
  141. std::lock_guard lock{HLE::g_hle_lock};
  142. callback(uuid);
  143. }