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