all repos — mgba @ 78e4083a562a1ce34a29dee8ecdb315401c9fb1a

mGBA Game Boy Advance Emulator

src/platform/qt/library/LibraryGrid.cpp (view raw)

 1/* Copyright (c) 2014-2017 waddlesplash
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "LibraryGrid.h"
 7
 8namespace QGBA {
 9
10LibraryGrid::LibraryGrid(LibraryController* parent)
11    : m_widget(new QListWidget(parent))
12{
13	m_widget->setObjectName("LibraryGrid");
14	m_widget->setWrapping(true);
15	m_widget->setResizeMode(QListView::Adjust);
16	m_widget->setUniformItemSizes(true);
17	setViewStyle(LibraryStyle::STYLE_GRID);
18
19	QObject::connect(m_widget, &QListWidget::itemActivated, parent, &LibraryController::startGame);
20}
21
22LibraryGrid::~LibraryGrid() {
23	delete m_widget;
24}
25
26LibraryEntryRef LibraryGrid::selectedEntry() {
27	if (!m_widget->selectedItems().empty()) {
28		return m_items.key(m_widget->selectedItems().at(0));
29	} else {
30		return LibraryEntryRef();
31	}
32}
33
34void LibraryGrid::selectEntry(LibraryEntryRef game) {
35	if (!game) {
36		return;
37	}
38	if (!m_widget->selectedItems().empty()) {
39		m_widget->selectedItems().at(0)->setSelected(false);
40	}
41	m_items.value(game)->setSelected(true);
42}
43
44void LibraryGrid::setViewStyle(LibraryStyle newStyle) {
45	if (newStyle == LibraryStyle::STYLE_GRID) {
46		m_currentStyle = LibraryStyle::STYLE_GRID;
47		m_widget->setIconSize(QSize(GRID_BANNER_WIDTH, GRID_BANNER_HEIGHT));
48		m_widget->setViewMode(QListView::IconMode);
49	} else {
50		m_currentStyle = LibraryStyle::STYLE_ICON;
51		m_widget->setIconSize(QSize(ICON_BANNER_WIDTH, ICON_BANNER_HEIGHT));
52		m_widget->setViewMode(QListView::ListMode);
53	}
54
55	// QListView resets this when you change the view mode, so let's set it again
56	m_widget->setDragEnabled(false);
57}
58
59void LibraryGrid::addEntry(LibraryEntryRef item) {
60	if (m_items.contains(item)) {
61		return;
62	}
63
64	QListWidgetItem* i = new QListWidgetItem;
65	i->setText(item->displayTitle());
66
67	m_widget->addItem(i);
68	m_items.insert(item, i);
69}
70
71void LibraryGrid::removeEntry(LibraryEntryRef entry) {
72	if (!m_items.contains(entry)) {
73		return;
74	}
75
76	delete m_items.take(entry);
77}
78
79}