From 17f1390f36ccfbd2b7dd355b02dc852a7bb7b8fb Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Mon, 13 Jul 2020 11:26:54 -0500 Subject: [PATCH] Add widget that displays the current color of all LEDs on a controller --- OpenRGB.pro | 2 + qt/DeviceView.cpp | 123 +++++++++++++++++++++++++++++++++++++++ qt/DeviceView.h | 49 ++++++++++++++++ qt/OpenRGBDevicePage.cpp | 2 + qt/OpenRGBDevicePage.ui | 13 ++++- 5 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 qt/DeviceView.cpp create mode 100644 qt/DeviceView.h diff --git a/OpenRGB.pro b/OpenRGB.pro index 2a89cc35..e7d5a591 100644 --- a/OpenRGB.pro +++ b/OpenRGB.pro @@ -108,6 +108,7 @@ HEADERS += \ i2c_smbus/i2c_smbus.h \ i2c_tools/i2c_tools.h \ net_port/net_port.h \ + qt/DeviceView.h \ qt/OpenRGBDialog2.h \ qt/OpenRGBProfileSaveDialog.h \ qt/OpenRGBServerInfoPage.h \ @@ -246,6 +247,7 @@ SOURCES += \ i2c_smbus/i2c_smbus.cpp \ i2c_tools/i2c_tools.cpp \ net_port/net_port.cpp \ + qt/DeviceView.cpp \ qt/OpenRGBDialog2.cpp \ qt/OpenRGBProfileSaveDialog.cpp \ qt/OpenRGBServerInfoPage.cpp \ diff --git a/qt/DeviceView.cpp b/qt/DeviceView.cpp new file mode 100644 index 00000000..982ecb33 --- /dev/null +++ b/qt/DeviceView.cpp @@ -0,0 +1,123 @@ +/*-----------------------------------------------------*\ +| DeviceView.cpp | +| | +| OpenRGB Device view widget for Qt | +| | +| Adam Honse (calcprogrammer1@gmail.com) | +\*-----------------------------------------------------*/ + +#include "DeviceView.h" +#include "RGBController.h" +#include +#include +#include +#include +#include + +DeviceView::DeviceView(QWidget *parent) : + QWidget(parent), + initSize(128,128), + mouseDown(false), + margin(0), + wheelWidth(10), + current(Qt::red), + inWheel(false), + inSquare(false) +{ + controller = NULL; + current = current.toHsv(); +} + +QColor DeviceView::color() +{ + return current; +} + +void DeviceView::setController(RGBController * controller_ptr) +{ + controller = controller_ptr; +} + +QSize DeviceView::sizeHint () const +{ + return QSize(height(),height()); +} + +QSize DeviceView::minimumSizeHint () const +{ + return initSize; +} + +void DeviceView::mousePressEvent(QMouseEvent *event) +{ + +} + +void DeviceView::mouseMoveEvent(QMouseEvent *event) +{ + +} + +void DeviceView::mouseReleaseEvent(QMouseEvent *) +{ + /*-----------------------------------------------------*\ + | Clear mouse down and in-region flags | + \*-----------------------------------------------------*/ + mouseDown = false; + inWheel = false; + inSquare = false; +} + +void DeviceView::resizeEvent(QResizeEvent *event) +{ + unsigned int size = 0; + + if(event->size().width() < event->size().height()) + { + size = event->size().width(); + } + else + { + size = event->size().height(); + } + + wheel = QPixmap(event->size()); + wheel.fill(Qt::transparent); + update(); +} + +void DeviceView::paintEvent(QPaintEvent *) +{ + #define MAX_COLS 100 + + int row = 0; + int col = 0; + int box_size = 20; + int box_margin = 2; + + QPainter painter(this); + QStyleOption opt; + opt.initFrom(this); + + if(controller != NULL) + { + for(int zone_idx = 0; zone_idx < controller->zones.size(); zone_idx++) + { + for(int led_idx = 0; led_idx < controller->zones[zone_idx].leds_count; led_idx++) + { + painter.fillRect((col * (box_size + box_margin)), (row * (box_size + box_margin)), box_size, box_size, QColor::fromRgb(RGBGetRValue(controller->zones[zone_idx].colors[led_idx]), RGBGetGValue(controller->zones[zone_idx].colors[led_idx]), RGBGetBValue(controller->zones[zone_idx].colors[led_idx]))); + painter.drawRect((col * (box_size + box_margin)), (row * (box_size + box_margin)), box_size, box_size); + col++; + + if(col > MAX_COLS) + { + row++; + col = 0; + } + } + + row += 2; + col = 0; + } + } +} diff --git a/qt/DeviceView.h b/qt/DeviceView.h new file mode 100644 index 00000000..11cc5c46 --- /dev/null +++ b/qt/DeviceView.h @@ -0,0 +1,49 @@ +#ifndef DEVICEVIEW_H +#define DEVICEVIEW_H + +#include +#include "RGBController.h" + +class DeviceView : public QWidget +{ + Q_OBJECT +public: + explicit DeviceView(QWidget *parent = 0); + + virtual QSize sizeHint () const; + virtual QSize minimumSizeHint () const; + QColor color(); + + void setController(RGBController * controller_ptr); +signals: + void colorChanged(const QColor color); + +protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *); + void resizeEvent(QResizeEvent *event); + void paintEvent(QPaintEvent *); +private: + QSize initSize; + QImage wheelImage; + QImage squareImage; + QPixmap wheel; + bool mouseDown; + QPoint lastPos; + int margin; + int wheelWidth; + QRegion wheelRegion; + QRegion squareRegion; + QColor current; + bool inWheel; + bool inSquare; + int x_offset; + int y_offset; + + RGBController* controller; + + QColor posColor(const QPoint &point); +}; + +#endif // DEVICEVIEW_H diff --git a/qt/OpenRGBDevicePage.cpp b/qt/OpenRGBDevicePage.cpp index 0c8cb3fe..babce9ca 100644 --- a/qt/OpenRGBDevicePage.cpp +++ b/qt/OpenRGBDevicePage.cpp @@ -20,6 +20,8 @@ OpenRGBDevicePage::OpenRGBDevicePage(RGBController *dev, QWidget *parent) : \*-----------------------------------------------------*/ QPalette pal; + ui->widget->setController(device); + pal = ui->ButtonRed->palette(); pal.setColor(QPalette::Button, QColor(255, 0, 0)); ui->ButtonRed->setAutoFillBackground(true); diff --git a/qt/OpenRGBDevicePage.ui b/qt/OpenRGBDevicePage.ui index d2613666..018cf215 100644 --- a/qt/OpenRGBDevicePage.ui +++ b/qt/OpenRGBDevicePage.ui @@ -6,7 +6,7 @@ 0 0 - 556 + 687 300 @@ -264,7 +264,7 @@ - + @@ -274,6 +274,9 @@ + + + @@ -286,6 +289,12 @@ colorChanged(QColor) + + DeviceView + QWidget +
DeviceView.h
+ 1 +
ZoneBox