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
11#include <mgba/core/interface.h>
12
13using namespace QGBA;
14
15Swatch::Swatch(QWidget* parent)
16 : QWidget(parent)
17{
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 setMinimumSize(size * (m_size + 1) - QSize(1, 1));
35}
36
37void Swatch::setColor(int index, uint16_t color) {
38 m_colors[index].setRgb(
39 M_R8(color),
40 M_G8(color),
41 M_B8(color));
42 updateFill(index);
43}
44
45void Swatch::paintEvent(QPaintEvent* event) {
46 QPainter painter(this);
47 painter.drawPixmap(QPoint(), m_backing);
48}
49
50void Swatch::mousePressEvent(QMouseEvent* event) {
51 int x = event->x() / (m_size + 1);
52 int y = event->y() / (m_size + 1);
53 emit indexPressed(y * m_dims.width() + x);
54}
55
56void Swatch::updateFill(int index) {
57 QPainter painter(&m_backing);
58 int x = index % m_dims.width();
59 int y = index / m_dims.width();
60 QRect r(x * (m_size + 1), y * (m_size + 1), m_size, m_size);
61 painter.fillRect(r, m_colors[index]);
62 update(r);
63}