From fc17bef7b16668af3f1f156d70912b30b6d0643c Mon Sep 17 00:00:00 2001 From: Luca Weiss Date: Mon, 6 May 2024 22:33:19 +0200 Subject: [PATCH] WIP DPI stages commit 2 --- src/devicewidget/dpisliderwidget.cpp | 61 ++++++++++++++++++++++++---- src/devicewidget/dpisliderwidget.h | 7 ++++ src/devicewidget/dpistagewidget.cpp | 38 +++++++++++++---- src/devicewidget/dpistagewidget.h | 3 ++ 4 files changed, 93 insertions(+), 16 deletions(-) diff --git a/src/devicewidget/dpisliderwidget.cpp b/src/devicewidget/dpisliderwidget.cpp index f7942a6..90c8288 100644 --- a/src/devicewidget/dpisliderwidget.cpp +++ b/src/devicewidget/dpisliderwidget.cpp @@ -39,6 +39,7 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device) auto *dpiStagesCheckbox = new QCheckBox(); dpiStagesCheckbox->setText(tr("Enable stages")); dpiStagesCheckbox->setChecked(true); // FIXME determine based on something + singleStage = false; // FIXME from checkbox dpiHeaderHBox->addWidget(dpiStagesCheckbox); connect(dpiStagesCheckbox, &QCheckBox::clicked, this, [=](bool checked) { @@ -52,6 +53,9 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device) } dpiStageWidgets[0]->setSingleStage(!checked); + singleStage = !checked; + + // FIXME apply current stages / current dpi directly }); } @@ -84,45 +88,66 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device) } catch (const libopenrazer::DBusException &e) { qWarning("Failed to get dpi stages"); } - uchar activeStage = stagesPair.first; - QVector stages = stagesPair.second; + activeStage = stagesPair.first; + dpiStages = stagesPair.second; // Assume user wants DPI synced if all values are currently equal bool isSynced = true; - for (openrazer::RazerDPI dpi : stages) { + for (openrazer::RazerDPI dpi : dpiStages) { isSynced &= dpi.dpi_x == dpi.dpi_y; } dpiSyncCheckbox->setChecked(isSynced); qDebug() << "activeStage:" << activeStage; // FIXME remove - qDebug() << "stages:" << stages; // FIXME remove + qDebug() << "stages:" << dpiStages; // FIXME remove /* Create widgets for the 5 possible DPI stages */ for (int stageNumber = 1; stageNumber <= 5; stageNumber++) { - /* Get DPI for current stage - or default value */ - openrazer::RazerDPI dpi = stages.value(stageNumber - 1); + /* Makes sure we have a DPI stage for every value - 0/0 if not provided */ + if (dpiStages.size() < stageNumber) { + dpiStages.append({ 0, 0 }); + } + /* Get DPI for current stage */ + openrazer::RazerDPI dpi = dpiStages[stageNumber - 1]; auto *stageWidget = new DpiStageWidget(stageNumber, minimumDpi, maximumDpi, dpi, activeStage == stageNumber); stageWidget->setSyncDpi(isSynced); connect(stageWidget, &DpiStageWidget::stageActivated, this, [=]() { - qDebug() << "stage active=" << stageNumber; + qDebug() << "stage active=" << stageNumber; // FIXME remove for (int i = 0; i < dpiStageWidgets.size(); i++) { /* Set all other stages as not active (loop is 0-based) */ if (i + 1 == stageNumber) continue; dpiStageWidgets[i]->setStageActive(false); } + activeStage = stageNumber; + + device->setDPIStages(activeStage, dpiStages); }); connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](openrazer::RazerDPI dpi) { - qDebug() << "stage=" << stageNumber << "dpi changed=" << dpi; + dpiStages.replace(stageNumber - 1, dpi); + qDebug() << dpiStages; + + handleLastStageEnabled(); + + /* Apply to device */ + if (singleStage) { + qDebug() << "singleStage dpi=" << dpi; // FIXME remove + device->setDPI(dpi); + } else { + qDebug() << "stage=" << stageNumber << "dpi changed=" << dpi; // FIXME remove + device->setDPIStages(activeStage, dpiStages); + } }); verticalLayout->addWidget(stageWidget); dpiStageWidgets.append(stageWidget); } + + handleLastStageEnabled(); } else { openrazer::RazerDPI currentDpi = { 0, 0 }; try { @@ -138,9 +163,29 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device) auto *stageWidget = new DpiStageWidget(0, minimumDpi, maximumDpi, currentDpi, false); stageWidget->setSingleStage(true); stageWidget->setSyncDpi(isSynced); + connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](openrazer::RazerDPI dpi) { + qDebug() << "single-stage dpi changed=" << dpi; + device->setDPI(dpi); + }); verticalLayout->addWidget(stageWidget); dpiStageWidgets.append(stageWidget); } } + +void DpiSliderWidget::handleLastStageEnabled() +{ + /* Disable "Enable" button if last stage active */ + int nrDisabledStages = 0; + for (openrazer::RazerDPI dpiStage : dpiStages) { + if (dpiStage.dpi_x == 0 && dpiStage.dpi_y == 0) { + nrDisabledStages++; + } + } + bool lastStage = nrDisabledStages == dpiStages.size() - 1; + qDebug() << "nrDisabledStages=" << nrDisabledStages << "lastStage=" << lastStage; + for (DpiStageWidget *stageWidget : dpiStageWidgets) { + stageWidget->informLastStage(lastStage); + } +} diff --git a/src/devicewidget/dpisliderwidget.h b/src/devicewidget/dpisliderwidget.h index 4b67e11..d19ab76 100644 --- a/src/devicewidget/dpisliderwidget.h +++ b/src/devicewidget/dpisliderwidget.h @@ -22,7 +22,14 @@ class DpiSliderWidget : public QWidget private: libopenrazer::Device *device; + bool singleStage; + + uchar activeStage; + QVector dpiStages; + QVector dpiStageWidgets; + + void handleLastStageEnabled(); }; #endif // DPISLIDERWIDGET_H diff --git a/src/devicewidget/dpistagewidget.cpp b/src/devicewidget/dpistagewidget.cpp index 706bf08..481a985 100644 --- a/src/devicewidget/dpistagewidget.cpp +++ b/src/devicewidget/dpistagewidget.cpp @@ -10,6 +10,8 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent) : QWidget(parent) { + this->stageNumber = stage; + auto *dpiStageHBox = new QHBoxLayout(this); dpiStageButton = new QPushButton(); @@ -77,14 +79,10 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openra enableCheckBox->setChecked(currentDpi.dpi_x != 0); dpiStageHBox->addWidget(enableCheckBox); - dpiXSlider->setEnabled(currentDpi.dpi_x != 0); - dpiYSlider->setEnabled(currentDpi.dpi_x != 0); - dpiStageButton->setEnabled(currentDpi.dpi_x != 0); + updateEnabled(currentDpi.dpi_x != 0); connect(enableCheckBox, &QCheckBox::clicked, this, [=](bool checked) { - dpiXSlider->setEnabled(checked); - dpiYSlider->setEnabled(checked); - dpiStageButton->setEnabled(checked); + updateEnabled(checked); /* Emit changed DPI indicating it's now enabled/disabled */ emitDpiChanged(); @@ -147,8 +145,6 @@ void DpiStageWidget::setSyncDpi(bool syncDpi) dpiYSpinBox->show(); dpiYSlider->show(); } - - // FIXME: emit dpi changed signal? } void DpiStageWidget::setSingleStage(bool singleStage) @@ -156,6 +152,9 @@ void DpiStageWidget::setSingleStage(bool singleStage) if (singleStage) { dpiStageButton->hide(); enableCheckBox->hide(); + enableCheckBox->setChecked(true); + updateEnabled(true); + emitDpiChanged(); } else { dpiStageButton->show(); enableCheckBox->show(); @@ -167,6 +166,16 @@ void DpiStageWidget::setStageActive(bool activeStage) dpiStageButton->setChecked(activeStage); } +void DpiStageWidget::informLastStage(bool lastStage) +{ + /* We need to disable the checkbox if we're still enabled */ + if (lastStage && enableCheckBox->isChecked()) { + enableCheckBox->setEnabled(false); + } else { + enableCheckBox->setEnabled(true); + } +} + void DpiStageWidget::emitDpiChanged() { openrazer::RazerDPI dpi = { 0, 0 }; @@ -178,3 +187,16 @@ void DpiStageWidget::emitDpiChanged() qDebug() << "emit dpi changed=" << dpi; emit dpiChanged(dpi); } + +void DpiStageWidget::updateEnabled(bool enabled) +{ + dpiXSlider->setEnabled(enabled); + dpiYSlider->setEnabled(enabled); + dpiStageButton->setEnabled(enabled); + + if (enabled) { + dpiStageButton->setText(QString::number(stageNumber)); + } else { + dpiStageButton->setText(""); + } +} diff --git a/src/devicewidget/dpistagewidget.h b/src/devicewidget/dpistagewidget.h index fb67874..77a687b 100644 --- a/src/devicewidget/dpistagewidget.h +++ b/src/devicewidget/dpistagewidget.h @@ -22,6 +22,7 @@ class DpiStageWidget : public QWidget void setSyncDpi(bool syncDpi); void setSingleStage(bool singleStage); void setStageActive(bool activeStage); + void informLastStage(bool lastStage); signals: void dpiChanged(openrazer::RazerDPI dpi); @@ -39,8 +40,10 @@ class DpiStageWidget : public QWidget QSlider *dpiYSlider; bool syncDpi; + int stageNumber; void emitDpiChanged(); + void updateEnabled(bool enabled); }; #endif // DPISTAGEWIDGET_H