all repos — mgba @ cda804656b37925c35f2347be7b057fe2787f076

mGBA Game Boy Advance Emulator

src/platform/qt/Swatch.cpp (view raw)

 1/* Copyright (c) 2013-2015 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 "Swatch.h"
 7
 8#include <QMouseEvent>
 9#include <QPainter>
10
11using namespace QGBA;
12
13Swatch::Swatch(QWidget* parent)
14	: QLabel(parent)
15{
16	m_size = 10;
17	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
18}
19
20void Swatch::setSize(int size) {
21	m_size = size;
22	setDimensions(m_dims);
23}
24
25void Swatch::setDimensions(const QSize& size) {
26	m_dims = size;
27	m_backing = QPixmap(size * (m_size + 1) - QSize(1, 1));
28	m_backing.fill(Qt::transparent);
29	int elem = size.width() * size.height();
30	m_colors.resize(elem);
31	for (int i = 0; i < elem; ++i) {
32		updateFill(i);
33	}
34}
35
36void Swatch::setColor(int index, uint16_t color) {
37	m_colors[index].setRgb(
38		(color << 3) & 0xF8,
39		(color >> 2) & 0xF8,
40		(color >> 7) & 0xF8);
41	updateFill(index);
42}
43
44void Swatch::paintEvent(QPaintEvent* event) {
45	setPixmap(m_backing);
46	QLabel::paintEvent(event);
47}
48
49void Swatch::mousePressEvent(QMouseEvent* event) {
50	int x = event->x() / (m_size + 1);
51	int y = event->y() / (m_size + 1);
52	emit indexPressed(y * m_dims.width() + x);
53}
54
55void Swatch::updateFill(int index) {
56	QPainter painter(&m_backing);
57	int x = index % m_dims.width();
58	int y = index / m_dims.width();
59	QRect r(x * (m_size + 1), y * (m_size + 1), m_size, m_size);
60	painter.fillRect(r, m_colors[index]);
61	update(r);
62}