duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

colorpickerbutton.cpp (1474B)


      1 // SPDX-FileCopyrightText: 2023 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #include "colorpickerbutton.h"
      5 #include "qtutils.h"
      6 
      7 #include <QtWidgets/QColorDialog>
      8 
      9 ColorPickerButton::ColorPickerButton(QWidget* parent) : QPushButton(parent)
     10 {
     11   connect(this, &QPushButton::clicked, this, &ColorPickerButton::onClicked);
     12   updateBackgroundColor();
     13 }
     14 
     15 u32 ColorPickerButton::color()
     16 {
     17   return m_color;
     18 }
     19 
     20 void ColorPickerButton::setColor(u32 rgb)
     21 {
     22   if (m_color == rgb)
     23     return;
     24 
     25   m_color = rgb;
     26   updateBackgroundColor();
     27 }
     28 
     29 void ColorPickerButton::updateBackgroundColor()
     30 {
     31   setStyleSheet(QStringLiteral("background-color: #%1;").arg(static_cast<uint>(m_color), 8, 16, QChar('0')));
     32 }
     33 
     34 void ColorPickerButton::onClicked()
     35 {
     36   const u32 red = (m_color >> 16) & 0xff;
     37   const u32 green = (m_color >> 8) & 0xff;
     38   const u32 blue = m_color & 0xff;
     39 
     40   const QColor initial(QColor::fromRgb(red, green, blue));
     41   const QColor selected(QColorDialog::getColor(initial, QtUtils::GetRootWidget(this), tr("Select LED Color")));
     42 
     43   // QColorDialog returns Invalid on cancel, and apparently initial == Invalid is true...
     44   if (!selected.isValid() || initial == selected)
     45     return;
     46 
     47   const u32 new_rgb = (static_cast<u32>(selected.red()) << 16) | (static_cast<u32>(selected.green()) << 8) |
     48                       static_cast<u32>(selected.blue());
     49   m_color = new_rgb;
     50   updateBackgroundColor();
     51   emit colorChanged(new_rgb);
     52 }