diff --git a/RGBController/RGBController.cpp b/RGBController/RGBController.cpp index 1434bb79..4802f053 100644 --- a/RGBController/RGBController.cpp +++ b/RGBController/RGBController.cpp @@ -1328,9 +1328,31 @@ void RGBController::SetMode(int mode) UpdateMode(); } +void RGBController::RegisterUpdateCallback(RGBControllerCallback new_callback, void * new_callback_arg) +{ + UpdateCallbacks.push_back(new_callback); + UpdateCallbackArgs.push_back(new_callback_arg); +} + +void RGBController::SignalUpdate() +{ + UpdateMutex.lock(); + + /*-------------------------------------------------*\ + | Client info has changed, call the callbacks | + \*-------------------------------------------------*/ + for(unsigned int callback_idx = 0; callback_idx < UpdateCallbacks.size(); callback_idx++) + { + UpdateCallbacks[callback_idx](UpdateCallbackArgs[callback_idx]); + } + + UpdateMutex.unlock(); +} void RGBController::UpdateLEDs() { CallFlag_UpdateLEDs = true; + + SignalUpdate(); } void RGBController::UpdateMode() diff --git a/RGBController/RGBController.h b/RGBController/RGBController.h index 1aa54968..e4805eb8 100644 --- a/RGBController/RGBController.h +++ b/RGBController/RGBController.h @@ -14,6 +14,7 @@ #include #include #include +#include typedef unsigned int RGBColor; @@ -139,6 +140,8 @@ typedef struct matrix_map_type * matrix_map; /* Matrix map pointer */ } zone; +typedef void (*RGBControllerCallback)(void *); + class RGBController { public: @@ -188,6 +191,9 @@ public: unsigned char * GetSingleLEDColorDescription(int led); void SetSingleLEDColorDescription(unsigned char* data_buf); + void RegisterUpdateCallback(RGBControllerCallback new_callback, void * new_callback_arg); + void SignalUpdate(); + void UpdateLEDs(); //void UpdateZoneLEDs(int zone); //void UpdateSingleLED(int led); @@ -219,4 +225,8 @@ private: //bool CallFlag_UpdateZoneLEDs = false; //bool CallFlag_UpdateSingleLED = false; //bool CallFlag_UpdateMode = false; + + std::mutex UpdateMutex; + std::vector UpdateCallbacks; + std::vector UpdateCallbackArgs; }; diff --git a/qt/DeviceView.cpp b/qt/DeviceView.cpp index 982ecb33..c0b11a41 100644 --- a/qt/DeviceView.cpp +++ b/qt/DeviceView.cpp @@ -17,20 +17,9 @@ DeviceView::DeviceView(QWidget *parent) : QWidget(parent), initSize(128,128), - mouseDown(false), - margin(0), - wheelWidth(10), - current(Qt::red), - inWheel(false), - inSquare(false) + mouseDown(false) { controller = NULL; - current = current.toHsv(); -} - -QColor DeviceView::color() -{ - return current; } void DeviceView::setController(RGBController * controller_ptr) @@ -60,12 +49,7 @@ 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) @@ -81,19 +65,18 @@ void DeviceView::resizeEvent(QResizeEvent *event) size = event->size().height(); } - wheel = QPixmap(event->size()); - wheel.fill(Qt::transparent); update(); } -void DeviceView::paintEvent(QPaintEvent *) +void DeviceView::paintEvent(QPaintEvent *event) { - #define MAX_COLS 100 - + int width = event->rect().width(); + int height = event->rect().height(); int row = 0; int col = 0; int box_size = 20; int box_margin = 2; + int max_cols = ( width / (box_size + box_margin) ) - 1; QPainter painter(this); QStyleOption opt; @@ -109,7 +92,7 @@ void DeviceView::paintEvent(QPaintEvent *) painter.drawRect((col * (box_size + box_margin)), (row * (box_size + box_margin)), box_size, box_size); col++; - if(col > MAX_COLS) + if(col > max_cols) { row++; col = 0; diff --git a/qt/DeviceView.h b/qt/DeviceView.h index 11cc5c46..e4d73705 100644 --- a/qt/DeviceView.h +++ b/qt/DeviceView.h @@ -12,11 +12,8 @@ public: 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); @@ -26,20 +23,8 @@ protected: 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; diff --git a/qt/OpenRGBDevicePage.cpp b/qt/OpenRGBDevicePage.cpp index babce9ca..6c437947 100644 --- a/qt/OpenRGBDevicePage.cpp +++ b/qt/OpenRGBDevicePage.cpp @@ -4,6 +4,13 @@ using namespace Ui; +static void UpdateCallback(void * this_ptr) +{ + OpenRGBDevicePage * this_obj = (OpenRGBDevicePage *)this_ptr; + + QMetaObject::invokeMethod(this_obj, "UpdateInterface", Qt::QueuedConnection); +} + OpenRGBDevicePage::OpenRGBDevicePage(RGBController *dev, QWidget *parent) : QFrame(parent), ui(new Ui::OpenRGBDevicePageUi) @@ -22,6 +29,8 @@ OpenRGBDevicePage::OpenRGBDevicePage(RGBController *dev, QWidget *parent) : ui->widget->setController(device); + device->RegisterUpdateCallback(UpdateCallback, this); + pal = ui->ButtonRed->palette(); pal.setColor(QPalette::Button, QColor(255, 0, 0)); ui->ButtonRed->setAutoFillBackground(true); @@ -270,6 +279,12 @@ void Ui::OpenRGBDevicePage::on_DirectionBox_currentIndexChanged(int /*index*/) UpdateMode(); } +void Ui::OpenRGBDevicePage::UpdateInterface() +{ + UpdateModeUi(); + ui->widget->repaint(); +} + void Ui::OpenRGBDevicePage::UpdateModeUi() { /*-----------------------------------------------------*\ diff --git a/qt/OpenRGBDevicePage.h b/qt/OpenRGBDevicePage.h index cfb571a0..5974a0cf 100644 --- a/qt/OpenRGBDevicePage.h +++ b/qt/OpenRGBDevicePage.h @@ -60,6 +60,8 @@ private slots: void on_ColorWheelBox_colorChanged(const QColor color); + void UpdateInterface(); + private: Ui::OpenRGBDevicePageUi *ui; RGBController *device;