| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/NetPlay/GameListDialog.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | |
| 8 | #include <QDialogButtonBox> |
| 9 | #include <QListWidget> |
| 10 | #include <QVBoxLayout> |
| 11 | |
| 12 | #include "UICommon/GameFile.h" |
| 13 | |
| 14 | GameListDialog::GameListDialog(const GameListModel& game_list_model, QWidget* parent) |
| 15 | : QDialog(parent), m_game_list_model(game_list_model) |
| 16 | { |
| 17 | setWindowTitle(tr("Select a game")); |
| 18 | |
| 19 | CreateWidgets(); |
| 20 | ConnectWidgets(); |
| 21 | } |
| 22 | |
| 23 | void GameListDialog::CreateWidgets() |
| 24 | { |
| 25 | m_main_layout = new QVBoxLayout; |
| 26 | m_game_list = new QListWidget; |
| 27 | m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok); |
| 28 | m_button_box->setEnabled(false); |
| 29 | |
| 30 | m_main_layout->addWidget(m_game_list); |
| 31 | m_main_layout->addWidget(m_button_box); |
| 32 | |
| 33 | setLayout(m_main_layout); |
| 34 | } |
| 35 | |
| 36 | void GameListDialog::ConnectWidgets() |
| 37 | { |
| 38 | connect(m_game_list, &QListWidget::itemSelectionChanged, |
| 39 | [this] { m_button_box->setEnabled(m_game_list->currentRow() != -1); }); |
| 40 | |
| 41 | connect(m_game_list, &QListWidget::itemDoubleClicked, this, &GameListDialog::accept); |
| 42 | connect(m_button_box, &QDialogButtonBox::accepted, this, &GameListDialog::accept); |
| 43 | } |
| 44 | |
| 45 | void GameListDialog::PopulateGameList() |
| 46 | { |
| 47 | m_game_list->clear(); |
| 48 | |
| 49 | for (int i = 0; i < m_game_list_model.rowCount(QModelIndex()); i++) |
| 50 | { |
| 51 | std::shared_ptr<const UICommon::GameFile> game = m_game_list_model.GetGameFile(i); |
| 52 | |
| 53 | auto* item = |
| 54 | new QListWidgetItem(QString::fromStdString(m_game_list_model.GetNetPlayName(*game))); |
| 55 | item->setData(Qt::UserRole, QVariant::fromValue(std::move(game))); |
| 56 | m_game_list->addItem(item); |
| 57 | } |
| 58 | |
| 59 | m_game_list->sortItems(); |
| 60 | } |
| 61 | |
| 62 | const UICommon::GameFile& GameListDialog::GetSelectedGame() const |
| 63 | { |
| 64 | auto items = m_game_list->selectedItems(); |
| 65 | return *items[0]->data(Qt::UserRole).value<std::shared_ptr<const UICommon::GameFile>>(); |
| 66 | } |
| 67 | |
| 68 | int GameListDialog::exec() |
| 69 | { |
| 70 | PopulateGameList(); |
| 71 | return QDialog::exec(); |
| 72 | } |