PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Core/DolphinQt2/GameList/GameList.cpp

https://gitlab.com/Hexexpeck/dolphin-emulator
C++ | 191 lines | 166 code | 21 blank | 4 comment | 11 complexity | 399e994f8b5459657d69b2dfa81fc59c MD5 | raw file
  1. // Copyright 2015 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <QDesktopServices>
  5. #include <QHeaderView>
  6. #include <QKeyEvent>
  7. #include <QMenu>
  8. #include <QUrl>
  9. #include "DiscIO/Enums.h"
  10. #include "DolphinQt2/Config/PropertiesDialog.h"
  11. #include "DolphinQt2/GameList/GameList.h"
  12. #include "DolphinQt2/GameList/ListProxyModel.h"
  13. #include "DolphinQt2/GameList/TableDelegate.h"
  14. #include "DolphinQt2/Settings.h"
  15. GameList::GameList(QWidget* parent) : QStackedWidget(parent)
  16. {
  17. m_model = new GameListModel(this);
  18. m_table_proxy = new QSortFilterProxyModel(this);
  19. m_table_proxy->setSourceModel(m_model);
  20. m_list_proxy = new ListProxyModel(this);
  21. m_list_proxy->setSourceModel(m_model);
  22. m_delegate = new TableDelegate(this);
  23. MakeTableView();
  24. MakeListView();
  25. MakeEmptyView();
  26. connect(m_table, &QTableView::doubleClicked, this, &GameList::GameSelected);
  27. connect(m_list, &QListView::doubleClicked, this, &GameList::GameSelected);
  28. connect(this, &GameList::DirectoryAdded, m_model, &GameListModel::DirectoryAdded);
  29. connect(this, &GameList::DirectoryRemoved, m_model, &GameListModel::DirectoryRemoved);
  30. connect(m_model, &QAbstractItemModel::rowsInserted, this, &GameList::ConsiderViewChange);
  31. connect(m_model, &QAbstractItemModel::rowsRemoved, this, &GameList::ConsiderViewChange);
  32. addWidget(m_table);
  33. addWidget(m_list);
  34. addWidget(m_empty);
  35. m_prefer_table = Settings().GetPreferredView();
  36. ConsiderViewChange();
  37. }
  38. void GameList::MakeTableView()
  39. {
  40. m_table = new QTableView(this);
  41. m_table->setModel(m_table_proxy);
  42. m_table->setItemDelegate(m_delegate);
  43. m_table->setSelectionMode(QAbstractItemView::SingleSelection);
  44. m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
  45. m_table->setAlternatingRowColors(true);
  46. m_table->setShowGrid(false);
  47. m_table->setSortingEnabled(true);
  48. m_table->setCurrentIndex(QModelIndex());
  49. m_table->setContextMenuPolicy(Qt::CustomContextMenu);
  50. connect(m_table, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
  51. // TODO load from config
  52. m_table->setColumnHidden(GameListModel::COL_PLATFORM, false);
  53. m_table->setColumnHidden(GameListModel::COL_ID, true);
  54. m_table->setColumnHidden(GameListModel::COL_BANNER, false);
  55. m_table->setColumnHidden(GameListModel::COL_TITLE, false);
  56. m_table->setColumnHidden(GameListModel::COL_DESCRIPTION, true);
  57. m_table->setColumnHidden(GameListModel::COL_MAKER, false);
  58. m_table->setColumnHidden(GameListModel::COL_SIZE, false);
  59. m_table->setColumnHidden(GameListModel::COL_COUNTRY, false);
  60. m_table->setColumnHidden(GameListModel::COL_RATING, false);
  61. QHeaderView* hor_header = m_table->horizontalHeader();
  62. hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::ResizeToContents);
  63. hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::ResizeToContents);
  64. hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::ResizeToContents);
  65. hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::ResizeToContents);
  66. hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
  67. hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Stretch);
  68. hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::ResizeToContents);
  69. hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Stretch);
  70. hor_header->setSectionResizeMode(GameListModel::COL_RATING, QHeaderView::ResizeToContents);
  71. QHeaderView* ver_header = m_table->verticalHeader();
  72. ver_header->setSectionResizeMode(QHeaderView::ResizeToContents);
  73. }
  74. void GameList::MakeEmptyView()
  75. {
  76. m_empty = new QLabel(this);
  77. m_empty->setText(tr("Dolphin did not find any game files.\n"
  78. "Open the Paths dialog to add game folders."));
  79. m_empty->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  80. }
  81. void GameList::MakeListView()
  82. {
  83. m_list = new QListView(this);
  84. m_list->setModel(m_list_proxy);
  85. m_list->setViewMode(QListView::IconMode);
  86. m_list->setResizeMode(QListView::Adjust);
  87. m_list->setUniformItemSizes(true);
  88. m_list->setContextMenuPolicy(Qt::CustomContextMenu);
  89. connect(m_list, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
  90. }
  91. void GameList::ShowContextMenu(const QPoint&)
  92. {
  93. QMenu* menu = new QMenu(this);
  94. DiscIO::Platform platform = GameFile(GetSelectedGame()).GetPlatformID();
  95. if (platform == DiscIO::Platform::GAMECUBE_DISC || platform == DiscIO::Platform::WII_DISC)
  96. {
  97. menu->addAction(tr("Properties"), this, SLOT(OpenProperties()));
  98. menu->addAction(tr("Open Wiki Page"), this, SLOT(OpenWiki()));
  99. menu->addAction(tr("Set as Default ISO"), this, SLOT(SetDefaultISO()));
  100. }
  101. else
  102. {
  103. return;
  104. }
  105. menu->exec(QCursor::pos());
  106. }
  107. void GameList::OpenProperties()
  108. {
  109. PropertiesDialog* properties = new PropertiesDialog(this, GameFile(GetSelectedGame()));
  110. properties->show();
  111. }
  112. void GameList::OpenWiki()
  113. {
  114. QString game_id = GameFile(GetSelectedGame()).GetUniqueID();
  115. QString url = QStringLiteral("https://wiki.dolphin-emu.org/index.php?title=").append(game_id);
  116. QDesktopServices::openUrl(QUrl(url));
  117. }
  118. void GameList::SetDefaultISO()
  119. {
  120. Settings().SetDefaultGame(GetSelectedGame());
  121. }
  122. QString GameList::GetSelectedGame() const
  123. {
  124. QAbstractItemView* view;
  125. QSortFilterProxyModel* proxy;
  126. if (currentWidget() == m_table)
  127. {
  128. view = m_table;
  129. proxy = m_table_proxy;
  130. }
  131. else
  132. {
  133. view = m_list;
  134. proxy = m_list_proxy;
  135. }
  136. QItemSelectionModel* sel_model = view->selectionModel();
  137. if (sel_model->hasSelection())
  138. {
  139. QModelIndex model_index = proxy->mapToSource(sel_model->selectedIndexes()[0]);
  140. return m_model->GetPath(model_index.row());
  141. }
  142. return QStringLiteral("");
  143. }
  144. void GameList::SetPreferredView(bool table)
  145. {
  146. m_prefer_table = table;
  147. Settings().SetPreferredView(table);
  148. ConsiderViewChange();
  149. }
  150. void GameList::ConsiderViewChange()
  151. {
  152. if (m_model->rowCount(QModelIndex()) > 0)
  153. {
  154. if (m_prefer_table)
  155. setCurrentWidget(m_table);
  156. else
  157. setCurrentWidget(m_list);
  158. }
  159. else
  160. {
  161. setCurrentWidget(m_empty);
  162. }
  163. }
  164. void GameList::keyReleaseEvent(QKeyEvent* event)
  165. {
  166. if (event->key() == Qt::Key_Return)
  167. emit GameSelected();
  168. else
  169. QStackedWidget::keyReleaseEvent(event);
  170. }