PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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