From 9506bcb2bb3661425819e18c963de79b164beb80 Mon Sep 17 00:00:00 2001 From: Cyril Bosselut Date: Thu, 8 Feb 2024 14:03:20 +0000 Subject: [PATCH] Nanoleaf device configuration --- OpenRGB.pro | 3 + .../OpenRGBNanoleafNewDeviceDialog.cpp | 48 ++++++++++ .../OpenRGBNanoleafNewDeviceDialog.h | 36 ++++++++ .../OpenRGBNanoleafNewDeviceDialog.ui | 90 +++++++++++++++++++ .../OpenRGBNanoleafSettingsEntry.h | 4 +- .../OpenRGBNanoleafSettingsPage.cpp | 67 ++++++++++++++ .../OpenRGBNanoleafSettingsPage.h | 2 + .../OpenRGBNanoleafSettingsPage.ui | 16 +++- 8 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.cpp create mode 100644 qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.h create mode 100644 qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.ui diff --git a/OpenRGB.pro b/OpenRGB.pro index 1353027e..84e3317d 100644 --- a/OpenRGB.pro +++ b/OpenRGB.pro @@ -287,6 +287,7 @@ HEADERS += qt/OpenRGBKasaSmartSettingsPage/OpenRGBKasaSmartSettingsPage.h \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsEntry.h \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsPage.h \ + qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.h \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.h \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.h \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafScanningThread.h \ @@ -916,6 +917,7 @@ SOURCES += qt/OpenRGBKasaSmartSettingsPage/OpenRGBKasaSmartSettingsPage.cpp \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsEntry.cpp \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsPage.cpp \ + qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.cpp \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.cpp \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.cpp \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafScanningThread.cpp \ @@ -1640,6 +1642,7 @@ FORMS += qt/OpenRGBKasaSmartSettingsPage/OpenRGBKasaSmartSettingsPage.ui \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsEntry.ui \ qt/OpenRGBLIFXSettingsPage/OpenRGBLIFXSettingsPage.ui \ + qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.ui \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.ui \ qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.ui \ qt/OpenRGBPhilipsHueSettingsPage/OpenRGBPhilipsHueSettingsEntry.ui \ diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.cpp b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.cpp new file mode 100644 index 00000000..557268dd --- /dev/null +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.cpp @@ -0,0 +1,48 @@ +#include +#include "ResourceManager.h" +#include "OpenRGBNanoleafNewDeviceDialog.h" +#include "ui_OpenRGBNanoleafNewDeviceDialog.h" + +#ifdef _WIN32 +#include +#endif + +Ui::OpenRGBNanoleafNewDeviceDialog::OpenRGBNanoleafNewDeviceDialog(QWidget *parent) : + QDialog(parent), ui(new Ui::OpenRGBNanoleafNewDeviceDialogUi) +{ + ui->setupUi(this); + ui->devicePortEdit->setText("16021"); +} + +Ui::OpenRGBNanoleafNewDeviceDialog::~OpenRGBNanoleafNewDeviceDialog() +{ + delete ui; +} + +void Ui::OpenRGBNanoleafNewDeviceDialog::changeEvent(QEvent *event) +{ + if(event->type() == QEvent::LanguageChange) + { + ui->retranslateUi(this); + } +} + +NanoleafDevice Ui::OpenRGBNanoleafNewDeviceDialog::show() +{ + NanoleafDevice return_device; + + int result = this->exec(); + + if(result != QDialog::Rejected) + { + return_device.ip = ui->deviceIPEdit->text().toStdString(); + return_device.port = ui->devicePortEdit->text().toInt(); + } + else + { + return_device.ip = ""; + return_device.port = 0; + } + + return(return_device); +} diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.h b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.h new file mode 100644 index 00000000..32c96b95 --- /dev/null +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.h @@ -0,0 +1,36 @@ +#ifndef OPENRGBNANOLEAFNEWDEVICEDIALOG_H +#define OPENRGBNANOLEAFNEWDEVICEDIALOG_H + +#include +#include "ui_OpenRGBNanoleafNewDeviceDialog.h" +#include "OpenRGBDialog2.h" + +struct NanoleafDevice +{ + std::string ip; + unsigned int port; +}; + +namespace Ui +{ +class OpenRGBNanoleafNewDeviceDialog; +} + +class Ui::OpenRGBNanoleafNewDeviceDialog : public QDialog +{ + Q_OBJECT + +public: + explicit OpenRGBNanoleafNewDeviceDialog(QWidget *parent = nullptr); + ~OpenRGBNanoleafNewDeviceDialog(); + + NanoleafDevice show(); + +private: + Ui::OpenRGBNanoleafNewDeviceDialogUi *ui; + +private slots: + void changeEvent(QEvent *event); +}; + +#endif // OPENRGBNANOLEAFNEWDEVICEDIALOG_H diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.ui b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.ui new file mode 100644 index 00000000..ee4ca5a3 --- /dev/null +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafNewDeviceDialog.ui @@ -0,0 +1,90 @@ + + + OpenRGBNanoleafNewDeviceDialogUi + + + + 0 + 0 + 184 + 186 + + + + + 0 + 0 + + + + New Nanoleaf device + + + + + + IP address: + + + + + + + + + + Port: + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + OpenRGBNanoleafNewDeviceDialogUi + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + OpenRGBNanoleafNewDeviceDialogUi + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.h b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.h index 10f56bc4..7cfd6a6d 100644 --- a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.h +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsEntry.h @@ -19,6 +19,8 @@ public: OpenRGBNanoleafSettingsEntry(QString a_address, int a_port); ~OpenRGBNanoleafSettingsEntry(); Ui::OpenRGBNanoleafSettingsEntryUi *ui; + QString address; + int port; private slots: void changeEvent(QEvent *event); @@ -26,8 +28,6 @@ private slots: void on_PairButton_clicked(); private: - QString address; - int port; std::string auth_token; bool paired; }; diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.cpp b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.cpp index e53182a4..865ffb71 100644 --- a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.cpp +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.cpp @@ -1,7 +1,9 @@ #include "OpenRGBNanoleafSettingsPage.h" #include "ui_OpenRGBNanoleafSettingsPage.h" +#include "OpenRGBNanoleafNewDeviceDialog.h" #include "ResourceManager.h" #include "SettingsManager.h" +#include "LogManager.h" using json = nlohmann::json; @@ -53,6 +55,71 @@ void OpenRGBNanoleafSettingsPage::changeEvent(QEvent *event) } } +void Ui::OpenRGBNanoleafSettingsPage::on_AddNanoleafDeviceButton_clicked() +{ + /*-----------------------------------------------------*\ + | Open a popup to manually add a device by setting ip | + | and port | + \*-----------------------------------------------------*/ + OpenRGBNanoleafNewDeviceDialog dialog; + NanoleafDevice device = dialog.show(); + if(!device.ip.empty()) + { + LOG_TRACE("[%s] Add %s:%d", "Nanoleaf", device.ip.c_str(), device.port); + std::string location = device.ip+":"+std::to_string(device.port); + + if(entries.find(location) == entries.end()) + { + OpenRGBNanoleafSettingsEntry* entry = new OpenRGBNanoleafSettingsEntry(QString::fromUtf8(device.ip.c_str()), device.port); + + entries[location] = entry; + + QListWidgetItem* item = new QListWidgetItem; + + item->setSizeHint(entry->sizeHint()); + + ui->NanoleafDeviceList->addItem(item); + ui->NanoleafDeviceList->setItemWidget(item, entry); + ui->NanoleafDeviceList->show(); + + json nanoleaf_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("NanoleafDevices"); + nanoleaf_settings["devices"][location]["ip"] = device.ip; + nanoleaf_settings["devices"][location]["port"] = device.port; + ResourceManager::get()->GetSettingsManager()->SetSettings("NanoleafDevices", nanoleaf_settings); + ResourceManager::get()->GetSettingsManager()->SaveSettings(); + } + } +} + +void Ui::OpenRGBNanoleafSettingsPage::on_RemoveNanoleafDeviceButton_clicked() +{ + /*-------------------------------------------------*\ + | Remove the selected device | + \*-------------------------------------------------*/ + int cur_row = ui->NanoleafDeviceList->currentRow(); + + if(cur_row < 0) + { + return; + } + + QListWidgetItem* item = ui->NanoleafDeviceList->item(cur_row); + OpenRGBNanoleafSettingsEntry* entry = (OpenRGBNanoleafSettingsEntry*) ui->NanoleafDeviceList->itemWidget(item); + LOG_TRACE("[%s] Remove %s:%d", "Nanoleaf", entry->address.toStdString().c_str(), entry->port); + + ui->NanoleafDeviceList->removeItemWidget(item); + delete item; + + std::string location = entry->address.toStdString()+":"+std::to_string(entry->port); + delete entries[location]; + entries.erase(location); + + json nanoleaf_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("NanoleafDevices"); + nanoleaf_settings["devices"].erase(location); + ResourceManager::get()->GetSettingsManager()->SetSettings("NanoleafDevices", nanoleaf_settings); + ResourceManager::get()->GetSettingsManager()->SaveSettings(); +} + void Ui::OpenRGBNanoleafSettingsPage::on_ScanForNanoleafDevicesButton_clicked() { /*-----------------------------------------------------*\ diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.h b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.h index a5b8431a..aea7dc4c 100644 --- a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.h +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.h @@ -21,6 +21,8 @@ public: private slots: void changeEvent(QEvent *event); + void on_AddNanoleafDeviceButton_clicked(); + void on_RemoveNanoleafDeviceButton_clicked(); void on_ScanForNanoleafDevicesButton_clicked(); void on_DeviceFound(QString address, int port); diff --git a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.ui b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.ui index b43f195c..8b846c42 100644 --- a/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.ui +++ b/qt/OpenRGBNanoleafSettingsPage/OpenRGBNanoleafSettingsPage.ui @@ -14,7 +14,21 @@ Form - + + + + Add + + + + + + + Remove + + + + Scan