all repos — mgba @ 8f1148498e12197745f62e477d9b8e07382cc72e

mGBA Game Boy Advance Emulator

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		levels ^= 1 << (index.column() - 1);
 60	}
 61	if (index.row() == 0) {
 62		beginResetModel();
 63		m_levels = levels;
 64		endResetModel();
 65	} else {
 66		m_cache[index.row() - 1].levels = levels;
 67		emit dataChanged(createIndex(0, index.row(), nullptr), createIndex(8, index.row(), nullptr));
 68	}
 69	return true;
 70}
 71
 72QVariant LogConfigModel::headerData(int section, Qt::Orientation orientation, int role) const {
 73	if (role != Qt::DisplayRole) {
 74		return QVariant();
 75	}
 76	if (orientation == Qt::Horizontal) {
 77		switch (section) {
 78		case 0:
 79			return tr("Default");
 80		case 1:
 81			return tr("Fatal");
 82		case 2:
 83			return tr("Error");
 84		case 3:
 85			return tr("Warning");
 86		case 4:
 87			return tr("Info");
 88		case 5:
 89			return tr("Debug");
 90		case 6:
 91			return tr("Stub");
 92		case 7:
 93			return tr("Game Error");
 94		default:
 95			return QVariant();
 96		}
 97	} else if (section) {
 98		return m_cache[section - 1].name;
 99	} else {
100		return tr("Default");
101	}
102}
103
104QModelIndex LogConfigModel::index(int row, int column, const QModelIndex& parent) const {
105	return createIndex(row, column, nullptr);
106}
107
108QModelIndex LogConfigModel::parent(const QModelIndex& index) const {
109	return QModelIndex();
110}
111
112int LogConfigModel::columnCount(const QModelIndex& parent) const {
113	return 8;
114}
115
116int LogConfigModel::rowCount(const QModelIndex& parent) const {
117	return m_cache.size() + 1;
118}
119
120Qt::ItemFlags LogConfigModel::flags(const QModelIndex& index) const {
121	if (!index.isValid()) {
122		return 0;
123	}
124	return Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
125}
126
127void LogConfigModel::reset() {
128	beginResetModel();
129	for (auto& row : m_cache) {
130		row.levels = m_controller->levels(row.index);
131		if (!row.levels) {
132			row.levels = -1;
133		}
134	}
135	m_levels = m_controller->levels();
136	endResetModel();
137}
138
139void LogConfigModel::save(ConfigController* config) {
140	for (auto& row : m_cache) {
141		if (row.levels < 0) {
142			m_controller->clearLevels(row.index);
143		} else {
144			m_controller->setLevels(row.levels, row.index);
145		}
146	}
147	m_controller->setLevels(m_levels);
148	m_controller->save(config);
149}