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{
19 QPalette palette = parent->palette();
20 palette.setColor(parent->backgroundRole(), defaultColor);
21 parent->setPalette(palette);
22 parent->installEventFilter(this);
23}
24
25ColorPicker& ColorPicker::operator=(const ColorPicker& other) {
26 if (m_parent) {
27 m_parent->removeEventFilter(this);
28 }
29 m_parent = other.m_parent;
30 m_parent->installEventFilter(this);
31
32 return *this;
33}
34
35bool ColorPicker::eventFilter(QObject* obj, QEvent* event) {
36 if (event->type() != QEvent::MouseButtonRelease) {
37 return false;
38 }
39 int colorId;
40 if (obj != m_parent) {
41 return false;
42 }
43
44 QWidget* swatch = static_cast<QWidget*>(obj);
45
46 QColorDialog* colorPicker = new QColorDialog;
47 colorPicker->setAttribute(Qt::WA_DeleteOnClose);
48 colorPicker->open();
49 connect(colorPicker, &QColorDialog::colorSelected, [this, swatch](const QColor& color) {
50 QPalette palette = swatch->palette();
51 palette.setColor(swatch->backgroundRole(), color);
52 swatch->setPalette(palette);
53 emit colorChanged(color);
54 });
55 return true;
56}