src/platform/qt/ShortcutModel.cpp (view raw)
1/* Copyright (c) 2013-2019 Jeffrey Pfau
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 "ShortcutModel.h"
7
8#include "ShortcutController.h"
9
10using namespace QGBA;
11
12ShortcutModel::ShortcutModel(QObject* parent)
13 : QAbstractItemModel(parent)
14{
15}
16
17void ShortcutModel::setController(ShortcutController* controller) {
18 beginResetModel();
19 m_controller = controller;
20 m_cache.clear();
21 connect(controller, &ShortcutController::shortcutAdded, this, &ShortcutModel::addRowNamed);
22 connect(controller, &ShortcutController::menuCleared, this, &ShortcutModel::clearMenu);
23 endResetModel();
24}
25
26QVariant ShortcutModel::data(const QModelIndex& index, int role) const {
27 if (role != Qt::DisplayRole || !index.isValid()) {
28 return QVariant();
29 }
30 const Item* item = static_cast<Item*>(index.internalPointer());
31 const Shortcut* shortcut = item->shortcut;
32 switch (index.column()) {
33 case 0:
34 return m_controller->visibleName(item->name);
35 case 1:
36 return shortcut ? QKeySequence(shortcut->shortcut()).toString(QKeySequence::NativeText) : QVariant();
37 case 2:
38 if (!shortcut) {
39 return QVariant();
40 }
41 if (shortcut->button() >= 0) {
42 return shortcut->button();
43 }
44 if (shortcut->axis() >= 0) {
45 char d = '\0';
46 if (shortcut->direction() == GamepadAxisEvent::POSITIVE) {
47 d = '+';
48 }
49 if (shortcut->direction() == GamepadAxisEvent::NEGATIVE) {
50 d = '-';
51 }
52 return QString("%1%2").arg(d).arg(shortcut->axis());
53 }
54 break;
55 }
56 return QVariant();
57}
58
59QVariant ShortcutModel::headerData(int section, Qt::Orientation orientation, int role) const {
60 if (role != Qt::DisplayRole) {
61 return QAbstractItemModel::headerData(section, orientation, role);
62 }
63 if (orientation == Qt::Horizontal) {
64 switch (section) {
65 case 0:
66 return tr("Action");
67 case 1:
68 return tr("Keyboard");
69 case 2:
70 return tr("Gamepad");
71 }
72 }
73 return section;
74}
75
76QModelIndex ShortcutModel::index(int row, int column, const QModelIndex& parent) const {
77 QString pmenu;
78 if (parent.isValid()) {
79 pmenu = static_cast<Item*>(parent.internalPointer())->name;
80 }
81 QString name = m_controller->name(row, pmenu);
82 Item* item = &m_cache[name];
83 item->name = name;
84 item->shortcut = m_controller->shortcut(name);
85 return createIndex(row, column, item);
86}
87
88QModelIndex ShortcutModel::parent(const QModelIndex& index) const {
89 if (!index.isValid() || !index.internalPointer()) {
90 return QModelIndex();
91 }
92 Item* item = static_cast<Item*>(index.internalPointer());
93 QString parent = m_controller->parent(item->name);
94 if (parent.isNull()) {
95 return QModelIndex();
96 }
97 Item* pitem = &m_cache[parent];
98 pitem->name = parent;
99 pitem->shortcut = m_controller->shortcut(parent);
100 return createIndex(m_controller->indexIn(parent), 0, pitem);
101}
102
103int ShortcutModel::columnCount(const QModelIndex&) const {
104 return 3;
105}
106
107int ShortcutModel::rowCount(const QModelIndex& index) const {
108 if (!index.isValid()) {
109 return m_controller->count();
110 }
111 Item* item = static_cast<Item*>(index.internalPointer());
112 return m_controller->count(item->name);
113}
114
115QString ShortcutModel::name(const QModelIndex& index) const {
116 if (!index.isValid()) {
117 return {};
118 }
119 Item* item = static_cast<Item*>(index.internalPointer());
120 return item->name;
121}
122
123void ShortcutModel::addRowNamed(const QString& name) {
124 QString parent = m_controller->parent(name);
125 Item* item = &m_cache[parent];
126 item->name = parent;
127 item->shortcut = m_controller->shortcut(parent);
128 int index = m_controller->indexIn(name);
129 beginInsertRows(createIndex(m_controller->indexIn(parent), 0, item), index, index + 1);
130 endInsertRows();
131}
132
133void ShortcutModel::clearMenu(const QString&) {
134 // TODO
135 beginResetModel();
136 endResetModel();
137}