Skip to content

Commit

Permalink
Simplify brightness slider creation in LedWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
z3ntu committed Apr 29, 2024
1 parent 2b9dad7 commit 9a2fd78
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions src/ledwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ LedWidget::LedWidget(QWidget *parent, libopenrazer::Led *led)
verticalLayout->addLayout(lightingHBox);

auto *comboBox = new QComboBox;
QLabel *brightnessLabel = nullptr;
QSlider *brightnessSlider = nullptr;
QLabel *brightnessSliderValue = nullptr;

comboBox->setObjectName("combobox");
comboBox->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
Expand Down Expand Up @@ -65,36 +62,6 @@ LedWidget::LedWidget(QWidget *parent, libopenrazer::Led *led)
// Connect signal from combobox
connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &LedWidget::fxComboboxChanged);

// Brightness slider
if (led->hasBrightness()) {
brightnessLabel = new QLabel(tr("Brightness"));

brightnessSlider = new QSlider(Qt::Horizontal, this);
brightnessSlider->setMaximum(255);

brightnessSliderValue = new QLabel;

uchar brightness;
try {
brightness = led->getBrightness();
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to get brightness");
brightness = 100;
}
brightnessSlider->setValue(brightness);
brightnessSliderValue->setText(QString("%1%").arg(brightness * 100 / 255));
connect(brightnessSlider, &QSlider::valueChanged, this, [=](int value) {
brightnessSliderValue->setText(QString("%1%").arg(value * 100 / 255));

try {
mLed->setBrightness(value);
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to change brightness");
util::showError(tr("Failed to change brightness"));
}
});
}

// Only add combobox if a capability was actually added
if (comboBox->count() != 0) {
lightingHBox->addWidget(comboBox);
Expand Down Expand Up @@ -152,8 +119,37 @@ LedWidget::LedWidget(QWidget *parent, libopenrazer::Led *led)
comboBox = nullptr;
}

/* Brightness sliders */
if (brightnessLabel != nullptr && brightnessSlider != nullptr) { // only if brightness capability exists
/* Brightness slider */
if (led->hasBrightness()) {
auto *brightnessLabel = new QLabel(tr("Brightness"));

auto *brightnessSlider = new QSlider(Qt::Horizontal, this);
brightnessSlider->setMaximum(255);

auto *brightnessSliderValue = new QLabel;

uchar brightness;
try {
brightness = led->getBrightness();
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to get brightness");
brightness = 100;
}

brightnessSlider->setValue(brightness);
brightnessSliderValue->setText(QString("%1%").arg(brightness * 100 / 255));

connect(brightnessSlider, &QSlider::valueChanged, this, [=](int value) {
brightnessSliderValue->setText(QString("%1%").arg(value * 100 / 255));

try {
mLed->setBrightness(value);
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to change brightness");
util::showError(tr("Failed to change brightness"));
}
});

verticalLayout->addWidget(brightnessLabel);

auto *hboxSlider = new QHBoxLayout();
Expand Down

0 comments on commit 9a2fd78

Please sign in to comment.