src/platform/qt/LogConfigModel.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 "LogConfigModel.h"
7
8#include <algorithm>
9
10using namespace QGBA;
11
12LogConfigModel::LogConfigModel(LogController* controller, QObject* parent)
13 : QAbstractItemModel(parent)
14 , m_controller(controller)
15{
16 for (int i = 0; mLogCategoryId(i); ++i) {
17 int levels = controller->levels(i);
18 m_cache.append({ i, mLogCategoryName(i), mLogCategoryId(i), levels ? levels : -1 });
19 }
20 std::sort(m_cache.begin(), m_cache.end());
21 m_levels = m_controller->levels();
22}
23
24QVariant LogConfigModel::data(const QModelIndex& index, int role) const {
25 if (role != Qt::CheckStateRole) {
26 return QVariant();
27 }
28 int levels;
29 if (index.row() == 0) {
30 levels = m_levels;
31 } else {
32 levels = m_cache[index.row() - 1].levels;
33 }
34 if (index.column() == 0) {
35 return levels < 0 ? Qt::Checked : Qt::Unchecked;
36 } else if (levels < 0 && index.row() > 0) {
37 return (m_levels >> (index.column() - 1)) & 1 ? Qt::PartiallyChecked : Qt::Unchecked;
38 } else {
39 return (levels >> (index.column() - 1)) & 1 ? Qt::Checked : Qt::Unchecked;
40 }
41}
42
43bool LogConfigModel::setData(const QModelIndex& index, const QVariant& value, int role) {
44 if (role != Qt::CheckStateRole) {
45 return false;
46 }
47 int levels;
48 if (index.row() == 0) {
49 levels = m_levels;
50 } else {
51 levels = m_cache[index.row() - 1].levels;
52 }
53 if (index.column() == 0) {
54 levels = -1;
55 } else {
56 if (levels < 0) {
57 levels = m_levels;
58 }
59 int bit = 1 << (index.column() - 1);
60 if (value.value<Qt::CheckState>() == Qt::Unchecked) {
61 levels &= ~bit;
62 } else {
63 levels |= bit;
64 }
65 }
66 if (index.row() == 0) {
67 beginResetModel();
68 m_levels = levels;
69 endResetModel();
70 } else {
71 m_cache[index.row() - 1].levels = levels;
72 emit dataChanged(createIndex(0, index.row(), nullptr), createIndex(8, index.row(), nullptr));
73 }
74 return true;
75}
76
77QVariant LogConfigModel::headerData(int section, Qt::Orientation orientation, int role) const {
78 if (role != Qt::DisplayRole) {
79 return QVariant();
80 }
81 if (orientation == Qt::Horizontal) {
82 switch (section) {
83 case 0:
84 return tr("Default");
85 case 1:
86 return tr("Fatal");
87 case 2:
88 return tr("Error");
89 case 3:
90 return tr("Warning");
91 case 4:
92 return tr("Info");
93 case 5:
94 return tr("Debug");
95 case 6:
96 return tr("Stub");
97 case 7:
98 return tr("Game Error");
99 default:
100 return QVariant();
101 }
102 } else if (section) {
103 return m_cache[section - 1].name;
104 } else {
105 return tr("Default");
106 }
107}
108
109QModelIndex LogConfigModel::index(int row, int column, const QModelIndex& parent) const {
110 if (parent.isValid()) {
111 return QModelIndex();
112 }
113 return createIndex(row, column, nullptr);
114}
115
116QModelIndex LogConfigModel::parent(const QModelIndex&) const {
117 return QModelIndex();
118}
119
120int LogConfigModel::columnCount(const QModelIndex& parent) const {
121 if (parent.isValid()) {
122 return 0;
123 }
124 return 8;
125}
126
127int LogConfigModel::rowCount(const QModelIndex& parent) const {
128 if (parent.isValid()) {
129 return 0;
130 }
131 return m_cache.size() + 1;
132}
133
134Qt::ItemFlags LogConfigModel::flags(const QModelIndex& index) const {
135 if (!index.isValid()) {
136 return 0;
137 }
138 return Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
139}
140
141void LogConfigModel::reset() {
142 beginResetModel();
143 for (auto& row : m_cache) {
144 row.levels = m_controller->levels(row.index);
145 if (!row.levels) {
146 row.levels = -1;
147 }
148 }
149 m_levels = m_controller->levels();
150 endResetModel();
151}
152
153void LogConfigModel::save(ConfigController* config) {
154 for (auto& row : m_cache) {
155 if (row.levels < 0) {
156 m_controller->clearLevels(row.index);
157 } else {
158 m_controller->setLevels(row.levels, row.index);
159 }
160 }
161 m_controller->setLevels(m_levels);
162 m_controller->save(config);
163}