diff --git a/OpenRGB.pro b/OpenRGB.pro index c786b8d6..b218e1ed 100644 --- a/OpenRGB.pro +++ b/OpenRGB.pro @@ -48,6 +48,7 @@ DEFINES += #-----------------------------------------------------------------------------------------------# INCLUDEPATH += \ dependencies/ColorWheel \ + dependencies/Swatches/ \ dependencies/CRCpp/ \ dependencies/hueplusplus-1.0.0/include \ dependencies/hueplusplus-1.0.0/include/hueplusplus \ @@ -135,6 +136,7 @@ INCLUDEPATH += HEADERS += \ dependencies/ColorWheel/ColorWheel.h \ + dependencies/Swatches/swatches.h \ dependencies/json/json.hpp \ dependencies/libcmmk/include/libcmmk/libcmmk.h \ LogManager.h \ @@ -397,6 +399,7 @@ HEADERS += RGBController/RGBController_Network.h \ SOURCES += \ + dependencies/Swatches/swatches.cpp \ dependencies/dmiinfo.cpp \ dependencies/ColorWheel/ColorWheel.cpp \ dependencies/hueplusplus-1.0.0/src/Action.cpp \ diff --git a/dependencies/Swatches/swatches.cpp b/dependencies/Swatches/swatches.cpp new file mode 100644 index 00000000..5c6ea951 --- /dev/null +++ b/dependencies/Swatches/swatches.cpp @@ -0,0 +1,193 @@ +/*-------------------------------------------------------------------*\ +| Swatches.cpp | +| | +| Custom Colour Swatch widget that allows for adding user colours | +| | +| Chris M (Dr_No) 23rd April 2021 | +| | +\*-------------------------------------------------------------------*/ + +#include "swatches.h" +#include +#include +#include + +Swatches::Swatches(QWidget *parent) : + QWidget(parent), + initSize(width_inc_margin * minColumns, height_inc_margin * minRows) +{ + setBaseSize(initSize); + setSizeIncrement(width_inc_margin, height_inc_margin); + add_swatch.color.setRgb( 0, 0, 0, 0); //transparent + + /*-----------------------------------------------------*\ + | Add default swatches to the list | + \*-----------------------------------------------------*/ + swatch black_swatch; + black_swatch.color.setRgb(0, 0, 0, 255); + swatch_list.push_back(black_swatch); + + swatch red_swatch; + red_swatch.color.setRgb(255, 0, 0, 255); + swatch_list.push_back(red_swatch); + + swatch yellow_swatch; + yellow_swatch.color.setRgb(255, 255, 0, 255); + swatch_list.push_back(yellow_swatch); + + swatch green_swatch; + green_swatch.color.setRgb( 0, 255, 0, 255); + swatch_list.push_back(green_swatch); + + swatch cyan_swatch; + cyan_swatch.color.setRgb( 0, 255, 255, 255); + swatch_list.push_back(cyan_swatch); + + swatch blue_swatch; + blue_swatch.color.setRgb( 0, 0, 255, 255); + swatch_list.push_back(blue_swatch); + + swatch magenta_swatch; + magenta_swatch.color.setRgb(255, 0, 255, 255); + swatch_list.push_back(magenta_swatch); + + swatch white_swatch; + white_swatch.color.setRgb(255, 255, 255, 255); + swatch_list.push_back(white_swatch); + + min_swatches = swatch_list.size(); +} + +QColor Swatches::color() +{ + return swatch_list[selected].color.toRgb(); +} + +QSize Swatches::sizeHint () const +{ + return QSize(width(), height()); +} + +QSize Swatches::minimumSizeHint () const +{ + return baseSize(); +} + +void Swatches::setCurrentColor(const QColor &color) +{ + if(color == picker_color) + { + return; + } + + picker_color = color; +} + +void Swatches::addCustomSwatch(const QColor &color) +{ + swatch new_swatch; + new_swatch.color = color; + swatch_list.push_back(new_swatch); + update(); +} + +void Swatches::resizeEvent(QResizeEvent *event) +{ + swatch_pixmap = QPixmap(event->size()); + swatch_pixmap.fill(Qt::transparent); + drawSwatches(event->size()); + update(); +} + +void Swatches::mousePressEvent(QMouseEvent* /*event*/) +{ + mouseDown = true; +} + +void Swatches::mouseReleaseEvent(QMouseEvent* event) +{ + if(!mouseDown) + { + return; + } + + /*-----------------------------------------------------*\ + | Clear mouse down and in-region flags | + \*-----------------------------------------------------*/ + mouseDown = false; + + if(add_swatch.region.contains(event->pos())) + { + addCustomSwatch(picker_color); + } + else + { + int swatch_count = swatch_list.size(); + for(int i = 0; i < swatch_count; i++) + { + if(swatch_list[i].region.contains(event->pos())) + { + emit swatchChanged(swatch_list[i].color.toRgb()); + break; + } + } + } +} + +void Swatches::drawSwatches(const QSize &newSize) +{ + /*-----------------------------------------------------*\ + | Create image canvas & paint background transparent | + \*-----------------------------------------------------*/ + swatch_image = QImage(newSize, QImage::Format_ARGB32_Premultiplied); + swatch_image.fill(Qt::transparent); + + /*-----------------------------------------------------*\ + | Set up painter | + \*-----------------------------------------------------*/ + QPainter painter(&swatch_image); + painter.setPen(border_pen); + + /*-----------------------------------------------------*\ + | Paint the swatch cluster | + \*-----------------------------------------------------*/ + int width = qMin(width_inc_margin, (newSize.width() / minColumns) - (margin * 2)); + int height = minSize; + width_inc_margin = width + (margin * 2); + height_inc_margin = height + (margin * 2); + int swatch_count = swatch_list.size(); + + QPoint pointNewSwatch(margin, margin); + for(int i = 0; i < swatch_count; i++) + { + QBrush brush(swatch_list[i].color, Qt::SolidPattern); + swatch_list[i].region.setSize(QSize(width, height)); + swatch_list[i].region.marginsAdded(QMargins(margin, margin, margin, margin)); + swatch_list[i].region.moveTo(((i % minColumns) * width_inc_margin), ((i / minColumns) * height_inc_margin)); + + painter.setBrush(brush); + painter.drawRect(swatch_list[i].region); + } + + //QBrush brush(add_swatch.color); + //add_swatch.region.setSize(QSize(width, height)); + //add_swatch.region.marginsAdded(QMargins(margin, margin, margin, margin)); + //add_swatch.region.moveTo(((swatch_count % minColumns) * width_inc_margin), ((swatch_count / minColumns) * height_inc_margin)); + + //painter.setBrush(brush); + //painter.drawRect(add_swatch.region); + //painter.drawText(add_swatch.region, Qt::AlignCenter, QString("+")); + + swatch_pixmap = QPixmap().fromImage(swatch_image); +} + + +void Swatches::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + QStyleOption opt; + opt.initFrom(this); + drawSwatches(this->size()); //This is the main draw function + painter.drawPixmap(0,0,swatch_pixmap); + style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); +} diff --git a/dependencies/Swatches/swatches.h b/dependencies/Swatches/swatches.h new file mode 100644 index 00000000..dfe745e1 --- /dev/null +++ b/dependencies/Swatches/swatches.h @@ -0,0 +1,71 @@ +/*-------------------------------------------------------------------*\ +| Swatches.cpp | +| | +| Custom Colour Swatch widget that allows for adding user colours | +| | +| Chris M (Dr_No) 23rd April 2021 | +| | +\*-------------------------------------------------------------------*/ + +#ifndef SWATCHES_H +#define SWATCHES_H + +#include +#include +#include + +class Swatches : public QWidget +{ + Q_OBJECT +public: + explicit Swatches(QWidget *parent = nullptr); + + virtual QSize sizeHint () const; + virtual QSize minimumSizeHint () const; + QColor color(); + +signals: + void swatchChanged(const QColor color); + //void customSwatches(const QVector swatch); + +public slots: + void addCustomSwatch(const QColor &color); + //void addCustomSwatches(const QVector &swatch); + void setCurrentColor(const QColor &color); + +protected: + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *); + void resizeEvent(QResizeEvent *event); + void paintEvent(QPaintEvent *); + +private: + const int minSize = 20; + const int minRows = 1; + const int minColumns = 8; + const int margin = 2; + const float corner_radius = 0.0f; + bool mouseDown = false; + int selected = 0; + int width_inc_margin = minSize + (margin * 2); + int height_inc_margin = minSize + (margin * 2); + int min_swatches; + + struct swatch + { + QColor color = QColor(0, 0, 0); + QRect region = QRect(0, 0, 25, 25); + }; + + swatch add_swatch; + QPen border_pen = QColor(128,128,128); //Grey50 + QVector swatch_list; + QColor picker_color; + QSize initSize; + QPixmap swatch_pixmap; + QImage swatch_image; + + void drawSwatches(const QSize &newSize); +}; + +#endif // SWATCHES_H diff --git a/qt/OpenRGBDevicePage.cpp b/qt/OpenRGBDevicePage.cpp index a0e4b2d7..664a4722 100644 --- a/qt/OpenRGBDevicePage.cpp +++ b/qt/OpenRGBDevicePage.cpp @@ -85,49 +85,6 @@ OpenRGBDevicePage::OpenRGBDevicePage(RGBController *dev, QWidget *parent) : ui->DeviceViewBox->setController(device); ui->DeviceViewBox->hide(); - - /*-----------------------------------------------------*\ - | Set up the color palette buttons | - \*-----------------------------------------------------*/ - ui->ButtonBlack->setStyleSheet("QPushButton {background-color: rgb(0,0,0); color: rgb(0,0,0); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonBlack->setFlat(true); - ui->ButtonBlack->setMinimumWidth(20); - ui->ButtonBlack->update(); - - ui->ButtonRed->setStyleSheet("QPushButton {background-color: rgb(255,0,0); color: rgb(255,0,0); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonRed->setFlat(true); - ui->ButtonRed->setMinimumWidth(20); - ui->ButtonRed->update(); - - ui->ButtonYellow->setStyleSheet("QPushButton {background-color: rgb(255,255,0); color: rgb(255,255,0); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonYellow->setFlat(true); - ui->ButtonYellow->setMinimumWidth(20); - ui->ButtonYellow->update(); - - ui->ButtonGreen->setStyleSheet("QPushButton {background-color: rgb(0,255,0); color: rgb(0,255,0); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonGreen->setFlat(true); - ui->ButtonGreen->setMinimumWidth(20); - ui->ButtonGreen->update(); - - ui->ButtonCyan->setStyleSheet("QPushButton {background-color: rgb(0,255,255); color: rgb(0,255,255); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonCyan->setFlat(true); - ui->ButtonCyan->setMinimumWidth(20); - ui->ButtonCyan->update(); - - ui->ButtonBlue->setStyleSheet("QPushButton {background-color: rgb(0,0,255); color: rgb(0,0,255); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonBlue->setFlat(true); - ui->ButtonBlue->setMinimumWidth(20); - ui->ButtonBlue->update(); - - ui->ButtonMagenta->setStyleSheet("QPushButton {background-color: rgb(255,0,255); color: rgb(255,0,255); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonMagenta->setFlat(true); - ui->ButtonMagenta->setMinimumWidth(20); - ui->ButtonMagenta->update(); - - ui->ButtonWhite->setStyleSheet("QPushButton {background-color: rgb(255,255,255); color: rgb(255,255,255); border: 1px solid rgb(128, 128, 128); padding-top: 1px; padding-bottom: 1px;}"); - ui->ButtonWhite->setFlat(true); - ui->ButtonWhite->setMinimumWidth(20); - ui->ButtonWhite->update(); /*-----------------------------------------------------*\ | Fill in the mode selection box | @@ -934,44 +891,21 @@ void Ui::OpenRGBDevicePage::SetCustomMode(unsigned char red, unsigned char green UpdateMode(); } -void Ui::OpenRGBDevicePage::on_ButtonBlack_clicked() +void Ui::OpenRGBDevicePage::on_SwatchBox_swatchChanged(const QColor color) { - SetDevice(0, 0, 0); -} + if(UpdatingColor) + { + return; + } -void Ui::OpenRGBDevicePage::on_ButtonRed_clicked() -{ - SetDevice(255, 0, 0); -} + UpdatingColor = true; + ui->RedSpinBox->setValue(color.red()); + ui->GreenSpinBox->setValue(color.green()); + ui->BlueSpinBox->setValue(color.blue()); + UpdatingColor = false; -void Ui::OpenRGBDevicePage::on_ButtonYellow_clicked() -{ - SetDevice(255, 255, 0); -} - -void Ui::OpenRGBDevicePage::on_ButtonGreen_clicked() -{ - SetDevice(0, 255, 0); -} - -void Ui::OpenRGBDevicePage::on_ButtonCyan_clicked() -{ - SetDevice(0, 255, 255); -} - -void Ui::OpenRGBDevicePage::on_ButtonBlue_clicked() -{ - SetDevice(0, 0, 255); -} - -void Ui::OpenRGBDevicePage::on_ButtonMagenta_clicked() -{ - SetDevice(255, 0, 255); -} - -void Ui::OpenRGBDevicePage::on_ButtonWhite_clicked() -{ - SetDevice(255, 255, 255); + ui->ColorWheelBox->setColor(color); + updateDeviceView(); } void Ui::OpenRGBDevicePage::on_ColorWheelBox_colorChanged(const QColor color) @@ -988,6 +922,8 @@ void Ui::OpenRGBDevicePage::on_ColorWheelBox_colorChanged(const QColor color) UpdatingColor = false; updateHSV(); + + ui->SwatchBox->setCurrentColor(color); updateDeviceView(); } diff --git a/qt/OpenRGBDevicePage.h b/qt/OpenRGBDevicePage.h index bbc6d50e..194fb8e7 100644 --- a/qt/OpenRGBDevicePage.h +++ b/qt/OpenRGBDevicePage.h @@ -32,6 +32,7 @@ private slots: void UpdateInterface(); void on_ColorWheelBox_colorChanged(const QColor color); + void on_SwatchBox_swatchChanged(const QColor color); void on_DirectionBox_currentIndexChanged(int index); void on_ZoneBox_currentIndexChanged(int index); void on_LEDBox_currentIndexChanged(int index); @@ -45,15 +46,6 @@ private slots: void on_ValSpinBox_valueChanged(int arg1); void on_DeviceViewBox_selectionChanged(QVector); - void on_ButtonBlack_clicked(); - void on_ButtonRed_clicked(); - void on_ButtonYellow_clicked(); - void on_ButtonGreen_clicked(); - void on_ButtonCyan_clicked(); - void on_ButtonBlue_clicked(); - void on_ButtonMagenta_clicked(); - void on_ButtonWhite_clicked(); - void on_SetAllButton_clicked(); void on_RandomCheck_clicked(); void on_PerLEDCheck_clicked(); diff --git a/qt/OpenRGBDevicePage.ui b/qt/OpenRGBDevicePage.ui index 93ae55d3..0cf1bc52 100644 --- a/qt/OpenRGBDevicePage.ui +++ b/qt/OpenRGBDevicePage.ui @@ -6,7 +6,7 @@ 0 0 - 800 + 843 374 @@ -14,10 +14,61 @@ Frame - - + + - Select All + Per-LED + + + + + + + Mode-Specific + + + + + + + Zone: + + + + + + + Apply Colors To Selection + + + + + + + + + + 359 + + + + + + + 255 + + + + + + + + + + + 0 + 0 + @@ -37,13 +88,44 @@ - - + + - Resize + V: + + + + Select All + + + + + + + G: + + + + + + + H: + + + + + + + 255 + + + + + + @@ -51,10 +133,17 @@ - - + + - Per-LED + B: + + + + + + + S: @@ -65,27 +154,17 @@ - - + + - LED: + R: - - - - Mode: - - - - - - - - - - Apply Colors To Selection + + + + Qt::Horizontal @@ -96,10 +175,10 @@ - - - - Qt::Horizontal + + + + 255 @@ -113,22 +192,19 @@ - - - - Zone: - - - - - - - - - + + + + + + + Resize + + + @@ -136,41 +212,6 @@ - - - - Mode-Specific - - - - - - - B: - - - - - - - G: - - - - - - - R: - - - - - - - 255 - - - @@ -178,45 +219,10 @@ - - - - 255 - - - - - + + - V: - - - - - - - S: - - - - - - - H: - - - - - - - 359 - - - - - - - 255 + LED: @@ -227,73 +233,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 0 - 0 - + + + + Mode: @@ -315,6 +258,16 @@
DeviceView.h
1 + + Swatches + QWidget +
swatches.h
+ 1 + + swatchChanged(QColor) + currentColorInput(QColor) + +
LEDBox @@ -331,14 +284,6 @@ HueSpinBox SatSpinBox ValSpinBox - ButtonBlack - ButtonRed - ButtonYellow - ButtonGreen - ButtonCyan - ButtonBlue - ButtonMagenta - ButtonWhite