summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/NetPlay/GameListDialog.cpp
blob: 75828cb4660d472da954ef9dbb993b3b9a492cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/NetPlay/GameListDialog.h"

#include <memory>

#include <QDialogButtonBox>
#include <QListWidget>
#include <QVBoxLayout>

#include "UICommon/GameFile.h"

GameListDialog::GameListDialog(const GameListModel& game_list_model, QWidget* parent)
    : QDialog(parent), m_game_list_model(game_list_model)
{
  setWindowTitle(tr("Select a game"));

  CreateWidgets();
  ConnectWidgets();
}

void GameListDialog::CreateWidgets()
{
  m_main_layout = new QVBoxLayout;
  m_game_list = new QListWidget;
  m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok);
  m_button_box->setEnabled(false);

  m_main_layout->addWidget(m_game_list);
  m_main_layout->addWidget(m_button_box);

  setLayout(m_main_layout);
}

void GameListDialog::ConnectWidgets()
{
  connect(m_game_list, &QListWidget::itemSelectionChanged,
          [this] { m_button_box->setEnabled(m_game_list->currentRow() != -1); });

  connect(m_game_list, &QListWidget::itemDoubleClicked, this, &GameListDialog::accept);
  connect(m_button_box, &QDialogButtonBox::accepted, this, &GameListDialog::accept);
}

void GameListDialog::PopulateGameList()
{
  m_game_list->clear();

  for (int i = 0; i < m_game_list_model.rowCount(QModelIndex()); i++)
  {
    std::shared_ptr<const UICommon::GameFile> game = m_game_list_model.GetGameFile(i);

    auto* item =
        new QListWidgetItem(QString::fromStdString(m_game_list_model.GetNetPlayName(*game)));
    item->setData(Qt::UserRole, QVariant::fromValue(std::move(game)));
    m_game_list->addItem(item);
  }

  m_game_list->sortItems();
}

const UICommon::GameFile& GameListDialog::GetSelectedGame() const
{
  auto items = m_game_list->selectedItems();
  return *items[0]->data(Qt::UserRole).value<std::shared_ptr<const UICommon::GameFile>>();
}

int GameListDialog::exec()
{
  PopulateGameList();
  return QDialog::exec();
}