qt_profile_select.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "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::NewUUID 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::NewUUID 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::NewUUID 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. });
  86. const auto& profiles = profile_manager->GetAllUsers();
  87. for (const auto& user : profiles) {
  88. Service::Account::ProfileBase profile{};
  89. if (!profile_manager->GetProfileBase(user, profile))
  90. continue;
  91. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  92. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  93. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  94. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  95. }
  96. for (const auto& item : list_items)
  97. item_model->appendRow(item);
  98. setLayout(outer_layout);
  99. setWindowTitle(tr("Profile Selector"));
  100. resize(550, 400);
  101. }
  102. QtProfileSelectionDialog::~QtProfileSelectionDialog() {
  103. controller_navigation->UnloadController();
  104. };
  105. int QtProfileSelectionDialog::exec() {
  106. // Skip profile selection when there's only one.
  107. if (profile_manager->GetUserCount() == 1) {
  108. user_index = 0;
  109. return QDialog::Accepted;
  110. }
  111. return QDialog::exec();
  112. }
  113. void QtProfileSelectionDialog::accept() {
  114. QDialog::accept();
  115. }
  116. void QtProfileSelectionDialog::reject() {
  117. user_index = 0;
  118. QDialog::reject();
  119. }
  120. int QtProfileSelectionDialog::GetIndex() const {
  121. return user_index;
  122. }
  123. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  124. user_index = index.row();
  125. }
  126. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  127. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  128. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  129. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  130. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  131. }
  132. QtProfileSelector::~QtProfileSelector() = default;
  133. void QtProfileSelector::SelectProfile(
  134. std::function<void(std::optional<Common::NewUUID>)> callback_) const {
  135. callback = std::move(callback_);
  136. emit MainWindowSelectProfile();
  137. }
  138. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::NewUUID> uuid) {
  139. callback(uuid);
  140. }