Path: blob/master/src/duckstation-qt/colorpickerbutton.cpp
7447 views
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <[email protected]>1// SPDX-License-Identifier: CC-BY-NC-ND-4.023#include "colorpickerbutton.h"45#include <QtGui/QPainter>6#include <QtWidgets/QColorDialog>7#include <QtWidgets/QStyle>8#include <QtWidgets/QStyleOptionButton>910#include "moc_colorpickerbutton.cpp"1112ColorPickerButton::ColorPickerButton(QWidget* parent) : QPushButton(parent)13{14connect(this, &QPushButton::clicked, this, &ColorPickerButton::onClicked);15}1617u32 ColorPickerButton::color()18{19return m_color;20}2122void ColorPickerButton::setColor(u32 rgb)23{24if (m_color == rgb)25return;2627m_color = rgb;28update();29}3031void ColorPickerButton::paintEvent(QPaintEvent* event)32{33Q_UNUSED(event);3435QPainter painter(this);36QStyleOptionButton option;37option.initFrom(this);3839if (isDown())40option.state |= QStyle::State_Sunken;41if (isDefault())42option.features |= QStyleOptionButton::DefaultButton;4344// Get the content rect (area inside the border)45const QRect contentRect = style()->subElementRect(QStyle::SE_PushButtonContents, &option, this);4647// Draw the button frame first (this includes the border but should not fill the interior)48style()->drawPrimitive(QStyle::PE_PanelButtonBevel, &option, &painter, this);4950// Fill the content area with our custom color51painter.fillRect(contentRect, QColor::fromRgb(m_color));5253// Draw the focus rectangle if needed54if (option.state & QStyle::State_HasFocus)55{56QStyleOptionFocusRect focusOption;57focusOption.initFrom(this);58focusOption.rect = style()->subElementRect(QStyle::SE_PushButtonFocusRect, &option, this);59style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOption, &painter, this);60}6162// Draw the button label (text/icon) on top63style()->drawControl(QStyle::CE_PushButtonLabel, &option, &painter, this);64}6566void ColorPickerButton::onClicked()67{68const u32 red = (m_color >> 16) & 0xff;69const u32 green = (m_color >> 8) & 0xff;70const u32 blue = m_color & 0xff;7172const QColor initial(QColor::fromRgb(red, green, blue));73const QColor selected(QColorDialog::getColor(initial, this, tr("Select LED Color")));7475// QColorDialog returns Invalid on cancel, and apparently initial == Invalid is true...76if (!selected.isValid() || initial == selected)77return;7879const u32 new_rgb = (static_cast<u32>(selected.red()) << 16) | (static_cast<u32>(selected.green()) << 8) |80static_cast<u32>(selected.blue());81m_color = new_rgb;82update();83emit colorChanged(new_rgb);84}858687