Nanoleaf device configuration

This commit is contained in:
Cyril Bosselut 2024-02-08 14:03:20 +00:00 committed by Adam Honse
parent cc376b71c8
commit 9506bcb2bb
8 changed files with 263 additions and 3 deletions

View file

@ -0,0 +1,48 @@
#include <QCloseEvent>
#include "ResourceManager.h"
#include "OpenRGBNanoleafNewDeviceDialog.h"
#include "ui_OpenRGBNanoleafNewDeviceDialog.h"
#ifdef _WIN32
#include <QSettings>
#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);
}

View file

@ -0,0 +1,36 @@
#ifndef OPENRGBNANOLEAFNEWDEVICEDIALOG_H
#define OPENRGBNANOLEAFNEWDEVICEDIALOG_H
#include <QDialog>
#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

View file

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OpenRGBNanoleafNewDeviceDialogUi</class>
<widget class="QDialog" name="OpenRGBNanoleafNewDeviceDialogUi">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>184</width>
<height>186</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>New Nanoleaf device</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="deviceIPLabel">
<property name="text">
<string>IP address:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="deviceIPEdit"/>
</item>
<item>
<widget class="QLabel" name="devicePortLabel">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="devicePortEdit"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>OpenRGBNanoleafNewDeviceDialogUi</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>OpenRGBNanoleafNewDeviceDialogUi</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View file

@ -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;
};

View file

@ -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()
{
/*-----------------------------------------------------*\

View file

@ -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);

View file

@ -14,7 +14,21 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0" colspan="3">
<item row="2" column="0">
<widget class="QPushButton" name="AddNanoleafDeviceButton">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="RemoveNanoleafDeviceButton">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="ScanForNanoleafDevicesButton">
<property name="text">
<string>Scan</string>