all repos — mgba @ 16131c9702ca1469008f2263dc990be369049b09

mGBA Game Boy Advance Emulator

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

 1/* Copyright (c) 2013-2017 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 "ColorPicker.h"
 7
 8#include <QColorDialog>
 9#include <QEvent>
10
11using namespace QGBA;
12
13ColorPicker::ColorPicker() {
14}
15
16ColorPicker::ColorPicker(QWidget* parent, const QColor& defaultColor)
17	: m_parent(parent)
18	, m_defaultColor(defaultColor)
19{
20	QPalette palette = parent->palette();
21	palette.setColor(parent->backgroundRole(), defaultColor);
22	parent->setPalette(palette);
23	parent->installEventFilter(this);
24}
25
26ColorPicker& ColorPicker::operator=(const ColorPicker& other) {
27	if (m_parent) {
28		m_parent->removeEventFilter(this);
29	}
30	m_parent = other.m_parent;
31	m_defaultColor = other.m_defaultColor;
32	m_parent->installEventFilter(this);
33
34	return *this;
35}
36
37bool ColorPicker::eventFilter(QObject* obj, QEvent* event) {
38	if (event->type() != QEvent::MouseButtonRelease) {
39		return false;
40	}
41	int colorId;
42	if (obj != m_parent) {
43		return false;
44	}
45
46	QWidget* swatch = static_cast<QWidget*>(obj);
47
48	QColorDialog* colorPicker = new QColorDialog;
49	colorPicker->setAttribute(Qt::WA_DeleteOnClose);
50	colorPicker->setCurrentColor(m_defaultColor);
51	colorPicker->open();
52	connect(colorPicker, &QColorDialog::colorSelected, [this, swatch](const QColor& color) {
53		m_defaultColor = color;
54		QPalette palette = swatch->palette();
55		palette.setColor(swatch->backgroundRole(), color);
56		swatch->setPalette(palette);
57		emit colorChanged(color);
58	});
59	return true;
60}